Skip to content

Commit

Permalink
隧道列表排序
Browse files Browse the repository at this point in the history
  • Loading branch information
yisier committed Nov 7, 2024
1 parent 700332e commit bf622b8
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
76 changes: 61 additions & 15 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"math"
"os"
"sort"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -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 != "" {
Expand All @@ -50,7 +51,7 @@ func InitFromCsv() {
})
}

//get bridge command
// get bridge command
func DealBridgeTask() {
for {
select {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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++
Expand All @@ -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 {
Expand All @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -358,7 +404,7 @@ func DelTunnelAndHostByClientId(clientId int, justDelNoStore bool) {
}
}

//close the client
// close the client
func DelClientConnect(clientId int) {
Bridge.DelClient(clientId)
}
Expand Down
2 changes: 1 addition & 1 deletion web/controllers/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
11 changes: 9 additions & 2 deletions web/views/index/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ <h5><span id="langtag"></span><span></span></h5>
"limit": params.limit,
"type":{{.type}},
"client_id":{{.client_id}},
"search": params.search
"search": params.search,
"sort": params.sort,
"order": params.order
}
},
search: true,
Expand Down Expand Up @@ -98,12 +100,14 @@ <h5><span id="langtag"></span><span></span></h5>
title: '<span langtag="word-id"></span>',//标题
halign: 'center',
visible: true,//false表示不显示
sortable: true,//启用排序
},
{
field: 'Id',//域值
field: 'ClientId',//域值
title: '<span langtag="word-clientid"></span>',//标题
halign: 'center',
visible: true,//false表示不显示
sortable: true, //启用排序
formatter: function (value, row, index) {
return row.Client.Id + '-' + row.Client.Remark
}
Expand All @@ -118,6 +122,7 @@ <h5><span id="langtag"></span><span></span></h5>
field: 'Client.VerifyKey',//域值
title: '<span langtag="word-verifykey"></span>',//标题
halign: 'center',
sortable: true, //启用排序
visible: true//false表示不显示
},
{
Expand All @@ -133,13 +138,15 @@ <h5><span id="langtag"></span><span></span></h5>
field: 'Port',//域值
title: '<span langtag="word-port"></span>',//标题
halign: 'center',
sortable: true, //启用排序
visible: true//false表示不显示
},
{
field: 'Target',//域值
title: '<span langtag="word-target"></span>',//标题
halign: 'center',
visible: true,//false表示不显示
sortable: true, //启用排序
formatter: function (value, row, index) {
return row.Target.TargetStr
}
Expand Down

0 comments on commit bf622b8

Please sign in to comment.