//go:build !npcgui // +build !npcgui package proxy import ( "errors" "net" "net/http" "path/filepath" "strconv" "ehang.io/nps/bridge" "ehang.io/nps/lib/common" "ehang.io/nps/lib/conn" "ehang.io/nps/lib/file" "ehang.io/nps/server/connection" "ehang.io/nps/web" "github.com/astaxie/beego" "github.com/astaxie/beego/logs" ) type TunnelModeServer struct { BaseServer process process listener net.Listener } // tcp|http|host func NewTunnelModeServer(process process, bridge NetBridge, task *file.Tunnel) *TunnelModeServer { s := new(TunnelModeServer) s.bridge = bridge s.process = process s.task = task return s } // 开始 func (s *TunnelModeServer) Start() error { return conn.NewTcpListenerAndProcess(s.task.ServerIp+":"+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 } 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) } // close func (s *TunnelModeServer) Close() error { return s.listener.Close() } // web管理方式 type WebServer struct { BaseServer } // 开始 func (s *WebServer) Start() error { p, _ := beego.AppConfig.Int("web_port") if p == 0 { stop := make(chan struct{}) <-stop } beego.BConfig.WebConfig.Session.SessionOn = true // Serve management UI from embedded assets only (no disk web/ directory). web.InitBeegoAssets() err := errors.New("Web management startup failure ") var l net.Listener if l, err = connection.GetWebManagerListener(); err == nil { beego.InitBeforeHTTPRun() if beego.AppConfig.String("web_open_ssl") == "true" { keyPath := beego.AppConfig.String("web_key_file") certPath := beego.AppConfig.String("web_cert_file") // Resolve relative paths against the nps run path instead of the // process working directory. Otherwise the web panel fails to start // when nps runs from another directory (e.g. as a system service). if !filepath.IsAbs(certPath) { certPath = filepath.Join(common.GetRunPath(), certPath) } if !filepath.IsAbs(keyPath) { keyPath = filepath.Join(common.GetRunPath(), keyPath) } err = http.ServeTLS(l, beego.BeeApp.Handlers, certPath, keyPath) } else { err = http.Serve(l, beego.BeeApp.Handlers) } } else { logs.Error(err) } return err } func (s *WebServer) Close() error { return nil } // new func NewWebServer(bridge *bridge.Bridge) *WebServer { s := new(WebServer) s.bridge = bridge return s } type process func(c *conn.Conn, s *TunnelModeServer) error // tcp proxy func ProcessTunnel(c *conn.Conn, s *TunnelModeServer) error { 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()) return err } return s.DealClient(c, s.task.Client, targetAddr, nil, common.CONN_TCP, nil, s.task.Client.Flow, s.task.Target.LocalProxy, s.task, nil) } // http proxy func ProcessHttp(c *conn.Conn, s *TunnelModeServer) error { _, addr, rb, err, r := c.GetHost() if err != nil { c.Close() logs.Info(err) return err } // Authenticate before establishing CONNECT tunnel (fixes auth-after-200 bug). if err := s.proxyAuth(r, c, s.task.Client.Cnf.U, s.task.Client.Cnf.P); err != nil { logs.Warn("http proxy auth failed, client %d, remote %s: %v", s.task.Client.Id, c.Conn.RemoteAddr(), err) return err } if r.Method == "CONNECT" { if _, err := c.Write([]byte("HTTP/1.1 200 Connection established\r\n\r\n")); err != nil { c.Close() return err } rb = nil } logs.Info("http proxy request, method %s, host %s, client %d, remote %s", r.Method, addr, s.task.Client.Id, c.Conn.RemoteAddr()) return s.DealClient(c, s.task.Client, addr, rb, common.CONN_TCP, nil, s.task.Client.Flow, s.task.Target.LocalProxy, nil, nil) }