conn.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. 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 ConnectServerByProxy(proxyUrl string, protocol string, addr string) (c Conn, err error) {
  108. switch protocol {
  109. case "tcp":
  110. var conn net.Conn
  111. if conn, err = gnet.DialTcpByProxy(proxyUrl, addr); err != nil {
  112. return
  113. }
  114. return WrapConn(conn), nil
  115. case "kcp":
  116. // http proxy is not supported for kcp
  117. return ConnectServer(protocol, addr)
  118. default:
  119. return nil, fmt.Errorf("unsupport protocol: %s", protocol)
  120. }
  121. }
  122. type StatsConn struct {
  123. Conn
  124. closed int64 // 1 means closed
  125. totalRead int64
  126. totalWrite int64
  127. statsFunc func(totalRead, totalWrite int64)
  128. }
  129. func WrapStatsConn(conn Conn, statsFunc func(total, totalWrite int64)) *StatsConn {
  130. return &StatsConn{
  131. Conn: conn,
  132. statsFunc: statsFunc,
  133. }
  134. }
  135. func (statsConn *StatsConn) Read(p []byte) (n int, err error) {
  136. n, err = statsConn.Conn.Read(p)
  137. statsConn.totalRead += int64(n)
  138. return
  139. }
  140. func (statsConn *StatsConn) Write(p []byte) (n int, err error) {
  141. n, err = statsConn.Conn.Write(p)
  142. statsConn.totalWrite += int64(n)
  143. return
  144. }
  145. func (statsConn *StatsConn) Close() (err error) {
  146. old := atomic.SwapInt64(&statsConn.closed, 1)
  147. if old != 1 {
  148. err = statsConn.Conn.Close()
  149. if statsConn.statsFunc != nil {
  150. statsConn.statsFunc(statsConn.totalRead, statsConn.totalWrite)
  151. }
  152. }
  153. return
  154. }