conn.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. "errors"
  17. "fmt"
  18. "io"
  19. "net"
  20. "sync/atomic"
  21. "time"
  22. "github.com/fatedier/frp/utils/log"
  23. gnet "github.com/fatedier/golib/net"
  24. kcp "github.com/fatedier/kcp-go"
  25. )
  26. // Conn is the interface of connections used in frp.
  27. type Conn interface {
  28. net.Conn
  29. log.Logger
  30. }
  31. type WrapLogConn struct {
  32. net.Conn
  33. log.Logger
  34. }
  35. func WrapConn(c net.Conn) Conn {
  36. return &WrapLogConn{
  37. Conn: c,
  38. Logger: log.NewPrefixLogger(""),
  39. }
  40. }
  41. type WrapReadWriteCloserConn struct {
  42. io.ReadWriteCloser
  43. log.Logger
  44. underConn net.Conn
  45. }
  46. func WrapReadWriteCloserToConn(rwc io.ReadWriteCloser, underConn net.Conn) Conn {
  47. return &WrapReadWriteCloserConn{
  48. ReadWriteCloser: rwc,
  49. Logger: log.NewPrefixLogger(""),
  50. underConn: underConn,
  51. }
  52. }
  53. func (conn *WrapReadWriteCloserConn) LocalAddr() net.Addr {
  54. if conn.underConn != nil {
  55. return conn.underConn.LocalAddr()
  56. }
  57. return (*net.TCPAddr)(nil)
  58. }
  59. func (conn *WrapReadWriteCloserConn) RemoteAddr() net.Addr {
  60. if conn.underConn != nil {
  61. return conn.underConn.RemoteAddr()
  62. }
  63. return (*net.TCPAddr)(nil)
  64. }
  65. func (conn *WrapReadWriteCloserConn) SetDeadline(t time.Time) error {
  66. if conn.underConn != nil {
  67. return conn.underConn.SetDeadline(t)
  68. }
  69. return &net.OpError{Op: "set", Net: "wrap", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
  70. }
  71. func (conn *WrapReadWriteCloserConn) SetReadDeadline(t time.Time) error {
  72. if conn.underConn != nil {
  73. return conn.underConn.SetReadDeadline(t)
  74. }
  75. return &net.OpError{Op: "set", Net: "wrap", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
  76. }
  77. func (conn *WrapReadWriteCloserConn) SetWriteDeadline(t time.Time) error {
  78. if conn.underConn != nil {
  79. return conn.underConn.SetWriteDeadline(t)
  80. }
  81. return &net.OpError{Op: "set", Net: "wrap", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
  82. }
  83. type CloseNotifyConn struct {
  84. net.Conn
  85. log.Logger
  86. // 1 means closed
  87. closeFlag int32
  88. closeFn func()
  89. }
  90. // closeFn will be only called once
  91. func WrapCloseNotifyConn(c net.Conn, closeFn func()) Conn {
  92. return &CloseNotifyConn{
  93. Conn: c,
  94. Logger: log.NewPrefixLogger(""),
  95. closeFn: closeFn,
  96. }
  97. }
  98. func (cc *CloseNotifyConn) Close() (err error) {
  99. pflag := atomic.SwapInt32(&cc.closeFlag, 1)
  100. if pflag == 0 {
  101. err = cc.Close()
  102. if cc.closeFn != nil {
  103. cc.closeFn()
  104. }
  105. }
  106. return
  107. }
  108. type StatsConn struct {
  109. Conn
  110. closed int64 // 1 means closed
  111. totalRead int64
  112. totalWrite int64
  113. statsFunc func(totalRead, totalWrite int64)
  114. }
  115. func WrapStatsConn(conn Conn, statsFunc func(total, totalWrite int64)) *StatsConn {
  116. return &StatsConn{
  117. Conn: conn,
  118. statsFunc: statsFunc,
  119. }
  120. }
  121. func (statsConn *StatsConn) Read(p []byte) (n int, err error) {
  122. n, err = statsConn.Conn.Read(p)
  123. statsConn.totalRead += int64(n)
  124. return
  125. }
  126. func (statsConn *StatsConn) Write(p []byte) (n int, err error) {
  127. n, err = statsConn.Conn.Write(p)
  128. statsConn.totalWrite += int64(n)
  129. return
  130. }
  131. func (statsConn *StatsConn) Close() (err error) {
  132. old := atomic.SwapInt64(&statsConn.closed, 1)
  133. if old != 1 {
  134. err = statsConn.Conn.Close()
  135. if statsConn.statsFunc != nil {
  136. statsConn.statsFunc(statsConn.totalRead, statsConn.totalWrite)
  137. }
  138. }
  139. return
  140. }
  141. func ConnectServer(protocol string, addr string) (c Conn, err error) {
  142. switch protocol {
  143. case "tcp":
  144. return ConnectTcpServer(addr)
  145. case "kcp":
  146. kcpConn, errRet := kcp.DialWithOptions(addr, nil, 10, 3)
  147. if errRet != nil {
  148. err = errRet
  149. return
  150. }
  151. kcpConn.SetStreamMode(true)
  152. kcpConn.SetWriteDelay(true)
  153. kcpConn.SetNoDelay(1, 20, 2, 1)
  154. kcpConn.SetWindowSize(128, 512)
  155. kcpConn.SetMtu(1350)
  156. kcpConn.SetACKNoDelay(false)
  157. kcpConn.SetReadBuffer(4194304)
  158. kcpConn.SetWriteBuffer(4194304)
  159. c = WrapConn(kcpConn)
  160. return
  161. default:
  162. return nil, fmt.Errorf("unsupport protocol: %s", protocol)
  163. }
  164. }
  165. func ConnectServerByProxy(proxyUrl string, protocol string, addr string) (c Conn, err error) {
  166. switch protocol {
  167. case "tcp":
  168. var conn net.Conn
  169. if conn, err = gnet.DialTcpByProxy(proxyUrl, addr); err != nil {
  170. return
  171. }
  172. return WrapConn(conn), nil
  173. case "kcp":
  174. // http proxy is not supported for kcp
  175. return ConnectServer(protocol, addr)
  176. case "websocket":
  177. return ConnectWebsocketServer(addr)
  178. default:
  179. return nil, fmt.Errorf("unsupport protocol: %s", protocol)
  180. }
  181. }