local.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package client
  2. import (
  3. "github.com/cnlh/nps/lib/common"
  4. "github.com/cnlh/nps/lib/config"
  5. "github.com/cnlh/nps/lib/conn"
  6. "github.com/cnlh/nps/lib/crypt"
  7. "github.com/cnlh/nps/lib/file"
  8. "github.com/cnlh/nps/lib/mux"
  9. "github.com/cnlh/nps/server/proxy"
  10. "github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
  11. "github.com/cnlh/nps/vender/github.com/xtaci/kcp"
  12. "net"
  13. "net/http"
  14. )
  15. var (
  16. LocalServer []*net.TCPListener
  17. udpConn net.Conn
  18. muxSession *mux.Mux
  19. fileServer []*http.Server
  20. p2pNetBridge *p2pBridge
  21. )
  22. type p2pBridge struct {
  23. }
  24. func (p2pBridge *p2pBridge) SendLinkInfo(clientId int, link *conn.Link, t *file.Tunnel) (target net.Conn, err error) {
  25. nowConn, err := muxSession.NewConn()
  26. if err != nil {
  27. udpConn = nil
  28. return nil, err
  29. }
  30. if _, err := conn.NewConn(nowConn).SendInfo(link, ""); err != nil {
  31. return nil, err
  32. }
  33. return nowConn, nil
  34. }
  35. func CloseLocalServer() {
  36. for _, v := range LocalServer {
  37. v.Close()
  38. }
  39. for _, v := range fileServer {
  40. v.Close()
  41. }
  42. }
  43. func startLocalFileServer(config *config.CommonConfig, t *file.Tunnel, vkey string) {
  44. remoteConn, err := NewConn(config.Tp, vkey, config.Server, common.WORK_FILE, config.ProxyUrl)
  45. if err != nil {
  46. logs.Error("Local connection server failed ", err.Error())
  47. return
  48. }
  49. srv := &http.Server{
  50. Handler: http.StripPrefix(t.StripPre, http.FileServer(http.Dir(t.LocalPath))),
  51. }
  52. logs.Info("start local file system, local path %s, strip prefix %s ,remote port %s ", t.LocalPath, t.StripPre, t.Ports)
  53. fileServer = append(fileServer, srv)
  54. listener := mux.NewMux(remoteConn.Conn, common.CONN_TCP)
  55. logs.Error(srv.Serve(listener))
  56. }
  57. func StartLocalServer(l *config.LocalServer, config *config.CommonConfig) error {
  58. tmpConn, err := common.GetLocalUdpAddr()
  59. if err != nil {
  60. return err
  61. }
  62. for i := 0; i < 10; i++ {
  63. logs.Notice("try to connect to the server", i+1)
  64. newUdpConn(tmpConn.LocalAddr().String(), config, l)
  65. if udpConn != nil {
  66. break
  67. }
  68. }
  69. task := &file.Tunnel{
  70. Port: l.Port,
  71. ServerIp: "0.0.0.0",
  72. Status: true,
  73. Client: &file.Client{
  74. Cnf: &file.Config{
  75. U: "",
  76. P: "",
  77. Compress: config.Client.Cnf.Compress,
  78. },
  79. Status: true,
  80. RateLimit: 0,
  81. Flow: &file.Flow{},
  82. },
  83. Flow: &file.Flow{},
  84. Target: &file.Target{},
  85. }
  86. switch l.Type {
  87. case "p2ps":
  88. logs.Info("successful start-up of local socks5 monitoring, port", l.Port)
  89. return proxy.NewSock5ModeServer(p2pNetBridge, task).Start()
  90. case "p2pt":
  91. logs.Info("successful start-up of local tcp trans monitoring, port", l.Port)
  92. return proxy.NewTunnelModeServer(proxy.HandleTrans, p2pNetBridge, task).Start()
  93. case "p2p", "secret":
  94. listener, err := net.ListenTCP("tcp", &net.TCPAddr{net.ParseIP("0.0.0.0"), l.Port, ""})
  95. if err != nil {
  96. logs.Error("local listener startup failed port %d, error %s", l.Port, err.Error())
  97. return err
  98. }
  99. LocalServer = append(LocalServer, listener)
  100. logs.Info("successful start-up of local tcp monitoring, port", l.Port)
  101. conn.Accept(listener, func(c net.Conn) {
  102. logs.Trace("new %s connection", l.Type)
  103. if l.Type == "secret" {
  104. handleSecret(c, config, l)
  105. } else if l.Type == "p2p" {
  106. handleP2PVisitor(c, config, l)
  107. }
  108. })
  109. }
  110. return nil
  111. }
  112. func handleSecret(localTcpConn net.Conn, config *config.CommonConfig, l *config.LocalServer) {
  113. remoteConn, err := NewConn(config.Tp, config.VKey, config.Server, common.WORK_SECRET, config.ProxyUrl)
  114. if err != nil {
  115. logs.Error("Local connection server failed ", err.Error())
  116. return
  117. }
  118. if _, err := remoteConn.Write([]byte(crypt.Md5(l.Password))); err != nil {
  119. logs.Error("Local connection server failed ", err.Error())
  120. return
  121. }
  122. conn.CopyWaitGroup(remoteConn.Conn, localTcpConn, false, false, nil, nil, false, nil)
  123. }
  124. func handleP2PVisitor(localTcpConn net.Conn, config *config.CommonConfig, l *config.LocalServer) {
  125. if udpConn == nil {
  126. logs.Notice("new conn, P2P can not penetrate successfully, traffic will be transferred through the server")
  127. handleSecret(localTcpConn, config, l)
  128. }
  129. logs.Trace("start trying to connect with the server")
  130. //TODO just support compress now because there is not tls file in client packages
  131. link := conn.NewLink(common.CONN_TCP, l.Target, false, config.Client.Cnf.Compress, localTcpConn.LocalAddr().String(), false)
  132. if target, err := p2pNetBridge.SendLinkInfo(0, link, nil); err != nil {
  133. logs.Error(err)
  134. return
  135. } else {
  136. conn.CopyWaitGroup(target, localTcpConn, false, config.Client.Cnf.Compress, nil, nil, false, nil)
  137. }
  138. }
  139. func newUdpConn(localAddr string, config *config.CommonConfig, l *config.LocalServer) {
  140. remoteConn, err := NewConn(config.Tp, config.VKey, config.Server, common.WORK_P2P, config.ProxyUrl)
  141. if err != nil {
  142. logs.Error("Local connection server failed ", err.Error())
  143. return
  144. }
  145. if _, err := remoteConn.Write([]byte(crypt.Md5(l.Password))); err != nil {
  146. logs.Error("Local connection server failed ", err.Error())
  147. return
  148. }
  149. var rAddr []byte
  150. //读取服务端地址、密钥 继续做处理
  151. if rAddr, err = remoteConn.GetShortLenContent(); err != nil {
  152. logs.Error(err)
  153. return
  154. }
  155. var localConn net.PacketConn
  156. var remoteAddress string
  157. if remoteAddress, localConn, err = handleP2PUdp(localAddr, string(rAddr), crypt.Md5(l.Password), common.WORK_P2P_VISITOR); err != nil {
  158. logs.Error(err)
  159. return
  160. }
  161. udpTunnel, err := kcp.NewConn(remoteAddress, nil, 150, 3, localConn)
  162. if err != nil || udpTunnel == nil {
  163. logs.Warn(err)
  164. return
  165. }
  166. logs.Trace("successful create a connection with server", remoteAddress)
  167. conn.SetUdpSession(udpTunnel)
  168. udpConn = udpTunnel
  169. muxSession = mux.NewMux(udpConn, "kcp")
  170. p2pNetBridge = &p2pBridge{}
  171. }