1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-09 03:37:37 -05:00
v2fly/transport/internet/tcp/hub.go

135 lines
3.6 KiB
Go
Raw Normal View History

2019-02-01 14:08:21 -05:00
// +build !confonly
2016-06-14 16:54:08 -04:00
package tcp
import (
2017-02-23 19:05:16 -05:00
"context"
2017-04-20 05:00:15 -04:00
gotls "crypto/tls"
2018-02-08 09:39:46 -05:00
"strings"
"time"
2017-01-03 08:53:59 -05:00
2017-01-03 09:16:48 -05:00
"v2ray.com/core/common"
2017-08-29 08:32:54 -04:00
"v2ray.com/core/common/net"
"v2ray.com/core/common/session"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/transport/internet"
2017-04-20 05:00:15 -04:00
"v2ray.com/core/transport/internet/tls"
2016-06-14 16:54:08 -04:00
)
2018-01-02 12:57:22 -05:00
// Listener is an internet.Listener that listens for TCP connections.
type Listener struct {
2018-09-15 15:35:32 -04:00
listener net.Listener
2017-04-20 05:00:15 -04:00
tlsConfig *gotls.Config
2017-02-26 08:38:41 -05:00
authConfig internet.ConnectionAuthenticator
config *Config
2018-02-08 09:39:46 -05:00
addConn internet.ConnHandler
2020-10-29 03:30:38 -04:00
locker *internet.FileLocker // for unix domain socket
2016-06-14 16:54:08 -04:00
}
2018-01-02 12:57:22 -05:00
// ListenTCP creates a new Listener based on configurations.
func ListenTCP(ctx context.Context, address net.Address, port net.Port, streamSettings *internet.MemoryStreamConfig, handler internet.ConnHandler) (internet.Listener, error) {
2020-10-29 03:30:38 -04:00
l := &Listener{
addConn: handler,
2016-06-14 16:54:08 -04:00
}
tcpSettings := streamSettings.ProtocolSettings.(*Config)
2020-10-29 03:30:38 -04:00
l.config = tcpSettings
if l.config != nil {
if streamSettings.SocketSettings == nil {
streamSettings.SocketSettings = &internet.SocketConfig{}
}
streamSettings.SocketSettings.AcceptProxyProtocol = l.config.AcceptProxyProtocol
}
var listener net.Listener
var err error
if port == net.Port(0) { // unix
2020-10-29 03:30:38 -04:00
listener, err = internet.ListenSystem(ctx, &net.UnixAddr{
Name: address.Domain(),
Net: "unix",
}, streamSettings.SocketSettings)
if err != nil {
2020-12-15 20:26:25 -05:00
return nil, newError("failed to listen Unix Domain Socket on ", address).Base(err)
2020-10-29 03:30:38 -04:00
}
newError("listening Unix Domain Socket on ", address).WriteToLog(session.ExportIDToError(ctx))
locker := ctx.Value(address.Domain())
if locker != nil {
l.locker = locker.(*internet.FileLocker)
}
} else {
2020-10-29 03:30:38 -04:00
listener, err = internet.ListenSystem(ctx, &net.TCPAddr{
IP: address.IP(),
Port: int(port),
}, streamSettings.SocketSettings)
if err != nil {
return nil, newError("failed to listen TCP on ", address, ":", port).Base(err)
}
2020-10-29 03:30:38 -04:00
newError("listening TCP on ", address, ":", port).WriteToLog(session.ExportIDToError(ctx))
}
if streamSettings.SocketSettings != nil && streamSettings.SocketSettings.AcceptProxyProtocol {
newError("accepting PROXY protocol").AtWarning().WriteToLog(session.ExportIDToError(ctx))
2016-06-14 16:54:08 -04:00
}
2017-12-16 18:53:17 -05:00
2020-10-29 03:30:38 -04:00
l.listener = listener
if config := tls.ConfigFromStreamSettings(streamSettings); config != nil {
2018-02-28 09:15:22 -05:00
l.tlsConfig = config.GetTLSConfig(tls.WithNextProto("h2"))
2016-09-30 10:53:40 -04:00
}
2017-12-16 18:53:17 -05:00
2016-11-02 17:26:21 -04:00
if tcpSettings.HeaderSettings != nil {
headerConfig, err := tcpSettings.HeaderSettings.GetInstance()
if err != nil {
2017-04-09 07:30:46 -04:00
return nil, newError("invalid header settings").Base(err).AtError()
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-09 07:30:46 -04:00
return nil, newError("invalid header settings.").Base(err).AtError()
2016-11-02 17:26:21 -04:00
}
l.authConfig = auth
}
2018-02-08 09:39:46 -05:00
go l.keepAccepting()
2016-06-14 16:54:08 -04:00
return l, nil
}
2018-02-08 09:39:46 -05:00
func (v *Listener) keepAccepting() {
2017-02-27 09:13:22 -05:00
for {
2018-02-08 09:39:46 -05:00
conn, err := v.listener.Accept()
2017-02-26 08:38:41 -05:00
if err != nil {
errStr := err.Error()
if strings.Contains(errStr, "closed") {
2018-02-08 09:39:46 -05:00
break
}
2017-12-19 15:28:12 -05:00
newError("failed to accepted raw connections").Base(err).AtWarning().WriteToLog()
if strings.Contains(errStr, "too many") {
time.Sleep(time.Millisecond * 500)
}
2017-02-26 08:38:41 -05:00
continue
}
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
}
2018-02-08 09:39:46 -05:00
v.addConn(internet.Connection(conn))
2016-06-14 16:54:08 -04:00
}
}
2018-01-02 12:57:22 -05:00
// Addr implements internet.Listener.Addr.
func (v *Listener) Addr() net.Addr {
2016-11-27 15:39:09 -05:00
return v.listener.Addr()
2016-06-14 16:54:08 -04:00
}
2018-01-02 12:57:22 -05:00
// Close implements internet.Listener.Close.
func (v *Listener) Close() error {
2020-10-29 03:30:38 -04:00
if v.locker != nil {
v.locker.Release()
}
2017-04-20 08:37:53 -04:00
return v.listener.Close()
2016-06-14 16:54:08 -04:00
}
func init() {
2018-08-06 07:48:35 -04:00
common.Must(internet.RegisterTransportListener(protocolName, ListenTCP))
2016-06-14 16:54:08 -04:00
}