2019-01-09 20:33:00 +08:00
|
|
|
|
package server
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2020-01-08 21:57:14 +08:00
|
|
|
|
"ehang.io/nps/lib/version"
|
2019-01-09 20:33:00 +08:00
|
|
|
|
"errors"
|
2019-07-02 19:31:33 +08:00
|
|
|
|
"math"
|
|
|
|
|
|
"os"
|
|
|
|
|
|
"strconv"
|
|
|
|
|
|
"strings"
|
2020-01-18 16:53:52 +08:00
|
|
|
|
"sync"
|
2019-07-02 19:31:33 +08:00
|
|
|
|
"time"
|
|
|
|
|
|
|
2020-01-08 21:57:14 +08:00
|
|
|
|
"ehang.io/nps/bridge"
|
|
|
|
|
|
"ehang.io/nps/lib/common"
|
|
|
|
|
|
"ehang.io/nps/lib/file"
|
|
|
|
|
|
"ehang.io/nps/server/proxy"
|
|
|
|
|
|
"ehang.io/nps/server/tool"
|
2019-08-10 11:15:25 +08:00
|
|
|
|
"github.com/astaxie/beego"
|
|
|
|
|
|
"github.com/astaxie/beego/logs"
|
2021-04-07 20:02:47 +08:00
|
|
|
|
"github.com/shirou/gopsutil/v3/cpu"
|
|
|
|
|
|
"github.com/shirou/gopsutil/v3/load"
|
|
|
|
|
|
"github.com/shirou/gopsutil/v3/mem"
|
|
|
|
|
|
"github.com/shirou/gopsutil/v3/net"
|
2019-01-09 20:33:00 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
2019-03-29 15:21:30 +08:00
|
|
|
|
Bridge *bridge.Bridge
|
2020-01-18 16:53:52 +08:00
|
|
|
|
RunList sync.Map //map[int]interface{}
|
2025-02-21 17:21:20 +08:00
|
|
|
|
once sync.Once
|
2019-01-09 20:33:00 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func init() {
|
2020-01-18 16:53:52 +08:00
|
|
|
|
RunList = sync.Map{}
|
2019-01-09 20:33:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-07 15:40:37 +08:00
|
|
|
|
// init task from db
|
2019-01-09 20:33:00 +08:00
|
|
|
|
func InitFromCsv() {
|
2019-02-13 03:54:00 +08:00
|
|
|
|
//Add a public password
|
2019-03-05 09:23:18 +08:00
|
|
|
|
if vkey := beego.AppConfig.String("public_vkey"); vkey != "" {
|
2019-02-16 20:43:26 +08:00
|
|
|
|
c := file.NewClient(vkey, true, true)
|
2019-03-29 15:21:30 +08:00
|
|
|
|
file.GetDb().NewClient(c)
|
2020-01-18 16:53:52 +08:00
|
|
|
|
RunList.Store(c.Id, nil)
|
|
|
|
|
|
//RunList[c.Id] = nil
|
2019-02-16 20:43:26 +08:00
|
|
|
|
}
|
2019-02-13 03:54:00 +08:00
|
|
|
|
//Initialize services in server-side files
|
2019-03-29 15:21:30 +08:00
|
|
|
|
file.GetDb().JsonDb.Tasks.Range(func(key, value interface{}) bool {
|
2019-03-23 22:19:59 +08:00
|
|
|
|
if value.(*file.Tunnel).Status {
|
|
|
|
|
|
AddTask(value.(*file.Tunnel))
|
2019-01-09 20:33:00 +08:00
|
|
|
|
}
|
2019-03-23 22:19:59 +08:00
|
|
|
|
return true
|
|
|
|
|
|
})
|
2019-01-09 20:33:00 +08:00
|
|
|
|
}
|
2019-02-18 01:05:05 +08:00
|
|
|
|
|
2024-11-07 15:40:37 +08:00
|
|
|
|
// get bridge command
|
2019-02-13 03:54:00 +08:00
|
|
|
|
func DealBridgeTask() {
|
|
|
|
|
|
for {
|
|
|
|
|
|
select {
|
|
|
|
|
|
case t := <-Bridge.OpenTask:
|
2026-05-23 16:34:08 -06:00
|
|
|
|
StartTask(t.Id)
|
2019-03-15 14:03:49 +08:00
|
|
|
|
case t := <-Bridge.CloseTask:
|
|
|
|
|
|
StopServer(t.Id)
|
2019-02-13 03:54:00 +08:00
|
|
|
|
case id := <-Bridge.CloseClient:
|
2019-03-29 10:41:57 +08:00
|
|
|
|
DelTunnelAndHostByClientId(id, true)
|
2019-03-29 15:21:30 +08:00
|
|
|
|
if v, ok := file.GetDb().JsonDb.Clients.Load(id); ok {
|
2019-03-29 10:41:57 +08:00
|
|
|
|
if v.(*file.Client).NoStore {
|
2019-03-29 15:21:30 +08:00
|
|
|
|
file.GetDb().DelClient(id)
|
2019-03-29 10:41:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-02-23 23:29:48 +08:00
|
|
|
|
case s := <-Bridge.SecretChan:
|
|
|
|
|
|
logs.Trace("New secret connection, addr", s.Conn.Conn.RemoteAddr())
|
2019-03-29 15:21:30 +08:00
|
|
|
|
if t := file.GetDb().GetTaskByMd5Password(s.Password); t != nil {
|
2019-04-13 19:48:34 +08:00
|
|
|
|
if t.Status {
|
2026-06-05 16:45:35 -06:00
|
|
|
|
go proxy.NewBaseServer(Bridge, t).DealClient(s.Conn, t.Client, t.Target.TargetStr, nil, common.CONN_TCP, nil, t.Flow, t.Target.LocalProxy, nil, nil)
|
2019-02-24 13:17:43 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
s.Conn.Close()
|
|
|
|
|
|
logs.Trace("This key %s cannot be processed,status is close", s.Password)
|
2019-02-23 23:29:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
logs.Trace("This key %s cannot be processed", s.Password)
|
|
|
|
|
|
s.Conn.Close()
|
|
|
|
|
|
}
|
2019-02-13 03:54:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-01-09 20:33:00 +08:00
|
|
|
|
|
2024-11-07 15:40:37 +08:00
|
|
|
|
// start a new server
|
2020-05-07 23:29:45 +08:00
|
|
|
|
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)
|
2026-06-05 15:56:38 -06:00
|
|
|
|
// 启动流量持久化(只启动一次,避免每次 AddTask 创建泄漏的 goroutine)
|
|
|
|
|
|
if minute, err := beego.AppConfig.Int("flow_store_interval"); err == nil && minute > 0 {
|
|
|
|
|
|
go flowSession(time.Minute * time.Duration(minute))
|
|
|
|
|
|
}
|
|
|
|
|
|
// 启动后台 IO 速率采集,Dashboard 直接读缓存,无需 Sleep
|
|
|
|
|
|
tool.StartIORateCollector()
|
2019-03-23 22:19:59 +08:00
|
|
|
|
go func() {
|
|
|
|
|
|
if err := Bridge.StartTunnel(); err != nil {
|
|
|
|
|
|
logs.Error("start server bridge error", err)
|
|
|
|
|
|
os.Exit(0)
|
|
|
|
|
|
}
|
|
|
|
|
|
}()
|
2019-03-05 09:23:18 +08:00
|
|
|
|
if p, err := beego.AppConfig.Int("p2p_port"); err == nil {
|
2019-02-26 22:40:28 +08:00
|
|
|
|
go proxy.NewP2PServer(p).Start()
|
2019-04-21 23:03:58 +08:00
|
|
|
|
go proxy.NewP2PServer(p + 1).Start()
|
|
|
|
|
|
go proxy.NewP2PServer(p + 2).Start()
|
2019-02-26 22:40:28 +08:00
|
|
|
|
}
|
2019-02-13 03:54:00 +08:00
|
|
|
|
go DealBridgeTask()
|
2019-03-18 14:18:58 +08:00
|
|
|
|
go dealClientFlow()
|
2026-06-06 14:13:05 -06:00
|
|
|
|
go dealClientExpire()
|
2019-02-06 00:35:23 +08:00
|
|
|
|
if svr := NewMode(Bridge, cnf); svr != nil {
|
2019-02-18 01:05:05 +08:00
|
|
|
|
if err := svr.Start(); err != nil {
|
2019-02-23 23:29:48 +08:00
|
|
|
|
logs.Error(err)
|
2019-02-06 00:35:23 +08:00
|
|
|
|
}
|
2020-01-18 16:53:52 +08:00
|
|
|
|
RunList.Store(cnf.Id, svr)
|
|
|
|
|
|
//RunList[cnf.Id] = svr
|
2019-02-06 00:35:23 +08:00
|
|
|
|
} else {
|
2019-02-23 23:29:48 +08:00
|
|
|
|
logs.Error("Incorrect startup mode %s", cnf.Mode)
|
2019-02-06 00:35:23 +08:00
|
|
|
|
}
|
2019-01-09 20:33:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-03-18 14:18:58 +08:00
|
|
|
|
func dealClientFlow() {
|
|
|
|
|
|
ticker := time.NewTicker(time.Minute)
|
2019-12-02 04:29:36 +13:00
|
|
|
|
defer ticker.Stop()
|
2019-03-18 14:18:58 +08:00
|
|
|
|
for {
|
|
|
|
|
|
select {
|
|
|
|
|
|
case <-ticker.C:
|
2019-03-23 22:19:59 +08:00
|
|
|
|
dealClientData()
|
2019-03-18 14:18:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-06 14:13:05 -06:00
|
|
|
|
// dealClientExpire 周期性扫描客户端到期时间,过期则自动暂停
|
|
|
|
|
|
func dealClientExpire() {
|
|
|
|
|
|
// 启动时立即检查一次,避免重启后到期客户端最多 1 分钟内才被暂停
|
|
|
|
|
|
checkClientExpire()
|
|
|
|
|
|
ticker := time.NewTicker(time.Minute)
|
|
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
for {
|
|
|
|
|
|
select {
|
|
|
|
|
|
case <-ticker.C:
|
|
|
|
|
|
checkClientExpire()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// checkClientExpire 遍历所有客户端,若 ExpireTime 已过则将 Status 置为 false 并断开连接
|
|
|
|
|
|
func checkClientExpire() {
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
|
changed := false
|
|
|
|
|
|
file.GetDb().JsonDb.Clients.Range(func(key, value interface{}) bool {
|
|
|
|
|
|
v, ok := value.(*file.Client)
|
|
|
|
|
|
if !ok || v == nil {
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
if v.ExpireTime == "" || !v.Status {
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
t, err := time.ParseInLocation("2006-01-02 15:04:05", v.ExpireTime, time.Local)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
if now.Before(t) {
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
v.Status = false
|
|
|
|
|
|
changed = true
|
|
|
|
|
|
logs.Info("client id %d (remark: %s) expired at %s, auto paused", v.Id, v.Remark, v.ExpireTime)
|
|
|
|
|
|
DelClientConnect(v.Id)
|
|
|
|
|
|
return true
|
|
|
|
|
|
})
|
|
|
|
|
|
if changed {
|
|
|
|
|
|
file.GetDb().JsonDb.StoreClientsToJsonFile()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-07 15:40:37 +08:00
|
|
|
|
// new a server by mode name
|
2019-02-18 01:05:05 +08:00
|
|
|
|
func NewMode(Bridge *bridge.Bridge, c *file.Tunnel) proxy.Service {
|
|
|
|
|
|
var service proxy.Service
|
2019-01-26 17:27:28 +08:00
|
|
|
|
switch c.Mode {
|
2019-03-02 17:43:21 +08:00
|
|
|
|
case "tcp", "file":
|
2019-02-18 01:05:05 +08:00
|
|
|
|
service = proxy.NewTunnelModeServer(proxy.ProcessTunnel, Bridge, c)
|
2019-03-01 17:23:14 +08:00
|
|
|
|
case "socks5":
|
2019-02-18 01:05:05 +08:00
|
|
|
|
service = proxy.NewSock5ModeServer(Bridge, c)
|
2019-03-01 17:23:14 +08:00
|
|
|
|
case "httpProxy":
|
2019-02-18 01:05:05 +08:00
|
|
|
|
service = proxy.NewTunnelModeServer(proxy.ProcessHttp, Bridge, c)
|
2019-04-21 23:03:58 +08:00
|
|
|
|
case "tcpTrans":
|
|
|
|
|
|
service = proxy.NewTunnelModeServer(proxy.HandleTrans, Bridge, c)
|
2019-03-01 17:23:14 +08:00
|
|
|
|
case "udp":
|
2019-02-18 01:05:05 +08:00
|
|
|
|
service = proxy.NewUdpModeServer(Bridge, c)
|
2019-01-09 20:33:00 +08:00
|
|
|
|
case "webServer":
|
|
|
|
|
|
InitFromCsv()
|
2019-02-09 17:07:47 +08:00
|
|
|
|
t := &file.Tunnel{
|
2019-02-13 03:54:00 +08:00
|
|
|
|
Port: 0,
|
|
|
|
|
|
Mode: "httpHostServer",
|
|
|
|
|
|
Status: true,
|
2019-01-09 20:33:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
AddTask(t)
|
2019-02-18 01:05:05 +08:00
|
|
|
|
service = proxy.NewWebServer(Bridge)
|
2019-01-09 20:33:00 +08:00
|
|
|
|
case "httpHostServer":
|
2019-04-08 17:01:08 +08:00
|
|
|
|
httpPort, _ := beego.AppConfig.Int("http_proxy_port")
|
|
|
|
|
|
httpsPort, _ := beego.AppConfig.Int("https_proxy_port")
|
|
|
|
|
|
useCache, _ := beego.AppConfig.Bool("http_cache")
|
|
|
|
|
|
cacheLen, _ := beego.AppConfig.Int("http_cache_length")
|
2020-01-13 18:31:03 +08:00
|
|
|
|
addOrigin, _ := beego.AppConfig.Bool("http_add_origin_header")
|
|
|
|
|
|
service = proxy.NewHttp(Bridge, c, httpPort, httpsPort, useCache, cacheLen, addOrigin)
|
2019-01-09 20:33:00 +08:00
|
|
|
|
}
|
2019-02-18 01:05:05 +08:00
|
|
|
|
return service
|
2019-01-09 20:33:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-07 15:40:37 +08:00
|
|
|
|
// stop server
|
2019-01-25 12:10:12 +08:00
|
|
|
|
func StopServer(id int) error {
|
2020-01-18 16:53:52 +08:00
|
|
|
|
if v, ok := RunList.Load(id); ok {
|
2019-02-18 01:05:05 +08:00
|
|
|
|
if svr, ok := v.(proxy.Service); ok {
|
|
|
|
|
|
if err := svr.Close(); err != nil {
|
2026-05-23 16:34:08 -06:00
|
|
|
|
logs.Error("stop server id %d error", id, err)
|
2019-02-18 01:05:05 +08:00
|
|
|
|
}
|
2019-03-23 22:19:59 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
logs.Warn("stop server id %d error", id)
|
2019-02-23 23:29:48 +08:00
|
|
|
|
}
|
2026-05-23 16:34:08 -06:00
|
|
|
|
RunList.Delete(id)
|
|
|
|
|
|
if t, err := file.GetDb().GetTask(id); err == nil {
|
2019-02-23 23:29:48 +08:00
|
|
|
|
t.Status = false
|
2021-10-15 10:52:16 +08:00
|
|
|
|
logs.Info("close port %d,remark %s,client id %d,task id %d", t.Port, t.Remark, t.Client.Id, t.Id)
|
2019-03-29 15:21:30 +08:00
|
|
|
|
file.GetDb().UpdateTask(t)
|
2019-01-09 20:33:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
2019-03-02 17:43:21 +08:00
|
|
|
|
return errors.New("task is not running")
|
2019-01-09 20:33:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-07 15:40:37 +08:00
|
|
|
|
// add task
|
2019-02-09 17:07:47 +08:00
|
|
|
|
func AddTask(t *file.Tunnel) error {
|
2019-03-01 17:23:14 +08:00
|
|
|
|
if t.Mode == "secret" || t.Mode == "p2p" {
|
2019-02-23 23:29:48 +08:00
|
|
|
|
logs.Info("secret task %s start ", t.Remark)
|
2020-01-18 16:53:52 +08:00
|
|
|
|
//RunList[t.Id] = nil
|
|
|
|
|
|
RunList.Store(t.Id, nil)
|
2019-02-23 23:29:48 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
2019-02-13 03:54:00 +08:00
|
|
|
|
if b := tool.TestServerPort(t.Port, t.Mode); !b && t.Mode != "httpHostServer" {
|
2019-02-23 23:29:48 +08:00
|
|
|
|
logs.Error("taskId %d start error port %d open failed", t.Id, t.Port)
|
2019-02-15 22:59:28 +08:00
|
|
|
|
return errors.New("the port open error")
|
2019-02-13 03:54:00 +08:00
|
|
|
|
}
|
2019-01-09 20:33:00 +08:00
|
|
|
|
if svr := NewMode(Bridge, t); svr != nil {
|
2019-02-23 23:29:48 +08:00
|
|
|
|
logs.Info("tunnel task %s start mode:%s port %d", t.Remark, t.Mode, t.Port)
|
2020-01-18 16:53:52 +08:00
|
|
|
|
//RunList[t.Id] = svr
|
|
|
|
|
|
RunList.Store(t.Id, svr)
|
2019-01-09 20:33:00 +08:00
|
|
|
|
go func() {
|
2019-02-18 01:05:05 +08:00
|
|
|
|
if err := svr.Start(); err != nil {
|
2019-02-23 23:29:48 +08:00
|
|
|
|
logs.Error("clientId %d taskId %d start error %s", t.Client.Id, t.Id, err)
|
2020-01-18 16:53:52 +08:00
|
|
|
|
//delete(RunList, t.Id)
|
|
|
|
|
|
RunList.Delete(t.Id)
|
2019-02-13 03:54:00 +08:00
|
|
|
|
return
|
2019-01-09 20:33:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
}()
|
|
|
|
|
|
} else {
|
2019-02-13 03:54:00 +08:00
|
|
|
|
return errors.New("the mode is not correct")
|
2019-01-09 20:33:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-07 15:40:37 +08:00
|
|
|
|
// start task
|
2019-01-25 12:10:12 +08:00
|
|
|
|
func StartTask(id int) error {
|
2026-08-09 15:20:41 +08:00
|
|
|
|
t, err := file.GetDb().GetTask(id)
|
|
|
|
|
|
if err != nil {
|
2019-01-09 20:33:00 +08:00
|
|
|
|
return err
|
|
|
|
|
|
}
|
2026-08-09 15:20:41 +08:00
|
|
|
|
if err := AddTask(t); err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
t.Status = true
|
|
|
|
|
|
file.GetDb().UpdateTask(t)
|
2019-01-09 20:33:00 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-07 15:40:37 +08:00
|
|
|
|
// delete task
|
2019-01-25 12:10:12 +08:00
|
|
|
|
func DelTask(id int) error {
|
2020-01-18 16:53:52 +08:00
|
|
|
|
//if _, ok := RunList[id]; ok {
|
|
|
|
|
|
if _, ok := RunList.Load(id); ok {
|
2019-02-13 03:54:00 +08:00
|
|
|
|
if err := StopServer(id); err != nil {
|
|
|
|
|
|
return err
|
2019-01-09 20:33:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-03-29 15:21:30 +08:00
|
|
|
|
return file.GetDb().DelTask(id)
|
2019-01-09 20:33:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-07 15:40:37 +08:00
|
|
|
|
// get task list by page num
|
|
|
|
|
|
func GetTunnel(start, length int, typeVal string, clientId int, search string, sortField string, order string) ([]*file.Tunnel, int) {
|
2026-07-12 19:28:00 -06:00
|
|
|
|
all_list := make([]*file.Tunnel, 0)
|
2024-11-07 15:40:37 +08:00
|
|
|
|
|
2026-07-12 19:28:00 -06:00
|
|
|
|
// collect + fill runtime fields before sort/paginate
|
2026-06-05 15:56:38 -06:00
|
|
|
|
file.GetDb().JsonDb.Tasks.Range(func(key, value interface{}) bool {
|
|
|
|
|
|
v := value.(*file.Tunnel)
|
|
|
|
|
|
if (typeVal != "" && v.Mode != typeVal || (clientId != 0 && v.Client.Id != clientId)) || (typeVal == "" && clientId != v.Client.Id) {
|
|
|
|
|
|
return true
|
2024-11-07 15:40:37 +08:00
|
|
|
|
}
|
2026-07-12 19:28:00 -06:00
|
|
|
|
if search != "" {
|
|
|
|
|
|
targetStr := ""
|
|
|
|
|
|
if v.Target != nil {
|
|
|
|
|
|
targetStr = v.Target.TargetStr
|
|
|
|
|
|
}
|
|
|
|
|
|
if !(v.Id == common.GetIntNoErrByStr(search) || v.Port == common.GetIntNoErrByStr(search) || strings.Contains(v.Password, search) || strings.Contains(v.Remark, search) || strings.Contains(targetStr, search)) {
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if v.Client != nil {
|
|
|
|
|
|
if _, ok := Bridge.Client.Load(v.Client.Id); ok {
|
|
|
|
|
|
v.Client.IsConnect = true
|
|
|
|
|
|
} else {
|
|
|
|
|
|
v.Client.IsConnect = false
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if _, ok := RunList.Load(v.Id); ok {
|
|
|
|
|
|
v.RunStatus = true
|
|
|
|
|
|
} else {
|
|
|
|
|
|
v.RunStatus = false
|
|
|
|
|
|
}
|
2026-06-05 15:56:38 -06:00
|
|
|
|
all_list = append(all_list, v)
|
|
|
|
|
|
return true
|
|
|
|
|
|
})
|
2024-11-07 15:40:37 +08:00
|
|
|
|
|
2026-07-12 19:28:00 -06:00
|
|
|
|
file.SortTunnels(all_list, sortField, order)
|
|
|
|
|
|
|
|
|
|
|
|
cnt := len(all_list)
|
|
|
|
|
|
if start < 0 {
|
|
|
|
|
|
start = 0
|
2019-01-09 20:33:00 +08:00
|
|
|
|
}
|
2026-07-12 19:28:00 -06:00
|
|
|
|
if length <= 0 || start >= cnt {
|
|
|
|
|
|
return []*file.Tunnel{}, cnt
|
|
|
|
|
|
}
|
|
|
|
|
|
end := start + length
|
|
|
|
|
|
if end > cnt {
|
|
|
|
|
|
end = cnt
|
|
|
|
|
|
}
|
|
|
|
|
|
return all_list[start:end], cnt
|
2019-01-09 20:33:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-07 15:40:37 +08:00
|
|
|
|
// get client list
|
2026-07-12 19:28:00 -06:00
|
|
|
|
func GetClientList(start, length int, search, sortField, order string, clientId int) (list []*file.Client, cnt int) {
|
|
|
|
|
|
// fill IsConnect / Version before sort so IsConnect ordering is correct
|
2019-03-23 22:19:59 +08:00
|
|
|
|
dealClientData()
|
2026-07-12 19:28:00 -06:00
|
|
|
|
list, cnt = file.GetDb().GetClientList(start, length, search, sortField, order, clientId)
|
2019-01-25 12:10:12 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-12 19:28:00 -06:00
|
|
|
|
// GetHostList returns hosts with client online status filled before sort/pagination.
|
|
|
|
|
|
func GetHostList(start, length int, clientId int, search, sortField, order string) ([]*file.Host, int) {
|
|
|
|
|
|
// ensure Client.IsConnect is current for sort and display
|
|
|
|
|
|
dealClientData()
|
|
|
|
|
|
return file.GetDb().GetHost(start, length, clientId, search, sortField, order)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-03-23 22:19:59 +08:00
|
|
|
|
func dealClientData() {
|
2022-10-19 17:46:42 +08:00
|
|
|
|
|
2019-03-29 15:21:30 +08:00
|
|
|
|
file.GetDb().JsonDb.Clients.Range(func(key, value interface{}) bool {
|
2019-03-23 22:19:59 +08:00
|
|
|
|
v := value.(*file.Client)
|
2019-12-04 01:28:26 +08:00
|
|
|
|
if vv, ok := Bridge.Client.Load(v.Id); ok {
|
2019-01-25 12:10:12 +08:00
|
|
|
|
v.IsConnect = true
|
2023-06-01 18:01:43 +08:00
|
|
|
|
v.LastOnlineTime = time.Now().Format("2006-01-02 15:04:05")
|
2019-12-04 01:28:26 +08:00
|
|
|
|
v.Version = vv.(*bridge.Client).Version
|
2019-01-25 12:10:12 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
v.IsConnect = false
|
|
|
|
|
|
}
|
2025-11-01 04:21:57 +08:00
|
|
|
|
|
2019-03-23 22:19:59 +08:00
|
|
|
|
return true
|
|
|
|
|
|
})
|
2019-01-25 12:10:12 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-07 15:40:37 +08:00
|
|
|
|
// delete all host and tasks by client id
|
2019-03-29 10:41:57 +08:00
|
|
|
|
func DelTunnelAndHostByClientId(clientId int, justDelNoStore bool) {
|
2019-02-13 03:54:00 +08:00
|
|
|
|
var ids []int
|
2019-03-29 15:21:30 +08:00
|
|
|
|
file.GetDb().JsonDb.Tasks.Range(func(key, value interface{}) bool {
|
2019-03-23 22:19:59 +08:00
|
|
|
|
v := value.(*file.Tunnel)
|
2019-03-29 10:41:57 +08:00
|
|
|
|
if justDelNoStore && !v.NoStore {
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
2019-01-26 17:27:28 +08:00
|
|
|
|
if v.Client.Id == clientId {
|
2019-02-13 03:54:00 +08:00
|
|
|
|
ids = append(ids, v.Id)
|
2019-01-25 12:10:12 +08:00
|
|
|
|
}
|
2019-03-23 22:19:59 +08:00
|
|
|
|
return true
|
|
|
|
|
|
})
|
2019-02-13 03:54:00 +08:00
|
|
|
|
for _, id := range ids {
|
|
|
|
|
|
DelTask(id)
|
|
|
|
|
|
}
|
2019-03-15 14:03:49 +08:00
|
|
|
|
ids = ids[:0]
|
2019-03-29 15:21:30 +08:00
|
|
|
|
file.GetDb().JsonDb.Hosts.Range(func(key, value interface{}) bool {
|
2019-03-23 22:19:59 +08:00
|
|
|
|
v := value.(*file.Host)
|
2019-03-29 10:41:57 +08:00
|
|
|
|
if justDelNoStore && !v.NoStore {
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
2019-01-26 17:27:28 +08:00
|
|
|
|
if v.Client.Id == clientId {
|
2019-03-15 14:03:49 +08:00
|
|
|
|
ids = append(ids, v.Id)
|
2019-01-25 12:10:12 +08:00
|
|
|
|
}
|
2019-03-23 22:19:59 +08:00
|
|
|
|
return true
|
|
|
|
|
|
})
|
2019-03-15 14:03:49 +08:00
|
|
|
|
for _, id := range ids {
|
2019-03-29 15:21:30 +08:00
|
|
|
|
file.GetDb().DelHost(id)
|
2019-03-15 14:03:49 +08:00
|
|
|
|
}
|
2019-01-25 12:10:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-07 15:40:37 +08:00
|
|
|
|
// close the client
|
2019-01-25 12:10:12 +08:00
|
|
|
|
func DelClientConnect(clientId int) {
|
2019-03-23 22:19:59 +08:00
|
|
|
|
Bridge.DelClient(clientId)
|
2019-01-25 12:10:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-03-01 17:23:14 +08:00
|
|
|
|
func GetDashboardData() map[string]interface{} {
|
|
|
|
|
|
data := make(map[string]interface{})
|
2019-12-04 01:28:26 +08:00
|
|
|
|
data["version"] = version.VERSION
|
2019-03-29 15:21:30 +08:00
|
|
|
|
data["hostCount"] = common.GeSynctMapLen(file.GetDb().JsonDb.Hosts)
|
2019-12-06 23:42:37 +08:00
|
|
|
|
data["clientCount"] = common.GeSynctMapLen(file.GetDb().JsonDb.Clients)
|
|
|
|
|
|
if beego.AppConfig.String("public_vkey") != "" { //remove public vkey
|
|
|
|
|
|
data["clientCount"] = data["clientCount"].(int) - 1
|
|
|
|
|
|
}
|
2019-03-23 22:19:59 +08:00
|
|
|
|
dealClientData()
|
2019-01-25 12:10:12 +08:00
|
|
|
|
c := 0
|
|
|
|
|
|
var in, out int64
|
2019-03-29 15:21:30 +08:00
|
|
|
|
file.GetDb().JsonDb.Clients.Range(func(key, value interface{}) bool {
|
2019-03-23 22:19:59 +08:00
|
|
|
|
v := value.(*file.Client)
|
2019-01-25 12:10:12 +08:00
|
|
|
|
if v.IsConnect {
|
|
|
|
|
|
c += 1
|
|
|
|
|
|
}
|
|
|
|
|
|
in += v.Flow.InletFlow
|
|
|
|
|
|
out += v.Flow.ExportFlow
|
2019-03-23 22:19:59 +08:00
|
|
|
|
return true
|
|
|
|
|
|
})
|
2019-01-25 12:10:12 +08:00
|
|
|
|
data["clientOnlineCount"] = c
|
|
|
|
|
|
data["inletFlowCount"] = int(in)
|
|
|
|
|
|
data["exportFlowCount"] = int(out)
|
2019-03-01 17:23:14 +08:00
|
|
|
|
var tcp, udp, secret, socks5, p2p, http int
|
2019-03-29 15:21:30 +08:00
|
|
|
|
file.GetDb().JsonDb.Tasks.Range(func(key, value interface{}) bool {
|
2019-03-23 22:19:59 +08:00
|
|
|
|
switch value.(*file.Tunnel).Mode {
|
2019-03-01 17:23:14 +08:00
|
|
|
|
case "tcp":
|
|
|
|
|
|
tcp += 1
|
|
|
|
|
|
case "socks5":
|
2019-07-02 19:31:33 +08:00
|
|
|
|
socks5 += 1
|
2019-03-01 17:23:14 +08:00
|
|
|
|
case "httpProxy":
|
|
|
|
|
|
http += 1
|
|
|
|
|
|
case "udp":
|
|
|
|
|
|
udp += 1
|
|
|
|
|
|
case "p2p":
|
|
|
|
|
|
p2p += 1
|
|
|
|
|
|
case "secret":
|
|
|
|
|
|
secret += 1
|
2019-01-25 12:10:12 +08:00
|
|
|
|
}
|
2019-03-23 22:19:59 +08:00
|
|
|
|
return true
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2019-03-01 17:23:14 +08:00
|
|
|
|
data["tcpC"] = tcp
|
|
|
|
|
|
data["udpCount"] = udp
|
|
|
|
|
|
data["socks5Count"] = socks5
|
|
|
|
|
|
data["httpProxyCount"] = http
|
|
|
|
|
|
data["secretCount"] = secret
|
|
|
|
|
|
data["p2pCount"] = p2p
|
2019-03-05 09:23:18 +08:00
|
|
|
|
data["bridgeType"] = beego.AppConfig.String("bridge_type")
|
|
|
|
|
|
data["httpProxyPort"] = beego.AppConfig.String("http_proxy_port")
|
|
|
|
|
|
data["httpsProxyPort"] = beego.AppConfig.String("https_proxy_port")
|
|
|
|
|
|
data["ipLimit"] = beego.AppConfig.String("ip_limit")
|
|
|
|
|
|
data["flowStoreInterval"] = beego.AppConfig.String("flow_store_interval")
|
|
|
|
|
|
data["serverIp"] = beego.AppConfig.String("p2p_ip")
|
|
|
|
|
|
data["p2pPort"] = beego.AppConfig.String("p2p_port")
|
|
|
|
|
|
data["logLevel"] = beego.AppConfig.String("log_level")
|
2019-02-23 23:29:48 +08:00
|
|
|
|
tcpCount := 0
|
2019-03-23 22:19:59 +08:00
|
|
|
|
|
2019-03-29 15:21:30 +08:00
|
|
|
|
file.GetDb().JsonDb.Clients.Range(func(key, value interface{}) bool {
|
2019-03-29 10:41:57 +08:00
|
|
|
|
tcpCount += int(value.(*file.Client).NowConn)
|
2019-03-23 22:19:59 +08:00
|
|
|
|
return true
|
|
|
|
|
|
})
|
2019-02-23 23:29:48 +08:00
|
|
|
|
data["tcpCount"] = tcpCount
|
2019-03-01 17:23:14 +08:00
|
|
|
|
cpuPercet, _ := cpu.Percent(0, true)
|
|
|
|
|
|
var cpuAll float64
|
|
|
|
|
|
for _, v := range cpuPercet {
|
|
|
|
|
|
cpuAll += v
|
|
|
|
|
|
}
|
|
|
|
|
|
loads, _ := load.Avg()
|
|
|
|
|
|
data["load"] = loads.String()
|
|
|
|
|
|
data["cpu"] = math.Round(cpuAll / float64(len(cpuPercet)))
|
|
|
|
|
|
swap, _ := mem.SwapMemory()
|
|
|
|
|
|
data["swap_mem"] = math.Round(swap.UsedPercent)
|
|
|
|
|
|
vir, _ := mem.VirtualMemory()
|
|
|
|
|
|
data["virtual_mem"] = math.Round(vir.UsedPercent)
|
2026-06-05 15:56:38 -06:00
|
|
|
|
// IO 速率从后台缓存读取,无需 Sleep 500ms
|
|
|
|
|
|
if cached, ok := tool.IORateCache.Load().(map[string]uint64); ok {
|
|
|
|
|
|
data["io_send"] = cached["io_send"]
|
|
|
|
|
|
data["io_recv"] = cached["io_recv"]
|
2019-03-01 17:23:14 +08:00
|
|
|
|
}
|
2026-06-05 15:56:38 -06:00
|
|
|
|
conn, _ := net.ProtoCounters(nil)
|
2019-03-01 17:23:14 +08:00
|
|
|
|
for _, v := range conn {
|
|
|
|
|
|
data[v.Protocol] = v.Stats["CurrEstab"]
|
|
|
|
|
|
}
|
|
|
|
|
|
//chart
|
|
|
|
|
|
var fg int
|
2026-06-05 15:56:38 -06:00
|
|
|
|
tool.ServerStatusMu.RLock()
|
|
|
|
|
|
statusLen := len(tool.ServerStatus)
|
|
|
|
|
|
if statusLen >= 10 {
|
|
|
|
|
|
fg = statusLen / 10
|
2019-03-01 17:23:14 +08:00
|
|
|
|
for i := 0; i <= 9; i++ {
|
2019-03-29 15:21:30 +08:00
|
|
|
|
data["sys"+strconv.Itoa(i+1)] = tool.ServerStatus[i*fg]
|
2019-03-01 17:23:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-06-05 15:56:38 -06:00
|
|
|
|
tool.ServerStatusMu.RUnlock()
|
2019-01-25 12:10:12 +08:00
|
|
|
|
return data
|
2019-01-09 20:33:00 +08:00
|
|
|
|
}
|
2019-02-23 23:29:48 +08:00
|
|
|
|
|
2022-10-19 17:46:42 +08:00
|
|
|
|
// 实例化流量数据到文件
|
2019-02-23 23:29:48 +08:00
|
|
|
|
func flowSession(m time.Duration) {
|
2025-02-21 17:21:20 +08:00
|
|
|
|
once.Do(func() {
|
|
|
|
|
|
ticker := time.NewTicker(m)
|
|
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
for {
|
|
|
|
|
|
select {
|
|
|
|
|
|
case <-ticker.C:
|
|
|
|
|
|
file.GetDb().JsonDb.StoreHostToJsonFile()
|
|
|
|
|
|
file.GetDb().JsonDb.StoreTasksToJsonFile()
|
|
|
|
|
|
file.GetDb().JsonDb.StoreClientsToJsonFile()
|
|
|
|
|
|
file.GetDb().JsonDb.StoreGlobalToJsonFile()
|
|
|
|
|
|
}
|
2019-02-23 23:29:48 +08:00
|
|
|
|
}
|
2025-02-21 17:21:20 +08:00
|
|
|
|
})
|
2019-02-23 23:29:48 +08:00
|
|
|
|
}
|