1
0

conn.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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(error)
  113. }
  114. // closeFn will be only called once with the error (nil if Close() was called, non-nil if CloseWithError() was called)
  115. func WrapCloseNotifyConn(c net.Conn, closeFn func(error)) *CloseNotifyConn {
  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.Conn.Close()
  125. if cc.closeFn != nil {
  126. cc.closeFn(nil)
  127. }
  128. }
  129. return
  130. }
  131. // CloseWithError closes the connection and passes the error to the close callback.
  132. func (cc *CloseNotifyConn) CloseWithError(err error) error {
  133. pflag := atomic.SwapInt32(&cc.closeFlag, 1)
  134. if pflag == 0 {
  135. closeErr := cc.Conn.Close()
  136. if cc.closeFn != nil {
  137. cc.closeFn(err)
  138. }
  139. return closeErr
  140. }
  141. return nil
  142. }
  143. type StatsConn struct {
  144. net.Conn
  145. closed int64 // 1 means closed
  146. totalRead int64
  147. totalWrite int64
  148. statsFunc func(totalRead, totalWrite int64)
  149. }
  150. func WrapStatsConn(conn net.Conn, statsFunc func(total, totalWrite int64)) *StatsConn {
  151. return &StatsConn{
  152. Conn: conn,
  153. statsFunc: statsFunc,
  154. }
  155. }
  156. func (statsConn *StatsConn) Read(p []byte) (n int, err error) {
  157. n, err = statsConn.Conn.Read(p)
  158. statsConn.totalRead += int64(n)
  159. return
  160. }
  161. func (statsConn *StatsConn) Write(p []byte) (n int, err error) {
  162. n, err = statsConn.Conn.Write(p)
  163. statsConn.totalWrite += int64(n)
  164. return
  165. }
  166. func (statsConn *StatsConn) Close() (err error) {
  167. old := atomic.SwapInt64(&statsConn.closed, 1)
  168. if old != 1 {
  169. err = statsConn.Conn.Close()
  170. if statsConn.statsFunc != nil {
  171. statsConn.statsFunc(statsConn.totalRead, statsConn.totalWrite)
  172. }
  173. }
  174. return
  175. }
  176. type wrapQuicStream struct {
  177. *quic.Stream
  178. c *quic.Conn
  179. }
  180. func QuicStreamToNetConn(s *quic.Stream, c *quic.Conn) net.Conn {
  181. return &wrapQuicStream{
  182. Stream: s,
  183. c: c,
  184. }
  185. }
  186. func (conn *wrapQuicStream) LocalAddr() net.Addr {
  187. if conn.c != nil {
  188. return conn.c.LocalAddr()
  189. }
  190. return (*net.TCPAddr)(nil)
  191. }
  192. func (conn *wrapQuicStream) RemoteAddr() net.Addr {
  193. if conn.c != nil {
  194. return conn.c.RemoteAddr()
  195. }
  196. return (*net.TCPAddr)(nil)
  197. }
  198. func (conn *wrapQuicStream) Close() error {
  199. conn.CancelRead(0)
  200. return conn.Stream.Close()
  201. }
  202. func NewCryptoReadWriter(rw io.ReadWriter, key []byte) (io.ReadWriter, error) {
  203. encReader := crypto.NewReader(rw, key)
  204. encWriter, err := crypto.NewWriter(rw, key)
  205. if err != nil {
  206. return nil, err
  207. }
  208. return struct {
  209. io.Reader
  210. io.Writer
  211. }{
  212. Reader: encReader,
  213. Writer: encWriter,
  214. }, nil
  215. }