2019-01-09 04:33:00 -08:00
|
|
|
|
package server
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
2019-02-02 20:40:43 -08:00
|
|
|
|
"github.com/cnlh/nps/bridge"
|
2019-02-16 04:43:26 -08:00
|
|
|
|
"github.com/cnlh/nps/lib/common"
|
2019-02-09 01:07:47 -08:00
|
|
|
|
"github.com/cnlh/nps/lib/file"
|
2019-02-12 11:54:00 -08:00
|
|
|
|
"github.com/cnlh/nps/server/proxy"
|
|
|
|
|
"github.com/cnlh/nps/server/tool"
|
2019-02-16 04:43:26 -08:00
|
|
|
|
"github.com/cnlh/nps/vender/github.com/astaxie/beego"
|
2019-02-23 07:29:48 -08:00
|
|
|
|
"github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
|
2019-03-01 01:23:14 -08:00
|
|
|
|
"github.com/shirou/gopsutil/cpu"
|
|
|
|
|
"github.com/shirou/gopsutil/load"
|
|
|
|
|
"github.com/shirou/gopsutil/mem"
|
|
|
|
|
"github.com/shirou/gopsutil/net"
|
|
|
|
|
"math"
|
2019-02-23 07:29:48 -08:00
|
|
|
|
"os"
|
2019-03-01 01:23:14 -08:00
|
|
|
|
"strconv"
|
2019-02-23 07:29:48 -08:00
|
|
|
|
"time"
|
2019-01-09 04:33:00 -08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2019-03-01 01:23:14 -08:00
|
|
|
|
Bridge *bridge.Bridge
|
|
|
|
|
RunList map[int]interface{} //运行中的任务
|
|
|
|
|
serverStatus []map[string]interface{}
|
2019-01-09 04:33:00 -08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func init() {
|
2019-01-24 20:10:12 -08:00
|
|
|
|
RunList = make(map[int]interface{})
|
2019-03-01 01:23:14 -08:00
|
|
|
|
serverStatus = make([]map[string]interface{}, 0, 1500)
|
|
|
|
|
go getSeverStatus()
|
2019-01-09 04:33:00 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//从csv文件中恢复任务
|
|
|
|
|
func InitFromCsv() {
|
2019-02-12 11:54:00 -08:00
|
|
|
|
//Add a public password
|
2019-02-16 04:43:26 -08:00
|
|
|
|
if vkey := beego.AppConfig.String("publicVkey"); vkey != "" {
|
|
|
|
|
c := file.NewClient(vkey, true, true)
|
|
|
|
|
file.GetCsvDb().NewClient(c)
|
|
|
|
|
RunList[c.Id] = nil
|
|
|
|
|
}
|
2019-02-12 11:54:00 -08:00
|
|
|
|
//Initialize services in server-side files
|
2019-02-09 01:07:47 -08:00
|
|
|
|
for _, v := range file.GetCsvDb().Tasks {
|
2019-01-26 01:27:28 -08:00
|
|
|
|
if v.Status {
|
2019-01-09 04:33:00 -08:00
|
|
|
|
AddTask(v)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-17 09:05:05 -08:00
|
|
|
|
|
2019-02-12 11:54:00 -08:00
|
|
|
|
func DealBridgeTask() {
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case t := <-Bridge.OpenTask:
|
|
|
|
|
AddTask(t)
|
|
|
|
|
case id := <-Bridge.CloseClient:
|
|
|
|
|
DelTunnelAndHostByClientId(id)
|
|
|
|
|
file.GetCsvDb().DelClient(id)
|
2019-02-23 07:29:48 -08:00
|
|
|
|
case s := <-Bridge.SecretChan:
|
|
|
|
|
logs.Trace("New secret connection, addr", s.Conn.Conn.RemoteAddr())
|
2019-02-26 06:40:28 -08:00
|
|
|
|
if t := file.GetCsvDb().GetTaskByMd5Password(s.Password); t != nil {
|
2019-02-23 07:29:48 -08:00
|
|
|
|
if !t.Client.GetConn() {
|
|
|
|
|
logs.Info("Connections exceed the current client %d limit", t.Client.Id)
|
|
|
|
|
s.Conn.Close()
|
2019-02-23 21:17:43 -08:00
|
|
|
|
} else if t.Status {
|
2019-03-01 01:23:14 -08:00
|
|
|
|
go proxy.NewBaseServer(Bridge, t).DealClient(s.Conn, t.Target, nil, common.CONN_TCP)
|
2019-02-23 21:17:43 -08:00
|
|
|
|
} else {
|
|
|
|
|
s.Conn.Close()
|
|
|
|
|
logs.Trace("This key %s cannot be processed,status is close", s.Password)
|
2019-02-23 07:29:48 -08:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
logs.Trace("This key %s cannot be processed", s.Password)
|
|
|
|
|
s.Conn.Close()
|
|
|
|
|
}
|
2019-02-12 11:54:00 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-09 04:33:00 -08:00
|
|
|
|
|
|
|
|
|
//start a new server
|
2019-02-09 01:07:47 -08:00
|
|
|
|
func StartNewServer(bridgePort int, cnf *file.Tunnel, bridgeType string) {
|
2019-02-16 07:18:58 -08:00
|
|
|
|
Bridge = bridge.NewTunnel(bridgePort, bridgeType, common.GetBoolByStr(beego.AppConfig.String("ipLimit")), RunList)
|
2019-02-05 08:35:23 -08:00
|
|
|
|
if err := Bridge.StartTunnel(); err != nil {
|
2019-02-23 07:29:48 -08:00
|
|
|
|
logs.Error("服务端开启失败", err)
|
|
|
|
|
os.Exit(0)
|
2019-02-12 11:54:00 -08:00
|
|
|
|
} else {
|
2019-02-23 07:29:48 -08:00
|
|
|
|
logs.Info("Server startup, the bridge type is %s, the bridge port is %d", bridgeType, bridgePort)
|
2019-02-05 08:35:23 -08:00
|
|
|
|
}
|
2019-02-26 06:40:28 -08:00
|
|
|
|
if p, err := beego.AppConfig.Int("p2pPort"); err == nil {
|
2019-03-01 01:23:14 -08:00
|
|
|
|
logs.Info("start p2p server port", p)
|
2019-02-26 06:40:28 -08:00
|
|
|
|
go proxy.NewP2PServer(p).Start()
|
|
|
|
|
}
|
2019-02-12 11:54:00 -08:00
|
|
|
|
go DealBridgeTask()
|
2019-02-05 08:35:23 -08:00
|
|
|
|
if svr := NewMode(Bridge, cnf); svr != nil {
|
2019-02-17 09:05:05 -08:00
|
|
|
|
if err := svr.Start(); err != nil {
|
2019-02-23 07:29:48 -08:00
|
|
|
|
logs.Error(err)
|
2019-01-09 04:33:00 -08:00
|
|
|
|
}
|
2019-02-17 09:05:05 -08:00
|
|
|
|
RunList[cnf.Id] = svr
|
2019-02-05 08:35:23 -08:00
|
|
|
|
} else {
|
2019-02-23 07:29:48 -08:00
|
|
|
|
logs.Error("Incorrect startup mode %s", cnf.Mode)
|
2019-01-09 04:33:00 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//new a server by mode name
|
2019-02-17 09:05:05 -08:00
|
|
|
|
func NewMode(Bridge *bridge.Bridge, c *file.Tunnel) proxy.Service {
|
|
|
|
|
var service proxy.Service
|
2019-01-26 01:27:28 -08:00
|
|
|
|
switch c.Mode {
|
2019-03-01 01:23:14 -08:00
|
|
|
|
case "tcp":
|
2019-02-17 09:05:05 -08:00
|
|
|
|
service = proxy.NewTunnelModeServer(proxy.ProcessTunnel, Bridge, c)
|
2019-03-01 01:23:14 -08:00
|
|
|
|
case "socks5":
|
2019-02-17 09:05:05 -08:00
|
|
|
|
service = proxy.NewSock5ModeServer(Bridge, c)
|
2019-03-01 01:23:14 -08:00
|
|
|
|
case "httpProxy":
|
2019-02-17 09:05:05 -08:00
|
|
|
|
service = proxy.NewTunnelModeServer(proxy.ProcessHttp, Bridge, c)
|
2019-03-01 01:23:14 -08:00
|
|
|
|
case "udp":
|
2019-02-17 09:05:05 -08:00
|
|
|
|
service = proxy.NewUdpModeServer(Bridge, c)
|
2019-01-09 04:33:00 -08:00
|
|
|
|
case "webServer":
|
|
|
|
|
InitFromCsv()
|
2019-02-09 01:07:47 -08:00
|
|
|
|
t := &file.Tunnel{
|
2019-02-12 11:54:00 -08:00
|
|
|
|
Port: 0,
|
|
|
|
|
Mode: "httpHostServer",
|
|
|
|
|
Target: "",
|
|
|
|
|
Status: true,
|
2019-01-09 04:33:00 -08:00
|
|
|
|
}
|
|
|
|
|
AddTask(t)
|
2019-02-17 09:05:05 -08:00
|
|
|
|
service = proxy.NewWebServer(Bridge)
|
2019-01-09 04:33:00 -08:00
|
|
|
|
case "httpHostServer":
|
2019-02-17 09:05:05 -08:00
|
|
|
|
service = proxy.NewHttp(Bridge, c)
|
2019-01-09 04:33:00 -08:00
|
|
|
|
}
|
2019-02-17 09:05:05 -08:00
|
|
|
|
return service
|
2019-01-09 04:33:00 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//stop server
|
2019-01-24 20:10:12 -08:00
|
|
|
|
func StopServer(id int) error {
|
|
|
|
|
if v, ok := RunList[id]; ok {
|
2019-02-17 09:05:05 -08:00
|
|
|
|
if svr, ok := v.(proxy.Service); ok {
|
|
|
|
|
if err := svr.Close(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2019-02-23 07:29:48 -08:00
|
|
|
|
}
|
|
|
|
|
if t, err := file.GetCsvDb().GetTask(id); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else {
|
|
|
|
|
t.Status = false
|
|
|
|
|
file.GetCsvDb().UpdateTask(t)
|
2019-01-09 04:33:00 -08:00
|
|
|
|
}
|
2019-02-12 11:54:00 -08:00
|
|
|
|
delete(RunList, id)
|
2019-01-09 04:33:00 -08:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return errors.New("未在运行中")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//add task
|
2019-02-09 01:07:47 -08:00
|
|
|
|
func AddTask(t *file.Tunnel) error {
|
2019-03-01 01:23:14 -08:00
|
|
|
|
if t.Mode == "secret" || t.Mode == "p2p" {
|
2019-02-23 07:29:48 -08:00
|
|
|
|
logs.Info("secret task %s start ", t.Remark)
|
|
|
|
|
RunList[t.Id] = nil
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2019-02-12 11:54:00 -08:00
|
|
|
|
if b := tool.TestServerPort(t.Port, t.Mode); !b && t.Mode != "httpHostServer" {
|
2019-02-23 07:29:48 -08:00
|
|
|
|
logs.Error("taskId %d start error port %d open failed", t.Id, t.Port)
|
2019-02-15 06:59:28 -08:00
|
|
|
|
return errors.New("the port open error")
|
2019-02-12 11:54:00 -08:00
|
|
|
|
}
|
2019-02-23 07:29:48 -08:00
|
|
|
|
if minute, err := beego.AppConfig.Int("flowStoreInterval"); err == nil && minute > 0 {
|
|
|
|
|
go flowSession(time.Minute * time.Duration(minute))
|
|
|
|
|
}
|
2019-01-09 04:33:00 -08:00
|
|
|
|
if svr := NewMode(Bridge, t); svr != nil {
|
2019-02-23 07:29:48 -08:00
|
|
|
|
logs.Info("tunnel task %s start mode:%s port %d", t.Remark, t.Mode, t.Port)
|
2019-01-24 20:10:12 -08:00
|
|
|
|
RunList[t.Id] = svr
|
2019-01-09 04:33:00 -08:00
|
|
|
|
go func() {
|
2019-02-17 09:05:05 -08:00
|
|
|
|
if err := svr.Start(); err != nil {
|
2019-02-23 07:29:48 -08:00
|
|
|
|
logs.Error("clientId %d taskId %d start error %s", t.Client.Id, t.Id, err)
|
2019-01-24 20:10:12 -08:00
|
|
|
|
delete(RunList, t.Id)
|
2019-02-12 11:54:00 -08:00
|
|
|
|
return
|
2019-01-09 04:33:00 -08:00
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
} else {
|
2019-02-12 11:54:00 -08:00
|
|
|
|
return errors.New("the mode is not correct")
|
2019-01-09 04:33:00 -08:00
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//start task
|
2019-01-24 20:10:12 -08:00
|
|
|
|
func StartTask(id int) error {
|
2019-02-09 01:07:47 -08:00
|
|
|
|
if t, err := file.GetCsvDb().GetTask(id); err != nil {
|
2019-01-09 04:33:00 -08:00
|
|
|
|
return err
|
|
|
|
|
} else {
|
|
|
|
|
AddTask(t)
|
2019-01-26 01:27:28 -08:00
|
|
|
|
t.Status = true
|
2019-02-09 01:07:47 -08:00
|
|
|
|
file.GetCsvDb().UpdateTask(t)
|
2019-01-09 04:33:00 -08:00
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//delete task
|
2019-01-24 20:10:12 -08:00
|
|
|
|
func DelTask(id int) error {
|
2019-02-12 11:54:00 -08:00
|
|
|
|
if _, ok := RunList[id]; ok {
|
|
|
|
|
if err := StopServer(id); err != nil {
|
|
|
|
|
return err
|
2019-01-09 04:33:00 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-12 11:54:00 -08:00
|
|
|
|
return file.GetCsvDb().DelTask(id)
|
2019-01-09 04:33:00 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//get task list by page num
|
2019-02-09 01:07:47 -08:00
|
|
|
|
func GetTunnel(start, length int, typeVal string, clientId int) ([]*file.Tunnel, int) {
|
|
|
|
|
list := make([]*file.Tunnel, 0)
|
2019-01-09 04:33:00 -08:00
|
|
|
|
var cnt int
|
2019-02-09 01:07:47 -08:00
|
|
|
|
for _, v := range file.GetCsvDb().Tasks {
|
2019-01-26 01:27:28 -08:00
|
|
|
|
if (typeVal != "" && v.Mode != typeVal) || (typeVal == "" && clientId != v.Client.Id) {
|
2019-01-09 04:33:00 -08:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
cnt++
|
2019-01-31 10:06:30 -08:00
|
|
|
|
if _, ok := Bridge.Client[v.Client.Id]; ok {
|
2019-01-26 01:27:28 -08:00
|
|
|
|
v.Client.IsConnect = true
|
|
|
|
|
} else {
|
|
|
|
|
v.Client.IsConnect = false
|
|
|
|
|
}
|
2019-01-09 04:33:00 -08:00
|
|
|
|
if start--; start < 0 {
|
|
|
|
|
if length--; length > 0 {
|
2019-01-24 20:10:12 -08:00
|
|
|
|
if _, ok := RunList[v.Id]; ok {
|
2019-02-12 11:54:00 -08:00
|
|
|
|
v.RunStatus = true
|
2019-01-09 04:33:00 -08:00
|
|
|
|
} else {
|
2019-02-12 11:54:00 -08:00
|
|
|
|
v.RunStatus = false
|
2019-01-09 04:33:00 -08:00
|
|
|
|
}
|
|
|
|
|
list = append(list, v)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return list, cnt
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-24 20:10:12 -08:00
|
|
|
|
//获取客户端列表
|
2019-02-09 01:07:47 -08:00
|
|
|
|
func GetClientList(start, length int) (list []*file.Client, cnt int) {
|
|
|
|
|
list, cnt = file.GetCsvDb().GetClientList(start, length)
|
2019-01-24 20:10:12 -08:00
|
|
|
|
dealClientData(list)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-09 01:07:47 -08:00
|
|
|
|
func dealClientData(list []*file.Client) {
|
2019-01-24 20:10:12 -08:00
|
|
|
|
for _, v := range list {
|
2019-01-31 10:06:30 -08:00
|
|
|
|
if _, ok := Bridge.Client[v.Id]; ok {
|
2019-01-24 20:10:12 -08:00
|
|
|
|
v.IsConnect = true
|
|
|
|
|
} else {
|
|
|
|
|
v.IsConnect = false
|
|
|
|
|
}
|
|
|
|
|
v.Flow.InletFlow = 0
|
|
|
|
|
v.Flow.ExportFlow = 0
|
2019-02-09 01:07:47 -08:00
|
|
|
|
for _, h := range file.GetCsvDb().Hosts {
|
2019-01-26 01:27:28 -08:00
|
|
|
|
if h.Client.Id == v.Id {
|
2019-01-24 20:10:12 -08:00
|
|
|
|
v.Flow.InletFlow += h.Flow.InletFlow
|
|
|
|
|
v.Flow.ExportFlow += h.Flow.ExportFlow
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-09 01:07:47 -08:00
|
|
|
|
for _, t := range file.GetCsvDb().Tasks {
|
2019-01-26 01:27:28 -08:00
|
|
|
|
if t.Client.Id == v.Id {
|
2019-01-24 20:10:12 -08:00
|
|
|
|
v.Flow.InletFlow += t.Flow.InletFlow
|
|
|
|
|
v.Flow.ExportFlow += t.Flow.ExportFlow
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//根据客户端id删除其所属的所有隧道和域名
|
|
|
|
|
func DelTunnelAndHostByClientId(clientId int) {
|
2019-02-12 11:54:00 -08:00
|
|
|
|
var ids []int
|
2019-02-09 01:07:47 -08:00
|
|
|
|
for _, v := range file.GetCsvDb().Tasks {
|
2019-01-26 01:27:28 -08:00
|
|
|
|
if v.Client.Id == clientId {
|
2019-02-12 11:54:00 -08:00
|
|
|
|
ids = append(ids, v.Id)
|
2019-01-24 20:10:12 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-12 11:54:00 -08:00
|
|
|
|
for _, id := range ids {
|
|
|
|
|
DelTask(id)
|
|
|
|
|
}
|
2019-02-09 01:07:47 -08:00
|
|
|
|
for _, v := range file.GetCsvDb().Hosts {
|
2019-01-26 01:27:28 -08:00
|
|
|
|
if v.Client.Id == clientId {
|
2019-02-15 06:59:28 -08:00
|
|
|
|
file.GetCsvDb().DelHost(v.Id)
|
2019-01-24 20:10:12 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//关闭客户端连接
|
|
|
|
|
func DelClientConnect(clientId int) {
|
2019-03-01 01:23:14 -08:00
|
|
|
|
Bridge.DelClient(clientId, false)
|
2019-01-24 20:10:12 -08:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-01 01:23:14 -08:00
|
|
|
|
func GetDashboardData() map[string]interface{} {
|
|
|
|
|
data := make(map[string]interface{})
|
2019-02-16 07:18:58 -08:00
|
|
|
|
data["hostCount"] = len(file.GetCsvDb().Hosts)
|
|
|
|
|
data["clientCount"] = len(file.GetCsvDb().Clients) - 1 //Remove the public key client
|
2019-02-09 01:07:47 -08:00
|
|
|
|
list := file.GetCsvDb().Clients
|
2019-01-24 20:10:12 -08:00
|
|
|
|
dealClientData(list)
|
|
|
|
|
c := 0
|
|
|
|
|
var in, out int64
|
|
|
|
|
for _, v := range list {
|
|
|
|
|
if v.IsConnect {
|
|
|
|
|
c += 1
|
|
|
|
|
}
|
|
|
|
|
in += v.Flow.InletFlow
|
|
|
|
|
out += v.Flow.ExportFlow
|
|
|
|
|
}
|
|
|
|
|
data["clientOnlineCount"] = c
|
|
|
|
|
data["inletFlowCount"] = int(in)
|
|
|
|
|
data["exportFlowCount"] = int(out)
|
2019-03-01 01:23:14 -08:00
|
|
|
|
var tcp, udp, secret, socks5, p2p, http int
|
2019-02-09 01:07:47 -08:00
|
|
|
|
for _, v := range file.GetCsvDb().Tasks {
|
2019-01-24 20:10:12 -08:00
|
|
|
|
switch v.Mode {
|
2019-03-01 01:23:14 -08:00
|
|
|
|
case "tcp":
|
|
|
|
|
tcp += 1
|
|
|
|
|
case "socks5":
|
|
|
|
|
udp += 1
|
|
|
|
|
case "httpProxy":
|
|
|
|
|
http += 1
|
|
|
|
|
case "udp":
|
|
|
|
|
udp += 1
|
|
|
|
|
case "p2p":
|
|
|
|
|
p2p += 1
|
|
|
|
|
case "secret":
|
|
|
|
|
secret += 1
|
2019-01-24 20:10:12 -08:00
|
|
|
|
}
|
2019-01-09 04:33:00 -08:00
|
|
|
|
}
|
2019-03-01 01:23:14 -08:00
|
|
|
|
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")
|
2019-02-23 07:29:48 -08:00
|
|
|
|
tcpCount := 0
|
|
|
|
|
for _, v := range file.GetCsvDb().Clients {
|
|
|
|
|
tcpCount += v.NowConn
|
|
|
|
|
}
|
|
|
|
|
data["tcpCount"] = tcpCount
|
2019-03-01 01: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)
|
|
|
|
|
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]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-24 20:10:12 -08:00
|
|
|
|
return data
|
2019-01-09 04:33:00 -08:00
|
|
|
|
}
|
2019-02-23 07:29:48 -08:00
|
|
|
|
|
|
|
|
|
func flowSession(m time.Duration) {
|
|
|
|
|
ticker := time.NewTicker(m)
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-ticker.C:
|
|
|
|
|
file.GetCsvDb().StoreHostToCsv()
|
|
|
|
|
file.GetCsvDb().StoreTasksToCsv()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-01 01:23:14 -08:00
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|