Xray-core/transport/internet/tcp/hub.go

137 lines
3.9 KiB
Go
Raw Normal View History

2020-11-25 14:01:53 +03:00
package tcp
import (
"context"
gotls "crypto/tls"
"strings"
"time"
goreality "github.com/xtls/reality"
2020-12-04 04:36:16 +03:00
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/errors"
2020-12-04 04:36:16 +03:00
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/reality"
"github.com/xtls/xray-core/transport/internet/stat"
2020-12-04 04:36:16 +03:00
"github.com/xtls/xray-core/transport/internet/tls"
2020-11-25 14:01:53 +03:00
)
// Listener is an internet.Listener that listens for TCP connections.
type Listener struct {
listener net.Listener
tlsConfig *gotls.Config
realityConfig *goreality.Config
authConfig internet.ConnectionAuthenticator
config *Config
addConn internet.ConnHandler
2020-11-25 14:01:53 +03:00
}
// ListenTCP creates a new Listener based on configurations.
func ListenTCP(ctx context.Context, address net.Address, port net.Port, streamSettings *internet.MemoryStreamConfig, handler internet.ConnHandler) (internet.Listener, error) {
l := &Listener{
addConn: handler,
}
tcpSettings := streamSettings.ProtocolSettings.(*Config)
l.config = tcpSettings
if l.config != nil {
if streamSettings.SocketSettings == nil {
streamSettings.SocketSettings = &internet.SocketConfig{}
}
2022-05-18 10:29:01 +03:00
streamSettings.SocketSettings.AcceptProxyProtocol = l.config.AcceptProxyProtocol || streamSettings.SocketSettings.AcceptProxyProtocol
2020-11-25 14:01:53 +03:00
}
var listener net.Listener
var err error
2021-10-19 19:57:14 +03:00
if port == net.Port(0) { // unix
2020-11-25 14:01:53 +03:00
listener, err = internet.ListenSystem(ctx, &net.UnixAddr{
Name: address.Domain(),
Net: "unix",
}, streamSettings.SocketSettings)
if err != nil {
return nil, errors.New("failed to listen Unix Domain Socket on ", address).Base(err)
2020-11-25 14:01:53 +03:00
}
errors.LogInfo(ctx, "listening Unix Domain Socket on ", address)
2020-11-25 14:01:53 +03:00
} else {
listener, err = internet.ListenSystem(ctx, &net.TCPAddr{
IP: address.IP(),
Port: int(port),
}, streamSettings.SocketSettings)
if err != nil {
return nil, errors.New("failed to listen TCP on ", address, ":", port).Base(err)
2020-11-25 14:01:53 +03:00
}
errors.LogInfo(ctx, "listening TCP on ", address, ":", port)
2020-11-25 14:01:53 +03:00
}
if streamSettings.SocketSettings != nil && streamSettings.SocketSettings.AcceptProxyProtocol {
errors.LogWarning(ctx, "accepting PROXY protocol")
2020-11-25 14:01:53 +03:00
}
l.listener = listener
if config := tls.ConfigFromStreamSettings(streamSettings); config != nil {
l.tlsConfig = config.GetTLSConfig()
2020-11-25 14:01:53 +03:00
}
if config := reality.ConfigFromStreamSettings(streamSettings); config != nil {
l.realityConfig = config.GetREALITYConfig()
}
2020-11-25 14:01:53 +03:00
if tcpSettings.HeaderSettings != nil {
headerConfig, err := tcpSettings.HeaderSettings.GetInstance()
if err != nil {
return nil, errors.New("invalid header settings").Base(err).AtError()
2020-11-25 14:01:53 +03:00
}
auth, err := internet.CreateConnectionAuthenticator(headerConfig)
if err != nil {
return nil, errors.New("invalid header settings.").Base(err).AtError()
2020-11-25 14:01:53 +03:00
}
l.authConfig = auth
}
go l.keepAccepting()
return l, nil
}
func (v *Listener) keepAccepting() {
for {
conn, err := v.listener.Accept()
if err != nil {
errStr := err.Error()
if strings.Contains(errStr, "closed") {
break
}
errors.LogWarningInner(context.Background(), err, "failed to accepted raw connections")
2020-11-25 14:01:53 +03:00
if strings.Contains(errStr, "too many") {
time.Sleep(time.Millisecond * 500)
}
continue
}
go func() {
if v.tlsConfig != nil {
conn = tls.Server(conn, v.tlsConfig)
} else if v.realityConfig != nil {
if conn, err = reality.Server(conn, v.realityConfig); err != nil {
errors.LogInfo(context.Background(), err.Error())
return
}
}
if v.authConfig != nil {
conn = v.authConfig.Server(conn)
}
v.addConn(stat.Connection(conn))
}()
2020-11-25 14:01:53 +03:00
}
}
// Addr implements internet.Listener.Addr.
func (v *Listener) Addr() net.Addr {
return v.listener.Addr()
}
// Close implements internet.Listener.Close.
func (v *Listener) Close() error {
return v.listener.Close()
}
func init() {
common.Must(internet.RegisterTransportListener(protocolName, ListenTCP))
}