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
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
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-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 {
|
|
|
|
return nil, errors.New("TCP: Failed to get header settings: " + err.Error())
|
|
|
|
}
|
|
|
|
auth, err := internet.CreateConnectionAuthenticator(tcpSettings.HeaderSettings.Type, headerConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("TCP: Failed to create header authenticator: " + err.Error())
|
|
|
|
}
|
|
|
|
l.authConfig = auth
|
|
|
|
}
|
2016-06-14 16:54:08 -04:00
|
|
|
go l.KeepAccepting()
|
|
|
|
return l, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *TCPListener) Accept() (internet.Connection, error) {
|
|
|
|
for this.acccepting {
|
|
|
|
select {
|
|
|
|
case connErr, open := <-this.awaitingConns:
|
|
|
|
if !open {
|
|
|
|
return nil, ErrClosedListener
|
|
|
|
}
|
|
|
|
if connErr.err != nil {
|
|
|
|
return nil, connErr.err
|
|
|
|
}
|
2016-09-30 10:53:40 -04:00
|
|
|
conn := connErr.conn
|
2016-10-02 17:43:58 -04:00
|
|
|
return NewConnection("", conn, this, this.config), nil
|
2016-06-14 16:54:08 -04:00
|
|
|
case <-time.After(time.Second * 2):
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, ErrClosedListener
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *TCPListener) KeepAccepting() {
|
|
|
|
for this.acccepting {
|
|
|
|
conn, err := this.listener.Accept()
|
|
|
|
this.Lock()
|
|
|
|
if !this.acccepting {
|
|
|
|
this.Unlock()
|
|
|
|
break
|
|
|
|
}
|
2016-10-31 19:41:46 -04:00
|
|
|
if this.tlsConfig != nil {
|
|
|
|
conn = tls.Server(conn, this.tlsConfig)
|
|
|
|
}
|
2016-11-02 17:26:21 -04:00
|
|
|
if this.authConfig != nil {
|
|
|
|
conn = this.authConfig.Server(conn)
|
|
|
|
}
|
2016-06-14 16:54:08 -04:00
|
|
|
select {
|
|
|
|
case this.awaitingConns <- &ConnectionWithError{
|
|
|
|
conn: conn,
|
|
|
|
err: err,
|
|
|
|
}:
|
|
|
|
default:
|
|
|
|
if conn != nil {
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *TCPListener) Recycle(dest string, conn net.Conn) {
|
|
|
|
this.Lock()
|
|
|
|
defer this.Unlock()
|
|
|
|
if !this.acccepting {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case this.awaitingConns <- &ConnectionWithError{conn: conn}:
|
|
|
|
default:
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *TCPListener) Addr() net.Addr {
|
|
|
|
return this.listener.Addr()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *TCPListener) Close() error {
|
|
|
|
this.Lock()
|
|
|
|
defer this.Unlock()
|
|
|
|
this.acccepting = false
|
|
|
|
this.listener.Close()
|
|
|
|
close(this.awaitingConns)
|
|
|
|
for connErr := range this.awaitingConns {
|
|
|
|
if connErr.conn != nil {
|
|
|
|
go connErr.conn.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type RawTCPListener struct {
|
|
|
|
accepting bool
|
|
|
|
listener *net.TCPListener
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *RawTCPListener) Accept() (internet.Connection, error) {
|
|
|
|
conn, err := this.listener.AcceptTCP()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &RawConnection{
|
|
|
|
TCPConn: *conn,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *RawTCPListener) Addr() net.Addr {
|
|
|
|
return this.listener.Addr()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *RawTCPListener) Close() error {
|
|
|
|
this.accepting = false
|
|
|
|
this.listener.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-30 10:53:40 -04:00
|
|
|
func ListenRawTCP(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-09-30 10:53:40 -04:00
|
|
|
// TODO: handle listen options
|
2016-06-14 16:54:08 -04:00
|
|
|
return &RawTCPListener{
|
|
|
|
accepting: true,
|
|
|
|
listener: listener,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
internet.TCPListenFunc = ListenTCP
|
|
|
|
internet.RawTCPListenFunc = ListenRawTCP
|
|
|
|
}
|