github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/transport/internet/tcp/hub.go (about) 1 // +build !confonly 2 3 package tcp 4 5 import ( 6 "context" 7 gotls "crypto/tls" 8 "strings" 9 "time" 10 11 "github.com/pires/go-proxyproto" 12 goxtls "github.com/xtls/go" 13 14 "v2ray.com/core/common" 15 "v2ray.com/core/common/net" 16 "v2ray.com/core/common/session" 17 "v2ray.com/core/transport/internet" 18 "v2ray.com/core/transport/internet/tls" 19 "v2ray.com/core/transport/internet/xtls" 20 ) 21 22 // Listener is an internet.Listener that listens for TCP connections. 23 type Listener struct { 24 listener net.Listener 25 tlsConfig *gotls.Config 26 xtlsConfig *goxtls.Config 27 authConfig internet.ConnectionAuthenticator 28 config *Config 29 addConn internet.ConnHandler 30 } 31 32 // ListenTCP creates a new Listener based on configurations. 33 func ListenTCP(ctx context.Context, address net.Address, port net.Port, streamSettings *internet.MemoryStreamConfig, handler internet.ConnHandler) (internet.Listener, error) { 34 listener, err := internet.ListenSystem(ctx, &net.TCPAddr{ 35 IP: address.IP(), 36 Port: int(port), 37 }, streamSettings.SocketSettings) 38 if err != nil { 39 return nil, newError("failed to listen TCP on", address, ":", port).Base(err) 40 } 41 newError("listening TCP on ", address, ":", port).WriteToLog(session.ExportIDToError(ctx)) 42 43 tcpSettings := streamSettings.ProtocolSettings.(*Config) 44 var l *Listener 45 46 if tcpSettings.AcceptProxyProtocol { 47 policyFunc := func(upstream net.Addr) (proxyproto.Policy, error) { return proxyproto.REQUIRE, nil } 48 l = &Listener{ 49 listener: &proxyproto.Listener{Listener: listener, Policy: policyFunc}, 50 config: tcpSettings, 51 addConn: handler, 52 } 53 newError("accepting PROXY protocol").AtWarning().WriteToLog(session.ExportIDToError(ctx)) 54 } else { 55 l = &Listener{ 56 listener: listener, 57 config: tcpSettings, 58 addConn: handler, 59 } 60 } 61 62 if config := tls.ConfigFromStreamSettings(streamSettings); config != nil { 63 l.tlsConfig = config.GetTLSConfig(tls.WithNextProto("h2")) 64 } 65 if config := xtls.ConfigFromStreamSettings(streamSettings); config != nil { 66 l.xtlsConfig = config.GetXTLSConfig(xtls.WithNextProto("h2")) 67 } 68 69 if tcpSettings.HeaderSettings != nil { 70 headerConfig, err := tcpSettings.HeaderSettings.GetInstance() 71 if err != nil { 72 return nil, newError("invalid header settings").Base(err).AtError() 73 } 74 auth, err := internet.CreateConnectionAuthenticator(headerConfig) 75 if err != nil { 76 return nil, newError("invalid header settings.").Base(err).AtError() 77 } 78 l.authConfig = auth 79 } 80 81 go l.keepAccepting() 82 return l, nil 83 } 84 85 func (v *Listener) keepAccepting() { 86 for { 87 conn, err := v.listener.Accept() 88 if err != nil { 89 errStr := err.Error() 90 if strings.Contains(errStr, "closed") { 91 break 92 } 93 newError("failed to accepted raw connections").Base(err).AtWarning().WriteToLog() 94 if strings.Contains(errStr, "too many") { 95 time.Sleep(time.Millisecond * 500) 96 } 97 continue 98 } 99 100 if v.tlsConfig != nil { 101 conn = tls.Server(conn, v.tlsConfig) 102 } else if v.xtlsConfig != nil { 103 conn = xtls.Server(conn, v.xtlsConfig) 104 } 105 if v.authConfig != nil { 106 conn = v.authConfig.Server(conn) 107 } 108 109 v.addConn(internet.Connection(conn)) 110 } 111 } 112 113 // Addr implements internet.Listener.Addr. 114 func (v *Listener) Addr() net.Addr { 115 return v.listener.Addr() 116 } 117 118 // Close implements internet.Listener.Close. 119 func (v *Listener) Close() error { 120 return v.listener.Close() 121 } 122 123 func init() { 124 common.Must(internet.RegisterTransportListener(protocolName, ListenTCP)) 125 }