Files

145 lines
4.0 KiB
Go
Raw Permalink Normal View History

2026-01-08 17:13:08 +08:00
//go:build !npcgui
// +build !npcgui
2019-02-13 03:54:00 +08:00
package proxy
2019-01-09 20:33:00 +08:00
import (
"errors"
2019-08-10 11:10:01 +08:00
"net"
"net/http"
2026-10-10 14:57:04 +08:00
"path/filepath"
2019-08-10 11:10:01 +08:00
"strconv"
2020-01-08 21:57:14 +08:00
"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"
2019-08-10 11:10:01 +08:00
"github.com/astaxie/beego"
"github.com/astaxie/beego/logs"
2019-01-09 20:33:00 +08:00
)
type TunnelModeServer struct {
2019-02-23 23:29:48 +08:00
BaseServer
2019-02-01 02:06:30 +08:00
process process
2019-03-23 22:19:59 +08:00
listener net.Listener
2019-01-09 20:33:00 +08:00
}
2026-01-08 17:13:08 +08:00
// tcp|http|host
2019-04-21 23:03:58 +08:00
func NewTunnelModeServer(process process, bridge NetBridge, task *file.Tunnel) *TunnelModeServer {
2019-01-09 20:33:00 +08:00
s := new(TunnelModeServer)
s.bridge = bridge
s.process = process
2019-01-26 17:27:28 +08:00
s.task = task
2019-01-09 20:33:00 +08:00
return s
}
2026-01-08 17:13:08 +08:00
// 开始
2019-01-09 20:33:00 +08:00
func (s *TunnelModeServer) Start() error {
return conn.NewTcpListenerAndProcess(s.task.ServerIp+":"+strconv.Itoa(s.task.Port), func(c net.Conn) {
2019-03-23 22:19:59 +08:00
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()
2019-03-23 22:19:59 +08:00
return
}
2019-03-23 22:19:59 +08:00
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)
2019-03-29 10:41:57 +08:00
s.task.Client.AddConn()
2019-03-23 22:19:59 +08:00
}, &s.listener)
2019-01-09 20:33:00 +08:00
}
2026-01-08 17:13:08 +08:00
// close
2019-01-09 20:33:00 +08:00
func (s *TunnelModeServer) Close() error {
return s.listener.Close()
}
2026-01-08 17:13:08 +08:00
// web管理方式
2019-01-09 20:33:00 +08:00
type WebServer struct {
2019-02-23 23:29:48 +08:00
BaseServer
2019-01-09 20:33:00 +08:00
}
2026-01-08 17:13:08 +08:00
// 开始
2019-02-03 00:54:43 +08:00
func (s *WebServer) Start() error {
2019-03-05 09:23:18 +08:00
p, _ := beego.AppConfig.Int("web_port")
if p == 0 {
stop := make(chan struct{})
<-stop
}
2019-01-09 20:33:00 +08:00
beego.BConfig.WebConfig.Session.SessionOn = true
// Serve management UI from embedded assets only (no disk web/ directory).
web.InitBeegoAssets()
2019-12-14 17:39:46 +08:00
err := errors.New("Web management startup failure ")
var l net.Listener
if l, err = connection.GetWebManagerListener(); err == nil {
2019-03-05 09:23:18 +08:00
beego.InitBeforeHTTPRun()
2019-12-14 17:39:46 +08:00
if beego.AppConfig.String("web_open_ssl") == "true" {
keyPath := beego.AppConfig.String("web_key_file")
certPath := beego.AppConfig.String("web_cert_file")
2026-10-10 14:57:04 +08:00
// 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)
}
2019-12-14 17:39:46 +08:00
err = http.ServeTLS(l, beego.BeeApp.Handlers, certPath, keyPath)
} else {
err = http.Serve(l, beego.BeeApp.Handlers)
}
2019-03-05 09:23:18 +08:00
} else {
logs.Error(err)
}
2019-12-14 17:39:46 +08:00
return err
2019-01-09 20:33:00 +08:00
}
2019-02-18 01:05:05 +08:00
func (s *WebServer) Close() error {
return nil
}
2026-01-08 17:13:08 +08:00
// new
2019-01-26 17:27:28 +08:00
func NewWebServer(bridge *bridge.Bridge) *WebServer {
2019-01-09 20:33:00 +08:00
s := new(WebServer)
s.bridge = bridge
return s
}
2019-02-09 17:07:47 +08:00
type process func(c *conn.Conn, s *TunnelModeServer) error
2019-02-01 02:06:30 +08:00
2026-01-08 17:13:08 +08:00
// tcp proxy
2019-02-09 17:07:47 +08:00
func ProcessTunnel(c *conn.Conn, s *TunnelModeServer) error {
2019-03-29 10:41:57 +08:00
targetAddr, err := s.task.Target.GetRandomTarget()
2019-03-15 14:03:49 +08:00
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)
2019-02-01 02:06:30 +08:00
}
2026-01-08 17:13:08 +08:00
// http proxy
2019-02-09 17:07:47 +08:00
func ProcessHttp(c *conn.Conn, s *TunnelModeServer) error {
2019-02-23 23:29:48 +08:00
_, addr, rb, err, r := c.GetHost()
2019-02-01 02:06:30 +08:00
if err != nil {
c.Close()
2019-02-23 23:29:48 +08:00
logs.Info(err)
2019-02-01 02:06:30 +08:00
return err
}
2026-07-12 21:31:06 -06:00
// 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)
2019-02-01 02:06:30 +08:00
return err
}
2026-07-12 21:31:06 -06:00
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)
2019-02-01 02:06:30 +08:00
}