2016-06-14 16:54:08 -04:00
|
|
|
package internet
|
|
|
|
|
|
|
|
import (
|
2017-02-23 19:05:16 -05:00
|
|
|
"context"
|
2017-05-08 18:01:15 -04:00
|
|
|
"time"
|
2017-02-23 19:05:16 -05:00
|
|
|
|
2017-08-29 06:56:57 -04:00
|
|
|
"v2ray.com/core/common/net"
|
2016-06-14 16:54:08 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-01-12 06:54:34 -05:00
|
|
|
transportListenerCache = make(map[TransportProtocol]ListenFunc)
|
2016-06-14 16:54:08 -04:00
|
|
|
)
|
|
|
|
|
2017-01-12 06:54:34 -05:00
|
|
|
func RegisterTransportListener(protocol TransportProtocol, listener ListenFunc) error {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-05-08 18:01:15 -04:00
|
|
|
type AddConnection func(context.Context, Connection) bool
|
|
|
|
|
2017-08-29 06:56:57 -04:00
|
|
|
type ListenFunc func(ctx context.Context, address net.Address, port net.Port, addConn AddConnection) (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
|
|
|
|
}
|
|
|
|
|
2017-08-29 06:56:57 -04:00
|
|
|
func ListenTCP(ctx context.Context, address net.Address, port net.Port, conns chan<- Connection) (Listener, error) {
|
2017-02-26 08:38:41 -05:00
|
|
|
settings := StreamSettingsFromContext(ctx)
|
2017-01-12 06:54:34 -05:00
|
|
|
protocol := settings.GetEffectiveProtocol()
|
2017-02-23 19:05:16 -05:00
|
|
|
transportSettings, err := settings.GetEffectiveTransportSettings()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ctx = ContextWithTransportSettings(ctx, transportSettings)
|
|
|
|
if settings != nil && settings.HasSecuritySettings() {
|
|
|
|
securitySettings, err := settings.GetEffectiveSecuritySettings()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ctx = ContextWithSecuritySettings(ctx, securitySettings)
|
|
|
|
}
|
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
|
|
|
}
|
2017-05-08 18:01:15 -04:00
|
|
|
listener, err := listenFunc(ctx, address, port, func(ctx context.Context, conn Connection) bool {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
conn.Close()
|
|
|
|
return false
|
|
|
|
case conns <- conn:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
conn.Close()
|
|
|
|
return false
|
|
|
|
case conns <- conn:
|
|
|
|
return true
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
conn.Close()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
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
|
|
|
}
|