nps/client/control.go

203 lines
4.9 KiB
Go
Raw Normal View History

2019-02-17 09:05:05 -08:00
package client
import (
2019-02-23 07:29:48 -08:00
"encoding/binary"
2019-02-17 09:05:05 -08:00
"errors"
"github.com/cnlh/nps/lib/common"
"github.com/cnlh/nps/lib/config"
"github.com/cnlh/nps/lib/conn"
2019-02-23 07:29:48 -08:00
"github.com/cnlh/nps/lib/crypt"
"github.com/cnlh/nps/lib/version"
"github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
2019-02-17 09:05:05 -08:00
"github.com/cnlh/nps/vender/github.com/xtaci/kcp"
"github.com/cnlh/nps/vender/golang.org/x/net/proxy"
"io/ioutil"
"log"
"net"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
func GetTaskStatus(path string) {
cnf, err := config.NewConfig(path)
if err != nil {
log.Fatalln(err)
}
c, err := NewConn(cnf.CommonConfig.Tp, cnf.CommonConfig.VKey, cnf.CommonConfig.Server, common.WORK_CONFIG, cnf.CommonConfig.ProxyUrl)
if err != nil {
log.Fatalln(err)
}
if _, err := c.Write([]byte(common.WORK_STATUS)); err != nil {
log.Fatalln(err)
}
if f, err := common.ReadAllFromFile(filepath.Join(common.GetTmpPath(), "npc_vkey.txt")); err != nil {
log.Fatalln(err)
} else if _, err := c.Write([]byte(string(f))); err != nil {
log.Fatalln(err)
}
if l, err := c.GetLen(); err != nil {
log.Fatalln(err)
} else if b, err := c.ReadLen(l); err != nil {
2019-02-23 07:29:48 -08:00
log.Fatalln(err)
2019-02-17 09:05:05 -08:00
} else {
arr := strings.Split(string(b), common.CONN_DATA_SEQ)
for _, v := range cnf.Hosts {
if common.InStrArr(arr, v.Remark) {
log.Println(v.Remark, "ok")
} else {
log.Println(v.Remark, "not running")
}
}
for _, v := range cnf.Tasks {
ports := common.GetPorts(v.Ports)
for _, vv := range ports {
var remark string
if len(ports) > 1 {
remark = v.Remark + "_" + strconv.Itoa(vv)
} else {
remark = v.Remark
}
if common.InStrArr(arr, remark) {
log.Println(remark, "ok")
} else {
log.Println(remark, "not running")
}
}
}
}
os.Exit(0)
}
var errAdd = errors.New("The server returned an error, which port or host may have been occupied or not allowed to open.")
func StartFromFile(path string) {
first := true
cnf, err := config.NewConfig(path)
2019-02-23 07:29:48 -08:00
if err != nil || cnf.CommonConfig == nil {
logs.Error("Config file %s loading error", path)
os.Exit(0)
2019-02-17 09:05:05 -08:00
}
2019-02-23 07:29:48 -08:00
logs.Info("Loading configuration file %s successfully", path)
2019-02-17 09:05:05 -08:00
re:
if first || cnf.CommonConfig.AutoReconnection {
if !first {
2019-02-23 07:29:48 -08:00
logs.Info("Reconnecting...")
2019-02-17 09:05:05 -08:00
time.Sleep(time.Second * 5)
}
} else {
return
}
first = false
c, err := NewConn(cnf.CommonConfig.Tp, cnf.CommonConfig.VKey, cnf.CommonConfig.Server, common.WORK_CONFIG, cnf.CommonConfig.ProxyUrl)
if err != nil {
2019-02-23 07:29:48 -08:00
logs.Error(err)
2019-02-17 09:05:05 -08:00
goto re
}
2019-02-23 07:29:48 -08:00
if _, err := c.SendConfigInfo(cnf.CommonConfig); err != nil {
logs.Error(err)
2019-02-17 09:05:05 -08:00
goto re
}
var b []byte
if b, err = c.ReadLen(16); err != nil {
2019-02-23 07:29:48 -08:00
logs.Error(err)
2019-02-17 09:05:05 -08:00
goto re
} else {
ioutil.WriteFile(filepath.Join(common.GetTmpPath(), "npc_vkey.txt"), []byte(string(b)), 0600)
}
if !c.GetAddStatus() {
2019-02-23 07:29:48 -08:00
logs.Error(errAdd)
2019-02-17 09:05:05 -08:00
goto re
}
2019-02-23 07:29:48 -08:00
2019-02-17 09:05:05 -08:00
for _, v := range cnf.Hosts {
if _, err := c.SendHostInfo(v); err != nil {
2019-02-23 07:29:48 -08:00
logs.Error(err)
2019-02-17 09:05:05 -08:00
goto re
}
if !c.GetAddStatus() {
2019-02-23 07:29:48 -08:00
logs.Error(errAdd, v.Host)
2019-02-17 09:05:05 -08:00
goto re
}
}
for _, v := range cnf.Tasks {
if _, err := c.SendTaskInfo(v); err != nil {
2019-02-23 07:29:48 -08:00
logs.Error(err)
2019-02-17 09:05:05 -08:00
goto re
}
if !c.GetAddStatus() {
2019-02-23 07:29:48 -08:00
logs.Error(errAdd, v.Ports)
2019-02-17 09:05:05 -08:00
goto re
}
}
2019-02-23 07:29:48 -08:00
for _, v := range cnf.LocalServer {
go StartLocalServer(v, cnf.CommonConfig)
}
2019-02-17 09:05:05 -08:00
c.Close()
NewRPClient(cnf.CommonConfig.Server, string(b), cnf.CommonConfig.Tp, cnf.CommonConfig.ProxyUrl).Start()
2019-02-23 07:29:48 -08:00
CloseLocalServer()
2019-02-17 09:05:05 -08:00
goto re
}
//Create a new connection with the server and verify it
func NewConn(tp string, vkey string, server string, connType string, proxyUrl string) (*conn.Conn, error) {
var err error
var connection net.Conn
var sess *kcp.UDPSession
if tp == "tcp" {
if proxyUrl != "" {
u, er := url.Parse(proxyUrl)
if er != nil {
return nil, er
}
n, er := proxy.FromURL(u, nil)
if er != nil {
return nil, er
}
connection, err = n.Dial("tcp", server)
} else {
connection, err = net.Dial("tcp", server)
}
} else {
sess, err = kcp.DialWithOptions(server, nil, 10, 3)
conn.SetUdpSession(sess)
connection = sess
}
if err != nil {
return nil, err
}
c := conn.NewConn(connection)
2019-02-23 07:29:48 -08:00
if b, err := c.ReadLen(32); err != nil {
logs.Error(err)
os.Exit(0)
} else if crypt.Md5(version.GetVersion()) != string(b) {
logs.Error("The client does not match the server version. The current version of the client is", version.GetVersion())
os.Exit(0)
} else if binary.Write(c, binary.LittleEndian, []byte(version.VERSION_OK)); err != nil {
logs.Error(err)
os.Exit(0)
}
2019-02-17 09:05:05 -08:00
if _, err := c.Write([]byte(common.Getverifyval(vkey))); err != nil {
2019-02-23 07:29:48 -08:00
logs.Error(err)
os.Exit(0)
2019-02-17 09:05:05 -08:00
}
if s, err := c.ReadFlag(); err != nil {
2019-02-23 07:29:48 -08:00
logs.Error(err)
os.Exit(0)
2019-02-17 09:05:05 -08:00
} else if s == common.VERIFY_EER {
2019-02-23 07:29:48 -08:00
logs.Error("Validation key %s incorrect", vkey)
os.Exit(0)
2019-02-17 09:05:05 -08:00
}
if _, err := c.Write([]byte(connType)); err != nil {
2019-02-23 07:29:48 -08:00
logs.Error(err)
os.Exit(0)
2019-02-17 09:05:05 -08:00
}
c.SetAlive(tp)
return c, nil
}