You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/transport/internet/tcp/hub.go

117 lines
2.6 KiB

package tcp
import (
"context"
"crypto/tls"
"net"
"time"
8 years ago
8 years ago
"v2ray.com/core/app/log"
"v2ray.com/core/common"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/retry"
"v2ray.com/core/transport/internet"
v2tls "v2ray.com/core/transport/internet/tls"
)
var (
ErrClosedListener = newError("Listener is closed.")
)
type TCPListener struct {
ctx context.Context
listener *net.TCPListener
tlsConfig *tls.Config
authConfig internet.ConnectionAuthenticator
config *Config
conns chan<- internet.Connection
}
func ListenTCP(ctx context.Context, address v2net.Address, port v2net.Port, conns chan<- internet.Connection) (internet.Listener, error) {
listener, err := net.ListenTCP("tcp", &net.TCPAddr{
IP: address.IP(),
Port: int(port),
})
if err != nil {
return nil, err
}
log.Trace(newError("TCP|Listener: Listening on ", address, ":", port))
networkSettings := internet.TransportSettingsFromContext(ctx)
tcpSettings := networkSettings.(*Config)
l := &TCPListener{
ctx: ctx,
listener: listener,
config: tcpSettings,
conns: conns,
}
if securitySettings := internet.SecuritySettingsFromContext(ctx); securitySettings != nil {
tlsConfig, ok := securitySettings.(*v2tls.Config)
if ok {
l.tlsConfig = tlsConfig.GetTLSConfig()
}
}
if tcpSettings.HeaderSettings != nil {
headerConfig, err := tcpSettings.HeaderSettings.GetInstance()
if err != nil {
return nil, newError("Internet|TCP: Invalid header settings.").Base(err)
}
auth, err := internet.CreateConnectionAuthenticator(headerConfig)
if err != nil {
return nil, newError("Internet|TCP: Invalid header settings.").Base(err)
}
l.authConfig = auth
}
go l.KeepAccepting()
return l, nil
}
8 years ago
func (v *TCPListener) KeepAccepting() {
for {
select {
case <-v.ctx.Done():
return
default:
}
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
})
if err != nil {
log.Trace(newError("TCP|Listener: Failed to accepted raw connections: ", err).AtWarning())
continue
}
if v.tlsConfig != nil {
8 years ago
conn = tls.Server(conn, v.tlsConfig)
}
if v.authConfig != nil {
8 years ago
conn = v.authConfig.Server(conn)
}
select {
case v.conns <- internet.Connection(conn):
case <-time.After(time.Second * 5):
conn.Close()
}
}
}
8 years ago
func (v *TCPListener) Addr() net.Addr {
return v.listener.Addr()
}
8 years ago
func (v *TCPListener) Close() error {
v.listener.Close()
return nil
}
func init() {
common.Must(internet.RegisterTransportListener(internet.TransportProtocol_TCP, ListenTCP))
}