conn.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. }
  65. func WrapReadWriteCloserToConn(rwc io.ReadWriteCloser, underConn net.Conn) net.Conn {
  66. return &WrapReadWriteCloserConn{
  67. ReadWriteCloser: rwc,
  68. underConn: underConn,
  69. }
  70. }
  71. func (conn *WrapReadWriteCloserConn) LocalAddr() net.Addr {
  72. if conn.underConn != nil {
  73. return conn.underConn.LocalAddr()
  74. }
  75. return (*net.TCPAddr)(nil)
  76. }
  77. func (conn *WrapReadWriteCloserConn) RemoteAddr() net.Addr {
  78. if conn.underConn != nil {
  79. return conn.underConn.RemoteAddr()
  80. }
  81. return (*net.TCPAddr)(nil)
  82. }
  83. func (conn *WrapReadWriteCloserConn) SetDeadline(t time.Time) error {
  84. if conn.underConn != nil {
  85. return conn.underConn.SetDeadline(t)
  86. }
  87. return &net.OpError{Op: "set", Net: "wrap", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
  88. }
  89. func (conn *WrapReadWriteCloserConn) SetReadDeadline(t time.Time) error {
  90. if conn.underConn != nil {
  91. return conn.underConn.SetReadDeadline(t)
  92. }
  93. return &net.OpError{Op: "set", Net: "wrap", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
  94. }
  95. func (conn *WrapReadWriteCloserConn) SetWriteDeadline(t time.Time) error {
  96. if conn.underConn != nil {
  97. return conn.underConn.SetWriteDeadline(t)
  98. }
  99. return &net.OpError{Op: "set", Net: "wrap", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
  100. }
  101. type CloseNotifyConn struct {
  102. net.Conn
  103. // 1 means closed
  104. closeFlag int32
  105. closeFn func()
  106. }
  107. // closeFn will be only called once
  108. func WrapCloseNotifyConn(c net.Conn, closeFn func()) net.Conn {
  109. return &CloseNotifyConn{
  110. Conn: c,
  111. closeFn: closeFn,
  112. }
  113. }
  114. func (cc *CloseNotifyConn) Close() (err error) {
  115. pflag := atomic.SwapInt32(&cc.closeFlag, 1)
  116. if pflag == 0 {
  117. err = cc.Close()
  118. if cc.closeFn != nil {
  119. cc.closeFn()
  120. }
  121. }
  122. return
  123. }
  124. type StatsConn struct {
  125. net.Conn
  126. closed int64 // 1 means closed
  127. totalRead int64
  128. totalWrite int64
  129. statsFunc func(totalRead, totalWrite int64)
  130. }
  131. func WrapStatsConn(conn net.Conn, statsFunc func(total, totalWrite int64)) *StatsConn {
  132. return &StatsConn{
  133. Conn: conn,
  134. statsFunc: statsFunc,
  135. }
  136. }
  137. func (statsConn *StatsConn) Read(p []byte) (n int, err error) {
  138. n, err = statsConn.Conn.Read(p)
  139. statsConn.totalRead += int64(n)
  140. return
  141. }
  142. func (statsConn *StatsConn) Write(p []byte) (n int, err error) {
  143. n, err = statsConn.Conn.Write(p)
  144. statsConn.totalWrite += int64(n)
  145. return
  146. }
  147. func (statsConn *StatsConn) Close() (err error) {
  148. old := atomic.SwapInt64(&statsConn.closed, 1)
  149. if old != 1 {
  150. err = statsConn.Conn.Close()
  151. if statsConn.statsFunc != nil {
  152. statsConn.statsFunc(statsConn.totalRead, statsConn.totalWrite)
  153. }
  154. }
  155. return
  156. }
  157. type wrapQuicStream struct {
  158. quic.Stream
  159. c quic.Connection
  160. }
  161. func QuicStreamToNetConn(s quic.Stream, c quic.Connection) net.Conn {
  162. return &wrapQuicStream{
  163. Stream: s,
  164. c: c,
  165. }
  166. }
  167. func (conn *wrapQuicStream) LocalAddr() net.Addr {
  168. if conn.c != nil {
  169. return conn.c.LocalAddr()
  170. }
  171. return (*net.TCPAddr)(nil)
  172. }
  173. func (conn *wrapQuicStream) RemoteAddr() net.Addr {
  174. if conn.c != nil {
  175. return conn.c.RemoteAddr()
  176. }
  177. return (*net.TCPAddr)(nil)
  178. }
  179. func (conn *wrapQuicStream) Close() error {
  180. conn.Stream.CancelRead(0)
  181. return conn.Stream.Close()
  182. }
  183. func NewCryptoReadWriter(rw io.ReadWriter, key []byte) (io.ReadWriter, error) {
  184. encReader := crypto.NewReader(rw, key)
  185. encWriter, err := crypto.NewWriter(rw, key)
  186. if err != nil {
  187. return nil, err
  188. }
  189. return struct {
  190. io.Reader
  191. io.Writer
  192. }{
  193. Reader: encReader,
  194. Writer: encWriter,
  195. }, nil
  196. }