2016-06-14 20:54:08 +00:00
|
|
|
package internet
|
|
|
|
|
|
|
|
import (
|
2017-02-24 00:05:16 +00:00
|
|
|
"context"
|
|
|
|
|
2017-08-29 10:56:57 +00:00
|
|
|
"v2ray.com/core/common/net"
|
2016-06-14 20:54:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-08-06 11:48:35 +00:00
|
|
|
transportListenerCache = make(map[string]ListenFunc)
|
2016-06-14 20:54:08 +00:00
|
|
|
)
|
|
|
|
|
2018-08-06 11:48:35 +00:00
|
|
|
func RegisterTransportListener(protocol string, listener ListenFunc) error {
|
2017-01-12 11:54:34 +00:00
|
|
|
if _, found := transportListenerCache[protocol]; found {
|
2017-04-08 23:43:25 +00:00
|
|
|
return newError(protocol, " listener already registered.").AtError()
|
2017-01-03 14:16:48 +00:00
|
|
|
}
|
2017-01-12 11:54:34 +00:00
|
|
|
transportListenerCache[protocol] = listener
|
2017-01-03 14:16:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-08 14:39:46 +00:00
|
|
|
type ConnHandler func(Connection)
|
2017-05-08 22:01:15 +00:00
|
|
|
|
2018-02-08 14:39:46 +00:00
|
|
|
type ListenFunc func(ctx context.Context, address net.Address, port net.Port, handler ConnHandler) (Listener, error)
|
2016-09-30 14:53:40 +00:00
|
|
|
|
2016-06-14 20:54:08 +00:00
|
|
|
type Listener interface {
|
|
|
|
Close() error
|
|
|
|
Addr() net.Addr
|
|
|
|
}
|
|
|
|
|
2018-02-08 14:39:46 +00:00
|
|
|
func ListenTCP(ctx context.Context, address net.Address, port net.Port, handler ConnHandler) (Listener, error) {
|
2017-02-26 13:38:41 +00:00
|
|
|
settings := StreamSettingsFromContext(ctx)
|
2018-09-07 13:00:46 +00:00
|
|
|
if settings == nil {
|
|
|
|
s, err := ToMemoryStreamConfig(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to create default stream settings").Base(err)
|
|
|
|
}
|
|
|
|
settings = s
|
|
|
|
ctx = ContextWithStreamSettings(ctx, settings)
|
|
|
|
}
|
|
|
|
|
2018-09-10 11:23:27 +00:00
|
|
|
if address.Family().IsDomain() && address.Domain() == "localhost" {
|
|
|
|
address = net.LocalHostIP
|
|
|
|
}
|
|
|
|
|
2018-09-07 12:50:25 +00:00
|
|
|
protocol := settings.ProtocolName
|
2017-01-12 11:54:34 +00:00
|
|
|
listenFunc := transportListenerCache[protocol]
|
2017-01-03 14:16:48 +00:00
|
|
|
if listenFunc == nil {
|
2017-04-08 23:43:25 +00:00
|
|
|
return nil, newError(protocol, " listener not registered.").AtError()
|
2016-06-14 20:54:08 +00:00
|
|
|
}
|
2018-02-08 14:39:46 +00:00
|
|
|
listener, err := listenFunc(ctx, address, port, handler)
|
2016-06-14 20:54:08 +00:00
|
|
|
if err != nil {
|
2017-04-08 23:43:25 +00:00
|
|
|
return nil, newError("failed to listen on address: ", address, ":", port).Base(err)
|
2016-06-14 20:54:08 +00:00
|
|
|
}
|
2017-02-26 13:38:41 +00:00
|
|
|
return listener, nil
|
2016-06-14 20:54:08 +00:00
|
|
|
}
|
2018-09-10 11:23:27 +00:00
|
|
|
|
2018-09-15 19:35:32 +00:00
|
|
|
func ListenSystem(ctx context.Context, addr net.Addr) (net.Listener, error) {
|
|
|
|
return effectiveListener.Listen(ctx, addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ListenSystemPacket(ctx context.Context, addr net.Addr) (net.PacketConn, error) {
|
|
|
|
return effectiveListener.ListenPacket(ctx, addr)
|
2018-09-10 11:23:27 +00:00
|
|
|
}
|