connection.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package internal
  2. import (
  3. "io"
  4. "net"
  5. "sync"
  6. "time"
  7. v2net "v2ray.com/core/common/net"
  8. )
  9. // ConnectionID is the ID of a connection.
  10. type ConnectionID struct {
  11. Local v2net.Address
  12. Remote v2net.Address
  13. RemotePort v2net.Port
  14. }
  15. // NewConnectionID creates a new ConnectionId.
  16. func NewConnectionID(source v2net.Address, dest v2net.Destination) ConnectionID {
  17. return ConnectionID{
  18. Local: source,
  19. Remote: dest.Address,
  20. RemotePort: dest.Port,
  21. }
  22. }
  23. // Reuser determines whether a connection can be reused or not.
  24. type Reuser struct {
  25. // userEnabled indicates connection-reuse enabled by user.
  26. userEnabled bool
  27. // appEnabled indicates connection-reuse enabled by app.
  28. appEnabled bool
  29. }
  30. // ReuseConnection returns a tracker for tracking connection reusability.
  31. func ReuseConnection(reuse bool) *Reuser {
  32. return &Reuser{
  33. userEnabled: reuse,
  34. appEnabled: reuse,
  35. }
  36. }
  37. // Connection is an implementation of net.Conn with re-usability.
  38. type Connection struct {
  39. sync.RWMutex
  40. id ConnectionID
  41. conn net.Conn
  42. listener ConnectionRecyler
  43. reuser *Reuser
  44. }
  45. // NewConnection creates a new connection.
  46. func NewConnection(id ConnectionID, conn net.Conn, manager ConnectionRecyler, reuser *Reuser) *Connection {
  47. return &Connection{
  48. id: id,
  49. conn: conn,
  50. listener: manager,
  51. reuser: reuser,
  52. }
  53. }
  54. // Read implements net.Conn.Read().
  55. func (v *Connection) Read(b []byte) (int, error) {
  56. conn := v.underlyingConn()
  57. if conn == nil {
  58. return 0, io.EOF
  59. }
  60. return conn.Read(b)
  61. }
  62. // Write implement net.Conn.Write().
  63. func (v *Connection) Write(b []byte) (int, error) {
  64. conn := v.underlyingConn()
  65. if conn == nil {
  66. return 0, io.ErrClosedPipe
  67. }
  68. return conn.Write(b)
  69. }
  70. // Close implements net.Conn.Close(). If the connection is reusable, the underlying connection will be recycled.
  71. func (v *Connection) Close() error {
  72. if v == nil {
  73. return io.ErrClosedPipe
  74. }
  75. v.Lock()
  76. defer v.Unlock()
  77. if v.conn == nil {
  78. return io.ErrClosedPipe
  79. }
  80. if v.Reusable() {
  81. v.listener.Put(v.id, v.conn)
  82. return nil
  83. }
  84. err := v.conn.Close()
  85. v.conn = nil
  86. return err
  87. }
  88. // LocalAddr implements net.Conn.LocalAddr().
  89. func (v *Connection) LocalAddr() net.Addr {
  90. conn := v.underlyingConn()
  91. if conn == nil {
  92. return nil
  93. }
  94. return conn.LocalAddr()
  95. }
  96. // RemoteAddr implements net.Conn.RemoteAddr().
  97. func (v *Connection) RemoteAddr() net.Addr {
  98. conn := v.underlyingConn()
  99. if conn == nil {
  100. return nil
  101. }
  102. return conn.RemoteAddr()
  103. }
  104. // SetDeadline implements net.Conn.SetDeadline().
  105. func (v *Connection) SetDeadline(t time.Time) error {
  106. conn := v.underlyingConn()
  107. if conn == nil {
  108. return nil
  109. }
  110. return conn.SetDeadline(t)
  111. }
  112. // SetReadDeadline implements net.Conn.SetReadDeadline().
  113. func (v *Connection) SetReadDeadline(t time.Time) error {
  114. conn := v.underlyingConn()
  115. if conn == nil {
  116. return nil
  117. }
  118. return conn.SetReadDeadline(t)
  119. }
  120. // SetWriteDeadline implements net.Conn.SetWriteDeadline().
  121. func (v *Connection) SetWriteDeadline(t time.Time) error {
  122. conn := v.underlyingConn()
  123. if conn == nil {
  124. return nil
  125. }
  126. return conn.SetWriteDeadline(t)
  127. }
  128. // SetReusable implements internet.Reusable.SetReusable().
  129. func (v *Connection) SetReusable(reusable bool) {
  130. if v == nil {
  131. return
  132. }
  133. v.reuser.appEnabled = reusable
  134. }
  135. // Reusable implements internet.Reusable.Reusable().
  136. func (v *Connection) Reusable() bool {
  137. if v == nil {
  138. return false
  139. }
  140. return v.reuser.userEnabled && v.reuser.appEnabled
  141. }
  142. // SysFd implement internet.SysFd.SysFd().
  143. func (v *Connection) SysFd() (int, error) {
  144. conn := v.underlyingConn()
  145. if conn == nil {
  146. return 0, io.ErrClosedPipe
  147. }
  148. return GetSysFd(conn)
  149. }
  150. func (v *Connection) underlyingConn() net.Conn {
  151. if v == nil {
  152. return nil
  153. }
  154. v.RLock()
  155. defer v.RUnlock()
  156. return v.conn
  157. }