mirror of
https://github.com/ehang-io/nps.git
synced 2025-09-01 02:46:52 +00:00
MUX optimization
This commit is contained in:
@@ -81,9 +81,10 @@ func (s *BaseServer) DealClient(c *conn.Conn, addr string, rb []byte, tp string)
|
||||
return err
|
||||
} else {
|
||||
if rb != nil {
|
||||
target.Write(rb)
|
||||
//HTTP proxy crypt or compress
|
||||
conn.GetConn(target, link.Crypt, link.Compress, s.task.Client.Rate, true).Write(rb)
|
||||
}
|
||||
conn.CopyWaitGroup(target, c.Conn, link.Crypt, link.Compress, s.task.Client.Rate, s.task.Client.Flow, true)
|
||||
conn.CopyWaitGroup(target, c.Conn, link.Crypt, link.Compress, s.task.Client.Rate, s.task.Flow, true)
|
||||
}
|
||||
|
||||
s.task.Client.AddConn()
|
||||
|
@@ -134,6 +134,9 @@ func (s *httpServer) process(c *conn.Conn, r *http.Request) {
|
||||
err error
|
||||
connClient io.ReadWriteCloser
|
||||
scheme = r.URL.Scheme
|
||||
lk *conn.Link
|
||||
targetAddr string
|
||||
wg sync.WaitGroup
|
||||
)
|
||||
if host, err = file.GetCsvDb().GetInfoByHost(r.Host, r); err != nil {
|
||||
logs.Notice("the url %s %s %s can't be parsed!", r.URL.Scheme, r.Host, r.RequestURI)
|
||||
@@ -159,7 +162,11 @@ func (s *httpServer) process(c *conn.Conn, r *http.Request) {
|
||||
logs.Warn("auth error", err, r.RemoteAddr)
|
||||
break
|
||||
}
|
||||
lk := conn.NewLink(common.CONN_TCP, host.GetRandomTarget(), host.Client.Cnf.Crypt, host.Client.Cnf.Compress, r.RemoteAddr)
|
||||
if targetAddr, err = host.GetRandomTarget(); err != nil {
|
||||
logs.Warn(err.Error())
|
||||
break
|
||||
}
|
||||
lk = conn.NewLink(common.CONN_TCP, targetAddr, host.Client.Cnf.Crypt, host.Client.Cnf.Compress, r.RemoteAddr)
|
||||
if target, err = s.bridge.SendLinkInfo(host.Client.Id, lk, c.Conn.RemoteAddr().String(), nil); err != nil {
|
||||
logs.Notice("connect to target %s error %s", lk.Host, err)
|
||||
break
|
||||
@@ -167,10 +174,12 @@ func (s *httpServer) process(c *conn.Conn, r *http.Request) {
|
||||
connClient = conn.GetConn(target, lk.Crypt, lk.Compress, host.Client.Rate, true)
|
||||
isConn = false
|
||||
go func() {
|
||||
wg.Add(1)
|
||||
w, _ := common.CopyBuffer(c, connClient)
|
||||
host.Flow.Add(0, w)
|
||||
c.Close()
|
||||
target.Close()
|
||||
wg.Done()
|
||||
}()
|
||||
} else {
|
||||
r, err = http.ReadRequest(bufio.NewReader(c))
|
||||
@@ -197,7 +206,6 @@ func (s *httpServer) process(c *conn.Conn, r *http.Request) {
|
||||
host = hostTmp
|
||||
lastHost = host
|
||||
isConn = true
|
||||
|
||||
goto start
|
||||
}
|
||||
}
|
||||
@@ -208,7 +216,7 @@ func (s *httpServer) process(c *conn.Conn, r *http.Request) {
|
||||
break
|
||||
}
|
||||
host.Flow.Add(int64(len(b)), 0)
|
||||
logs.Trace("%s request, method %s, host %s, url %s, remote address %s, target %s", r.URL.Scheme, r.Method, r.Host, r.RequestURI, r.RemoteAddr, host.Target)
|
||||
logs.Trace("%s request, method %s, host %s, url %s, remote address %s, target %s", r.URL.Scheme, r.Method, r.Host, r.RequestURI, r.RemoteAddr, lk.Host)
|
||||
//write
|
||||
connClient.Write(b)
|
||||
}
|
||||
@@ -220,6 +228,7 @@ end:
|
||||
if target != nil {
|
||||
target.Close()
|
||||
}
|
||||
wg.Wait()
|
||||
if host != nil {
|
||||
host.Client.AddConn()
|
||||
}
|
||||
|
@@ -149,7 +149,7 @@ func (s *Sock5ModeServer) doConnect(c net.Conn, command uint8) {
|
||||
return
|
||||
} else {
|
||||
s.sendReply(c, succeeded)
|
||||
conn.CopyWaitGroup(target, c, link.Crypt, link.Compress, s.task.Client.Rate, s.task.Client.Flow, true)
|
||||
conn.CopyWaitGroup(target, c, link.Crypt, link.Compress, s.task.Client.Rate, s.task.Flow, true)
|
||||
}
|
||||
|
||||
s.task.Client.AddConn()
|
||||
|
@@ -51,7 +51,7 @@ func (s *TunnelModeServer) Start() error {
|
||||
c.Close()
|
||||
}
|
||||
if s.task.Client.GetConn() {
|
||||
logs.Trace("New tcp connection,client %d,remote address %s", s.task.Client.Id, c.RemoteAddr())
|
||||
logs.Trace("New tcp connection,local port %d,client %d,remote address %s", s.task.Port, s.task.Client.Id, c.RemoteAddr())
|
||||
go s.process(conn.NewConn(c), s)
|
||||
} else {
|
||||
logs.Info("Connections exceed the current client %d limit", s.task.Client.Id)
|
||||
@@ -109,7 +109,13 @@ type process func(c *conn.Conn, s *TunnelModeServer) error
|
||||
|
||||
//tcp隧道模式
|
||||
func ProcessTunnel(c *conn.Conn, s *TunnelModeServer) error {
|
||||
return s.DealClient(c, s.task.GetRandomTarget(), nil, common.CONN_TCP)
|
||||
targetAddr, err := s.task.GetRandomTarget()
|
||||
if err != nil {
|
||||
c.Close()
|
||||
logs.Warn("tcp port %d ,client id %d,task id %d connect error %s", s.task.Port, s.task.Client.Id, s.task.Id, err.Error())
|
||||
return err
|
||||
}
|
||||
return s.DealClient(c, targetAddr, nil, common.CONN_TCP)
|
||||
}
|
||||
|
||||
//http代理模式
|
||||
|
@@ -57,6 +57,7 @@ func (s *UdpModeServer) process(addr *net.UDPAddr, data []byte) {
|
||||
buf := pool.BufPoolUdp.Get().([]byte)
|
||||
defer pool.BufPoolUdp.Put(buf)
|
||||
target.Write(data)
|
||||
s.task.Flow.Add(int64(len(data)), 0)
|
||||
if n, err := target.Read(buf); err != nil {
|
||||
logs.Warn(err)
|
||||
return
|
||||
|
@@ -52,9 +52,13 @@ func DealBridgeTask() {
|
||||
select {
|
||||
case t := <-Bridge.OpenTask:
|
||||
AddTask(t)
|
||||
case t := <-Bridge.CloseTask:
|
||||
StopServer(t.Id)
|
||||
case id := <-Bridge.CloseClient:
|
||||
DelTunnelAndHostByClientId(id)
|
||||
file.GetCsvDb().DelClient(id)
|
||||
case tunnel := <-Bridge.OpenTask:
|
||||
StartTask(tunnel.Id)
|
||||
case s := <-Bridge.SecretChan:
|
||||
logs.Trace("New secret connection, addr", s.Conn.Conn.RemoteAddr())
|
||||
if t := file.GetCsvDb().GetTaskByMd5Password(s.Password); t != nil {
|
||||
@@ -202,6 +206,8 @@ func DelTask(id int) error {
|
||||
func GetTunnel(start, length int, typeVal string, clientId int) ([]*file.Tunnel, int) {
|
||||
list := make([]*file.Tunnel, 0)
|
||||
var cnt int
|
||||
file.GetCsvDb().Lock()
|
||||
defer file.GetCsvDb().Unlock()
|
||||
for _, v := range file.GetCsvDb().Tasks {
|
||||
if (typeVal != "" && v.Mode != typeVal) || (typeVal == "" && clientId != v.Client.Id) {
|
||||
continue
|
||||
@@ -234,6 +240,8 @@ func GetClientList(start, length int) (list []*file.Client, cnt int) {
|
||||
}
|
||||
|
||||
func dealClientData(list []*file.Client) {
|
||||
file.GetCsvDb().Lock()
|
||||
defer file.GetCsvDb().Unlock()
|
||||
for _, v := range list {
|
||||
if _, ok := Bridge.Client[v.Id]; ok {
|
||||
v.IsConnect = true
|
||||
@@ -261,19 +269,27 @@ func dealClientData(list []*file.Client) {
|
||||
//根据客户端id删除其所属的所有隧道和域名
|
||||
func DelTunnelAndHostByClientId(clientId int) {
|
||||
var ids []int
|
||||
file.GetCsvDb().Lock()
|
||||
for _, v := range file.GetCsvDb().Tasks {
|
||||
if v.Client.Id == clientId {
|
||||
ids = append(ids, v.Id)
|
||||
}
|
||||
}
|
||||
file.GetCsvDb().Unlock()
|
||||
for _, id := range ids {
|
||||
DelTask(id)
|
||||
}
|
||||
ids = ids[:0]
|
||||
file.GetCsvDb().Lock()
|
||||
for _, v := range file.GetCsvDb().Hosts {
|
||||
if v.Client.Id == clientId {
|
||||
file.GetCsvDb().DelHost(v.Id)
|
||||
ids = append(ids, v.Id)
|
||||
}
|
||||
}
|
||||
file.GetCsvDb().Unlock()
|
||||
for _, id := range ids {
|
||||
file.GetCsvDb().DelHost(id)
|
||||
}
|
||||
}
|
||||
|
||||
//关闭客户端连接
|
||||
@@ -300,6 +316,8 @@ func GetDashboardData() map[string]interface{} {
|
||||
data["inletFlowCount"] = int(in)
|
||||
data["exportFlowCount"] = int(out)
|
||||
var tcp, udp, secret, socks5, p2p, http int
|
||||
file.GetCsvDb().Lock()
|
||||
defer file.GetCsvDb().Unlock()
|
||||
for _, v := range file.GetCsvDb().Tasks {
|
||||
switch v.Mode {
|
||||
case "tcp":
|
||||
@@ -366,7 +384,6 @@ func GetDashboardData() map[string]interface{} {
|
||||
data["sys"+strconv.Itoa(i+1)] = serverStatus[i*fg]
|
||||
}
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user