Code optimization

This commit is contained in:
刘河
2019-03-23 22:19:59 +08:00
parent efa341c7e8
commit b189fb1b6e
260 changed files with 50746 additions and 851 deletions

View File

@@ -23,8 +23,6 @@ import (
"time"
)
const cryptKey = "1234567812345678"
type Conn struct {
Conn net.Conn
sync.Mutex
@@ -246,7 +244,7 @@ func (s *Conn) GetHostInfo() (h *file.Host, err error) {
} else {
arr := strings.Split(string(buf[:l]), common.CONN_DATA_SEQ)
h = new(file.Host)
h.Id = file.GetCsvDb().GetHostId()
h.Id = int(file.GetCsvDb().GetHostId())
h.Host = arr[0]
h.Target = arr[1]
h.HeaderChange = arr[2]
@@ -339,7 +337,7 @@ func (s *Conn) GetTaskInfo() (t *file.Tunnel, err error) {
t.Mode = arr[0]
t.Ports = arr[1]
t.Target = arr[2]
t.Id = file.GetCsvDb().GetTaskId()
t.Id = int(file.GetCsvDb().GetTaskId())
t.Status = true
t.Flow = new(file.Flow)
t.Remark = arr[3]

50
lib/conn/listener.go Normal file
View File

@@ -0,0 +1,50 @@
package conn
import (
"github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
"github.com/cnlh/nps/vender/github.com/xtaci/kcp"
"net"
"strings"
)
func NewTcpListenerAndProcess(addr string, f func(c net.Conn), listener *net.Listener) error {
var err error
*listener, err = net.Listen("tcp", addr)
if err != nil {
return err
}
Accept(*listener, f)
return nil
}
func NewKcpListenerAndProcess(addr string, f func(c net.Conn)) error {
kcpListener, err := kcp.ListenWithOptions(addr, nil, 150, 3)
if err != nil {
logs.Error(err)
return err
}
for {
c, err := kcpListener.AcceptKCP()
SetUdpSession(c)
if err != nil {
logs.Warn(err)
continue
}
go f(c)
}
return nil
}
func Accept(l net.Listener, f func(c net.Conn)) {
for {
c, err := l.Accept()
if err != nil {
if strings.Contains(err.Error(), "use of closed network connection") {
break
}
logs.Warn(err)
continue
}
go f(c)
}
}