From bf622b87ba124aeabf593b256b743e74ba368215 Mon Sep 17 00:00:00 2001 From: yisier <1340976576@qq.com> Date: Thu, 7 Nov 2024 15:40:37 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9A=A7=E9=81=93=E5=88=97=E8=A1=A8=E6=8E=92?= =?UTF-8?q?=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- server/server.go | 76 +++++++++++++++++++++++++++++++-------- web/controllers/index.go | 2 +- web/views/index/list.html | 11 ++++-- 4 files changed, 72 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index f1e6fd4f..45cac265 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ https://natnps.com/ 修复: - 客户端限速单位不统一 [#185](https://github.com/yisier/nps/issues/185) - - 增加从下拉选择客户端,新增编辑后不会刷新界面 [#183](https://github.com/yisier/nps/issues/183) + - 增加从下拉选择客户端,隧道列表排序,新增编辑后不会刷新界面 [#183](https://github.com/yisier/nps/issues/183) - 隧道数量限制无法统计域名映射 [#209](https://github.com/yisier/nps/issues/209) diff --git a/server/server.go b/server/server.go index 9296c8b8..308f6e62 100644 --- a/server/server.go +++ b/server/server.go @@ -5,6 +5,7 @@ import ( "errors" "math" "os" + "sort" "strconv" "strings" "sync" @@ -32,7 +33,7 @@ func init() { RunList = sync.Map{} } -//init task from db +// init task from db func InitFromCsv() { //Add a public password if vkey := beego.AppConfig.String("public_vkey"); vkey != "" { @@ -50,7 +51,7 @@ func InitFromCsv() { }) } -//get bridge command +// get bridge command func DealBridgeTask() { for { select { @@ -84,7 +85,7 @@ func DealBridgeTask() { } } -//start a new server +// start a new server func StartNewServer(bridgePort int, cnf *file.Tunnel, bridgeType string, bridgeDisconnect int) { Bridge = bridge.NewTunnel(bridgePort, bridgeType, common.GetBoolByStr(beego.AppConfig.String("ip_limit")), RunList, bridgeDisconnect) go func() { @@ -122,7 +123,7 @@ func dealClientFlow() { } } -//new a server by mode name +// new a server by mode name func NewMode(Bridge *bridge.Bridge, c *file.Tunnel) proxy.Service { var service proxy.Service switch c.Mode { @@ -156,7 +157,7 @@ func NewMode(Bridge *bridge.Bridge, c *file.Tunnel) proxy.Service { return service } -//stop server +// stop server func StopServer(id int) error { //if v, ok := RunList[id]; ok { if v, ok := RunList.Load(id); ok { @@ -182,7 +183,7 @@ func StopServer(id int) error { return errors.New("task is not running") } -//add task +// add task func AddTask(t *file.Tunnel) error { if t.Mode == "secret" || t.Mode == "p2p" { logs.Info("secret task %s start ", t.Remark) @@ -215,7 +216,7 @@ func AddTask(t *file.Tunnel) error { return nil } -//start task +// start task func StartTask(id int) error { if t, err := file.GetDb().GetTask(id); err != nil { return err @@ -227,7 +228,7 @@ func StartTask(id int) error { return nil } -//delete task +// delete task func DelTask(id int) error { //if _, ok := RunList[id]; ok { if _, ok := RunList.Load(id); ok { @@ -238,18 +239,64 @@ func DelTask(id int) error { return file.GetDb().DelTask(id) } -//get task list by page num -func GetTunnel(start, length int, typeVal string, clientId int, search string) ([]*file.Tunnel, int) { +// get task list by page num +func GetTunnel(start, length int, typeVal string, clientId int, search string, sortField string, order string) ([]*file.Tunnel, int) { + all_list := make([]*file.Tunnel, 0) //store all Tunnel list := make([]*file.Tunnel, 0) var cnt int keys := file.GetMapKeys(file.GetDb().JsonDb.Tasks, false, "", "") + + //get all Tunnel and sort for _, key := range keys { if value, ok := file.GetDb().JsonDb.Tasks.Load(key); ok { v := value.(*file.Tunnel) if (typeVal != "" && v.Mode != typeVal || (clientId != 0 && v.Client.Id != clientId)) || (typeVal == "" && clientId != v.Client.Id) { continue } - if search != "" && !(v.Id == common.GetIntNoErrByStr(search) || v.Port == common.GetIntNoErrByStr(search) || strings.Contains(v.Password, search) || strings.Contains(v.Remark, search) || strings.Contains(v.Client.VerifyKey, search)) { + all_list = append(all_list, v) + } + } + //sort by Id, Remark, TargetStr, Port, asc or desc + if sortField == "Id" { + if order == "asc" { + sort.SliceStable(all_list, func(i, j int) bool { return all_list[i].Id < all_list[j].Id }) + } else { + sort.SliceStable(all_list, func(i, j int) bool { return all_list[i].Id > all_list[j].Id }) + } + } else if sortField == "ClientId" { + if order == "asc" { + sort.SliceStable(all_list, func(i, j int) bool { return all_list[i].Client.Id < all_list[j].Client.Id }) + } else { + sort.SliceStable(all_list, func(i, j int) bool { return all_list[i].Client.Id > all_list[j].Client.Id }) + } + } else if sortField == "Remark" { + if order == "asc" { + sort.SliceStable(all_list, func(i, j int) bool { return all_list[i].Remark < all_list[j].Remark }) + } else { + sort.SliceStable(all_list, func(i, j int) bool { return all_list[i].Remark > all_list[j].Remark }) + } + } else if sortField == "Client.VerifyKey" { + if order == "asc" { + sort.SliceStable(all_list, func(i, j int) bool { return all_list[i].Client.VerifyKey < all_list[j].Client.VerifyKey }) + } else { + sort.SliceStable(all_list, func(i, j int) bool { return all_list[i].Client.VerifyKey > all_list[j].Client.VerifyKey }) + } + } else if sortField == "Target" { + if order == "asc" { + sort.SliceStable(all_list, func(i, j int) bool { return all_list[i].Target.TargetStr < all_list[j].Target.TargetStr }) + } else { + sort.SliceStable(all_list, func(i, j int) bool { return all_list[i].Target.TargetStr > all_list[j].Target.TargetStr }) + } + } + + //search + for _, key := range all_list { + if value, ok := file.GetDb().JsonDb.Tasks.Load(key.Id); ok { + v := value.(*file.Tunnel) + if (typeVal != "" && v.Mode != typeVal || (clientId != 0 && v.Client.Id != clientId)) || (typeVal == "" && clientId != v.Client.Id) { + continue + } + if search != "" && !(v.Id == common.GetIntNoErrByStr(search) || v.Port == common.GetIntNoErrByStr(search) || strings.Contains(v.Password, search) || strings.Contains(v.Remark, search) || strings.Contains(v.Target.TargetStr, search)) { continue } cnt++ @@ -260,7 +307,6 @@ func GetTunnel(start, length int, typeVal string, clientId int, search string) ( } if start--; start < 0 { if length--; length >= 0 { - //if _, ok := RunList[v.Id]; ok { if _, ok := RunList.Load(v.Id); ok { v.RunStatus = true } else { @@ -274,7 +320,7 @@ func GetTunnel(start, length int, typeVal string, clientId int, search string) ( return list, cnt } -//get client list +// get client list func GetClientList(start, length int, search, sort, order string, clientId int) (list []*file.Client, cnt int) { list, cnt = file.GetDb().GetClientList(start, length, search, sort, order, clientId) dealClientData() @@ -326,7 +372,7 @@ func dealClientData() { return } -//delete all host and tasks by client id +// delete all host and tasks by client id func DelTunnelAndHostByClientId(clientId int, justDelNoStore bool) { var ids []int file.GetDb().JsonDb.Tasks.Range(func(key, value interface{}) bool { @@ -358,7 +404,7 @@ func DelTunnelAndHostByClientId(clientId int, justDelNoStore bool) { } } -//close the client +// close the client func DelClientConnect(clientId int) { Bridge.DelClient(clientId) } diff --git a/web/controllers/index.go b/web/controllers/index.go index e1a14e22..7ffb8423 100755 --- a/web/controllers/index.go +++ b/web/controllers/index.go @@ -81,7 +81,7 @@ func (s *IndexController) GetTunnel() { start, length := s.GetAjaxParams() taskType := s.getEscapeString("type") clientId := s.GetIntNoErr("client_id") - list, cnt := server.GetTunnel(start, length, taskType, clientId, s.getEscapeString("search")) + list, cnt := server.GetTunnel(start, length, taskType, clientId, s.getEscapeString("search"), s.getEscapeString("sort"), s.getEscapeString("order")) s.AjaxTable(list, cnt, cnt, nil) } diff --git a/web/views/index/list.html b/web/views/index/list.html index ab41b03c..8661f573 100644 --- a/web/views/index/list.html +++ b/web/views/index/list.html @@ -49,7 +49,9 @@
"limit": params.limit, "type":{{.type}}, "client_id":{{.client_id}}, - "search": params.search + "search": params.search, + "sort": params.sort, + "order": params.order } }, search: true, @@ -98,12 +100,14 @@
title: '',//标题 halign: 'center', visible: true,//false表示不显示 + sortable: true,//启用排序 }, { - field: 'Id',//域值 + field: 'ClientId',//域值 title: '',//标题 halign: 'center', visible: true,//false表示不显示 + sortable: true, //启用排序 formatter: function (value, row, index) { return row.Client.Id + '-' + row.Client.Remark } @@ -118,6 +122,7 @@
field: 'Client.VerifyKey',//域值 title: '',//标题 halign: 'center', + sortable: true, //启用排序 visible: true//false表示不显示 }, { @@ -133,6 +138,7 @@
field: 'Port',//域值 title: '',//标题 halign: 'center', + sortable: true, //启用排序 visible: true//false表示不显示 }, { @@ -140,6 +146,7 @@
title: '',//标题 halign: 'center', visible: true,//false表示不显示 + sortable: true, //启用排序 formatter: function (value, row, index) { return row.Target.TargetStr }