conn.go 4.1 KB

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