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