diff --git a/server/dbdata/user_act_log.go b/server/dbdata/user_act_log.go index dfa039f8..4daa9366 100644 --- a/server/dbdata/user_act_log.go +++ b/server/dbdata/user_act_log.go @@ -12,17 +12,19 @@ import ( ) const ( - UserAuthFail = 0 // 认证失败 - UserAuthSuccess = 1 // 认证成功 - UserConnected = 2 // 连线成功 - UserLogout = 3 // 用户登出 - UserLogoutLose = 0 // 用户掉线 - UserLogoutBanner = 1 // 用户banner弹窗取消 - UserLogoutClient = 2 // 用户主动登出 - UserLogoutTimeout = 3 // 用户超时登出 - UserLogoutAdmin = 4 // 账号被管理员踢下线 - UserLogoutExpire = 5 // 账号过期被踢下线 - UserIdleTimeout = 6 // 用户空闲链接超时 + UserAuthFail = 0 // 认证失败 + UserAuthSuccess = 1 // 认证成功 + UserConnected = 2 // 连线成功 + UserLogout = 3 // 用户登出 + UserLogoutLose = 0 // 用户掉线 + UserLogoutBanner = 1 // 用户banner弹窗取消 + UserLogoutClient = 2 // 用户主动登出 + UserLogoutTimeout = 3 // 用户超时登出 + UserLogoutAdmin = 4 // 账号被管理员踢下线 + UserLogoutExpire = 5 // 账号过期被踢下线 + UserIdleTimeout = 6 // 用户空闲链接超时 + UserLogoutOneAdmin = 7 // 账号被管理员一键下线 + ) type UserActLogProcess struct { @@ -57,13 +59,14 @@ var ( 3: "AnyLink", }, InfoOps: []string{ // 信息 - UserLogoutLose: "用户掉线", - UserLogoutBanner: "用户取消弹窗/客户端发起的logout", - UserLogoutClient: "用户/客户端主动断开", - UserLogoutTimeout: "Session过期被踢下线", - UserLogoutAdmin: "账号被管理员踢下线", - UserLogoutExpire: "账号过期被踢下线", - UserIdleTimeout: "用户空闲链接超时", + UserLogoutLose: "用户掉线", + UserLogoutBanner: "用户取消弹窗/客户端发起的logout", + UserLogoutClient: "用户/客户端主动断开", + UserLogoutTimeout: "Session过期被踢下线", + UserLogoutAdmin: "账号被管理员踢下线", + UserLogoutExpire: "账号过期被踢下线", + UserIdleTimeout: "用户空闲链接超时", + UserLogoutOneAdmin: "账号被管理员一键下线", }, } ) @@ -126,6 +129,9 @@ func (ua *UserActLogProcess) GetStatusOpsWithTag() interface{} { } func (ua *UserActLogProcess) GetInfoOpsById(id uint8) string { + if int(id) >= len(ua.InfoOps) { + return "未知的信息类型" + } return ua.InfoOps[id] } diff --git a/server/sessdata/online.go b/server/sessdata/online.go index 7ac526d1..8785c198 100644 --- a/server/sessdata/online.go +++ b/server/sessdata/online.go @@ -4,6 +4,7 @@ import ( "bytes" "net" "sort" + "strings" "time" "github.com/bjdgyc/anylink/pkg/utils" @@ -42,33 +43,74 @@ func (o Onlines) Swap(i, j int) { } func OnlineSess() []Online { + return GetOnlineSess("", "", false) +} + +/** + * @Description: GetOnlineSess + * @param search_cate 分类:用户名、登录组、MAC地址、IP地址、远端地址 + * @param search_text 关键字,模糊搜索 + * @param show_sleeper 是否显示休眠用户 + * @return []Online + */ +func GetOnlineSess(search_cate string, search_text string, show_sleeper bool) []Online { var datas Onlines + if strings.TrimSpace(search_text) == "" { + search_cate = "" + } sessMux.Lock() + defer sessMux.Unlock() for _, v := range sessions { v.mux.Lock() - if v.IsActive { + cSess := v.CSess + if cSess == nil { + cSess = &ConnSession{} + } + // 选择需要比较的字符串 + var compareText string + switch search_cate { + case "username": + compareText = v.Username + case "group": + compareText = v.Group + case "mac_addr": + compareText = v.MacAddr + case "ip": + if cSess != nil { + compareText = cSess.IpAddr.String() + } + case "remote_addr": + if cSess != nil { + compareText = cSess.RemoteAddr + } + } + if search_cate != "" && !strings.Contains(compareText, search_text) { + v.mux.Unlock() + continue + } + + if show_sleeper || v.IsActive { val := Online{ Token: v.Token, - Ip: v.CSess.IpAddr, + Ip: cSess.IpAddr, Username: v.Username, Group: v.Group, MacAddr: v.MacAddr, UniqueMac: v.UniqueMac, - RemoteAddr: v.CSess.RemoteAddr, - TunName: v.CSess.IfName, - Mtu: v.CSess.Mtu, - Client: v.CSess.Client, - BandwidthUp: utils.HumanByte(v.CSess.BandwidthUpPeriod.Load()) + "/s", - BandwidthDown: utils.HumanByte(v.CSess.BandwidthDownPeriod.Load()) + "/s", - BandwidthUpAll: utils.HumanByte(v.CSess.BandwidthUpAll.Load()), - BandwidthDownAll: utils.HumanByte(v.CSess.BandwidthDownAll.Load()), + RemoteAddr: cSess.RemoteAddr, + TunName: cSess.IfName, + Mtu: cSess.Mtu, + Client: cSess.Client, + BandwidthUp: utils.HumanByte(cSess.BandwidthUpPeriod.Load()) + "/s", + BandwidthDown: utils.HumanByte(cSess.BandwidthDownPeriod.Load()) + "/s", + BandwidthUpAll: utils.HumanByte(cSess.BandwidthUpAll.Load()), + BandwidthDownAll: utils.HumanByte(cSess.BandwidthDownAll.Load()), LastLogin: v.LastLogin, } datas = append(datas, val) } v.mux.Unlock() } - sessMux.Unlock() sort.Sort(&datas) return datas } diff --git a/server/sessdata/session.go b/server/sessdata/session.go index 8a555304..a4e6ecd7 100644 --- a/server/sessdata/session.go +++ b/server/sessdata/session.go @@ -464,7 +464,7 @@ func CloseSess(token string, code ...uint8) { sess.CSess.Close() return } - AddUserActLogBySess(sess) + AddUserActLogBySess(sess, code...) } func CloseCSess(token string) { @@ -501,7 +501,7 @@ func AddUserActLog(cs *ConnSession) { dbdata.UserActLogIns.Add(ua, cs.UserAgent) } -func AddUserActLogBySess(sess *Session) { +func AddUserActLogBySess(sess *Session, code ...uint8) { ua := dbdata.UserActLog{ Username: sess.Username, GroupName: sess.Group, @@ -512,5 +512,8 @@ func AddUserActLogBySess(sess *Session) { Status: dbdata.UserLogout, } ua.Info = dbdata.UserActLogIns.GetInfoOpsById(dbdata.UserLogoutBanner) + if len(code) > 0 { + ua.Info = dbdata.UserActLogIns.GetInfoOpsById(code[0]) + } dbdata.UserActLogIns.Add(ua, sess.UserAgent) } diff --git a/web/src/pages/user/Online.vue b/web/src/pages/user/Online.vue index 9b4f141a..2ba25ad6 100644 --- a/web/src/pages/user/Online.vue +++ b/web/src/pages/user/Online.vue @@ -1,6 +1,59 @@