2016-06-14 16:54:08 -04:00
|
|
|
package internet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/common/log"
|
|
|
|
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 (
|
2016-06-27 02:53:35 -04:00
|
|
|
ErrClosedConnection = errors.New("Connection already closed.")
|
2016-06-14 16:54:08 -04:00
|
|
|
|
|
|
|
KCPListenFunc ListenFunc
|
|
|
|
TCPListenFunc ListenFunc
|
|
|
|
RawTCPListenFunc ListenFunc
|
2016-08-13 09:43:46 -04:00
|
|
|
WSListenFunc ListenFunc
|
2016-06-14 16:54:08 -04:00
|
|
|
)
|
|
|
|
|
2016-09-30 10:53:40 -04:00
|
|
|
type ListenFunc func(address v2net.Address, port v2net.Port, options ListenOptions) (Listener, error)
|
|
|
|
type ListenOptions struct {
|
2016-10-02 17:43:58 -04:00
|
|
|
Stream *StreamConfig
|
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-06-14 16:54:08 -04:00
|
|
|
var listener Listener
|
|
|
|
var err error
|
2016-09-30 10:53:40 -04:00
|
|
|
options := ListenOptions{
|
|
|
|
Stream: settings,
|
|
|
|
}
|
2016-10-02 17:43:58 -04:00
|
|
|
switch settings.Network {
|
|
|
|
case v2net.Network_TCP:
|
2016-09-30 10:53:40 -04:00
|
|
|
listener, err = TCPListenFunc(address, port, options)
|
2016-10-02 17:43:58 -04:00
|
|
|
case v2net.Network_KCP:
|
2016-09-30 10:53:40 -04:00
|
|
|
listener, err = KCPListenFunc(address, port, options)
|
2016-10-02 17:43:58 -04:00
|
|
|
case v2net.Network_WebSocket:
|
2016-09-30 10:53:40 -04:00
|
|
|
listener, err = WSListenFunc(address, port, options)
|
2016-10-02 17:43:58 -04:00
|
|
|
case v2net.Network_RawTCP:
|
2016-09-30 10:53:40 -04:00
|
|
|
listener, err = RawTCPListenFunc(address, port, options)
|
2016-06-14 19:08:03 -04:00
|
|
|
default:
|
2016-10-02 17:43:58 -04:00
|
|
|
log.Error("Internet|Listener: Unknown stream type: ", settings.Network)
|
2016-06-14 19:08:03 -04:00
|
|
|
err = ErrUnsupportedStreamType
|
2016-06-14 16:54:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2016-06-17 12:03:31 -04:00
|
|
|
log.Warning("Internet|Listener: Failed to listen on ", address, ":", port)
|
2016-06-14 16:54:08 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
hub := &TCPHub{
|
|
|
|
listener: listener,
|
|
|
|
connCallback: callback,
|
|
|
|
}
|
|
|
|
|
|
|
|
go hub.start()
|
|
|
|
return hub, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *TCPHub) Close() {
|
|
|
|
this.accepting = false
|
|
|
|
this.listener.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *TCPHub) start() {
|
|
|
|
this.accepting = true
|
|
|
|
for this.accepting {
|
2016-11-20 15:47:51 -05:00
|
|
|
var newConn Connection
|
|
|
|
err := retry.ExponentialBackoff(10, 200).On(func() error {
|
|
|
|
if !this.accepting {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
conn, err := this.listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
if this.accepting {
|
|
|
|
log.Warning("Internet|Listener: Failed to accept new TCP connection: ", err)
|
|
|
|
}
|
|
|
|
return err
|
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 {
|
|
|
|
go this.connCallback(newConn)
|
2016-06-14 16:54:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|