1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-02 01:07:28 -04:00
v2fly/transport/internet/tcp/hub.go

164 lines
3.5 KiB
Go
Raw Normal View History

2016-06-14 16:54:08 -04:00
package tcp
import (
2016-09-30 10:53:40 -04:00
"crypto/tls"
2016-06-14 16:54:08 -04:00
"net"
"sync"
"time"
2017-01-03 08:53:59 -05:00
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-10-02 17:43:58 -04:00
"v2ray.com/core/common/log"
2016-08-20 14:55:45 -04:00
v2net "v2ray.com/core/common/net"
"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 ConnectionWithError struct {
conn net.Conn
err error
}
type TCPListener struct {
sync.Mutex
acccepting bool
listener *net.TCPListener
awaitingConns chan *ConnectionWithError
2016-09-30 10:53:40 -04:00
tlsConfig *tls.Config
2016-11-02 17:26:21 -04:00
authConfig internet.ConnectionAuthenticator
2016-10-02 17:43:58 -04:00
config *Config
2016-06-14 16:54:08 -04:00
}
2016-09-30 10:53:40 -04:00
func ListenTCP(address v2net.Address, port v2net.Port, options internet.ListenOptions) (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
}
2016-10-02 17:43:58 -04:00
networkSettings, err := options.Stream.GetEffectiveNetworkSettings()
if err != nil {
return nil, err
}
tcpSettings := networkSettings.(*Config)
2016-06-14 16:54:08 -04:00
l := &TCPListener{
acccepting: true,
listener: listener,
awaitingConns: make(chan *ConnectionWithError, 32),
2016-10-02 17:43:58 -04:00
config: tcpSettings,
2016-06-14 16:54:08 -04:00
}
2016-10-16 08:22:21 -04:00
if options.Stream != nil && options.Stream.HasSecuritySettings() {
2016-10-02 17:43:58 -04:00
securitySettings, err := options.Stream.GetEffectiveSecuritySettings()
if err != nil {
2016-10-16 08:22:21 -04:00
log.Error("TCP: Failed to get security config: ", err)
2016-10-02 17:43:58 -04:00
return nil, err
}
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 {
2016-12-04 03:43:33 -05:00
return nil, errors.Base(err).Message("Internet|TCP: Invalid header settings.")
2016-11-02 17:26:21 -04:00
}
auth, err := internet.CreateConnectionAuthenticator(tcpSettings.HeaderSettings.Type, headerConfig)
if err != nil {
2016-12-04 03:43:33 -05:00
return nil, errors.Base(err).Message("Internet|TCP: Invalid header settings.")
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) Accept() (internet.Connection, error) {
for v.acccepting {
2016-06-14 16:54:08 -04:00
select {
2016-11-27 15:39:09 -05:00
case connErr, open := <-v.awaitingConns:
2016-06-14 16:54:08 -04:00
if !open {
return nil, ErrClosedListener
}
if connErr.err != nil {
return nil, connErr.err
}
2016-09-30 10:53:40 -04:00
conn := connErr.conn
2017-01-01 16:12:44 -05:00
return NewConnection(internal.ConnectionID{}, conn, v, v.config), nil
2016-06-14 16:54:08 -04:00
case <-time.After(time.Second * 2):
}
}
return nil, ErrClosedListener
}
2016-11-27 15:39:09 -05:00
func (v *TCPListener) KeepAccepting() {
for v.acccepting {
conn, err := v.listener.Accept()
v.Lock()
if !v.acccepting {
v.Unlock()
2016-06-14 16:54:08 -04:00
break
}
2016-11-27 15:39:09 -05:00
if v.tlsConfig != nil {
conn = tls.Server(conn, v.tlsConfig)
2016-10-31 19:41:46 -04:00
}
2016-11-27 15:39:09 -05:00
if v.authConfig != nil {
conn = v.authConfig.Server(conn)
2016-11-02 17:26:21 -04:00
}
2016-06-14 16:54:08 -04:00
select {
2016-11-27 15:39:09 -05:00
case v.awaitingConns <- &ConnectionWithError{
2016-06-14 16:54:08 -04:00
conn: conn,
err: err,
}:
default:
if conn != nil {
conn.Close()
}
}
2016-11-27 15:39:09 -05:00
v.Unlock()
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-11-27 15:39:09 -05:00
v.Lock()
defer v.Unlock()
if !v.acccepting {
2016-06-14 16:54:08 -04:00
return
}
select {
2016-11-27 15:39:09 -05:00
case v.awaitingConns <- &ConnectionWithError{conn: conn}:
2016-06-14 16:54:08 -04:00
default:
conn.Close()
}
}
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.Lock()
defer v.Unlock()
v.acccepting = false
v.listener.Close()
close(v.awaitingConns)
for connErr := range v.awaitingConns {
2016-06-14 16:54:08 -04:00
if connErr.conn != nil {
go connErr.conn.Close()
}
}
return nil
}
func init() {
2017-01-03 09:16:48 -05:00
common.Must(internet.RegisterNetworkListener(v2net.Network_TCP, ListenTCP))
2016-06-14 16:54:08 -04:00
}