conn.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. "context"
  17. "errors"
  18. "io"
  19. "net"
  20. "sync/atomic"
  21. "time"
  22. "github.com/fatedier/golib/crypto"
  23. quic "github.com/quic-go/quic-go"
  24. "github.com/fatedier/frp/pkg/util/xlog"
  25. )
  26. type ContextGetter interface {
  27. Context() context.Context
  28. }
  29. type ContextSetter interface {
  30. WithContext(ctx context.Context)
  31. }
  32. func NewLogFromConn(conn net.Conn) *xlog.Logger {
  33. if c, ok := conn.(ContextGetter); ok {
  34. return xlog.FromContextSafe(c.Context())
  35. }
  36. return xlog.New()
  37. }
  38. func NewContextFromConn(conn net.Conn) context.Context {
  39. if c, ok := conn.(ContextGetter); ok {
  40. return c.Context()
  41. }
  42. return context.Background()
  43. }
  44. // ContextConn is the connection with context
  45. type ContextConn struct {
  46. net.Conn
  47. ctx context.Context
  48. }
  49. func NewContextConn(ctx context.Context, c net.Conn) *ContextConn {
  50. return &ContextConn{
  51. Conn: c,
  52. ctx: ctx,
  53. }
  54. }
  55. func (c *ContextConn) WithContext(ctx context.Context) {
  56. c.ctx = ctx
  57. }
  58. func (c *ContextConn) Context() context.Context {
  59. return c.ctx
  60. }
  61. type WrapReadWriteCloserConn struct {
  62. io.ReadWriteCloser
  63. underConn net.Conn
  64. remoteAddr net.Addr
  65. }
  66. func WrapReadWriteCloserToConn(rwc io.ReadWriteCloser, underConn net.Conn) *WrapReadWriteCloserConn {
  67. return &WrapReadWriteCloserConn{
  68. ReadWriteCloser: rwc,
  69. underConn: underConn,
  70. }
  71. }
  72. func (conn *WrapReadWriteCloserConn) LocalAddr() net.Addr {
  73. if conn.underConn != nil {
  74. return conn.underConn.LocalAddr()
  75. }
  76. return (*net.TCPAddr)(nil)
  77. }
  78. func (conn *WrapReadWriteCloserConn) SetRemoteAddr(addr net.Addr) {
  79. conn.remoteAddr = addr
  80. }
  81. func (conn *WrapReadWriteCloserConn) RemoteAddr() net.Addr {
  82. if conn.remoteAddr != nil {
  83. return conn.remoteAddr
  84. }
  85. if conn.underConn != nil {
  86. return conn.underConn.RemoteAddr()
  87. }
  88. return (*net.TCPAddr)(nil)
  89. }
  90. func (conn *WrapReadWriteCloserConn) SetDeadline(t time.Time) error {
  91. if conn.underConn != nil {
  92. return conn.underConn.SetDeadline(t)
  93. }
  94. return &net.OpError{Op: "set", Net: "wrap", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
  95. }
  96. func (conn *WrapReadWriteCloserConn) SetReadDeadline(t time.Time) error {
  97. if conn.underConn != nil {
  98. return conn.underConn.SetReadDeadline(t)
  99. }
  100. return &net.OpError{Op: "set", Net: "wrap", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
  101. }
  102. func (conn *WrapReadWriteCloserConn) SetWriteDeadline(t time.Time) error {
  103. if conn.underConn != nil {
  104. return conn.underConn.SetWriteDeadline(t)
  105. }
  106. return &net.OpError{Op: "set", Net: "wrap", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
  107. }
  108. type CloseNotifyConn struct {
  109. net.Conn
  110. // 1 means closed
  111. closeFlag int32
  112. closeFn func()
  113. }
  114. // closeFn will be only called once
  115. func WrapCloseNotifyConn(c net.Conn, closeFn func()) net.Conn {
  116. return &CloseNotifyConn{
  117. Conn: c,
  118. closeFn: closeFn,
  119. }
  120. }
  121. func (cc *CloseNotifyConn) Close() (err error) {
  122. pflag := atomic.SwapInt32(&cc.closeFlag, 1)
  123. if pflag == 0 {
  124. err = cc.Close()
  125. if cc.closeFn != nil {
  126. cc.closeFn()
  127. }
  128. }
  129. return
  130. }
  131. type StatsConn struct {
  132. net.Conn
  133. closed int64 // 1 means closed
  134. totalRead int64
  135. totalWrite int64
  136. statsFunc func(totalRead, totalWrite int64)
  137. }
  138. func WrapStatsConn(conn net.Conn, statsFunc func(total, totalWrite int64)) *StatsConn {
  139. return &StatsConn{
  140. Conn: conn,
  141. statsFunc: statsFunc,
  142. }
  143. }
  144. func (statsConn *StatsConn) Read(p []byte) (n int, err error) {
  145. n, err = statsConn.Conn.Read(p)
  146. statsConn.totalRead += int64(n)
  147. return
  148. }
  149. func (statsConn *StatsConn) Write(p []byte) (n int, err error) {
  150. n, err = statsConn.Conn.Write(p)
  151. statsConn.totalWrite += int64(n)
  152. return
  153. }
  154. func (statsConn *StatsConn) Close() (err error) {
  155. old := atomic.SwapInt64(&statsConn.closed, 1)
  156. if old != 1 {
  157. err = statsConn.Conn.Close()
  158. if statsConn.statsFunc != nil {
  159. statsConn.statsFunc(statsConn.totalRead, statsConn.totalWrite)
  160. }
  161. }
  162. return
  163. }
  164. type wrapQuicStream struct {
  165. quic.Stream
  166. c quic.Connection
  167. }
  168. func QuicStreamToNetConn(s quic.Stream, c quic.Connection) net.Conn {
  169. return &wrapQuicStream{
  170. Stream: s,
  171. c: c,
  172. }
  173. }
  174. func (conn *wrapQuicStream) LocalAddr() net.Addr {
  175. if conn.c != nil {
  176. return conn.c.LocalAddr()
  177. }
  178. return (*net.TCPAddr)(nil)
  179. }
  180. func (conn *wrapQuicStream) RemoteAddr() net.Addr {
  181. if conn.c != nil {
  182. return conn.c.RemoteAddr()
  183. }
  184. return (*net.TCPAddr)(nil)
  185. }
  186. func (conn *wrapQuicStream) Close() error {
  187. conn.Stream.CancelRead(0)
  188. return conn.Stream.Close()
  189. }
  190. func NewCryptoReadWriter(rw io.ReadWriter, key []byte) (io.ReadWriter, error) {
  191. encReader := crypto.NewReader(rw, key)
  192. encWriter, err := crypto.NewWriter(rw, key)
  193. if err != nil {
  194. return nil, err
  195. }
  196. return struct {
  197. io.Reader
  198. io.Writer
  199. }{
  200. Reader: encReader,
  201. Writer: encWriter,
  202. }, nil
  203. }