|
@@ -4,9 +4,9 @@ import (
|
|
|
"bufio"
|
|
|
"bytes"
|
|
|
"encoding/binary"
|
|
|
+ "encoding/json"
|
|
|
"errors"
|
|
|
"github.com/cnlh/nps/lib/common"
|
|
|
- "github.com/cnlh/nps/lib/config"
|
|
|
"github.com/cnlh/nps/lib/crypt"
|
|
|
"github.com/cnlh/nps/lib/file"
|
|
|
"github.com/cnlh/nps/lib/mux"
|
|
@@ -84,26 +84,6 @@ func (s *Conn) GetShortContent(l int) (b []byte, err error) {
|
|
|
return buf, binary.Read(s, binary.LittleEndian, &buf)
|
|
|
}
|
|
|
|
|
|
-func (s *Conn) LocalAddr() net.Addr {
|
|
|
- return s.Conn.LocalAddr()
|
|
|
-}
|
|
|
-
|
|
|
-func (s *Conn) RemoteAddr() net.Addr {
|
|
|
- return s.Conn.RemoteAddr()
|
|
|
-}
|
|
|
-
|
|
|
-func (s *Conn) SetDeadline(t time.Time) error {
|
|
|
- return s.Conn.SetDeadline(t)
|
|
|
-}
|
|
|
-
|
|
|
-func (s *Conn) SetWriteDeadline(t time.Time) error {
|
|
|
- return s.Conn.SetWriteDeadline(t)
|
|
|
-}
|
|
|
-
|
|
|
-func (s *Conn) SetReadDeadline(t time.Time) error {
|
|
|
- return s.Conn.SetReadDeadline(t)
|
|
|
-}
|
|
|
-
|
|
|
//读取指定长度内容
|
|
|
func (s *Conn) ReadLen(cLen int, buf []byte) (int, error) {
|
|
|
if cLen > len(buf) {
|
|
@@ -151,7 +131,7 @@ func (s *Conn) SetAlive(tp string) {
|
|
|
}
|
|
|
|
|
|
//set read deadline
|
|
|
-func (s *Conn) SetReadDeadlineByType(t time.Duration, tp string) {
|
|
|
+func (s *Conn) SetReadDeadlineBySecond(t time.Duration) {
|
|
|
switch s.Conn.(type) {
|
|
|
case *kcp.UDPSession:
|
|
|
s.Conn.(*kcp.UDPSession).SetReadDeadline(time.Now().Add(time.Duration(t) * time.Second))
|
|
@@ -162,31 +142,9 @@ func (s *Conn) SetReadDeadlineByType(t time.Duration, tp string) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-//send info for link
|
|
|
-func (s *Conn) SendLinkInfo(link *Link) (int, error) {
|
|
|
- raw := bytes.NewBuffer([]byte{})
|
|
|
- common.BinaryWrite(raw, link.ConnType, link.Host, common.GetStrByBool(link.Compress), common.GetStrByBool(link.Crypt), link.RemoteAddr)
|
|
|
- return s.Write(raw.Bytes())
|
|
|
-}
|
|
|
-
|
|
|
//get link info from conn
|
|
|
func (s *Conn) GetLinkInfo() (lk *Link, err error) {
|
|
|
- lk = new(Link)
|
|
|
- var l int
|
|
|
- buf := pool.BufPoolMax.Get().([]byte)
|
|
|
- defer pool.PutBufPoolMax(buf)
|
|
|
- if l, err = s.GetLen(); err != nil {
|
|
|
- return
|
|
|
- } else if _, err = s.ReadLen(l, buf); err != nil {
|
|
|
- return
|
|
|
- } else {
|
|
|
- arr := strings.Split(string(buf[:l]), common.CONN_DATA_SEQ)
|
|
|
- lk.ConnType = arr[0]
|
|
|
- lk.Host = arr[1]
|
|
|
- lk.Compress = common.GetBoolByStr(arr[2])
|
|
|
- lk.Crypt = common.GetBoolByStr(arr[3])
|
|
|
- lk.RemoteAddr = arr[4]
|
|
|
- }
|
|
|
+ err = s.getInfo(&lk)
|
|
|
return
|
|
|
}
|
|
|
|
|
@@ -215,95 +173,38 @@ func (s *Conn) GetHealthInfo() (info string, status bool, err error) {
|
|
|
return "", false, errors.New("receive health info error")
|
|
|
}
|
|
|
|
|
|
-//send host info
|
|
|
-func (s *Conn) SendHostInfo(h *file.Host) (int, error) {
|
|
|
- /*
|
|
|
- The task info is formed as follows:
|
|
|
- +----+-----+---------+
|
|
|
- |type| len | content |
|
|
|
- +----+---------------+
|
|
|
- | 4 | 4 | ... |
|
|
|
- +----+---------------+
|
|
|
- */
|
|
|
- raw := bytes.NewBuffer([]byte{})
|
|
|
- binary.Write(raw, binary.LittleEndian, []byte(common.NEW_HOST))
|
|
|
- common.BinaryWrite(raw, h.Host, h.Target.TargetStr, h.HeaderChange, h.HostChange, h.Remark, h.Location, h.Scheme)
|
|
|
- return s.Write(raw.Bytes())
|
|
|
-}
|
|
|
-
|
|
|
//get task info
|
|
|
func (s *Conn) GetHostInfo() (h *file.Host, err error) {
|
|
|
- var l int
|
|
|
- buf := pool.BufPoolMax.Get().([]byte)
|
|
|
- defer pool.PutBufPoolMax(buf)
|
|
|
- if l, err = s.GetLen(); err != nil {
|
|
|
- return
|
|
|
- } else if _, err = s.ReadLen(l, buf); err != nil {
|
|
|
- return
|
|
|
- } else {
|
|
|
- arr := strings.Split(string(buf[:l]), common.CONN_DATA_SEQ)
|
|
|
- h = new(file.Host)
|
|
|
- h.Target = new(file.Target)
|
|
|
- h.Id = int(file.GetDb().JsonDb.GetHostId())
|
|
|
- h.Host = arr[0]
|
|
|
- h.Target.TargetStr = arr[1]
|
|
|
- h.HeaderChange = arr[2]
|
|
|
- h.HostChange = arr[3]
|
|
|
- h.Remark = arr[4]
|
|
|
- h.Location = arr[5]
|
|
|
- h.Scheme = arr[6]
|
|
|
- if h.Scheme == "" {
|
|
|
- h.Scheme = "all"
|
|
|
- }
|
|
|
- h.Flow = new(file.Flow)
|
|
|
- h.NoStore = true
|
|
|
- }
|
|
|
+ err = s.getInfo(&h)
|
|
|
+ h.Id = int(file.GetDb().JsonDb.GetHostId())
|
|
|
+ h.Flow = new(file.Flow)
|
|
|
+ h.NoStore = true
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-//send task info
|
|
|
-func (s *Conn) SendConfigInfo(c *config.CommonConfig) (int, error) {
|
|
|
- /*
|
|
|
- The task info is formed as follows:
|
|
|
- +----+-----+---------+
|
|
|
- |type| len | content |
|
|
|
- +----+---------------+
|
|
|
- | 4 | 4 | ... |
|
|
|
- +----+---------------+
|
|
|
- */
|
|
|
- raw := bytes.NewBuffer([]byte{})
|
|
|
- binary.Write(raw, binary.LittleEndian, []byte(common.NEW_CONF))
|
|
|
- common.BinaryWrite(raw, c.Cnf.U, c.Cnf.P, common.GetStrByBool(c.Cnf.Crypt), common.GetStrByBool(c.Cnf.Compress), strconv.Itoa(c.Client.RateLimit),
|
|
|
- strconv.Itoa(int(c.Client.Flow.FlowLimit)), strconv.Itoa(c.Client.MaxConn), c.Client.Remark)
|
|
|
- return s.Write(raw.Bytes())
|
|
|
-}
|
|
|
-
|
|
|
//get task info
|
|
|
func (s *Conn) GetConfigInfo() (c *file.Client, err error) {
|
|
|
- var l int
|
|
|
- buf := pool.BufPoolMax.Get().([]byte)
|
|
|
- defer pool.PutBufPoolMax(buf)
|
|
|
- if l, err = s.GetLen(); err != nil {
|
|
|
- return
|
|
|
- } else if _, err = s.ReadLen(l, buf); err != nil {
|
|
|
- return
|
|
|
- } else {
|
|
|
- arr := strings.Split(string(buf[:l]), common.CONN_DATA_SEQ)
|
|
|
- c = file.NewClient("", true, false)
|
|
|
- c.Cnf.U = arr[0]
|
|
|
- c.Cnf.P = arr[1]
|
|
|
- c.Cnf.Crypt = common.GetBoolByStr(arr[2])
|
|
|
- c.Cnf.Compress = common.GetBoolByStr(arr[3])
|
|
|
- c.RateLimit = common.GetIntNoErrByStr(arr[4])
|
|
|
- c.Flow.FlowLimit = int64(common.GetIntNoErrByStr(arr[5]))
|
|
|
- c.MaxConn = common.GetIntNoErrByStr(arr[6])
|
|
|
- c.Remark = arr[7]
|
|
|
+ err = s.getInfo(&c)
|
|
|
+ c.NoStore = true
|
|
|
+ c.Status = true
|
|
|
+ if c.Flow == nil {
|
|
|
+ c.Flow = new(file.Flow)
|
|
|
}
|
|
|
+ c.NoDisplay = false
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+//get task info
|
|
|
+func (s *Conn) GetTaskInfo() (t *file.Tunnel, err error) {
|
|
|
+ err = s.getInfo(&t)
|
|
|
+ t.Id = int(file.GetDb().JsonDb.GetTaskId())
|
|
|
+ t.NoStore = true
|
|
|
+ t.Flow = new(file.Flow)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-//send task info
|
|
|
-func (s *Conn) SendTaskInfo(t *file.Tunnel) (int, error) {
|
|
|
+//send info
|
|
|
+func (s *Conn) SendInfo(t interface{}, flag string) (int, error) {
|
|
|
/*
|
|
|
The task info is formed as follows:
|
|
|
+----+-----+---------+
|
|
@@ -313,13 +214,23 @@ func (s *Conn) SendTaskInfo(t *file.Tunnel) (int, error) {
|
|
|
+----+---------------+
|
|
|
*/
|
|
|
raw := bytes.NewBuffer([]byte{})
|
|
|
- binary.Write(raw, binary.LittleEndian, []byte(common.NEW_TASK))
|
|
|
- common.BinaryWrite(raw, t.Mode, t.Ports, t.Target.TargetStr, t.Remark, t.TargetAddr, t.Password, t.LocalPath, t.StripPre, t.ServerIp)
|
|
|
+ if flag != "" {
|
|
|
+ binary.Write(raw, binary.LittleEndian, []byte(flag))
|
|
|
+ }
|
|
|
+ b, err := json.Marshal(t)
|
|
|
+ if err != nil {
|
|
|
+ return 0, err
|
|
|
+ }
|
|
|
+ lenBytes, err := GetLenBytes(b)
|
|
|
+ if err != nil {
|
|
|
+ return 0, err
|
|
|
+ }
|
|
|
+ binary.Write(raw, binary.LittleEndian, lenBytes)
|
|
|
return s.Write(raw.Bytes())
|
|
|
}
|
|
|
|
|
|
//get task info
|
|
|
-func (s *Conn) GetTaskInfo() (t *file.Tunnel, err error) {
|
|
|
+func (s *Conn) getInfo(t interface{}) (err error) {
|
|
|
var l int
|
|
|
buf := pool.BufPoolMax.Get().([]byte)
|
|
|
defer pool.PutBufPoolMax(buf)
|
|
@@ -328,24 +239,7 @@ func (s *Conn) GetTaskInfo() (t *file.Tunnel, err error) {
|
|
|
} else if _, err = s.ReadLen(l, buf); err != nil {
|
|
|
return
|
|
|
} else {
|
|
|
- arr := strings.Split(string(buf[:l]), common.CONN_DATA_SEQ)
|
|
|
- t = new(file.Tunnel)
|
|
|
- t.Target = new(file.Target)
|
|
|
- t.Mode = arr[0]
|
|
|
- t.Ports = arr[1]
|
|
|
- t.Target.TargetStr = arr[2]
|
|
|
- t.Id = int(file.GetDb().JsonDb.GetTaskId())
|
|
|
- t.Status = true
|
|
|
- t.Flow = new(file.Flow)
|
|
|
- t.Remark = arr[3]
|
|
|
- t.TargetAddr = arr[4]
|
|
|
- t.Password = arr[5]
|
|
|
- t.LocalPath = arr[6]
|
|
|
- t.StripPre = arr[7]
|
|
|
- if len(arr) > 8 {
|
|
|
- t.ServerIp = arr[8]
|
|
|
- }
|
|
|
- t.NoStore = true
|
|
|
+ json.Unmarshal(buf[:l], &t)
|
|
|
}
|
|
|
return
|
|
|
}
|
|
@@ -363,6 +257,7 @@ func (s *Conn) Write(b []byte) (int, error) {
|
|
|
//read
|
|
|
func (s *Conn) Read(b []byte) (n int, err error) {
|
|
|
if s.Rb != nil {
|
|
|
+ //if the rb is not nil ,read rb first
|
|
|
if len(s.Rb) > 0 {
|
|
|
n = copy(b, s.Rb)
|
|
|
s.Rb = s.Rb[n:]
|
|
@@ -408,6 +303,26 @@ func (s *Conn) WriteAddFail() error {
|
|
|
return binary.Write(s.Conn, binary.LittleEndian, false)
|
|
|
}
|
|
|
|
|
|
+func (s *Conn) LocalAddr() net.Addr {
|
|
|
+ return s.Conn.LocalAddr()
|
|
|
+}
|
|
|
+
|
|
|
+func (s *Conn) RemoteAddr() net.Addr {
|
|
|
+ return s.Conn.RemoteAddr()
|
|
|
+}
|
|
|
+
|
|
|
+func (s *Conn) SetDeadline(t time.Time) error {
|
|
|
+ return s.Conn.SetDeadline(t)
|
|
|
+}
|
|
|
+
|
|
|
+func (s *Conn) SetWriteDeadline(t time.Time) error {
|
|
|
+ return s.Conn.SetWriteDeadline(t)
|
|
|
+}
|
|
|
+
|
|
|
+func (s *Conn) SetReadDeadline(t time.Time) error {
|
|
|
+ return s.Conn.SetReadDeadline(t)
|
|
|
+}
|
|
|
+
|
|
|
//get the assembled amount data(len 4 and content)
|
|
|
func GetLenBytes(buf []byte) (b []byte, err error) {
|
|
|
raw := bytes.NewBuffer([]byte{})
|