Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新增在线用户的搜索和一键下线功能 #298

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions server/dbdata/user_act_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@
)

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 {
Expand Down Expand Up @@ -57,13 +59,14 @@
3: "AnyLink",
},
InfoOps: []string{ // 信息
UserLogoutLose: "用户掉线",
UserLogoutBanner: "用户取消弹窗/客户端发起的logout",
UserLogoutClient: "用户/客户端主动断开",
UserLogoutTimeout: "Session过期被踢下线",
UserLogoutAdmin: "账号被管理员踢下线",
UserLogoutExpire: "账号过期被踢下线",
UserIdleTimeout: "用户空闲链接超时",
UserLogoutLose: "用户掉线",
UserLogoutBanner: "用户取消弹窗/客户端发起的logout",
UserLogoutClient: "用户/客户端主动断开",
UserLogoutTimeout: "Session过期被踢下线",
UserLogoutAdmin: "账号被管理员踢下线",
UserLogoutExpire: "账号过期被踢下线",
UserIdleTimeout: "用户空闲链接超时",
UserLogoutOneAdmin: "账号被管理员一键下线",
},
}
)
Expand Down Expand Up @@ -126,6 +129,9 @@
}

func (ua *UserActLogProcess) GetInfoOpsById(id uint8) string {
if int(id) >= len(ua.InfoOps) {
return "未知的信息类型"
}

Check warning on line 134 in server/dbdata/user_act_log.go

View check run for this annotation

Codecov / codecov/patch

server/dbdata/user_act_log.go#L132-L134

Added lines #L132 - L134 were not covered by tests
return ua.InfoOps[id]
}

Expand Down
64 changes: 53 additions & 11 deletions server/sessdata/online.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"bytes"
"net"
"sort"
"strings"
"time"

"github.com/bjdgyc/anylink/pkg/utils"
Expand Down Expand Up @@ -42,33 +43,74 @@
}

func OnlineSess() []Online {
return GetOnlineSess("", "", false)

Check warning on line 46 in server/sessdata/online.go

View check run for this annotation

Codecov / codecov/patch

server/sessdata/online.go#L46

Added line #L46 was not covered by tests
}

