2016-06-14 16:54:08 -04:00
|
|
|
package internet
|
|
|
|
|
|
|
|
import (
|
2017-02-23 19:05:16 -05:00
|
|
|
"context"
|
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v5/common/net"
|
2016-06-14 16:54:08 -04:00
|
|
|
)
|
|
|
|
|
2021-05-19 17:28:52 -04:00
|
|
|
var transportListenerCache = make(map[string]ListenFunc)
|
2016-06-14 16:54:08 -04:00
|
|
|
|
2018-08-06 07:48:35 -04:00
|
|
|
func RegisterTransportListener(protocol string, listener ListenFunc) error {
|
2017-01-12 06:54:34 -05:00
|
|
|
if _, found := transportListenerCache[protocol]; found {
|
2017-04-08 19:43:25 -04:00
|
|
|
return newError(protocol, " listener already registered.").AtError()
|
2017-01-03 09:16:48 -05:00
|
|
|
}
|
2017-01-12 06:54:34 -05:00
|
|
|
transportListenerCache[protocol] = listener
|
2017-01-03 09:16:48 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-08 09:39:46 -05:00
|
|
|
type ConnHandler func(Connection)
|
2017-05-08 18:01:15 -04:00
|
|
|
|
2018-11-21 08:54:40 -05:00
|
|
|
type ListenFunc func(ctx context.Context, address net.Address, port net.Port, settings *MemoryStreamConfig, handler ConnHandler) (Listener, error)
|
2016-09-30 10:53:40 -04:00
|
|
|
|
2016-06-14 16:54:08 -04:00
|
|
|
type Listener interface {
|
|
|
|
Close() error
|
|
|
|
Addr() net.Addr
|
|
|
|
}
|
|
|
|
|
2020-10-29 03:30:38 -04:00
|
|
|
// 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, newError("failed to create default unix stream settings").Base(err)
|
|
|
|
}
|
|
|
|
settings = s
|
|
|
|
}
|
|
|
|
|
|
|
|
protocol := settings.ProtocolName
|
2022-10-07 21:04:05 -04:00
|
|
|
|
|
|
|
if originalProtocolName := getOriginalMessageName(settings); originalProtocolName != "" {
|
|
|
|
protocol = originalProtocolName
|
|
|
|
}
|
|
|
|
|
2020-10-29 03:30:38 -04:00
|
|
|
listenFunc := transportListenerCache[protocol]
|
|
|
|
if listenFunc == nil {
|
|
|
|
return nil, newError(protocol, " unix istener not registered.").AtError()
|
|
|
|
}
|
|
|
|
listener, err := listenFunc(ctx, address, net.Port(0), settings, handler)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to listen on unix address: ", address).Base(err)
|
|
|
|
}
|
|
|
|
return listener, nil
|
|
|
|
}
|
2021-05-19 17:28:52 -04:00
|
|
|
|
2018-11-21 08:54:40 -05:00
|
|
|
func ListenTCP(ctx context.Context, address net.Address, port net.Port, settings *MemoryStreamConfig, handler ConnHandler) (Listener, error) {
|
2018-09-07 09:00:46 -04:00
|
|
|
if settings == nil {
|
|
|
|
s, err := ToMemoryStreamConfig(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to create default stream settings").Base(err)
|
|
|
|
}
|
|
|
|
settings = s
|
|
|
|
}
|
|
|
|
|
2018-09-10 07:23:27 -04:00
|
|
|
if address.Family().IsDomain() && address.Domain() == "localhost" {
|
|
|
|
address = net.LocalHostIP
|
|
|
|
}
|
|
|
|
|
2019-01-19 04:59:47 -05:00
|
|
|
if address.Family().IsDomain() {
|
|
|
|
return nil, newError("domain address is not allowed for listening: ", address.Domain())
|
|
|
|
}
|
|
|
|
|
2018-09-07 08:50:25 -04:00
|
|
|
protocol := settings.ProtocolName
|
2021-09-07 11:53:09 -04:00
|
|
|
|
|
|
|
if originalProtocolName := getOriginalMessageName(settings); originalProtocolName != "" {
|
|
|
|
protocol = originalProtocolName
|
|
|
|
}
|
|
|
|
|
2017-01-12 06:54:34 -05:00
|
|
|
listenFunc := transportListenerCache[protocol]
|
2017-01-03 09:16:48 -05:00
|
|
|
if listenFunc == nil {
|
2017-04-08 19:43:25 -04:00
|
|
|
return nil, newError(protocol, " listener not registered.").AtError()
|
2016-06-14 16:54:08 -04:00
|
|
|
}
|
2018-11-21 08:54:40 -05:00
|
|
|
listener, err := listenFunc(ctx, address, port, settings, handler)
|
2016-06-14 16:54:08 -04:00
|
|
|
if err != nil {
|
2017-04-08 19:43:25 -04:00
|
|
|
return nil, newError("failed to listen on address: ", address, ":", port).Base(err)
|
2016-06-14 16:54:08 -04:00
|
|
|
}
|
2017-02-26 08:38:41 -05:00
|
|
|
return listener, nil
|
2016-06-14 16:54:08 -04:00
|
|
|
}
|
2018-09-10 07:23:27 -04:00
|
|
|
|
2018-12-03 17:39:21 -05:00
|
|
|
// ListenSystem listens on a local address for incoming TCP connections.
|
|
|
|
//
|
|
|
|
// v2ray:api:beta
|
2018-11-21 08:54:40 -05:00
|
|
|
func ListenSystem(ctx context.Context, addr net.Addr, sockopt *SocketConfig) (net.Listener, error) {
|
|
|
|
return effectiveListener.Listen(ctx, addr, sockopt)
|
2018-09-15 15:35:32 -04:00
|
|
|
}
|
|
|
|
|
2018-12-03 17:39:21 -05:00
|
|
|
// ListenSystemPacket listens on a local address for incoming UDP connections.
|
|
|
|
//
|
|
|
|
// v2ray:api:beta
|
2018-11-21 08:54:40 -05:00
|
|
|
func ListenSystemPacket(ctx context.Context, addr net.Addr, sockopt *SocketConfig) (net.PacketConn, error) {
|
|
|
|
return effectiveListener.ListenPacket(ctx, addr, sockopt)
|
2018-09-10 07:23:27 -04:00
|
|
|
}
|