2016-06-14 16:54:08 -04:00
|
|
|
package tcp
|
|
|
|
|
|
|
|
import (
|
2017-02-23 19:05:16 -05:00
|
|
|
"context"
|
2016-09-30 10:53:40 -04:00
|
|
|
"crypto/tls"
|
2016-06-14 16:54:08 -04:00
|
|
|
"net"
|
|
|
|
"time"
|
2017-01-03 08:53:59 -05:00
|
|
|
|
2017-02-10 10:42:24 -05:00
|
|
|
"v2ray.com/core/app/log"
|
2017-01-03 09:16:48 -05:00
|
|
|
"v2ray.com/core/common"
|
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"
|
2017-03-02 05:54:40 -05:00
|
|
|
"v2ray.com/core/common/retry"
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/transport/internet"
|
2016-11-24 17:16:05 -05:00
|
|
|
"v2ray.com/core/transport/internet/internal"
|
2016-10-02 17:43:58 -04:00
|
|
|
v2tls "v2ray.com/core/transport/internet/tls"
|
2016-06-14 16:54:08 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrClosedListener = errors.New("Listener is closed.")
|
|
|
|
)
|
|
|
|
|
|
|
|
type TCPListener struct {
|
2017-02-27 09:13:22 -05:00
|
|
|
ctx context.Context
|
2017-02-26 08:38:41 -05:00
|
|
|
listener *net.TCPListener
|
|
|
|
tlsConfig *tls.Config
|
|
|
|
authConfig internet.ConnectionAuthenticator
|
|
|
|
config *Config
|
|
|
|
conns chan<- internet.Connection
|
2016-06-14 16:54:08 -04:00
|
|
|
}
|
|
|
|
|
2017-02-26 08:38:41 -05:00
|
|
|
func ListenTCP(ctx context.Context, address v2net.Address, port v2net.Port, conns chan<- internet.Connection) (internet.Listener, error) {
|
2016-06-14 16:54:08 -04:00
|
|
|
listener, err := net.ListenTCP("tcp", &net.TCPAddr{
|
|
|
|
IP: address.IP(),
|
|
|
|
Port: int(port),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-04-06 15:13:17 -04:00
|
|
|
log.Trace(errors.New("TCP|Listener: Listening on ", address, ":", port))
|
2017-02-23 19:05:16 -05:00
|
|
|
networkSettings := internet.TransportSettingsFromContext(ctx)
|
2016-10-02 17:43:58 -04:00
|
|
|
tcpSettings := networkSettings.(*Config)
|
|
|
|
|
2016-06-14 16:54:08 -04:00
|
|
|
l := &TCPListener{
|
2017-02-27 09:13:22 -05:00
|
|
|
ctx: ctx,
|
|
|
|
listener: listener,
|
|
|
|
config: tcpSettings,
|
|
|
|
conns: conns,
|
2016-06-14 16:54:08 -04:00
|
|
|
}
|
2017-02-23 19:05:16 -05:00
|
|
|
if securitySettings := internet.SecuritySettingsFromContext(ctx); securitySettings != nil {
|
2016-10-16 08:22:21 -04:00
|
|
|
tlsConfig, ok := securitySettings.(*v2tls.Config)
|
|
|
|
if ok {
|
|
|
|
l.tlsConfig = tlsConfig.GetTLSConfig()
|
|
|
|
}
|
2016-09-30 10:53:40 -04:00
|
|
|
}
|
2016-11-02 17:26:21 -04:00
|
|
|
if tcpSettings.HeaderSettings != nil {
|
|
|
|
headerConfig, err := tcpSettings.HeaderSettings.GetInstance()
|
|
|
|
if err != nil {
|
2017-04-06 09:13:09 -04:00
|
|
|
return nil, errors.New("Internet|TCP: Invalid header settings.").Base(err)
|
2016-11-02 17:26:21 -04:00
|
|
|
}
|
2017-01-12 16:47:10 -05:00
|
|
|
auth, err := internet.CreateConnectionAuthenticator(headerConfig)
|
2016-11-02 17:26:21 -04:00
|
|
|
if err != nil {
|
2017-04-06 09:13:09 -04:00
|
|
|
return nil, errors.New("Internet|TCP: Invalid header settings.").Base(err)
|
2016-11-02 17:26:21 -04:00
|
|
|
}
|
|
|
|
l.authConfig = auth
|
|
|
|
}
|
2016-06-14 16:54:08 -04:00
|
|
|
go l.KeepAccepting()
|
|
|
|
return l, nil
|
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *TCPListener) KeepAccepting() {
|
2017-02-27 09:13:22 -05:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-v.ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
2017-03-02 05:54:40 -05:00
|
|
|
var conn net.Conn
|
|
|
|
err := retry.ExponentialBackoff(5, 200).On(func() error {
|
|
|
|
rawConn, err := v.listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
conn = rawConn
|
|
|
|
return nil
|
|
|
|
})
|
2017-02-26 08:38:41 -05:00
|
|
|
if err != nil {
|
2017-04-06 15:13:17 -04:00
|
|
|
log.Trace(errors.New("TCP|Listener: Failed to accepted raw connections: ", err).AtWarning())
|
2017-02-26 08:38:41 -05:00
|
|
|
continue
|
|
|
|
}
|
2017-03-02 05:54:40 -05:00
|
|
|
|
2017-02-26 08:38:41 -05:00
|
|
|
if v.tlsConfig != nil {
|
2016-11-27 15:39:09 -05:00
|
|
|
conn = tls.Server(conn, v.tlsConfig)
|
2016-10-31 19:41:46 -04:00
|
|
|
}
|
2017-02-26 08:38:41 -05:00
|
|
|
if v.authConfig != nil {
|
2016-11-27 15:39:09 -05:00
|
|
|
conn = v.authConfig.Server(conn)
|
2016-11-02 17:26:21 -04:00
|
|
|
}
|
2017-02-17 04:33:42 -05:00
|
|
|
|
2016-06-14 16:54:08 -04:00
|
|
|
select {
|
2017-02-26 08:38:41 -05:00
|
|
|
case v.conns <- internal.NewConnection(internal.ConnectionID{}, conn, v, internal.ReuseConnection(v.config.IsConnectionReuse())):
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
conn.Close()
|
2016-06-14 16:54:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-01 16:12:44 -05:00
|
|
|
func (v *TCPListener) Put(id internal.ConnectionID, conn net.Conn) {
|
2016-06-14 16:54:08 -04:00
|
|
|
select {
|
2017-02-26 08:38:41 -05:00
|
|
|
case v.conns <- internal.NewConnection(internal.ConnectionID{}, conn, v, internal.ReuseConnection(v.config.IsConnectionReuse())):
|
|
|
|
case <-time.After(time.Second * 5):
|
2016-06-14 16:54:08 -04:00
|
|
|
conn.Close()
|
2017-03-02 05:54:40 -05:00
|
|
|
case <-v.ctx.Done():
|
|
|
|
conn.Close()
|
2016-06-14 16:54:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *TCPListener) Addr() net.Addr {
|
|
|
|
return v.listener.Addr()
|
2016-06-14 16:54:08 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *TCPListener) Close() error {
|
|
|
|
v.listener.Close()
|
2016-06-14 16:54:08 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2017-01-12 06:54:34 -05:00
|
|
|
common.Must(internet.RegisterTransportListener(internet.TransportProtocol_TCP, ListenTCP))
|
2016-06-14 16:54:08 -04:00
|
|
|
}
|