mirror of
https://github.com/v2fly/v2ray-core.git
synced 2026-04-11 00:06:03 -04:00
integrate tls settings in ws
This commit is contained in:
@@ -1,14 +1,12 @@
|
||||
package internet
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"net"
|
||||
"sync"
|
||||
|
||||
"v2ray.com/core/common/log"
|
||||
v2net "v2ray.com/core/common/net"
|
||||
v2tls "v2ray.com/core/transport/internet/tls"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -20,7 +18,11 @@ var (
|
||||
WSListenFunc ListenFunc
|
||||
)
|
||||
|
||||
type ListenFunc func(address v2net.Address, port v2net.Port) (Listener, error)
|
||||
type ListenFunc func(address v2net.Address, port v2net.Port, options ListenOptions) (Listener, error)
|
||||
type ListenOptions struct {
|
||||
Stream *StreamSettings
|
||||
}
|
||||
|
||||
type Listener interface {
|
||||
Accept() (Connection, error)
|
||||
Close() error
|
||||
@@ -32,21 +34,23 @@ type TCPHub struct {
|
||||
listener Listener
|
||||
connCallback ConnectionHandler
|
||||
accepting bool
|
||||
tlsConfig *tls.Config
|
||||
}
|
||||
|
||||
func ListenTCP(address v2net.Address, port v2net.Port, callback ConnectionHandler, settings *StreamSettings) (*TCPHub, error) {
|
||||
var listener Listener
|
||||
var err error
|
||||
options := ListenOptions{
|
||||
Stream: settings,
|
||||
}
|
||||
switch {
|
||||
case settings.IsCapableOf(StreamConnectionTypeTCP):
|
||||
listener, err = TCPListenFunc(address, port)
|
||||
listener, err = TCPListenFunc(address, port, options)
|
||||
case settings.IsCapableOf(StreamConnectionTypeKCP):
|
||||
listener, err = KCPListenFunc(address, port)
|
||||
listener, err = KCPListenFunc(address, port, options)
|
||||
case settings.IsCapableOf(StreamConnectionTypeWebSocket):
|
||||
listener, err = WSListenFunc(address, port)
|
||||
listener, err = WSListenFunc(address, port, options)
|
||||
case settings.IsCapableOf(StreamConnectionTypeRawTCP):
|
||||
listener, err = RawTCPListenFunc(address, port)
|
||||
listener, err = RawTCPListenFunc(address, port, options)
|
||||
default:
|
||||
log.Error("Internet|Listener: Unknown stream type: ", settings.Type)
|
||||
err = ErrUnsupportedStreamType
|
||||
@@ -57,15 +61,9 @@ func ListenTCP(address v2net.Address, port v2net.Port, callback ConnectionHandle
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tlsConfig *tls.Config
|
||||
if settings.Security == StreamSecurityTypeTLS {
|
||||
tlsConfig = settings.TLSSettings.GetTLSConfig()
|
||||
}
|
||||
|
||||
hub := &TCPHub{
|
||||
listener: listener,
|
||||
connCallback: callback,
|
||||
tlsConfig: tlsConfig,
|
||||
}
|
||||
|
||||
go hub.start()
|
||||
@@ -88,10 +86,6 @@ func (this *TCPHub) start() {
|
||||
}
|
||||
continue
|
||||
}
|
||||
if this.tlsConfig != nil {
|
||||
tlsConn := tls.Server(conn, this.tlsConfig)
|
||||
conn = v2tls.NewConnection(tlsConn)
|
||||
}
|
||||
go this.connCallback(conn)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user