mirror of
https://github.com/ehang-io/nps.git
synced 2025-09-02 03:16:53 +00:00
redo web UI |web close| client log |system info |p2p |max、ump optimization
This commit is contained in:
@@ -48,22 +48,6 @@ func (s *BaseServer) FlowAddHost(host *file.Host, in, out int64) {
|
||||
host.Flow.InletFlow += in
|
||||
}
|
||||
|
||||
func (s *BaseServer) linkCopy(link *conn.Link, c *conn.Conn, rb []byte, tunnel *conn.Conn, flow *file.Flow) {
|
||||
if rb != nil {
|
||||
if _, err := tunnel.SendMsg(rb, link); err != nil {
|
||||
c.Close()
|
||||
return
|
||||
}
|
||||
flow.Add(len(rb), 0)
|
||||
<-link.StatusCh
|
||||
}
|
||||
if err := s.checkFlow(); err != nil {
|
||||
c.Close()
|
||||
}
|
||||
link.RunRead(tunnel)
|
||||
s.task.Client.AddConn()
|
||||
}
|
||||
|
||||
func (s *BaseServer) writeConnFail(c net.Conn) {
|
||||
c.Write([]byte(common.ConnectionFailBytes))
|
||||
c.Write(s.errorContent)
|
||||
@@ -87,15 +71,16 @@ func (s *BaseServer) checkFlow() error {
|
||||
}
|
||||
|
||||
//与客户端建立通道
|
||||
func (s *BaseServer) DealClient(c *conn.Conn, addr string, rb []byte) error {
|
||||
link := conn.NewLink(s.task.Client.GetId(), common.CONN_TCP, addr, s.task.Client.Cnf.CompressEncode, s.task.Client.Cnf.CompressDecode, s.task.Client.Cnf.Crypt, c, s.task.Flow, nil, s.task.Client.Rate, nil)
|
||||
func (s *BaseServer) DealClient(c *conn.Conn, addr string, rb []byte, tp string) error {
|
||||
link := conn.NewLink(tp, addr, s.task.Client.Cnf.Crypt, s.task.Client.Cnf.Compress, c.Conn.RemoteAddr().String())
|
||||
|
||||
if tunnel, err := s.bridge.SendLinkInfo(s.task.Client.Id, link, c.Conn.RemoteAddr().String()); err != nil {
|
||||
if target, err := s.bridge.SendLinkInfo(s.task.Client.Id, link, c.Conn.RemoteAddr().String()); err != nil {
|
||||
c.Close()
|
||||
return err
|
||||
} else {
|
||||
link.RunWrite()
|
||||
s.linkCopy(link, c, rb, tunnel, s.task.Flow)
|
||||
conn.CopyWaitGroup(target, c, link.Crypt, link.Compress, s.task.Client.Rate, s.task.Client.Flow)
|
||||
}
|
||||
|
||||
s.task.Client.AddConn()
|
||||
return nil
|
||||
}
|
||||
|
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/cnlh/nps/lib/file"
|
||||
"github.com/cnlh/nps/vender/github.com/astaxie/beego"
|
||||
"github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"os"
|
||||
@@ -116,17 +117,16 @@ func (s *httpServer) process(c *conn.Conn, r *http.Request) {
|
||||
//多客户端域名代理
|
||||
var (
|
||||
isConn = true
|
||||
lk *conn.Link
|
||||
host *file.Host
|
||||
tunnel *conn.Conn
|
||||
target net.Conn
|
||||
lastHost *file.Host
|
||||
err error
|
||||
)
|
||||
if host, err = file.GetCsvDb().GetInfoByHost(r.Host, r); err != nil {
|
||||
logs.Notice("the url %s %s can't be parsed!", r.Host, r.RequestURI)
|
||||
goto end
|
||||
} else if !host.Client.GetConn() {
|
||||
logs.Notice("Connections exceed the current client %d limit", host.Client.Id)
|
||||
} else if !host.Client.GetConn() { //conn num limit
|
||||
logs.Notice("Connections exceed the current client %d limit %d ,now connection num %d", host.Client.Id, host.Client.MaxConn, host.Client.NowConn)
|
||||
c.Close()
|
||||
return
|
||||
} else {
|
||||
@@ -138,20 +138,26 @@ func (s *httpServer) process(c *conn.Conn, r *http.Request) {
|
||||
if isConn {
|
||||
//流量限制
|
||||
if host.Client.Flow.FlowLimit > 0 && (host.Client.Flow.FlowLimit<<20) < (host.Client.Flow.ExportFlow+host.Client.Flow.InletFlow) {
|
||||
logs.Warn("Traffic exceeded client id %s", host.Client.Id)
|
||||
break
|
||||
}
|
||||
host.Client.Cnf.CompressDecode, host.Client.Cnf.CompressEncode = common.GetCompressType(host.Client.Cnf.Compress)
|
||||
//权限控制
|
||||
if err = s.auth(r, c, host.Client.Cnf.U, host.Client.Cnf.P); err != nil {
|
||||
logs.Warn("auth error", err, r.RemoteAddr)
|
||||
break
|
||||
}
|
||||
lk = conn.NewLink(host.Client.GetId(), common.CONN_TCP, host.GetRandomTarget(), host.Client.Cnf.CompressEncode, host.Client.Cnf.CompressDecode, host.Client.Cnf.Crypt, c, host.Flow, nil, host.Client.Rate, nil)
|
||||
if tunnel, err = s.bridge.SendLinkInfo(host.Client.Id, lk, c.Conn.RemoteAddr().String()); err != nil {
|
||||
logs.Notice(err)
|
||||
lk := conn.NewLink(common.CONN_TCP, host.Target, host.Client.Cnf.Crypt, host.Client.Cnf.Compress, r.RemoteAddr)
|
||||
if target, err = s.bridge.SendLinkInfo(host.Client.Id, lk, c.Conn.RemoteAddr().String()); err != nil {
|
||||
logs.Notice("connect to target %s error %s", lk.Host, err)
|
||||
break
|
||||
}
|
||||
lk.RunWrite()
|
||||
isConn = false
|
||||
go func() {
|
||||
w, _ := common.CopyBuffer(c, conn.GetConn(target, lk.Crypt, lk.Compress, host.Client.Rate))
|
||||
host.Flow.Add(0, w)
|
||||
c.Close()
|
||||
target.Close()
|
||||
}()
|
||||
} else {
|
||||
r, err = http.ReadRequest(bufio.NewReader(c))
|
||||
if err != nil {
|
||||
@@ -174,20 +180,18 @@ func (s *httpServer) process(c *conn.Conn, r *http.Request) {
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
host.Flow.Add(len(b), 0)
|
||||
if _, err := tunnel.SendMsg(b, lk); err != nil {
|
||||
c.Close()
|
||||
break
|
||||
}
|
||||
<-lk.StatusCh
|
||||
host.Flow.Add(int64(len(b)), 0)
|
||||
//write
|
||||
target.Write(b)
|
||||
}
|
||||
end:
|
||||
if isConn {
|
||||
s.writeConnFail(c.Conn)
|
||||
} else {
|
||||
tunnel.SendMsg([]byte(common.IO_EOF), lk)
|
||||
}
|
||||
c.Close()
|
||||
if target != nil {
|
||||
target.Close()
|
||||
}
|
||||
if host != nil {
|
||||
host.Client.AddConn()
|
||||
}
|
||||
|
@@ -48,7 +48,6 @@ func (s *P2PServer) Start() error {
|
||||
}
|
||||
|
||||
func (s *P2PServer) p2pProcess(c *conn.Conn) {
|
||||
logs.Warn("new link", c.Conn.RemoteAddr())
|
||||
//获取密钥
|
||||
var (
|
||||
f string
|
||||
@@ -57,19 +56,17 @@ func (s *P2PServer) p2pProcess(c *conn.Conn) {
|
||||
v *p2p
|
||||
ok bool
|
||||
)
|
||||
if b, err = c.ReadLen(32); err != nil {
|
||||
if b, err = c.GetShortContent(32); err != nil {
|
||||
return
|
||||
}
|
||||
//获取角色
|
||||
if f, err = c.ReadFlag(); err != nil {
|
||||
return
|
||||
}
|
||||
logs.Warn("收到", string(b), f)
|
||||
if v, ok = s.p2p[string(b)]; !ok {
|
||||
v = new(p2p)
|
||||
s.p2p[string(b)] = v
|
||||
}
|
||||
logs.Warn(f, c.Conn.RemoteAddr().String())
|
||||
//存储
|
||||
if f == common.WORK_P2P_VISITOR {
|
||||
v.visitorAddr = c.Conn.RemoteAddr().String()
|
||||
@@ -80,13 +77,10 @@ func (s *P2PServer) p2pProcess(c *conn.Conn) {
|
||||
break
|
||||
}
|
||||
}
|
||||
logs.Warn("等待确认")
|
||||
if _, err := v.provider.ReadFlag(); err == nil {
|
||||
v.visitor.WriteLenContent([]byte(v.providerAddr))
|
||||
logs.Warn("收到确认")
|
||||
delete(s.p2p, string(b))
|
||||
} else {
|
||||
logs.Warn("收到确认失败", err)
|
||||
}
|
||||
} else {
|
||||
v.providerAddr = c.Conn.RemoteAddr().String()
|
||||
@@ -99,6 +93,4 @@ func (s *P2PServer) p2pProcess(c *conn.Conn) {
|
||||
}
|
||||
}
|
||||
}
|
||||
//假设是连接者、等待对应的被连接者连上后,发送被连接者信息
|
||||
//假设是被连接者,等待对应的连接者脸上后,发送连接者信息
|
||||
}
|
||||
|
@@ -141,16 +141,18 @@ func (s *Sock5ModeServer) doConnect(c net.Conn, command uint8) {
|
||||
} else {
|
||||
ltype = common.CONN_TCP
|
||||
}
|
||||
link := conn.NewLink(s.task.Client.GetId(), ltype, addr, s.task.Client.Cnf.CompressEncode, s.task.Client.Cnf.CompressDecode, s.task.Client.Cnf.Crypt, conn.NewConn(c), s.task.Flow, nil, s.task.Client.Rate, nil)
|
||||
//s.DealClient(conn.NewConn(c), addr, nil, ltype)
|
||||
link := conn.NewLink(ltype, addr, s.task.Client.Cnf.Crypt, s.task.Client.Cnf.Compress, c.RemoteAddr().String())
|
||||
|
||||
if tunnel, err := s.bridge.SendLinkInfo(s.task.Client.Id, link, c.RemoteAddr().String()); err != nil {
|
||||
if target, err := s.bridge.SendLinkInfo(s.task.Client.Id, link, c.RemoteAddr().String()); err != nil {
|
||||
c.Close()
|
||||
return
|
||||
} else {
|
||||
s.sendReply(c, succeeded)
|
||||
link.RunWrite()
|
||||
s.linkCopy(link, conn.NewConn(c), nil, tunnel, s.task.Flow)
|
||||
conn.CopyWaitGroup(target, c, link.Crypt, link.Compress, s.task.Client.Rate, s.task.Client.Flow)
|
||||
}
|
||||
|
||||
s.task.Client.AddConn()
|
||||
return
|
||||
}
|
||||
|
||||
@@ -272,6 +274,10 @@ func (s *Sock5ModeServer) Start() error {
|
||||
}
|
||||
logs.Warn("accept error: ", err)
|
||||
}
|
||||
if err := s.checkFlow(); err != nil {
|
||||
logs.Warn("client id %d task id %d error %s", s.task.Client.Id, s.task.Id, err.Error())
|
||||
conn.Close()
|
||||
}
|
||||
if s.task.Client.GetConn() {
|
||||
logs.Trace("New socks5 connection,client %d,remote address %s", s.task.Client.Id, conn.RemoteAddr())
|
||||
go s.handleConn(conn)
|
||||
|
@@ -45,6 +45,10 @@ func (s *TunnelModeServer) Start() error {
|
||||
logs.Info(err)
|
||||
continue
|
||||
}
|
||||
if err := s.checkFlow(); err != nil {
|
||||
logs.Warn("client id %d task id %d error %s", s.task.Client.Id, s.task.Id, err.Error())
|
||||
c.Close()
|
||||
}
|
||||
if s.task.Client.GetConn() {
|
||||
logs.Trace("New tcp connection,client %d,remote address %s", s.task.Client.Id, c.RemoteAddr())
|
||||
go s.process(conn.NewConn(c), s)
|
||||
@@ -69,6 +73,10 @@ type WebServer struct {
|
||||
//开始
|
||||
func (s *WebServer) Start() error {
|
||||
p, _ := beego.AppConfig.Int("httpport")
|
||||
if p == 0 {
|
||||
stop := make(chan struct{})
|
||||
<-stop
|
||||
}
|
||||
if !common.TestTcpPort(p) {
|
||||
logs.Error("Web management port %d is occupied", p)
|
||||
os.Exit(0)
|
||||
@@ -96,7 +104,7 @@ type process func(c *conn.Conn, s *TunnelModeServer) error
|
||||
|
||||
//tcp隧道模式
|
||||
func ProcessTunnel(c *conn.Conn, s *TunnelModeServer) error {
|
||||
return s.DealClient(c, s.task.Target, nil)
|
||||
return s.DealClient(c, s.task.Target, nil, common.CONN_TCP)
|
||||
}
|
||||
|
||||
//http代理模式
|
||||
@@ -114,5 +122,5 @@ func ProcessHttp(c *conn.Conn, s *TunnelModeServer) error {
|
||||
if err := s.auth(r, c, s.task.Client.Cnf.U, s.task.Client.Cnf.P); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.DealClient(c, addr, rb)
|
||||
return s.DealClient(c, addr, rb, common.CONN_TCP)
|
||||
}
|
||||
|
@@ -14,13 +14,11 @@ import (
|
||||
type UdpModeServer struct {
|
||||
BaseServer
|
||||
listener *net.UDPConn
|
||||
udpMap map[string]*conn.Conn
|
||||
}
|
||||
|
||||
func NewUdpModeServer(bridge *bridge.Bridge, task *file.Tunnel) *UdpModeServer {
|
||||
s := new(UdpModeServer)
|
||||
s.bridge = bridge
|
||||
s.udpMap = make(map[string]*conn.Conn)
|
||||
s.task = task
|
||||
return s
|
||||
}
|
||||
@@ -41,24 +39,31 @@ func (s *UdpModeServer) Start() error {
|
||||
}
|
||||
continue
|
||||
}
|
||||
logs.Trace("New ydo connection,client %d,remote address %s", s.task.Client.Id, addr)
|
||||
logs.Trace("New udp connection,client %d,remote address %s", s.task.Client.Id, addr)
|
||||
go s.process(addr, buf[:n])
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *UdpModeServer) process(addr *net.UDPAddr, data []byte) {
|
||||
link := conn.NewLink(s.task.Client.GetId(), common.CONN_UDP, s.task.Target, s.task.Client.Cnf.CompressEncode, s.task.Client.Cnf.CompressDecode, s.task.Client.Cnf.Crypt, nil, s.task.Flow, s.listener, s.task.Client.Rate, addr)
|
||||
link := conn.NewLink(common.CONN_UDP, s.task.Target, s.task.Client.Cnf.Crypt, s.task.Client.Cnf.Compress, addr.String())
|
||||
if err := s.checkFlow(); err != nil {
|
||||
return
|
||||
}
|
||||
if tunnel, err := s.bridge.SendLinkInfo(s.task.Client.Id, link, addr.String()); err != nil {
|
||||
if target, err := s.bridge.SendLinkInfo(s.task.Client.Id, link, addr.String()); err != nil {
|
||||
return
|
||||
} else {
|
||||
s.task.Flow.Add(len(data), 0)
|
||||
tunnel.SendMsg(data, link)
|
||||
pool.PutBufPoolUdp(data)
|
||||
link.RunWrite()
|
||||
s.task.Flow.Add(int64(len(data)), 0)
|
||||
buf := pool.BufPoolUdp.Get().([]byte)
|
||||
defer pool.BufPoolUdp.Put(buf)
|
||||
target.Write(data)
|
||||
if n, err := target.Read(buf); err != nil {
|
||||
logs.Warn(err)
|
||||
return
|
||||
} else {
|
||||
s.listener.WriteTo(buf[:n], addr)
|
||||
s.task.Flow.Add(0, int64(n))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
142
server/server.go
142
server/server.go
@@ -9,17 +9,26 @@ import (
|
||||
"github.com/cnlh/nps/server/tool"
|
||||
"github.com/cnlh/nps/vender/github.com/astaxie/beego"
|
||||
"github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
|
||||
"github.com/shirou/gopsutil/cpu"
|
||||
"github.com/shirou/gopsutil/load"
|
||||
"github.com/shirou/gopsutil/mem"
|
||||
"github.com/shirou/gopsutil/net"
|
||||
"math"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
Bridge *bridge.Bridge
|
||||
RunList map[int]interface{} //运行中的任务
|
||||
Bridge *bridge.Bridge
|
||||
RunList map[int]interface{} //运行中的任务
|
||||
serverStatus []map[string]interface{}
|
||||
)
|
||||
|
||||
func init() {
|
||||
RunList = make(map[int]interface{})
|
||||
serverStatus = make([]map[string]interface{}, 0, 1500)
|
||||
go getSeverStatus()
|
||||
}
|
||||
|
||||
//从csv文件中恢复任务
|
||||
@@ -53,7 +62,7 @@ func DealBridgeTask() {
|
||||
logs.Info("Connections exceed the current client %d limit", t.Client.Id)
|
||||
s.Conn.Close()
|
||||
} else if t.Status {
|
||||
go proxy.NewBaseServer(Bridge, t).DealClient(s.Conn, t.Target, nil)
|
||||
go proxy.NewBaseServer(Bridge, t).DealClient(s.Conn, t.Target, nil, common.CONN_TCP)
|
||||
} else {
|
||||
s.Conn.Close()
|
||||
logs.Trace("This key %s cannot be processed,status is close", s.Password)
|
||||
@@ -76,6 +85,7 @@ func StartNewServer(bridgePort int, cnf *file.Tunnel, bridgeType string) {
|
||||
logs.Info("Server startup, the bridge type is %s, the bridge port is %d", bridgeType, bridgePort)
|
||||
}
|
||||
if p, err := beego.AppConfig.Int("p2pPort"); err == nil {
|
||||
logs.Info("start p2p server port", p)
|
||||
go proxy.NewP2PServer(p).Start()
|
||||
}
|
||||
go DealBridgeTask()
|
||||
@@ -93,13 +103,13 @@ func StartNewServer(bridgePort int, cnf *file.Tunnel, bridgeType string) {
|
||||
func NewMode(Bridge *bridge.Bridge, c *file.Tunnel) proxy.Service {
|
||||
var service proxy.Service
|
||||
switch c.Mode {
|
||||
case "tcpServer":
|
||||
case "tcp":
|
||||
service = proxy.NewTunnelModeServer(proxy.ProcessTunnel, Bridge, c)
|
||||
case "socks5Server":
|
||||
case "socks5":
|
||||
service = proxy.NewSock5ModeServer(Bridge, c)
|
||||
case "httpProxyServer":
|
||||
case "httpProxy":
|
||||
service = proxy.NewTunnelModeServer(proxy.ProcessHttp, Bridge, c)
|
||||
case "udpServer":
|
||||
case "udp":
|
||||
service = proxy.NewUdpModeServer(Bridge, c)
|
||||
case "webServer":
|
||||
InitFromCsv()
|
||||
@@ -139,7 +149,7 @@ func StopServer(id int) error {
|
||||
|
||||
//add task
|
||||
func AddTask(t *file.Tunnel) error {
|
||||
if t.Mode == "secretServer" {
|
||||
if t.Mode == "secret" || t.Mode == "p2p" {
|
||||
logs.Info("secret task %s start ", t.Remark)
|
||||
RunList[t.Id] = nil
|
||||
return nil
|
||||
@@ -269,11 +279,11 @@ func DelTunnelAndHostByClientId(clientId int) {
|
||||
|
||||
//关闭客户端连接
|
||||
func DelClientConnect(clientId int) {
|
||||
Bridge.DelClient(clientId)
|
||||
Bridge.DelClient(clientId, false)
|
||||
}
|
||||
|
||||
func GetDashboardData() map[string]int {
|
||||
data := make(map[string]int)
|
||||
func GetDashboardData() map[string]interface{} {
|
||||
data := make(map[string]interface{})
|
||||
data["hostCount"] = len(file.GetCsvDb().Hosts)
|
||||
data["clientCount"] = len(file.GetCsvDb().Clients) - 1 //Remove the public key client
|
||||
list := file.GetCsvDb().Clients
|
||||
@@ -290,23 +300,74 @@ func GetDashboardData() map[string]int {
|
||||
data["clientOnlineCount"] = c
|
||||
data["inletFlowCount"] = int(in)
|
||||
data["exportFlowCount"] = int(out)
|
||||
var tcp, udp, secret, socks5, p2p, http int
|
||||
for _, v := range file.GetCsvDb().Tasks {
|
||||
switch v.Mode {
|
||||
case "tcpServer":
|
||||
data["tcpServerCount"] += 1
|
||||
case "socks5Server":
|
||||
data["socks5ServerCount"] += 1
|
||||
case "httpProxyServer":
|
||||
data["httpProxyServerCount"] += 1
|
||||
case "udpServer":
|
||||
data["udpServerCount"] += 1
|
||||
case "tcp":
|
||||
tcp += 1
|
||||
case "socks5":
|
||||
udp += 1
|
||||
case "httpProxy":
|
||||
http += 1
|
||||
case "udp":
|
||||
udp += 1
|
||||
case "p2p":
|
||||
p2p += 1
|
||||
case "secret":
|
||||
secret += 1
|
||||
}
|
||||
}
|
||||
data["tcpC"] = tcp
|
||||
data["udpCount"] = udp
|
||||
data["socks5Count"] = socks5
|
||||
data["httpProxyCount"] = http
|
||||
data["secretCount"] = secret
|
||||
data["p2pCount"] = p2p
|
||||
data["bridgeType"] = beego.AppConfig.String("bridgeType")
|
||||
data["httpProxyPort"] = beego.AppConfig.String("httpProxyPort")
|
||||
data["httpsProxyPort"] = beego.AppConfig.String("httpsProxyPort")
|
||||
data["ipLimit"] = beego.AppConfig.String("ipLimit")
|
||||
data["flowStoreInterval"] = beego.AppConfig.String("flowStoreInterval")
|
||||
data["serverIp"] = beego.AppConfig.String("serverIp")
|
||||
data["p2pPort"] = beego.AppConfig.String("p2pPort")
|
||||
data["logLevel"] = beego.AppConfig.String("logLevel")
|
||||
tcpCount := 0
|
||||
for _, v := range file.GetCsvDb().Clients {
|
||||
tcpCount += v.NowConn
|
||||
}
|
||||
data["tcpCount"] = tcpCount
|
||||
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)
|
||||
conn, _ := net.ProtoCounters(nil)
|
||||
io1, _ := net.IOCounters(false)
|
||||
time.Sleep(time.Millisecond * 500)
|
||||
io2, _ := net.IOCounters(false)
|
||||
if len(io2) > 0 && len(io1) > 0 {
|
||||
data["io_send"] = (io2[0].BytesSent - io1[0].BytesSent) * 2
|
||||
data["io_recv"] = (io2[0].BytesRecv - io1[0].BytesRecv) * 2
|
||||
}
|
||||
for _, v := range conn {
|
||||
data[v.Protocol] = v.Stats["CurrEstab"]
|
||||
}
|
||||
//chart
|
||||
var fg int
|
||||
if len(serverStatus) >= 10 {
|
||||
fg = len(serverStatus) / 10
|
||||
for i := 0; i <= 9; i++ {
|
||||
data["sys"+strconv.Itoa(i+1)] = serverStatus[i*fg]
|
||||
}
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
@@ -320,3 +381,46 @@ func flowSession(m time.Duration) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func getSeverStatus() {
|
||||
for {
|
||||
if len(serverStatus) < 10 {
|
||||
time.Sleep(time.Second)
|
||||
} else {
|
||||
time.Sleep(time.Minute)
|
||||
}
|
||||
cpuPercet, _ := cpu.Percent(0, true)
|
||||
var cpuAll float64
|
||||
for _, v := range cpuPercet {
|
||||
cpuAll += v
|
||||
}
|
||||
m := make(map[string]interface{})
|
||||
loads, _ := load.Avg()
|
||||
m["load1"] = loads.Load1
|
||||
m["load5"] = loads.Load5
|
||||
m["load15"] = loads.Load15
|
||||
m["cpu"] = math.Round(cpuAll / float64(len(cpuPercet)))
|
||||
swap, _ := mem.SwapMemory()
|
||||
m["swap_mem"] = math.Round(swap.UsedPercent)
|
||||
vir, _ := mem.VirtualMemory()
|
||||
m["virtual_mem"] = math.Round(vir.UsedPercent)
|
||||
conn, _ := net.ProtoCounters(nil)
|
||||
io1, _ := net.IOCounters(false)
|
||||
time.Sleep(time.Millisecond * 500)
|
||||
io2, _ := net.IOCounters(false)
|
||||
if len(io2) > 0 && len(io1) > 0 {
|
||||
m["io_send"] = (io2[0].BytesSent - io1[0].BytesSent) * 2
|
||||
m["io_recv"] = (io2[0].BytesRecv - io1[0].BytesRecv) * 2
|
||||
}
|
||||
t := time.Now()
|
||||
m["time"] = strconv.Itoa(t.Hour()) + ":" + strconv.Itoa(t.Minute()) + ":" + strconv.Itoa(t.Second())
|
||||
|
||||
for _, v := range conn {
|
||||
m[v.Protocol] = v.Stats["CurrEstab"]
|
||||
}
|
||||
if len(serverStatus) >= 1440 {
|
||||
serverStatus = serverStatus[1:]
|
||||
}
|
||||
serverStatus = append(serverStatus, m)
|
||||
}
|
||||
}
|
||||
|
@@ -12,7 +12,7 @@ func TestServerConfig() {
|
||||
var postTcpArr []int
|
||||
var postUdpArr []int
|
||||
for _, v := range file.GetCsvDb().Tasks {
|
||||
if v.Mode == "udpServer" {
|
||||
if v.Mode == "udp" {
|
||||
isInArr(&postUdpArr, v.Port, v.Remark, "udp")
|
||||
} else {
|
||||
isInArr(&postTcpArr, v.Port, v.Remark, "tcp")
|
||||
@@ -72,7 +72,6 @@ func isInArr(arr *[]int, val int, remark string, tp string) {
|
||||
log.Fatalf("open the %d port error ,remark: %s", val, remark)
|
||||
}
|
||||
}
|
||||
|
||||
*arr = append(*arr, val)
|
||||
return
|
||||
}
|
||||
|
@@ -13,7 +13,7 @@ func init() {
|
||||
}
|
||||
|
||||
func TestServerPort(p int, m string) (b bool) {
|
||||
if p > 65535 || p <= 0 {
|
||||
if p > 65535 || p < 0 {
|
||||
return false
|
||||
}
|
||||
if len(ports) != 0 {
|
||||
@@ -21,7 +21,7 @@ func TestServerPort(p int, m string) (b bool) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
if m == "udpServer" {
|
||||
if m == "udp" {
|
||||
b = common.TestUdpPort(p)
|
||||
} else {
|
||||
b = common.TestTcpPort(p)
|
||||
|
Reference in New Issue
Block a user