2016-06-14 16:54:08 -04:00
|
|
|
package internet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"sync"
|
2017-01-03 08:53:59 -05:00
|
|
|
|
2016-12-04 03:10:47 -05:00
|
|
|
"v2ray.com/core/common/errors"
|
2016-08-20 14:55:45 -04:00
|
|
|
v2net "v2ray.com/core/common/net"
|
2016-11-20 15:47:51 -05:00
|
|
|
"v2ray.com/core/common/retry"
|
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 {
|
|
|
|
return errors.New("Internet|TCPHub: ", protocol, " listener already registered.")
|
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
|
|
|
|
}
|
|
|
|
|
2016-09-30 10:53:40 -04:00
|
|
|
type ListenFunc func(address v2net.Address, port v2net.Port, options ListenOptions) (Listener, error)
|
|
|
|
type ListenOptions struct {
|
2017-01-26 14:46:44 -05:00
|
|
|
Stream *StreamConfig
|
|
|
|
RecvOrigDest bool
|
2016-09-30 10:53:40 -04:00
|
|
|
}
|
|
|
|
|
2016-06-14 16:54:08 -04:00
|
|
|
type Listener interface {
|
|
|
|
Accept() (Connection, error)
|
|
|
|
Close() error
|
|
|
|
Addr() net.Addr
|
|
|
|
}
|
|
|
|
|
|
|
|
type TCPHub struct {
|
|
|
|
sync.Mutex
|
|
|
|
listener Listener
|
|
|
|
connCallback ConnectionHandler
|
|
|
|
accepting bool
|
|
|
|
}
|
|
|
|
|
2016-10-02 17:43:58 -04:00
|
|
|
func ListenTCP(address v2net.Address, port v2net.Port, callback ConnectionHandler, settings *StreamConfig) (*TCPHub, error) {
|
2016-09-30 10:53:40 -04:00
|
|
|
options := ListenOptions{
|
|
|
|
Stream: settings,
|
|
|
|
}
|
2017-01-12 06:54:34 -05:00
|
|
|
protocol := settings.GetEffectiveProtocol()
|
|
|
|
listenFunc := transportListenerCache[protocol]
|
2017-01-03 09:16:48 -05:00
|
|
|
if listenFunc == nil {
|
2017-01-12 06:54:34 -05:00
|
|
|
return nil, errors.New("Internet|TCPHub: ", protocol, " listener not registered.")
|
2016-06-14 16:54:08 -04:00
|
|
|
}
|
2017-01-03 09:16:48 -05:00
|
|
|
listener, err := listenFunc(address, port, options)
|
2016-06-14 16:54:08 -04:00
|
|
|
if err != nil {
|
2017-02-10 10:50:45 -05:00
|
|
|
return nil, errors.Base(err).Message("Internet|TCPHub: Failed to listen on address: ", address, ":", port)
|
2016-06-14 16:54:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
hub := &TCPHub{
|
|
|
|
listener: listener,
|
|
|
|
connCallback: callback,
|
|
|
|
}
|
|
|
|
|
|
|
|
go hub.start()
|
|
|
|
return hub, nil
|
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *TCPHub) Close() {
|
|
|
|
v.accepting = false
|
|
|
|
v.listener.Close()
|
2016-06-14 16:54:08 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *TCPHub) start() {
|
|
|
|
v.accepting = true
|
|
|
|
for v.accepting {
|
2016-11-20 15:47:51 -05:00
|
|
|
var newConn Connection
|
2017-02-10 05:41:50 -05:00
|
|
|
err := retry.ExponentialBackoff(10, 500).On(func() error {
|
2016-11-27 15:39:09 -05:00
|
|
|
if !v.accepting {
|
2016-11-20 15:47:51 -05:00
|
|
|
return nil
|
|
|
|
}
|
2016-11-27 15:39:09 -05:00
|
|
|
conn, err := v.listener.Accept()
|
2016-11-20 15:47:51 -05:00
|
|
|
if err != nil {
|
2017-02-10 05:41:50 -05:00
|
|
|
return errors.Base(err).Message("Internet|Listener: Failed to accept new TCP connection.")
|
2016-06-14 16:54:08 -04:00
|
|
|
}
|
2016-11-20 15:47:51 -05:00
|
|
|
newConn = conn
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err == nil && newConn != nil {
|
2016-11-27 15:39:09 -05:00
|
|
|
go v.connCallback(newConn)
|
2016-06-14 16:54:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|