tcp.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package proxy
  2. import (
  3. "errors"
  4. "net"
  5. "net/http"
  6. "path/filepath"
  7. "strconv"
  8. "github.com/astaxie/beego"
  9. "github.com/astaxie/beego/logs"
  10. "github.com/cnlh/nps/bridge"
  11. "github.com/cnlh/nps/lib/common"
  12. "github.com/cnlh/nps/lib/conn"
  13. "github.com/cnlh/nps/lib/file"
  14. "github.com/cnlh/nps/server/connection"
  15. )
  16. type TunnelModeServer struct {
  17. BaseServer
  18. process process
  19. listener net.Listener
  20. }
  21. //tcp|http|host
  22. func NewTunnelModeServer(process process, bridge NetBridge, task *file.Tunnel) *TunnelModeServer {
  23. s := new(TunnelModeServer)
  24. s.bridge = bridge
  25. s.process = process
  26. s.task = task
  27. return s
  28. }
  29. //开始
  30. func (s *TunnelModeServer) Start() error {
  31. return conn.NewTcpListenerAndProcess(s.task.ServerIp+":"+strconv.Itoa(s.task.Port), func(c net.Conn) {
  32. if err := s.CheckFlowAndConnNum(s.task.Client); err != nil {
  33. logs.Warn("client id %d, task id %d,error %s, when tcp connection", s.task.Client.Id, s.task.Id, err.Error())
  34. c.Close()
  35. return
  36. }
  37. logs.Trace("new tcp connection,local port %d,client %d,remote address %s", s.task.Port, s.task.Client.Id, c.RemoteAddr())
  38. s.process(conn.NewConn(c), s)
  39. s.task.Client.AddConn()
  40. }, &s.listener)
  41. }
  42. //close
  43. func (s *TunnelModeServer) Close() error {
  44. return s.listener.Close()
  45. }
  46. //web管理方式
  47. type WebServer struct {
  48. BaseServer
  49. }
  50. //开始
  51. func (s *WebServer) Start() error {
  52. p, _ := beego.AppConfig.Int("web_port")
  53. if p == 0 {
  54. stop := make(chan struct{})
  55. <-stop
  56. }
  57. beego.BConfig.WebConfig.Session.SessionOn = true
  58. beego.SetStaticPath("/static", filepath.Join(common.GetRunPath(), "web", "static"))
  59. beego.SetViewsPath(filepath.Join(common.GetRunPath(), "web", "views"))
  60. if l, err := connection.GetWebManagerListener(); err == nil {
  61. beego.InitBeforeHTTPRun()
  62. http.Serve(l, beego.BeeApp.Handlers)
  63. } else {
  64. logs.Error(err)
  65. }
  66. return errors.New("Web management startup failure")
  67. }
  68. func (s *WebServer) Close() error {
  69. return nil
  70. }
  71. //new
  72. func NewWebServer(bridge *bridge.Bridge) *WebServer {
  73. s := new(WebServer)
  74. s.bridge = bridge
  75. return s
  76. }
  77. type process func(c *conn.Conn, s *TunnelModeServer) error
  78. //tcp proxy
  79. func ProcessTunnel(c *conn.Conn, s *TunnelModeServer) error {
  80. targetAddr, err := s.task.Target.GetRandomTarget()
  81. if err != nil {
  82. c.Close()
  83. 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())
  84. return err
  85. }
  86. return s.DealClient(c, s.task.Client, targetAddr, nil, common.CONN_TCP, nil, s.task.Flow, s.task.Target.LocalProxy)
  87. }
  88. //http proxy
  89. func ProcessHttp(c *conn.Conn, s *TunnelModeServer) error {
  90. _, addr, rb, err, r := c.GetHost()
  91. if err != nil {
  92. c.Close()
  93. logs.Info(err)
  94. return err
  95. }
  96. if r.Method == "CONNECT" {
  97. c.Write([]byte("HTTP/1.1 200 Connection established\r\n\r\n"))
  98. rb = nil
  99. }
  100. if err := s.auth(r, c, s.task.Client.Cnf.U, s.task.Client.Cnf.P); err != nil {
  101. return err
  102. }
  103. return s.DealClient(c, s.task.Client, addr, rb, common.CONN_TCP, nil, s.task.Flow, s.task.Target.LocalProxy)
  104. }