conn.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. "bytes"
  17. "errors"
  18. "fmt"
  19. "io"
  20. "net"
  21. "sync"
  22. "time"
  23. "github.com/fatedier/frp/utils/log"
  24. kcp "github.com/xtaci/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. func ConnectServer(protocol string, addr string) (c Conn, err error) {
  84. switch protocol {
  85. case "tcp":
  86. return ConnectTcpServer(addr)
  87. case "kcp":
  88. kcpConn, errRet := kcp.DialWithOptions(addr, nil, 10, 3)
  89. if errRet != nil {
  90. err = errRet
  91. return
  92. }
  93. kcpConn.SetStreamMode(true)
  94. kcpConn.SetWriteDelay(true)
  95. kcpConn.SetNoDelay(1, 20, 2, 1)
  96. kcpConn.SetWindowSize(128, 512)
  97. kcpConn.SetMtu(1350)
  98. kcpConn.SetACKNoDelay(false)
  99. kcpConn.SetReadBuffer(4194304)
  100. kcpConn.SetWriteBuffer(4194304)
  101. c = WrapConn(kcpConn)
  102. return
  103. default:
  104. return nil, fmt.Errorf("unsupport protocol: %s", protocol)
  105. }
  106. }
  107. func ConnectServerByHttpProxy(httpProxy string, protocol string, addr string) (c Conn, err error) {
  108. switch protocol {
  109. case "tcp":
  110. return ConnectTcpServerByHttpProxy(httpProxy, addr)
  111. case "kcp":
  112. // http proxy is not supported for kcp
  113. return ConnectServer(protocol, addr)
  114. default:
  115. return nil, fmt.Errorf("unsupport protocol: %s", protocol)
  116. }
  117. }
  118. type SharedConn struct {
  119. Conn
  120. sync.Mutex
  121. buf *bytes.Buffer
  122. }
  123. // the bytes you read in io.Reader, will be reserved in SharedConn
  124. func NewShareConn(conn Conn) (*SharedConn, io.Reader) {
  125. sc := &SharedConn{
  126. Conn: conn,
  127. buf: bytes.NewBuffer(make([]byte, 0, 1024)),
  128. }
  129. return sc, io.TeeReader(conn, sc.buf)
  130. }
  131. func (sc *SharedConn) Read(p []byte) (n int, err error) {
  132. sc.Lock()
  133. if sc.buf == nil {
  134. sc.Unlock()
  135. return sc.Conn.Read(p)
  136. }
  137. sc.Unlock()
  138. n, err = sc.buf.Read(p)
  139. if err == io.EOF {
  140. sc.Lock()
  141. sc.buf = nil
  142. sc.Unlock()
  143. var n2 int
  144. n2, err = sc.Conn.Read(p[n:])
  145. n += n2
  146. }
  147. return
  148. }
  149. func (sc *SharedConn) WriteBuff(buffer []byte) (err error) {
  150. sc.buf.Reset()
  151. _, err = sc.buf.Write(buffer)
  152. return err
  153. }
  154. type StatsConn struct {
  155. Conn
  156. totalRead int64
  157. totalWrite int64
  158. statsFunc func(totalRead, totalWrite int64)
  159. }
  160. func WrapStatsConn(conn Conn, statsFunc func(total, totalWrite int64)) *StatsConn {
  161. return &StatsConn{
  162. Conn: conn,
  163. statsFunc: statsFunc,
  164. }
  165. }
  166. func (statsConn *StatsConn) Read(p []byte) (n int, err error) {
  167. n, err = statsConn.Conn.Read(p)
  168. statsConn.totalRead += int64(n)
  169. return
  170. }
  171. func (statsConn *StatsConn) Write(p []byte) (n int, err error) {
  172. n, err = statsConn.Conn.Write(p)
  173. statsConn.totalWrite += int64(n)
  174. return
  175. }
  176. func (statsConn *StatsConn) Close() (err error) {
  177. err = statsConn.Conn.Close()
  178. if statsConn.statsFunc != nil {
  179. statsConn.statsFunc(statsConn.totalRead, statsConn.totalWrite)
  180. }
  181. return
  182. }