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-09 01:07:47 -08:00
|
|
|
"github.com/cnlh/nps/lib/file"
|
|
|
|
"github.com/cnlh/nps/lib/lg"
|
2019-01-09 04:33:00 -08:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-02-02 08:54:43 -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-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-09 01:07:47 -08:00
|
|
|
lg.Println("启动模式:", v.Mode, "监听端口:", v.TcpPort)
|
2019-01-09 04:33:00 -08:00
|
|
|
AddTask(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//start a new server
|
2019-02-09 01:07:47 -08:00
|
|
|
func StartNewServer(bridgePort int, cnf *file.Tunnel, bridgeType string) {
|
|
|
|
Bridge = bridge.NewTunnel(bridgePort, RunList, 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-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-09 01:07:47 -08:00
|
|
|
lg.Fatalln("启动模式不正确")
|
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-01-26 01:27:28 -08:00
|
|
|
return NewTunnelModeServer(ProcessTunnel, Bridge, c)
|
2019-01-09 04:33:00 -08:00
|
|
|
case "socks5Server":
|
2019-01-26 01:27:28 -08:00
|
|
|
return NewSock5ModeServer(Bridge, c)
|
2019-01-09 04:33:00 -08:00
|
|
|
case "httpProxyServer":
|
2019-01-26 01:27:28 -08:00
|
|
|
return NewTunnelModeServer(ProcessHttp, Bridge, c)
|
2019-01-09 04:33:00 -08:00
|
|
|
case "udpServer":
|
2019-01-26 01:27:28 -08:00
|
|
|
return 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-01-31 10:06:30 -08:00
|
|
|
TcpPort: 0,
|
2019-01-26 01:27:28 -08:00
|
|
|
Mode: "httpHostServer",
|
|
|
|
Target: "",
|
2019-02-09 01:07:47 -08:00
|
|
|
Config: &file.Config{},
|
2019-01-26 01:27:28 -08:00
|
|
|
Status: true,
|
2019-01-09 04:33:00 -08:00
|
|
|
}
|
|
|
|
AddTask(t)
|
|
|
|
return NewWebServer(Bridge)
|
|
|
|
case "httpHostServer":
|
2019-01-31 10:06:30 -08:00
|
|
|
return 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
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.New("未在运行中")
|
|
|
|
}
|
|
|
|
|
|
|
|
//add task
|
2019-02-09 01:07:47 -08:00
|
|
|
func AddTask(t *file.Tunnel) 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-09 01:07:47 -08:00
|
|
|
lg.Fatalln("客户端", t.Id, "启动失败,错误:", err)
|
2019-01-24 20:10:12 -08:00
|
|
|
delete(RunList, t.Id)
|
2019-01-09 04:33:00 -08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
return errors.New("启动模式不正确")
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
if err := StopServer(id); err != nil {
|
2019-01-09 04:33:00 -08:00
|
|
|
return err
|
|
|
|
}
|
2019-02-09 01:07:47 -08:00
|
|
|
return file.GetCsvDb().DelTask(id)
|
2019-01-09 04:33:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
//get key by host from x
|
2019-02-09 01:07:47 -08:00
|
|
|
func GetInfoByHost(host string) (h *file.Host, err error) {
|
|
|
|
for _, v := range file.GetCsvDb().Hosts {
|
2019-01-10 05:40:57 -08:00
|
|
|
s := strings.Split(host, ":")
|
|
|
|
if s[0] == v.Host {
|
2019-01-09 04:33:00 -08:00
|
|
|
h = v
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = errors.New("未找到host对应的内网目标")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//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-01-26 01:27:28 -08:00
|
|
|
v.Client.Status = true
|
2019-01-09 04:33:00 -08:00
|
|
|
} else {
|
2019-01-26 01:27:28 -08:00
|
|
|
v.Client.Status = 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-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-01-24 20:10:12 -08:00
|
|
|
DelTask(v.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
|
|
|
}
|