add functions

This commit is contained in:
刘河
2019-02-23 23:29:48 +08:00
parent 2c608ddb7f
commit 750ecb824a
36 changed files with 607 additions and 289 deletions

View File

@@ -3,9 +3,10 @@ package client
import (
"github.com/cnlh/nps/lib/common"
"github.com/cnlh/nps/lib/conn"
"github.com/cnlh/nps/lib/lg"
"github.com/cnlh/nps/lib/pool"
"github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
"net"
"os"
"sync"
"time"
)
@@ -40,11 +41,11 @@ func (s *TRPClient) Start() {
retry:
c, err := NewConn(s.bridgeConnType, s.vKey, s.svrAddr, common.WORK_MAIN, s.proxyUrl)
if err != nil {
lg.Println("The connection server failed and will be reconnected in five seconds")
logs.Error("The connection server failed and will be reconnected in five seconds")
time.Sleep(time.Second * 5)
goto retry
}
lg.Printf("Successful connection with server %s", s.svrAddr)
logs.Info("Successful connection with server %s", s.svrAddr)
s.processor(c)
}
@@ -65,12 +66,13 @@ func (s *TRPClient) processor(c *conn.Conn) {
for {
flags, err := c.ReadFlag()
if err != nil {
lg.Printf("Accept server data error %s, end this service", err.Error())
logs.Error("Accept server data error %s, end this service", err.Error())
break
}
switch flags {
case common.VERIFY_EER:
lg.Fatalf("VKey:%s is incorrect, the server refuses to connect, please check", s.vKey)
logs.Error("VKey:%s is incorrect, the server refuses to connect, please check", s.vKey)
os.Exit(0)
case common.NEW_CONN:
if link, err := c.GetLinkInfo(); err != nil {
break
@@ -83,12 +85,13 @@ func (s *TRPClient) processor(c *conn.Conn) {
link.Run(false)
}
case common.RES_CLOSE:
lg.Fatalln("The authentication key is connected by another client or the server closes the client.")
logs.Error("The authentication key is connected by another client or the server closes the client.")
os.Exit(0)
case common.RES_MSG:
lg.Println("Server-side return error")
logs.Error("Server-side return error")
break
default:
lg.Println("The error could not be resolved")
logs.Warn("The error could not be resolved")
break
}
}
@@ -103,7 +106,7 @@ func (s *TRPClient) linkProcess(link *conn.Link, c *conn.Conn) {
if err != nil {
c.WriteFail(link.Id)
lg.Println("connect to ", link.Host, "error:", err)
logs.Warn("connect to ", link.Host, "error:", err)
return
}
c.WriteSuccess(link.Id)
@@ -134,7 +137,7 @@ func (s *TRPClient) getMsgStatus() {
var err error
s.msgTunnel, err = NewConn(s.bridgeConnType, s.vKey, s.svrAddr, common.WORK_SEND_STATUS, s.proxyUrl)
if err != nil {
lg.Println("connect to ", s.svrAddr, "error:", err)
logs.Error("connect to ", s.svrAddr, "error:", err)
return
}
go func() {
@@ -160,7 +163,7 @@ func (s *TRPClient) dealChan() {
var err error
s.tunnel, err = NewConn(s.bridgeConnType, s.vKey, s.svrAddr, common.WORK_CHAN, s.proxyUrl)
if err != nil {
lg.Println("connect to ", s.svrAddr, "error:", err)
logs.Error("connect to ", s.svrAddr, "error:", err)
return
}
go func() {

View File

@@ -1,11 +1,14 @@
package client
import (
"encoding/binary"
"errors"
"github.com/cnlh/nps/lib/common"
"github.com/cnlh/nps/lib/config"
"github.com/cnlh/nps/lib/conn"
"github.com/cnlh/nps/lib/lg"
"github.com/cnlh/nps/lib/crypt"
"github.com/cnlh/nps/lib/version"
"github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
"github.com/cnlh/nps/vender/github.com/xtaci/kcp"
"github.com/cnlh/nps/vender/golang.org/x/net/proxy"
"io/ioutil"
@@ -39,7 +42,7 @@ func GetTaskStatus(path string) {
if l, err := c.GetLen(); err != nil {
log.Fatalln(err)
} else if b, err := c.ReadLen(l); err != nil {
lg.Fatalln(err)
log.Fatalln(err)
} else {
arr := strings.Split(string(b), common.CONN_DATA_SEQ)
for _, v := range cnf.Hosts {
@@ -74,14 +77,16 @@ var errAdd = errors.New("The server returned an error, which port or host may ha
func StartFromFile(path string) {
first := true
cnf, err := config.NewConfig(path)
if err != nil {
lg.Fatalln(err)
if err != nil || cnf.CommonConfig == nil {
logs.Error("Config file %s loading error", path)
os.Exit(0)
}
lg.Printf("Loading configuration file %s successfully", path)
logs.Info("Loading configuration file %s successfully", path)
re:
if first || cnf.CommonConfig.AutoReconnection {
if !first {
lg.Println("Reconnecting...")
logs.Info("Reconnecting...")
time.Sleep(time.Second * 5)
}
} else {
@@ -90,48 +95,51 @@ re:
first = false
c, err := NewConn(cnf.CommonConfig.Tp, cnf.CommonConfig.VKey, cnf.CommonConfig.Server, common.WORK_CONFIG, cnf.CommonConfig.ProxyUrl)
if err != nil {
lg.Println(err)
logs.Error(err)
goto re
}
if _, err := c.SendConfigInfo(cnf.CommonConfig.Cnf); err != nil {
lg.Println(err)
if _, err := c.SendConfigInfo(cnf.CommonConfig); err != nil {
logs.Error(err)
goto re
}
var b []byte
if b, err = c.ReadLen(16); err != nil {
lg.Println(err)
logs.Error(err)
goto re
} else {
ioutil.WriteFile(filepath.Join(common.GetTmpPath(), "npc_vkey.txt"), []byte(string(b)), 0600)
}
if !c.GetAddStatus() {
lg.Println(errAdd)
logs.Error(errAdd)
goto re
}
for _, v := range cnf.Hosts {
if _, err := c.SendHostInfo(v); err != nil {
lg.Println(err)
logs.Error(err)
goto re
}
if !c.GetAddStatus() {
lg.Println(errAdd, v.Host)
logs.Error(errAdd, v.Host)
goto re
}
}
for _, v := range cnf.Tasks {
if _, err := c.SendTaskInfo(v); err != nil {
lg.Println(err)
logs.Error(err)
goto re
}
if !c.GetAddStatus() {
lg.Println(errAdd, v.Ports)
logs.Error(errAdd, v.Ports)
goto re
}
}
for _, v := range cnf.LocalServer {
go StartLocalServer(v, cnf.CommonConfig)
}
c.Close()
NewRPClient(cnf.CommonConfig.Server, string(b), cnf.CommonConfig.Tp, cnf.CommonConfig.ProxyUrl).Start()
CloseLocalServer()
goto re
}
@@ -163,16 +171,30 @@ func NewConn(tp string, vkey string, server string, connType string, proxyUrl st
return nil, err
}
c := conn.NewConn(connection)
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)
}
if _, err := c.Write([]byte(common.Getverifyval(vkey))); err != nil {
lg.Println(err)
logs.Error(err)
os.Exit(0)
}
if s, err := c.ReadFlag(); err != nil {
lg.Println(err)
logs.Error(err)
os.Exit(0)
} else if s == common.VERIFY_EER {
lg.Fatalf("Validation key %s incorrect", vkey)
logs.Error("Validation key %s incorrect", vkey)
os.Exit(0)
}
if _, err := c.Write([]byte(connType)); err != nil {
lg.Println(err)
logs.Error(err)
os.Exit(0)
}
c.SetAlive(tp)

54
client/local.go Normal file
View File

@@ -0,0 +1,54 @@
package client
import (
"github.com/cnlh/nps/lib/common"
"github.com/cnlh/nps/lib/config"
"github.com/cnlh/nps/lib/crypt"
"github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
"net"
"strings"
)
var LocalServer []*net.TCPListener
func CloseLocalServer() {
for _, v := range LocalServer {
v.Close()
}
}
func StartLocalServer(l *config.LocalServer, config *config.CommonConfig) error {
listener, err := net.ListenTCP("tcp", &net.TCPAddr{net.ParseIP("0.0.0.0"), l.Port, ""})
if err != nil {
logs.Error("Local listener startup failed port %d, error %s", l.Port, err.Error())
return err
}
LocalServer = append(LocalServer, listener)
logs.Info("Successful start-up of local monitoring, port", l.Port)
for {
c, err := listener.AcceptTCP()
if err != nil {
if strings.Contains(err.Error(), "use of closed network connection") {
break
}
logs.Info(err)
continue
}
go process(c, config, l)
}
return nil
}
func process(conn net.Conn, config *config.CommonConfig, l *config.LocalServer) {
c, err := NewConn(config.Tp, config.VKey, config.Server, common.WORD_SECRET, config.ProxyUrl)
if err != nil {
logs.Error("Local connection server failed ", err.Error())
}
if _, err := c.Write([]byte(crypt.Md5(l.Password))); err != nil {
logs.Error("Local connection server failed ", err.Error())
}
go common.CopyBuffer(c, conn)
common.CopyBuffer(conn, c)
c.Close()
conn.Close()
}