conn.go 4.1 KB

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