mirror of
https://github.com/ehang-io/nps.git
synced 2025-09-02 03:16:53 +00:00
Code optimization
This commit is contained in:
@@ -64,17 +64,19 @@ func (s *BaseServer) auth(r *http.Request, c *conn.Conn, u, p string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *BaseServer) checkFlow() error {
|
||||
if s.task.Client.Flow.FlowLimit > 0 && (s.task.Client.Flow.FlowLimit<<20) < (s.task.Client.Flow.ExportFlow+s.task.Client.Flow.InletFlow) {
|
||||
func (s *BaseServer) CheckFlowAndConnNum(client *file.Client) error {
|
||||
if client.Flow.FlowLimit > 0 && (client.Flow.FlowLimit<<20) < (client.Flow.ExportFlow+client.Flow.InletFlow) {
|
||||
return errors.New("Traffic exceeded")
|
||||
}
|
||||
if !client.GetConn() {
|
||||
return errors.New("Connections exceed the current client limit")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//与客户端建立通道
|
||||
func (s *BaseServer) DealClient(c *conn.Conn, client *file.Client, addr string, rb []byte, tp string) error {
|
||||
func (s *BaseServer) DealClient(c *conn.Conn, client *file.Client, addr string, rb []byte, tp string, f func()) error {
|
||||
link := conn.NewLink(tp, addr, client.Cnf.Crypt, client.Cnf.Compress, c.Conn.RemoteAddr().String())
|
||||
|
||||
if target, err := s.bridge.SendLinkInfo(client.Id, link, c.Conn.RemoteAddr().String(), s.task); err != nil {
|
||||
logs.Warn("task id %d get connection from client id %d error %s", s.task.Id, client.Id, err.Error())
|
||||
c.Close()
|
||||
@@ -84,9 +86,11 @@ func (s *BaseServer) DealClient(c *conn.Conn, client *file.Client, addr string,
|
||||
//HTTP proxy crypt or compress
|
||||
conn.GetConn(target, link.Crypt, link.Compress, client.Rate, true).Write(rb)
|
||||
}
|
||||
if f != nil {
|
||||
f()
|
||||
}
|
||||
conn.CopyWaitGroup(target, c.Conn, link.Crypt, link.Compress, client.Rate, s.task.Flow, true)
|
||||
}
|
||||
|
||||
client.AddConn()
|
||||
return nil
|
||||
}
|
||||
|
@@ -28,8 +28,9 @@ type httpServer struct {
|
||||
httpsPort int //https监听端口
|
||||
pemPath string
|
||||
keyPath string
|
||||
stop chan bool
|
||||
httpslistener net.Listener
|
||||
httpServer *http.Server
|
||||
httpsServer *http.Server
|
||||
httpsListener net.Listener
|
||||
}
|
||||
|
||||
func NewHttp(bridge *bridge.Bridge, c *file.Tunnel) *httpServer {
|
||||
@@ -47,7 +48,6 @@ func NewHttp(bridge *bridge.Bridge, c *file.Tunnel) *httpServer {
|
||||
httpsPort: httpsPort,
|
||||
pemPath: pemPath,
|
||||
keyPath: keyPath,
|
||||
stop: make(chan bool),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,13 +58,17 @@ func (s *httpServer) processHttps(c net.Conn) {
|
||||
return
|
||||
}
|
||||
var host *file.Host
|
||||
file.GetCsvDb().Lock()
|
||||
for _, v := range file.GetCsvDb().Hosts {
|
||||
file.GetCsvDb().Hosts.Range(func(key, value interface{}) bool {
|
||||
v := value.(*file.Host)
|
||||
if v.Scheme != "https" && v.Scheme != "all" {
|
||||
return true
|
||||
}
|
||||
if bytes.Index(buf[:n], []byte(v.Host)) >= 0 && (host == nil || len(host.Host) < len(v.Host)) {
|
||||
host = v
|
||||
return false
|
||||
}
|
||||
}
|
||||
file.GetCsvDb().Unlock()
|
||||
return true
|
||||
})
|
||||
if host == nil {
|
||||
logs.Error("new https connection can't be parsed!", c.RemoteAddr().String())
|
||||
c.Close()
|
||||
@@ -76,40 +80,37 @@ func (s *httpServer) processHttps(c net.Conn) {
|
||||
r.URL = new(url.URL)
|
||||
r.URL.Scheme = "https"
|
||||
r.Host = host.Host
|
||||
//read the host form connection
|
||||
if !host.Client.GetConn() { //conn num limit
|
||||
logs.Notice("connections exceed the current client %d limit %d ,now connection num %d", host.Client.Id, host.Client.MaxConn, host.Client.NowConn)
|
||||
if err := s.CheckFlowAndConnNum(host.Client); err != nil {
|
||||
logs.Warn("client id %d, host id %d, error %s, when https connection", host.Client.Id, host.Id, err.Error())
|
||||
c.Close()
|
||||
return
|
||||
}
|
||||
//流量限制
|
||||
if host.Client.Flow.FlowLimit > 0 && (host.Client.Flow.FlowLimit<<20) < (host.Client.Flow.ExportFlow+host.Client.Flow.InletFlow) {
|
||||
logs.Warn("Traffic exceeded client id %s", host.Client.Id)
|
||||
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 {
|
||||
logs.Warn(err.Error())
|
||||
}
|
||||
logs.Trace("new https connection,clientId %d,host %s,remote address %s", host.Client.Id, r.Host, c.RemoteAddr().String())
|
||||
s.DealClient(conn.NewConn(c), host.Client, targetAddr, buf[:n], common.CONN_TCP)
|
||||
s.DealClient(conn.NewConn(c), host.Client, targetAddr, buf[:n], common.CONN_TCP, nil)
|
||||
}
|
||||
|
||||
func (s *httpServer) Start() error {
|
||||
var err error
|
||||
var httpSrv, httpsSrv *http.Server
|
||||
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 {
|
||||
httpSrv = s.NewServer(s.httpPort, "http")
|
||||
s.httpServer = s.NewServer(s.httpPort, "http")
|
||||
go func() {
|
||||
l, err := connection.GetHttpListener()
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
os.Exit(0)
|
||||
}
|
||||
err = httpSrv.Serve(l)
|
||||
err = s.httpServer.Serve(l)
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
os.Exit(0)
|
||||
@@ -117,23 +118,16 @@ func (s *httpServer) Start() error {
|
||||
}()
|
||||
}
|
||||
if s.httpsPort > 0 {
|
||||
if !common.FileExists(s.pemPath) {
|
||||
os.Exit(0)
|
||||
}
|
||||
if !common.FileExists(s.keyPath) {
|
||||
logs.Error("ssl keyFile %s exist", s.keyPath)
|
||||
os.Exit(0)
|
||||
}
|
||||
httpsSrv = s.NewServer(s.httpsPort, "https")
|
||||
s.httpsServer = s.NewServer(s.httpsPort, "https")
|
||||
go func() {
|
||||
l, err := connection.GetHttpsListener()
|
||||
s.httpsListener, err = connection.GetHttpsListener()
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
os.Exit(0)
|
||||
}
|
||||
if b, err := beego.AppConfig.Bool("https_just_proxy"); err == nil && b {
|
||||
for {
|
||||
c, err := l.Accept()
|
||||
c, err := s.httpsListener.Accept()
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
break
|
||||
@@ -141,7 +135,15 @@ func (s *httpServer) Start() error {
|
||||
go s.processHttps(c)
|
||||
}
|
||||
} else {
|
||||
err = httpsSrv.ServeTLS(l, s.pemPath, s.keyPath)
|
||||
if !common.FileExists(s.pemPath) {
|
||||
logs.Error("ssl certFile %s exist", s.keyPath)
|
||||
os.Exit(0)
|
||||
}
|
||||
if !common.FileExists(s.keyPath) {
|
||||
logs.Error("ssl keyFile %s exist", s.keyPath)
|
||||
os.Exit(0)
|
||||
}
|
||||
err = s.httpsServer.ServeTLS(s.httpsListener, s.pemPath, s.keyPath)
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
os.Exit(0)
|
||||
@@ -149,20 +151,19 @@ func (s *httpServer) Start() error {
|
||||
}
|
||||
}()
|
||||
}
|
||||
select {
|
||||
case <-s.stop:
|
||||
if httpSrv != nil {
|
||||
httpsSrv.Close()
|
||||
}
|
||||
if httpsSrv != nil {
|
||||
httpsSrv.Close()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *httpServer) Close() error {
|
||||
s.stop <- true
|
||||
if s.httpsListener != nil {
|
||||
s.httpsListener.Close()
|
||||
}
|
||||
if s.httpsServer != nil {
|
||||
s.httpsServer.Close()
|
||||
}
|
||||
if s.httpServer != nil {
|
||||
s.httpServer.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -196,23 +197,17 @@ func (s *httpServer) process(c *conn.Conn, r *http.Request) {
|
||||
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)
|
||||
goto end
|
||||
} else if !host.Client.GetConn() { //conn num limit
|
||||
logs.Notice("connections exceed the current client %d limit %d ,now connection num %d", host.Client.Id, host.Client.MaxConn, host.Client.NowConn)
|
||||
}
|
||||
if err := s.CheckFlowAndConnNum(host.Client); err != nil {
|
||||
logs.Warn("client id %d, host id %d, error %s, when https connection", host.Client.Id, host.Id, err.Error())
|
||||
c.Close()
|
||||
return
|
||||
} else {
|
||||
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
|
||||
}
|
||||
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 {
|
||||
start:
|
||||
if isConn {
|
||||
//流量限制
|
||||
if host.Client.Flow.FlowLimit > 0 && (host.Client.Flow.FlowLimit<<20) < (host.Client.Flow.ExportFlow+host.Client.Flow.InletFlow) {
|
||||
logs.Warn("Traffic exceeded client id %s", host.Client.Id)
|
||||
break
|
||||
}
|
||||
//权限控制
|
||||
if err = s.auth(r, c, host.Client.Cnf.U, host.Client.Cnf.P); err != nil {
|
||||
logs.Warn("auth error", err, r.RemoteAddr)
|
||||
break
|
||||
|
@@ -3,8 +3,7 @@ package proxy
|
||||
import (
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/conn"
|
||||
"github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
|
||||
"github.com/cnlh/nps/vender/github.com/xtaci/kcp"
|
||||
"net"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
@@ -30,21 +29,9 @@ func NewP2PServer(p2pPort int) *P2PServer {
|
||||
}
|
||||
|
||||
func (s *P2PServer) Start() error {
|
||||
kcpListener, err := kcp.ListenWithOptions(":"+strconv.Itoa(s.p2pPort), nil, 150, 3)
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
return err
|
||||
}
|
||||
for {
|
||||
c, err := kcpListener.AcceptKCP()
|
||||
conn.SetUdpSession(c)
|
||||
if err != nil {
|
||||
logs.Warn(err)
|
||||
continue
|
||||
}
|
||||
go s.p2pProcess(conn.NewConn(c))
|
||||
}
|
||||
return nil
|
||||
return conn.NewKcpListenerAndProcess(":"+strconv.Itoa(s.p2pPort), func(c net.Conn) {
|
||||
s.p2pProcess(conn.NewConn(c))
|
||||
})
|
||||
}
|
||||
|
||||
func (s *P2PServer) p2pProcess(c *conn.Conn) {
|
||||
|
@@ -11,7 +11,6 @@ import (
|
||||
"io"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -141,18 +140,9 @@ func (s *Sock5ModeServer) doConnect(c net.Conn, command uint8) {
|
||||
} else {
|
||||
ltype = common.CONN_TCP
|
||||
}
|
||||
//s.DealClient(conn.NewConn(c), addr, nil, ltype)
|
||||
link := conn.NewLink(ltype, addr, s.task.Client.Cnf.Crypt, s.task.Client.Cnf.Compress, c.RemoteAddr().String())
|
||||
|
||||
if target, err := s.bridge.SendLinkInfo(s.task.Client.Id, link, c.RemoteAddr().String(), s.task); err != nil {
|
||||
c.Close()
|
||||
return
|
||||
} else {
|
||||
s.DealClient(conn.NewConn(c), s.task.Client, addr, nil, ltype, func() {
|
||||
s.sendReply(c, succeeded)
|
||||
conn.CopyWaitGroup(target, c, link.Crypt, link.Compress, s.task.Client.Rate, s.task.Flow, true)
|
||||
}
|
||||
|
||||
s.task.Client.AddConn()
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -261,37 +251,15 @@ func (s *Sock5ModeServer) Auth(c net.Conn) error {
|
||||
|
||||
//start
|
||||
func (s *Sock5ModeServer) Start() error {
|
||||
var err error
|
||||
s.listener, err = net.Listen("tcp", ":"+strconv.Itoa(s.task.Port))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for {
|
||||
conn, err := s.listener.Accept()
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "use of closed network connection") {
|
||||
break
|
||||
}
|
||||
logs.Warn("accept error: ", err)
|
||||
return conn.NewTcpListenerAndProcess(":"+strconv.Itoa(s.task.Port), func(c net.Conn) {
|
||||
if err := s.CheckFlowAndConnNum(s.task.Client); err != nil {
|
||||
logs.Warn("client id %d, task id %d, error %s, when socks5 connection", s.task.Client.Id, s.task.Id, err.Error())
|
||||
c.Close()
|
||||
return
|
||||
}
|
||||
if err := s.checkFlow(); err != nil {
|
||||
logs.Warn("client id %d task id %d error %s", s.task.Client.Id, s.task.Id, err.Error())
|
||||
conn.Close()
|
||||
}
|
||||
if s.task.Client.GetConn() {
|
||||
logs.Trace("New socks5 connection,client %d,remote address %s", s.task.Client.Id, conn.RemoteAddr())
|
||||
go s.handleConn(conn)
|
||||
} else {
|
||||
logs.Warn("Connections exceed the current client %d limit", s.task.Client.Id)
|
||||
conn.Close()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//close
|
||||
func (s *Sock5ModeServer) Close() error {
|
||||
return s.listener.Close()
|
||||
logs.Trace("New socks5 connection,client %d,remote address %s", s.task.Client.Id, c.RemoteAddr())
|
||||
s.handleConn(c)
|
||||
}, &s.listener)
|
||||
}
|
||||
|
||||
//new
|
||||
@@ -301,3 +269,8 @@ func NewSock5ModeServer(bridge *bridge.Bridge, task *file.Tunnel) *Sock5ModeServ
|
||||
s.task = task
|
||||
return s
|
||||
}
|
||||
|
||||
//close
|
||||
func (s *Sock5ModeServer) Close() error {
|
||||
return s.listener.Close()
|
||||
}
|
||||
|
@@ -12,13 +12,13 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type TunnelModeServer struct {
|
||||
BaseServer
|
||||
process process
|
||||
listener *net.TCPListener
|
||||
listener net.Listener
|
||||
}
|
||||
|
||||
//tcp|http|host
|
||||
@@ -32,33 +32,15 @@ func NewTunnelModeServer(process process, bridge *bridge.Bridge, task *file.Tunn
|
||||
|
||||
//开始
|
||||
func (s *TunnelModeServer) Start() error {
|
||||
var err error
|
||||
s.listener, err = net.ListenTCP("tcp", &net.TCPAddr{net.ParseIP("0.0.0.0"), s.task.Port, ""})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for {
|
||||
c, err := s.listener.AcceptTCP()
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "use of closed network connection") {
|
||||
break
|
||||
}
|
||||
logs.Info(err)
|
||||
continue
|
||||
}
|
||||
if err := s.checkFlow(); err != nil {
|
||||
logs.Warn("client id %d task id %d error %s", s.task.Client.Id, s.task.Id, err.Error())
|
||||
return conn.NewTcpListenerAndProcess(":"+strconv.Itoa(s.task.Port), func(c net.Conn) {
|
||||
if err := s.CheckFlowAndConnNum(s.task.Client); err != nil {
|
||||
logs.Warn("client id %d, task id %d,error %s, when tcp connection", s.task.Client.Id, s.task.Id, err.Error())
|
||||
c.Close()
|
||||
return
|
||||
}
|
||||
if s.task.Client.GetConn() {
|
||||
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)
|
||||
c.Close()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
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.listener)
|
||||
}
|
||||
|
||||
//close
|
||||
@@ -78,10 +60,6 @@ func (s *WebServer) Start() error {
|
||||
stop := make(chan struct{})
|
||||
<-stop
|
||||
}
|
||||
//if !common.TestTcpPort(p) {
|
||||
// // logs.Error("Web management port %d is occupied", p)
|
||||
// // os.Exit(0)
|
||||
// //}
|
||||
beego.BConfig.WebConfig.Session.SessionOn = true
|
||||
beego.SetStaticPath("/static", filepath.Join(common.GetRunPath(), "web", "static"))
|
||||
beego.SetViewsPath(filepath.Join(common.GetRunPath(), "web", "views"))
|
||||
@@ -115,7 +93,7 @@ func ProcessTunnel(c *conn.Conn, s *TunnelModeServer) error {
|
||||
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, s.task.Client, targetAddr, nil, common.CONN_TCP)
|
||||
return s.DealClient(c, s.task.Client, targetAddr, nil, common.CONN_TCP, nil)
|
||||
}
|
||||
|
||||
//http代理模式
|
||||
@@ -133,5 +111,5 @@ func ProcessHttp(c *conn.Conn, s *TunnelModeServer) error {
|
||||
if err := s.auth(r, c, s.task.Client.Cnf.U, s.task.Client.Cnf.P); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.DealClient(c, s.task.Client, addr, rb, common.CONN_TCP)
|
||||
return s.DealClient(c, s.task.Client, addr, rb, common.CONN_TCP, nil)
|
||||
}
|
||||
|
@@ -46,10 +46,12 @@ func (s *UdpModeServer) Start() error {
|
||||
}
|
||||
|
||||
func (s *UdpModeServer) process(addr *net.UDPAddr, data []byte) {
|
||||
link := conn.NewLink(common.CONN_UDP, s.task.Target, s.task.Client.Cnf.Crypt, s.task.Client.Cnf.Compress, addr.String())
|
||||
if err := s.checkFlow(); err != nil {
|
||||
if err := s.CheckFlowAndConnNum(s.task.Client); err != nil {
|
||||
logs.Warn("client id %d, task id %d,error %s, when udp connection", s.task.Client.Id, s.task.Id, err.Error())
|
||||
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())
|
||||
if target, err := s.bridge.SendLinkInfo(s.task.Client.Id, link, addr.String(), s.task); err != nil {
|
||||
return
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user