conn.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // Copyright 2016 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package net
  15. import (
  16. "crypto/tls"
  17. "errors"
  18. "fmt"
  19. "io"
  20. "net"
  21. "sync/atomic"
  22. "time"
  23. "github.com/fatedier/frp/utils/log"
  24. gnet "github.com/fatedier/golib/net"
  25. kcp "github.com/fatedier/kcp-go"
  26. )
  27. // Conn is the interface of connections used in frp.
  28. type Conn interface {
  29. net.Conn
  30. log.Logger
  31. }
  32. type WrapLogConn struct {
  33. net.Conn
  34. log.Logger
  35. }
  36. func WrapConn(c net.Conn) Conn {
  37. return &WrapLogConn{
  38. Conn: c,
  39. Logger: log.NewPrefixLogger(""),
  40. }
  41. }
  42. type WrapReadWriteCloserConn struct {
  43. io.ReadWriteCloser
  44. log.Logger
  45. underConn net.Conn
  46. }
  47. func WrapReadWriteCloserToConn(rwc io.ReadWriteCloser, underConn net.Conn) Conn {
  48. return &WrapReadWriteCloserConn{
  49. ReadWriteCloser: rwc,
  50. Logger: log.NewPrefixLogger(""),
  51. underConn: underConn,
  52. }
  53. }
  54. func (conn *WrapReadWriteCloserConn) LocalAddr() net.Addr {
  55. if conn.underConn != nil {
  56. return conn.underConn.LocalAddr()
  57. }
  58. return (*net.TCPAddr)(nil)
  59. }
  60. func (conn *WrapReadWriteCloserConn) RemoteAddr() net.Addr {
  61. if conn.underConn != nil {
  62. return conn.underConn.RemoteAddr()
  63. }
  64. return (*net.TCPAddr)(nil)
  65. }
  66. func (conn *WrapReadWriteCloserConn) SetDeadline(t time.Time) error {
  67. if conn.underConn != nil {
  68. return conn.underConn.SetDeadline(t)
  69. }
  70. return &net.OpError{Op: "set", Net: "wrap", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
  71. }
  72. func (conn *WrapReadWriteCloserConn) SetReadDeadline(t time.Time) error {
  73. if conn.underConn != nil {
  74. return conn.underConn.SetReadDeadline(t)
  75. }
  76. return &net.OpError{Op: "set", Net: "wrap", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
  77. }
  78. func (conn *WrapReadWriteCloserConn) SetWriteDeadline(t time.Time) error {
  79. if conn.underConn != nil {
  80. return conn.underConn.SetWriteDeadline(t)
  81. }
  82. return &net.OpError{Op: "set", Net: "wrap", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
  83. }
  84. type CloseNotifyConn struct {
  85. net.Conn
  86. log.Logger
  87. // 1 means closed
  88. closeFlag int32
  89. closeFn func()
  90. }
  91. // closeFn will be only called once
  92. func WrapCloseNotifyConn(c net.Conn, closeFn func()) Conn {
  93. return &CloseNotifyConn{
  94. Conn: c,
  95. Logger: log.NewPrefixLogger(""),
  96. closeFn: closeFn,
  97. }
  98. }
  99. func (cc *CloseNotifyConn) Close() (err error) {
  100. pflag := atomic.SwapInt32(&cc.closeFlag, 1)
  101. if pflag == 0 {
  102. err = cc.Close()
  103. if cc.closeFn != nil {
  104. cc.closeFn()
  105. }
  106. }
  107. return
  108. }
  109. type StatsConn struct {
  110. Conn
  111. closed int64 // 1 means closed
  112. totalRead int64
  113. totalWrite int64
  114. statsFunc func(totalRead, totalWrite int64)
  115. }
  116. func WrapStatsConn(conn Conn, statsFunc func(total, totalWrite int64)) *StatsConn {
  117. return &StatsConn{
  118. Conn: conn,
  119. statsFunc: statsFunc,
  120. }
  121. }
  122. func (statsConn *StatsConn) Read(p []byte) (n int, err error) {
  123. n, err = statsConn.Conn.Read(p)
  124. statsConn.totalRead += int64(n)
  125. return
  126. }
  127. func (statsConn *StatsConn) Write(p []byte) (n int, err error) {
  128. n, err = statsConn.Conn.Write(p)
  129. statsConn.totalWrite += int64(n)
  130. return
  131. }
  132. func (statsConn *StatsConn) Close() (err error) {
  133. old := atomic.SwapInt64(&statsConn.closed, 1)
  134. if old != 1 {
  135. err = statsConn.Conn.Close()
  136. if statsConn.statsFunc != nil {
  137. statsConn.statsFunc(statsConn.totalRead, statsConn.totalWrite)
  138. }
  139. }
  140. return
  141. }
  142. func ConnectServer(protocol string, addr string) (c Conn, err error) {
  143. switch protocol {
  144. case "tcp":
  145. return ConnectTcpServer(addr)
  146. case "kcp":
  147. kcpConn, errRet := kcp.DialWithOptions(addr, nil, 10, 3)
  148. if errRet != nil {
  149. err = errRet
  150. return
  151. }
  152. kcpConn.SetStreamMode(true)
  153. kcpConn.SetWriteDelay(true)
  154. kcpConn.SetNoDelay(1, 20, 2, 1)
  155. kcpConn.SetWindowSize(128, 512)
  156. kcpConn.SetMtu(1350)
  157. kcpConn.SetACKNoDelay(false)
  158. kcpConn.SetReadBuffer(4194304)
  159. kcpConn.SetWriteBuffer(4194304)
  160. c = WrapConn(kcpConn)
  161. return
  162. default:
  163. return nil, fmt.Errorf("unsupport protocol: %s", protocol)
  164. }
  165. }
  166. func ConnectServerByProxy(proxyUrl string, protocol string, addr string) (c Conn, err error) {
  167. switch protocol {
  168. case "tcp":
  169. var conn net.Conn
  170. if conn, err = gnet.DialTcpByProxy(proxyUrl, addr); err != nil {
  171. return
  172. }
  173. return WrapConn(conn), nil
  174. case "kcp":
  175. // http proxy is not supported for kcp
  176. return ConnectServer(protocol, addr)
  177. case "websocket":
  178. return ConnectWebsocketServer(addr)
  179. default:
  180. return nil, fmt.Errorf("unsupport protocol: %s", protocol)
  181. }
  182. }
  183. func ConnectServerByProxyWithTLS(proxyUrl string, protocol string, addr string, tlsConfig *tls.Config) (c Conn, err error) {
  184. c, err = ConnectServerByProxy(proxyUrl, protocol, addr)
  185. if err != nil {
  186. return
  187. }
  188. if tlsConfig == nil {
  189. return
  190. }
  191. c = WrapTLSClientConn(c, tlsConfig)
  192. return
  193. }