/**
* @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 {

Check warning on line 56 in server/sessdata/online.go

View check run for this annotation

Codecov / codecov/patch

server/sessdata/online.go#L56

Added line #L56 was not covered by tests
var datas Onlines
if strings.TrimSpace(search_text) == "" {
search_cate = ""
}

Check warning on line 60 in server/sessdata/online.go

View check run for this annotation

Codecov / codecov/patch

server/sessdata/online.go#L58-L60

Added lines #L58 - L60 were not covered by tests
sessMux.Lock()
defer sessMux.Unlock()

Check warning on line 62 in server/sessdata/online.go

View check run for this annotation

Codecov / codecov/patch

server/sessdata/online.go#L62

Added line #L62 was not covered by tests
for _, v := range sessions {
v.mux.Lock()
if v.IsActive {
cSess := v.CSess
if cSess == nil {
cSess = &ConnSession{}
}

Check warning on line 68 in server/sessdata/online.go

View check run for this annotation

Codecov / codecov/patch

server/sessdata/online.go#L65-L68

Added lines #L65 - L68 were not covered by tests
// 选择需要比较的字符串
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
}

Check warning on line 85 in server/sessdata/online.go

View check run for this annotation

Codecov / codecov/patch

server/sessdata/online.go#L70-L85

Added lines #L70 - L85 were not covered by tests
}
if search_cate != "" && !strings.Contains(compareText, search_text) {
v.mux.Unlock()
continue

Check warning on line 89 in server/sessdata/online.go

View check run for this annotation

Codecov / codecov/patch

server/sessdata/online.go#L87-L89

Added lines #L87 - L89 were not covered by tests
}

if show_sleeper || v.IsActive {

Check warning on line 92 in server/sessdata/online.go

View check run for this annotation

Codecov / codecov/patch

server/sessdata/online.go#L92

Added line #L92 was not covered by tests
val := Online{
Token: v.Token,
Ip: v.CSess.IpAddr,
Ip: cSess.IpAddr,

Check warning on line 95 in server/sessdata/online.go

View check run for this annotation

Codecov / codecov/patch

server/sessdata/online.go#L95

Added line #L95 was not covered by tests
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()),

Check warning on line 107 in server/sessdata/online.go

View check run for this annotation

Codecov / codecov/patch

server/sessdata/online.go#L100-L107

Added lines #L100 - L107 were not covered by tests
LastLogin: v.LastLogin,
}
datas = append(datas, val)
}
v.mux.Unlock()
}
sessMux.Unlock()
sort.Sort(&datas)
return datas
}
7 changes: 5 additions & 2 deletions server/sessdata/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@
sess.CSess.Close()
return
}
AddUserActLogBySess(sess)
AddUserActLogBySess(sess, code...)

Check warning on line 467 in server/sessdata/session.go

View check run for this annotation

Codecov / codecov/patch

server/sessdata/session.go#L467

Added line #L467 was not covered by tests
}

func CloseCSess(token string) {
Expand Down Expand Up @@ -501,7 +501,7 @@
dbdata.UserActLogIns.Add(ua, cs.UserAgent)
}

func AddUserActLogBySess(sess *Session) {
func AddUserActLogBySess(sess *Session, code ...uint8) {

Check warning on line 504 in server/sessdata/session.go

View check run for this annotation

Codecov / codecov/patch

server/sessdata/session.go#L504

Added line #L504 was not covered by tests
ua := dbdata.UserActLog{
Username: sess.Username,
GroupName: sess.Group,
Expand All @@ -512,5 +512,8 @@
Status: dbdata.UserLogout,
}
ua.Info = dbdata.UserActLogIns.GetInfoOpsById(dbdata.UserLogoutBanner)
if len(code) > 0 {
ua.Info = dbdata.UserActLogIns.GetInfoOpsById(code[0])
}

Check warning on line 517 in server/sessdata/session.go

View check run for this annotation

Codecov / codecov/patch

server/sessdata/session.go#L515-L517

Added lines #L515 - L517 were not covered by tests
dbdata.UserActLogIns.Add(ua, sess.UserAgent)
}
124 changes: 117 additions & 7 deletions web/src/pages/user/Online.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,59 @@
<template>
<div>
<el-card>
<el-form :inline="true">
<el-form-item>
<el-select
v-model="searchCate"
style="width: 86px;"
@change="handleSearch">
<el-option
label="用户名"
value="username">
</el-option>
<el-option
label="登录组"
value="group">
</el-option>
<el-option
label="MAC地址"
value="mac_addr">
</el-option>
<el-option
label="IP地址"
value="ip">
</el-option>
<el-option
label="远端地址"
value="remote_addr">
</el-option>
</el-select>
</el-form-item>
<el-form-item>
<el-input
v-model="searchText"
placeholder="请输入搜索内容"
@input="handleSearch">
</el-input>
</el-form-item>
<el-form-item>
显示休眠用户:
<el-switch
v-model="showSleeper"
@change="handleSearch">
</el-switch>
</el-form-item>
<el-form-item>
<el-button
class="extra-small-button"
type="danger"
size="mini"
:loading="loadingOneOffline"
@click="handleOneOffline">
一键下线
</el-button>
</el-form>

<el-table
ref="multipleTable"
:data="tableData"
Expand All @@ -20,14 +73,14 @@

<el-table-column
prop="group"
label="登陆组">
label="登录组">
</el-table-column>

<el-table-column
prop="mac_addr"
label="MAC地址">
</el-table-column>

<el-table-column
prop="unique_mac"
label="唯一MAC">
Expand Down Expand Up @@ -67,7 +120,6 @@
</el-table-column>

<el-table-column
prop="status"
label="实时 上行/下行"
width="220">
<template slot-scope="scope">
Expand All @@ -77,7 +129,6 @@
</el-table-column>

<el-table-column
prop="status"
label="总量 上行/下行"
width="200">
<template slot-scope="scope">
Expand All @@ -88,7 +139,7 @@

<el-table-column
prop="last_login"
label="登陆时间"
label="登录时间"
:formatter="tableDateFormat">
</el-table-column>

Expand All @@ -99,6 +150,7 @@
<el-button
size="mini"
type="primary"
v-if="scope.row.remote_addr !== ''"
@click="handleReline(scope.row)">重连
</el-button>

Expand All @@ -123,6 +175,7 @@

<script>
import axios from "axios";
import { MessageBox } from 'element-ui';

export default {
name: "Online",
Expand All @@ -147,6 +200,10 @@ export default {
data() {
return {
tableData: [],
searchCate: 'username',
searchText: '',
showSleeper: false,
loadingOneOffline: false,
}
},
methods: {
Expand Down Expand Up @@ -185,8 +242,43 @@ export default {
handleEdit(a, row) {
console.log(a, row)
},
handleOneOffline() {
if (this.tableData === null || this.tableData.length === 0) {
this.$message.error('错误:当前在线用户表为空,无法执行一键下线操作!');
return;
}
MessageBox.confirm('当前搜索条件下的所有用户将会“下线”,你确定执行吗?', '危险', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'danger'
}).then(() => {
try {
this.loadingOneOffline = true;
this.getData();
this.$message.success('操作成功');
this.loadingOneOffline = false;
// 清空当前表格
this.tableData = [];
} catch (error) {
this.loadingOneOffline = false;
this.$message.error('操作失败');
}
});
},
handleSearch() {
this.getData();
},
getData() {
axios.get('/user/online').then(resp => {
axios.get('/user/online',
{
params: {
search_cate: this.searchCate,
search_text: this.searchText,
show_sleeper: this.showSleeper,
one_offline: this.loadingOneOffline
}
}
).then(resp => {
var data = resp.data.data
console.log(data);
this.tableData = data.datas;
Expand All @@ -201,5 +293,23 @@ export default {
</script>

<style scoped>

/deep/ .el-form .el-form-item__label,
/deep/ .el-form .el-form-item__content,
/deep/ .el-form .el-input,
/deep/ .el-form .el-select,
/deep/ .el-form .el-button,
/deep/ .el-form .el-select-dropdown__item {
font-size: 11px;
}
.el-select-dropdown .el-select-dropdown__item {
font-size: 11px;
padding: 0 10px;
}
/deep/ .el-input__inner{
height: 30px;
padding: 0 10px;
}
.extra-small-button {
padding: 5px 10px;
}
</style>
Loading