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-12 11:54:00 -08:00
|
|
|
"github.com/cnlh/nps/lib/beego"
|
2019-02-09 01:07:47 -08:00
|
|
|
"github.com/cnlh/nps/lib/file"
|
|
|
|
"github.com/cnlh/nps/lib/lg"
|
2019-02-12 11:54:00 -08:00
|
|
|
"github.com/cnlh/nps/server/proxy"
|
|
|
|
"github.com/cnlh/nps/server/tool"
|
2019-01-09 04:33:00 -08:00
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-02-12 11:54:00 -08:00
|
|
|
Bridge *bridge.Bridge
|
|
|
|
RunList map[int]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-01-09 04:33:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
//从csv文件中恢复任务
|
|
|
|
func InitFromCsv() {
|
2019-02-12 11:54:00 -08:00
|
|
|
//Add a public password
|
|
|
|
c := file.NewClient(beego.AppConfig.String("publicVkey"), true, true)
|
|
|
|
file.GetCsvDb().NewClient(c)
|
|
|
|
RunList[c.Id] = nil
|
|
|
|
//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-02-12 11:54:00 -08:00
|
|
|
lg.Println("启动模式:", v.Mode, "监听端口:", v.Port)
|
2019-01-09 04:33:00 -08:00
|
|
|
AddTask(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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-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-12 11:54:00 -08:00
|
|
|
Bridge = bridge.NewTunnel(bridgePort, bridgeType)
|
2019-02-05 08:35:23 -08:00
|
|
|
if err := Bridge.StartTunnel(); err != nil {
|
2019-02-09 01:07:47 -08:00
|
|
|
lg.Fatalln("服务端开启失败", err)
|
2019-02-12 11:54:00 -08:00
|
|
|
} else {
|
|
|
|
lg.Printf("Server startup, the bridge type is %s, the bridge port is %d", bridgeType, bridgePort)
|
2019-02-05 08:35:23 -08:00
|
|
|
}
|
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 {
|
|
|
|
RunList[cnf.Id] = svr
|
|
|
|
err := reflect.ValueOf(svr).MethodByName("Start").Call(nil)[0]
|
|
|
|
if err.Interface() != nil {
|
2019-02-09 01:07:47 -08:00
|
|
|
lg.Fatalln(err)
|
2019-01-09 04:33:00 -08:00
|
|
|
}
|
2019-02-05 08:35:23 -08:00
|
|
|
} else {
|
2019-02-12 11:54:00 -08:00
|
|
|
lg.Fatalln("启动模式%s不正确", cnf.Mode)
|
2019-01-09 04:33:00 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//new a server by mode name
|
2019-02-09 01:07:47 -08:00
|
|
|
func NewMode(Bridge *bridge.Bridge, c *file.Tunnel) interface{} {
|
2019-01-26 01:27:28 -08:00
|
|
|
switch c.Mode {
|
2019-01-09 04:33:00 -08:00
|
|
|
case "tunnelServer":
|
2019-02-12 11:54:00 -08:00
|
|
|
return proxy.NewTunnelModeServer(proxy.ProcessTunnel, Bridge, c)
|
2019-01-09 04:33:00 -08:00
|
|
|
case "socks5Server":
|
2019-02-12 11:54:00 -08:00
|
|
|
return proxy.NewSock5ModeServer(Bridge, c)
|
2019-01-09 04:33:00 -08:00
|
|
|
case "httpProxyServer":
|
2019-02-12 11:54:00 -08:00
|
|
|
return proxy.NewTunnelModeServer(proxy.ProcessHttp, Bridge, c)
|
2019-01-09 04:33:00 -08:00
|
|
|
case "udpServer":
|
2019-02-12 11:54:00 -08:00
|
|
|
return 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-12 11:54:00 -08:00
|
|
|
return proxy.NewWebServer(Bridge)
|
2019-01-09 04:33:00 -08:00
|
|
|
case "httpHostServer":
|
2019-02-12 11:54:00 -08:00
|
|
|
return proxy.NewHttp(Bridge, c)
|
2019-01-09 04:33:00 -08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//stop server
|
2019-01-24 20:10:12 -08:00
|
|
|
func StopServer(id int) error {
|
|
|
|
if v, ok := RunList[id]; ok {
|
2019-01-09 04:33:00 -08:00
|
|
|
reflect.ValueOf(v).MethodByName("Close").Call(nil)
|
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 {
|
2019-01-26 01:27:28 -08:00
|
|
|
t.Status = false
|
2019-02-09 01:07:47 -08:00
|
|
|
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-02-12 11:54:00 -08:00
|
|
|
if b := tool.TestServerPort(t.Port, t.Mode); !b && t.Mode != "httpHostServer" {
|
|
|
|
lg.Printf("taskId %d start error Port %d Open Failed", t.Id, t.Port)
|
|
|
|
return errors.New("error")
|
|
|
|
}
|
2019-01-09 04:33:00 -08:00
|
|
|
if svr := NewMode(Bridge, t); svr != nil {
|
2019-01-24 20:10:12 -08:00
|
|
|
RunList[t.Id] = svr
|
2019-01-09 04:33:00 -08:00
|
|
|
go func() {
|
|
|
|
err := reflect.ValueOf(svr).MethodByName("Start").Call(nil)[0]
|
|
|
|
if err.Interface() != nil {
|
2019-02-12 11:54:00 -08:00
|
|
|
lg.Println("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-09 01:07:47 -08:00
|
|
|
file.GetCsvDb().DelHost(v.Host)
|
2019-01-24 20:10:12 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//关闭客户端连接
|
|
|
|
func DelClientConnect(clientId int) {
|
2019-01-31 10:06:30 -08:00
|
|
|
Bridge.DelClient(clientId)
|
2019-01-24 20:10:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetDashboardData() map[string]int {
|
|
|
|
data := make(map[string]int)
|
2019-02-09 01:07:47 -08:00
|
|
|
data["hostCount"] = len(file.GetCsvDb().Hosts)
|
|
|
|
data["clientCount"] = len(file.GetCsvDb().Clients)
|
|
|
|
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-02-09 01:07:47 -08:00
|
|
|
for _, v := range file.GetCsvDb().Tasks {
|
2019-01-24 20:10:12 -08:00
|
|
|
switch v.Mode {
|
|
|
|
case "tunnelServer":
|
|
|
|
data["tunnelServerCount"] += 1
|
|
|
|
case "socks5Server":
|
|
|
|
data["socks5ServerCount"] += 1
|
|
|
|
case "httpProxyServer":
|
|
|
|
data["httpProxyServerCount"] += 1
|
|
|
|
case "udpServer":
|
|
|
|
data["udpServerCount"] += 1
|
|
|
|
}
|
2019-01-09 04:33:00 -08:00
|
|
|
}
|
2019-01-24 20:10:12 -08:00
|
|
|
return data
|
2019-01-09 04:33:00 -08:00
|
|
|
}
|