hub.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package tcp
  2. import (
  3. "crypto/tls"
  4. "errors"
  5. "net"
  6. "sync"
  7. "time"
  8. "v2ray.com/core/common/log"
  9. v2net "v2ray.com/core/common/net"
  10. "v2ray.com/core/transport/internet"
  11. v2tls "v2ray.com/core/transport/internet/tls"
  12. )
  13. var (
  14. ErrClosedListener = errors.New("Listener is closed.")
  15. )
  16. type ConnectionWithError struct {
  17. conn net.Conn
  18. err error
  19. }
  20. type TCPListener struct {
  21. sync.Mutex
  22. acccepting bool
  23. listener *net.TCPListener
  24. awaitingConns chan *ConnectionWithError
  25. tlsConfig *tls.Config
  26. config *Config
  27. }
  28. func ListenTCP(address v2net.Address, port v2net.Port, options internet.ListenOptions) (internet.Listener, error) {
  29. listener, err := net.ListenTCP("tcp", &net.TCPAddr{
  30. IP: address.IP(),
  31. Port: int(port),
  32. })
  33. if err != nil {
  34. return nil, err
  35. }
  36. networkSettings, err := options.Stream.GetEffectiveNetworkSettings()
  37. if err != nil {
  38. return nil, err
  39. }
  40. tcpSettings := networkSettings.(*Config)
  41. l := &TCPListener{
  42. acccepting: true,
  43. listener: listener,
  44. awaitingConns: make(chan *ConnectionWithError, 32),
  45. config: tcpSettings,
  46. }
  47. if options.Stream != nil && options.Stream.HasSecuritySettings() {
  48. securitySettings, err := options.Stream.GetEffectiveSecuritySettings()
  49. if err != nil {
  50. log.Error("TCP: Failed to get security config: ", err)
  51. return nil, err
  52. }
  53. tlsConfig, ok := securitySettings.(*v2tls.Config)
  54. if ok {
  55. l.tlsConfig = tlsConfig.GetTLSConfig()
  56. }
  57. }
  58. go l.KeepAccepting()
  59. return l, nil
  60. }
  61. func (this *TCPListener) Accept() (internet.Connection, error) {
  62. for this.acccepting {
  63. select {
  64. case connErr, open := <-this.awaitingConns:
  65. if !open {
  66. return nil, ErrClosedListener
  67. }
  68. if connErr.err != nil {
  69. return nil, connErr.err
  70. }
  71. conn := connErr.conn
  72. if this.tlsConfig != nil {
  73. conn = tls.Server(conn, this.tlsConfig)
  74. }
  75. return NewConnection("", conn, this, this.config), nil
  76. case <-time.After(time.Second * 2):
  77. }
  78. }
  79. return nil, ErrClosedListener
  80. }
  81. func (this *TCPListener) KeepAccepting() {
  82. for this.acccepting {
  83. conn, err := this.listener.Accept()
  84. this.Lock()
  85. if !this.acccepting {
  86. this.Unlock()
  87. break
  88. }
  89. select {
  90. case this.awaitingConns <- &ConnectionWithError{
  91. conn: conn,
  92. err: err,
  93. }:
  94. default:
  95. if conn != nil {
  96. conn.Close()
  97. }
  98. }
  99. this.Unlock()
  100. }
  101. }
  102. func (this *TCPListener) Recycle(dest string, conn net.Conn) {
  103. this.Lock()
  104. defer this.Unlock()
  105. if !this.acccepting {
  106. return
  107. }
  108. select {
  109. case this.awaitingConns <- &ConnectionWithError{conn: conn}:
  110. default:
  111. conn.Close()
  112. }
  113. }
  114. func (this *TCPListener) Addr() net.Addr {
  115. return this.listener.Addr()
  116. }
  117. func (this *TCPListener) Close() error {
  118. this.Lock()
  119. defer this.Unlock()
  120. this.acccepting = false
  121. this.listener.Close()
  122. close(this.awaitingConns)
  123. for connErr := range this.awaitingConns {
  124. if connErr.conn != nil {
  125. go connErr.conn.Close()
  126. }
  127. }
  128. return nil
  129. }
  130. type RawTCPListener struct {
  131. accepting bool
  132. listener *net.TCPListener
  133. }
  134. func (this *RawTCPListener) Accept() (internet.Connection, error) {
  135. conn, err := this.listener.AcceptTCP()
  136. if err != nil {
  137. return nil, err
  138. }
  139. return &RawConnection{
  140. TCPConn: *conn,
  141. }, nil
  142. }
  143. func (this *RawTCPListener) Addr() net.Addr {
  144. return this.listener.Addr()
  145. }
  146. func (this *RawTCPListener) Close() error {
  147. this.accepting = false
  148. this.listener.Close()
  149. return nil
  150. }
  151. func ListenRawTCP(address v2net.Address, port v2net.Port, options internet.ListenOptions) (internet.Listener, error) {
  152. listener, err := net.ListenTCP("tcp", &net.TCPAddr{
  153. IP: address.IP(),
  154. Port: int(port),
  155. })
  156. if err != nil {
  157. return nil, err
  158. }
  159. // TODO: handle listen options
  160. return &RawTCPListener{
  161. accepting: true,
  162. listener: listener,
  163. }, nil
  164. }
  165. func init() {
  166. internet.TCPListenFunc = ListenTCP
  167. internet.RawTCPListenFunc = ListenRawTCP
  168. }