Xray-core/transport/internet/tcp_hub.go

94 lines
2.9 KiB
Go
Raw Normal View History

2020-11-25 14:01:53 +03:00
package internet
import (
"context"
"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/stat"
2020-11-25 14:01:53 +03:00
)
2021-10-19 19:57:14 +03:00
var transportListenerCache = make(map[string]ListenFunc)
2020-11-25 14:01:53 +03:00
func RegisterTransportListener(protocol string, listener ListenFunc) error {
if _, found := transportListenerCache[protocol]; found {
return errors.New(protocol, " listener already registered.").AtError()
2020-11-25 14:01:53 +03:00
}
transportListenerCache[protocol] = listener
return nil
}
type ConnHandler func(stat.Connection)
2020-11-25 14:01:53 +03:00
type ListenFunc func(ctx context.Context, address net.Address, port net.Port, settings *MemoryStreamConfig, handler ConnHandler) (Listener, error)
type Listener interface {
Close() error
Addr() net.Addr
}
// ListenUnix is the UDS version of ListenTCP
func ListenUnix(ctx context.Context, address net.Address, settings *MemoryStreamConfig, handler ConnHandler) (Listener, error) {
if settings == nil {
s, err := ToMemoryStreamConfig(nil)
if err != nil {
return nil, errors.New("failed to create default unix stream settings").Base(err)
2020-11-25 14:01:53 +03:00
}
settings = s
}
protocol := settings.ProtocolName
listenFunc := transportListenerCache[protocol]
if listenFunc == nil {
return nil, errors.New(protocol, " unix istener not registered.").AtError()
2020-11-25 14:01:53 +03:00
}
listener, err := listenFunc(ctx, address, net.Port(0), settings, handler)
if err != nil {
return nil, errors.New("failed to listen on unix address: ", address).Base(err)
2020-11-25 14:01:53 +03:00
}
return listener, nil
}
2021-10-19 19:57:14 +03:00
2020-11-25 14:01:53 +03:00
func ListenTCP(ctx context.Context, address net.Address, port net.Port, settings *MemoryStreamConfig, handler ConnHandler) (Listener, error) {
if settings == nil {
s, err := ToMemoryStreamConfig(nil)
if err != nil {
return nil, errors.New("failed to create default stream settings").Base(err)
2020-11-25 14:01:53 +03:00
}
settings = s
}
if address.Family().IsDomain() && address.Domain() == "localhost" {
address = net.LocalHostIP
}
if address.Family().IsDomain() {
return nil, errors.New("domain address is not allowed for listening: ", address.Domain())
2020-11-25 14:01:53 +03:00
}
protocol := settings.ProtocolName
listenFunc := transportListenerCache[protocol]
if listenFunc == nil {
return nil, errors.New(protocol, " listener not registered.").AtError()
2020-11-25 14:01:53 +03:00
}
listener, err := listenFunc(ctx, address, port, settings, handler)
if err != nil {
return nil, errors.New("failed to listen on address: ", address, ":", port).Base(err)
2020-11-25 14:01:53 +03:00
}
return listener, nil
}
// ListenSystem listens on a local address for incoming TCP connections.
//
// xray:api:beta
func ListenSystem(ctx context.Context, addr net.Addr, sockopt *SocketConfig) (net.Listener, error) {
return effectiveListener.Listen(ctx, addr, sockopt)
}
// ListenSystemPacket listens on a local address for incoming UDP connections.
//
// xray:api:beta
func ListenSystemPacket(ctx context.Context, addr net.Addr, sockopt *SocketConfig) (net.PacketConn, error) {
return effectiveListener.ListenPacket(ctx, addr, sockopt)
}