mirror of
https://github.com/ehang-io/nps.git
synced 2025-09-01 02:46:52 +00:00
Code optimization
This commit is contained in:
@@ -87,6 +87,5 @@ func (s *BaseServer) DealClient(c *conn.Conn, client *file.Client, addr string,
|
||||
}
|
||||
conn.CopyWaitGroup(target, c.Conn, link.Crypt, link.Compress, client.Rate, flow, true, rb)
|
||||
}
|
||||
client.AddConn()
|
||||
return nil
|
||||
}
|
||||
|
@@ -85,11 +85,12 @@ func (s *httpServer) processHttps(c net.Conn) {
|
||||
c.Close()
|
||||
return
|
||||
}
|
||||
defer host.Client.AddConn()
|
||||
if err = s.auth(r, conn.NewConn(c), host.Client.Cnf.U, host.Client.Cnf.P); err != nil {
|
||||
logs.Warn("auth error", err, r.RemoteAddr)
|
||||
return
|
||||
}
|
||||
if targetAddr, err = host.GetRandomTarget(); err != nil {
|
||||
if targetAddr, err = host.Target.GetRandomTarget(); err != nil {
|
||||
logs.Warn(err.Error())
|
||||
}
|
||||
logs.Trace("new https connection,clientId %d,host %s,remote address %s", host.Client.Id, r.Host, c.RemoteAddr().String())
|
||||
@@ -101,7 +102,6 @@ func (s *httpServer) Start() error {
|
||||
if s.errorContent, err = common.ReadAllFromFile(filepath.Join(common.GetRunPath(), "web", "static", "page", "error.html")); err != nil {
|
||||
s.errorContent = []byte("easyProxy 404")
|
||||
}
|
||||
|
||||
if s.httpPort > 0 {
|
||||
s.httpServer = s.NewServer(s.httpPort, "http")
|
||||
go func() {
|
||||
@@ -181,7 +181,6 @@ func (s *httpServer) handleTunneling(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (s *httpServer) process(c *conn.Conn, r *http.Request) {
|
||||
//多客户端域名代理
|
||||
var (
|
||||
isConn = true
|
||||
host *file.Host
|
||||
@@ -203,6 +202,7 @@ func (s *httpServer) process(c *conn.Conn, r *http.Request) {
|
||||
c.Close()
|
||||
return
|
||||
}
|
||||
defer host.Client.AddConn()
|
||||
logs.Trace("new %s connection,clientId %d,host %s,url %s,remote address %s", r.URL.Scheme, host.Client.Id, r.Host, r.URL, r.RemoteAddr)
|
||||
lastHost = host
|
||||
for {
|
||||
@@ -212,7 +212,7 @@ func (s *httpServer) process(c *conn.Conn, r *http.Request) {
|
||||
logs.Warn("auth error", err, r.RemoteAddr)
|
||||
break
|
||||
}
|
||||
if targetAddr, err = host.GetRandomTarget(); err != nil {
|
||||
if targetAddr, err = host.Target.GetRandomTarget(); err != nil {
|
||||
logs.Warn(err.Error())
|
||||
break
|
||||
}
|
||||
@@ -249,10 +249,6 @@ func (s *httpServer) process(c *conn.Conn, r *http.Request) {
|
||||
logs.Notice("the url %s %s %s can't be parsed!", r.URL.Scheme, r.Host, r.RequestURI)
|
||||
break
|
||||
} else if host != lastHost {
|
||||
host.Client.AddConn()
|
||||
if !hostTmp.Client.GetConn() {
|
||||
break
|
||||
}
|
||||
host = hostTmp
|
||||
lastHost = host
|
||||
isConn = true
|
||||
@@ -283,9 +279,6 @@ end:
|
||||
target.Close()
|
||||
}
|
||||
wg.Wait()
|
||||
if host != nil {
|
||||
host.Client.AddConn()
|
||||
}
|
||||
}
|
||||
|
||||
func (s *httpServer) NewServer(port int, scheme string) *http.Server {
|
||||
|
@@ -259,6 +259,7 @@ func (s *Sock5ModeServer) Start() error {
|
||||
}
|
||||
logs.Trace("New socks5 connection,client %d,remote address %s", s.task.Client.Id, c.RemoteAddr())
|
||||
s.handleConn(c)
|
||||
s.task.Client.AddConn()
|
||||
}, &s.listener)
|
||||
}
|
||||
|
||||
|
@@ -40,6 +40,7 @@ func (s *TunnelModeServer) Start() error {
|
||||
}
|
||||
logs.Trace("new tcp connection,local port %d,client %d,remote address %s", s.task.Port, s.task.Client.Id, c.RemoteAddr())
|
||||
s.process(conn.NewConn(c), s)
|
||||
s.task.Client.AddConn()
|
||||
}, &s.listener)
|
||||
}
|
||||
|
||||
@@ -87,7 +88,7 @@ type process func(c *conn.Conn, s *TunnelModeServer) error
|
||||
|
||||
//tcp隧道模式
|
||||
func ProcessTunnel(c *conn.Conn, s *TunnelModeServer) error {
|
||||
targetAddr, err := s.task.GetRandomTarget()
|
||||
targetAddr, err := s.task.Target.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())
|
||||
|
@@ -54,7 +54,7 @@ func (s *UdpModeServer) process(addr *net.UDPAddr, data []byte) {
|
||||
return
|
||||
}
|
||||
defer s.task.Client.AddConn()
|
||||
link := conn.NewLink(common.CONN_UDP, s.task.Target, s.task.Client.Cnf.Crypt, s.task.Client.Cnf.Compress, addr.String())
|
||||
link := conn.NewLink(common.CONN_UDP, s.task.Target.TargetStr, s.task.Client.Cnf.Crypt, s.task.Client.Cnf.Compress, addr.String())
|
||||
if target, err := s.bridge.SendLinkInfo(s.task.Client.Id, link, addr.String(), s.task); err != nil {
|
||||
return
|
||||
} else {
|
||||
|
@@ -57,8 +57,12 @@ func DealBridgeTask() {
|
||||
case t := <-Bridge.CloseTask:
|
||||
StopServer(t.Id)
|
||||
case id := <-Bridge.CloseClient:
|
||||
DelTunnelAndHostByClientId(id)
|
||||
file.GetCsvDb().DelClient(id)
|
||||
DelTunnelAndHostByClientId(id, true)
|
||||
if v, ok := file.GetCsvDb().Clients.Load(id); ok {
|
||||
if v.(*file.Client).NoStore {
|
||||
file.GetCsvDb().DelClient(id)
|
||||
}
|
||||
}
|
||||
case tunnel := <-Bridge.OpenTask:
|
||||
StartTask(tunnel.Id)
|
||||
case s := <-Bridge.SecretChan:
|
||||
@@ -68,7 +72,7 @@ func DealBridgeTask() {
|
||||
logs.Info("Connections exceed the current client %d limit", t.Client.Id)
|
||||
s.Conn.Close()
|
||||
} else if t.Status {
|
||||
go proxy.NewBaseServer(Bridge, t).DealClient(s.Conn, t.Client, t.Target, nil, common.CONN_TCP, nil, t.Flow)
|
||||
go proxy.NewBaseServer(Bridge, t).DealClient(s.Conn, t.Client, t.Target.TargetStr, nil, common.CONN_TCP, nil, t.Flow)
|
||||
} else {
|
||||
s.Conn.Close()
|
||||
logs.Trace("This key %s cannot be processed,status is close", s.Password)
|
||||
@@ -133,7 +137,6 @@ func NewMode(Bridge *bridge.Bridge, c *file.Tunnel) proxy.Service {
|
||||
t := &file.Tunnel{
|
||||
Port: 0,
|
||||
Mode: "httpHostServer",
|
||||
Target: "",
|
||||
Status: true,
|
||||
}
|
||||
AddTask(t)
|
||||
@@ -223,7 +226,7 @@ func DelTask(id int) error {
|
||||
func GetTunnel(start, length int, typeVal string, clientId int, search string) ([]*file.Tunnel, int) {
|
||||
list := make([]*file.Tunnel, 0)
|
||||
var cnt int
|
||||
keys := common.GetMapKeys(file.GetCsvDb().Tasks)
|
||||
keys := file.GetMapKeys(file.GetCsvDb().Tasks, false, "", "")
|
||||
for _, key := range keys {
|
||||
if value, ok := file.GetCsvDb().Tasks.Load(key); ok {
|
||||
v := value.(*file.Tunnel)
|
||||
@@ -255,8 +258,8 @@ func GetTunnel(start, length int, typeVal string, clientId int, search string) (
|
||||
}
|
||||
|
||||
//获取客户端列表
|
||||
func GetClientList(start, length int, search string, clientId int) (list []*file.Client, cnt int) {
|
||||
list, cnt = file.GetCsvDb().GetClientList(start, length, search, clientId)
|
||||
func GetClientList(start, length int, search, sort, order string, clientId int) (list []*file.Client, cnt int) {
|
||||
list, cnt = file.GetCsvDb().GetClientList(start, length, search, sort, order, clientId)
|
||||
dealClientData()
|
||||
return
|
||||
}
|
||||
@@ -293,10 +296,13 @@ func dealClientData() {
|
||||
}
|
||||
|
||||
//根据客户端id删除其所属的所有隧道和域名
|
||||
func DelTunnelAndHostByClientId(clientId int) {
|
||||
func DelTunnelAndHostByClientId(clientId int, justDelNoStore bool) {
|
||||
var ids []int
|
||||
file.GetCsvDb().Tasks.Range(func(key, value interface{}) bool {
|
||||
v := value.(*file.Tunnel)
|
||||
if justDelNoStore && !v.NoStore {
|
||||
return true
|
||||
}
|
||||
if v.Client.Id == clientId {
|
||||
ids = append(ids, v.Id)
|
||||
}
|
||||
@@ -308,6 +314,9 @@ func DelTunnelAndHostByClientId(clientId int) {
|
||||
ids = ids[:0]
|
||||
file.GetCsvDb().Hosts.Range(func(key, value interface{}) bool {
|
||||
v := value.(*file.Host)
|
||||
if justDelNoStore && !v.NoStore {
|
||||
return true
|
||||
}
|
||||
if v.Client.Id == clientId {
|
||||
ids = append(ids, v.Id)
|
||||
}
|
||||
@@ -378,7 +387,7 @@ func GetDashboardData() map[string]interface{} {
|
||||
tcpCount := 0
|
||||
|
||||
file.GetCsvDb().Clients.Range(func(key, value interface{}) bool {
|
||||
tcpCount += value.(*file.Client).NowConn
|
||||
tcpCount += int(value.(*file.Client).NowConn)
|
||||
return true
|
||||
})
|
||||
data["tcpCount"] = tcpCount
|
||||
|
Reference in New Issue
Block a user