tunnel_client.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package services
  2. import (
  3. "bytes"
  4. "crypto/tls"
  5. "encoding/binary"
  6. "fmt"
  7. "io"
  8. "log"
  9. "net"
  10. "github.com/snail007/goproxy/utils"
  11. "time"
  12. )
  13. type TunnelClient struct {
  14. cfg TunnelClientArgs
  15. }
  16. func NewTunnelClient() Service {
  17. return &TunnelClient{
  18. cfg: TunnelClientArgs{},
  19. }
  20. }
  21. func (s *TunnelClient) InitService() {
  22. }
  23. func (s *TunnelClient) Check() {
  24. if *s.cfg.Parent != "" {
  25. log.Printf("use tls parent %s", *s.cfg.Parent)
  26. } else {
  27. log.Fatalf("parent required")
  28. }
  29. if s.cfg.CertBytes == nil || s.cfg.KeyBytes == nil {
  30. log.Fatalf("cert and key file required")
  31. }
  32. }
  33. func (s *TunnelClient) StopService() {
  34. }
  35. func (s *TunnelClient) Start(args interface{}) (err error) {
  36. s.cfg = args.(TunnelClientArgs)
  37. s.Check()
  38. s.InitService()
  39. for {
  40. ctrlConn, err := s.GetInConn(CONN_CONTROL)
  41. if err != nil {
  42. log.Printf("control connection err: %s", err)
  43. time.Sleep(time.Second * 3)
  44. utils.CloseConn(&ctrlConn)
  45. continue
  46. }
  47. if *s.cfg.IsUDP {
  48. log.Printf("proxy on udp tunnel client mode")
  49. } else {
  50. log.Printf("proxy on tcp tunnel client mode")
  51. }
  52. for {
  53. signal := make([]byte, 1)
  54. if signal[0] == 1 {
  55. continue
  56. }
  57. _, err = ctrlConn.Read(signal)
  58. if err != nil {
  59. utils.CloseConn(&ctrlConn)
  60. log.Printf("read connection signal err: %s", err)
  61. break
  62. }
  63. log.Printf("signal revecived:%s", signal)
  64. if *s.cfg.IsUDP {
  65. go s.ServeUDP()
  66. } else {
  67. go s.ServeConn()
  68. }
  69. }
  70. }
  71. }
  72. func (s *TunnelClient) Clean() {
  73. s.StopService()
  74. }
  75. func (s *TunnelClient) GetInConn(typ uint8) (outConn net.Conn, err error) {
  76. outConn, err = s.GetConn()
  77. if err != nil {
  78. err = fmt.Errorf("connection err: %s", err)
  79. return
  80. }
  81. keyBytes := []byte(*s.cfg.Key)
  82. keyLength := uint16(len(keyBytes))
  83. pkg := new(bytes.Buffer)
  84. binary.Write(pkg, binary.LittleEndian, typ)
  85. binary.Write(pkg, binary.LittleEndian, keyLength)
  86. binary.Write(pkg, binary.LittleEndian, keyBytes)
  87. _, err = outConn.Write(pkg.Bytes())
  88. if err != nil {
  89. err = fmt.Errorf("write connection data err: %s ,retrying...", err)
  90. utils.CloseConn(&outConn)
  91. return
  92. }
  93. return
  94. }
  95. func (s *TunnelClient) GetConn() (conn net.Conn, err error) {
  96. var _conn tls.Conn
  97. _conn, err = utils.TlsConnectHost(*s.cfg.Parent, *s.cfg.Timeout, s.cfg.CertBytes, s.cfg.KeyBytes)
  98. if err == nil {
  99. conn = net.Conn(&_conn)
  100. }
  101. return
  102. }
  103. func (s *TunnelClient) ServeUDP() {
  104. var inConn net.Conn
  105. var err error
  106. for {
  107. for {
  108. inConn, err = s.GetInConn(CONN_CLIENT)
  109. if err != nil {
  110. utils.CloseConn(&inConn)
  111. log.Printf("connection err: %s, retrying...", err)
  112. time.Sleep(time.Second * 3)
  113. continue
  114. } else {
  115. break
  116. }
  117. }
  118. log.Printf("conn created , remote : %s ", inConn.RemoteAddr())
  119. for {
  120. srcAddr, body, err := utils.ReadUDPPacket(&inConn)
  121. if err == io.EOF || err == io.ErrUnexpectedEOF {
  122. log.Printf("connection %s released", srcAddr)
  123. utils.CloseConn(&inConn)
  124. break
  125. }
  126. //log.Printf("udp packet revecived:%s,%v", srcAddr, body)
  127. go s.processUDPPacket(&inConn, srcAddr, body)
  128. }
  129. }
  130. }
  131. func (s *TunnelClient) processUDPPacket(inConn *net.Conn, srcAddr string, body []byte) {
  132. dstAddr, err := net.ResolveUDPAddr("udp", *s.cfg.Local)
  133. if err != nil {
  134. log.Printf("can't resolve address: %s", err)
  135. utils.CloseConn(inConn)
  136. return
  137. }
  138. clientSrcAddr := &net.UDPAddr{IP: net.IPv4zero, Port: 0}
  139. conn, err := net.DialUDP("udp", clientSrcAddr, dstAddr)
  140. if err != nil {
  141. log.Printf("connect to udp %s fail,ERR:%s", dstAddr.String(), err)
  142. return
  143. }
  144. conn.SetDeadline(time.Now().Add(time.Millisecond * time.Duration(*s.cfg.Timeout)))
  145. _, err = conn.Write(body)
  146. if err != nil {
  147. log.Printf("send udp packet to %s fail,ERR:%s", dstAddr.String(), err)
  148. return
  149. }
  150. //log.Printf("send udp packet to %s success", dstAddr.String())
  151. buf := make([]byte, 512)
  152. len, _, err := conn.ReadFromUDP(buf)
  153. if err != nil {
  154. log.Printf("read udp response from %s fail ,ERR:%s", dstAddr.String(), err)
  155. return
  156. }
  157. respBody := buf[0:len]
  158. //log.Printf("revecived udp packet from %s , %v", dstAddr.String(), respBody)
  159. _, err = (*inConn).Write(utils.UDPPacket(srcAddr, respBody))
  160. if err != nil {
  161. log.Printf("send udp response fail ,ERR:%s", err)
  162. utils.CloseConn(inConn)
  163. return
  164. }
  165. //log.Printf("send udp response success ,from:%s", dstAddr.String())
  166. }
  167. func (s *TunnelClient) ServeConn() {
  168. var inConn, outConn net.Conn
  169. var err error
  170. for {
  171. inConn, err = s.GetInConn(CONN_CLIENT)
  172. if err != nil {
  173. utils.CloseConn(&inConn)
  174. log.Printf("connection err: %s, retrying...", err)
  175. time.Sleep(time.Second * 3)
  176. continue
  177. } else {
  178. break
  179. }
  180. }
  181. i := 0
  182. for {
  183. i++
  184. outConn, err = utils.ConnectHost(*s.cfg.Local, *s.cfg.Timeout)
  185. if err == nil || i == 3 {
  186. break
  187. } else {
  188. if i == 3 {
  189. log.Printf("connect to %s err: %s, retrying...", *s.cfg.Local, err)
  190. time.Sleep(2 * time.Second)
  191. continue
  192. }
  193. }
  194. }
  195. if err != nil {
  196. utils.CloseConn(&inConn)
  197. utils.CloseConn(&outConn)
  198. log.Printf("build connection error, err: %s", err)
  199. return
  200. }
  201. utils.IoBind(inConn, outConn, func(isSrcErr bool, err error) {
  202. log.Printf("%s conn %s - %s - %s - %s released", *s.cfg.Key, inConn.RemoteAddr(), inConn.LocalAddr(), outConn.LocalAddr(), outConn.RemoteAddr())
  203. utils.CloseConn(&inConn)
  204. utils.CloseConn(&outConn)
  205. }, func(i int, b bool) {}, 0)
  206. log.Printf("%s conn %s - %s - %s - %s created", *s.cfg.Key, inConn.RemoteAddr(), inConn.LocalAddr(), outConn.LocalAddr(), outConn.RemoteAddr())
  207. }