You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/transport/internet/tcp/hub.go

96 lines
2.3 KiB

package tcp
import (
"context"
gotls "crypto/tls"
"strings"
8 years ago
"v2ray.com/core/common"
"v2ray.com/core/common/net"
"v2ray.com/core/transport/internet"
"v2ray.com/core/transport/internet/tls"
)
// Listener is an internet.Listener that listens for TCP connections.
type Listener struct {
listener *net.TCPListener
tlsConfig *gotls.Config
authConfig internet.ConnectionAuthenticator
config *Config
addConn internet.ConnHandler
}
// ListenTCP creates a new Listener based on configurations.
func ListenTCP(ctx context.Context, address net.Address, port net.Port, handler internet.ConnHandler) (internet.Listener, error) {
listener, err := net.ListenTCP("tcp", &net.TCPAddr{
IP: address.IP(),
Port: int(port),
})
if err != nil {
return nil, err
}
7 years ago
newError("listening TCP on ", address, ":", port).WithContext(ctx).WriteToLog()
networkSettings := internet.TransportSettingsFromContext(ctx)
tcpSettings := networkSettings.(*Config)
l := &Listener{
listener: listener,
config: tcpSettings,
addConn: handler,
}
if config := tls.ConfigFromContext(ctx, tls.WithNextProto("h2")); config != nil {
l.tlsConfig = config.GetTLSConfig()
}
if tcpSettings.HeaderSettings != nil {
headerConfig, err := tcpSettings.HeaderSettings.GetInstance()
if err != nil {
return nil, newError("invalid header settings").Base(err).AtError()
}
auth, err := internet.CreateConnectionAuthenticator(headerConfig)
if err != nil {
return nil, newError("invalid header settings.").Base(err).AtError()
}
l.authConfig = auth
}
go l.keepAccepting()
return l, nil
}
func (v *Listener) keepAccepting() {
for {
conn, err := v.listener.Accept()
if err != nil {
if strings.Contains(err.Error(), "closed") {
break
}
newError("failed to accepted raw connections").Base(err).AtWarning().WriteToLog()
continue
}
if v.tlsConfig != nil {
8 years ago
conn = tls.Server(conn, v.tlsConfig)
}
if v.authConfig != nil {
8 years ago
conn = v.authConfig.Server(conn)
}
v.addConn(internet.Connection(conn))
}
}
// Addr implements internet.Listener.Addr.
func (v *Listener) Addr() net.Addr {
8 years ago
return v.listener.Addr()
}
// Close implements internet.Listener.Close.
func (v *Listener) Close() error {
8 years ago
return v.listener.Close()
}
func init() {
common.Must(internet.RegisterTransportListener(internet.TransportProtocol_TCP, ListenTCP))
}