conn.go 4.7 KB

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