1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-02 15:36:41 -05:00

tcphub now takes tls config as construtor parameter

This commit is contained in:
v2ray 2016-05-03 00:16:07 +02:00
parent f594f5b606
commit 7d43952690
6 changed files with 24 additions and 13 deletions

View File

@ -107,7 +107,7 @@ func (this *DokodemoDoor) handleUDPPackets(payload *alloc.Buffer, dest v2net.Des
} }
func (this *DokodemoDoor) ListenTCP(port v2net.Port) error { func (this *DokodemoDoor) ListenTCP(port v2net.Port) error {
tcpListener, err := hub.ListenTCP(port, this.HandleTCPConnection) tcpListener, err := hub.ListenTCP(port, this.HandleTCPConnection, nil)
if err != nil { if err != nil {
log.Error("Dokodemo: Failed to listen on port ", port, ": ", err) log.Error("Dokodemo: Failed to listen on port ", port, ": ", err)
return err return err

View File

@ -60,7 +60,7 @@ func (this *HttpProxyServer) Listen(port v2net.Port) error {
} }
this.listeningPort = port this.listeningPort = port
tcpListener, err := hub.ListenTCP(port, this.handleConnection) tcpListener, err := hub.ListenTCP(port, this.handleConnection, nil)
if err != nil { if err != nil {
log.Error("Http: Failed listen on port ", port, ": ", err) log.Error("Http: Failed listen on port ", port, ": ", err)
return err return err

View File

@ -65,7 +65,7 @@ func (this *Shadowsocks) Listen(port v2net.Port) error {
} }
} }
tcpHub, err := hub.ListenTCP(port, this.handleConnection) tcpHub, err := hub.ListenTCP(port, this.handleConnection, nil)
if err != nil { if err != nil {
log.Error("Shadowsocks: Failed to listen TCP on port ", port, ": ", err) log.Error("Shadowsocks: Failed to listen TCP on port ", port, ": ", err)
return err return err

View File

@ -77,7 +77,7 @@ func (this *Server) Listen(port v2net.Port) error {
} }
this.listeningPort = port this.listeningPort = port
listener, err := hub.ListenTCP(port, this.handleConnection) listener, err := hub.ListenTCP(port, this.handleConnection, nil)
if err != nil { if err != nil {
log.Error("Socks: failed to listen on port ", port, ": ", err) log.Error("Socks: failed to listen on port ", port, ": ", err)
return err return err

View File

@ -103,7 +103,7 @@ func (this *VMessInboundHandler) Listen(port v2net.Port) error {
} }
this.listeningPort = port this.listeningPort = port
tcpListener, err := hub.ListenTCP(port, this.HandleConnection) tcpListener, err := hub.ListenTCP(port, this.HandleConnection, nil)
if err != nil { if err != nil {
log.Error("Unable to listen tcp port ", port, ": ", err) log.Error("Unable to listen tcp port ", port, ": ", err)
return err return err

View File

@ -1,6 +1,7 @@
package hub package hub
import ( import (
"crypto/tls"
"errors" "errors"
"net" "net"
@ -13,12 +14,12 @@ var (
) )
type TCPHub struct { type TCPHub struct {
listener *net.TCPListener listener net.Listener
connCallback ConnectionHandler connCallback ConnectionHandler
accepting bool accepting bool
} }
func ListenTCP(port v2net.Port, callback ConnectionHandler) (*TCPHub, error) { func ListenTCP(port v2net.Port, callback ConnectionHandler, tlsConfig *tls.Config) (*TCPHub, error) {
listener, err := net.ListenTCP("tcp", &net.TCPAddr{ listener, err := net.ListenTCP("tcp", &net.TCPAddr{
IP: []byte{0, 0, 0, 0}, IP: []byte{0, 0, 0, 0},
Port: int(port), Port: int(port),
@ -27,12 +28,22 @@ func ListenTCP(port v2net.Port, callback ConnectionHandler) (*TCPHub, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
tcpListener := &TCPHub{ var hub *TCPHub
if tlsConfig != nil {
tlsListener := tls.NewListener(listener, tlsConfig)
hub = &TCPHub{
listener: tlsListener,
connCallback: callback,
}
} else {
hub = &TCPHub{
listener: listener, listener: listener,
connCallback: callback, connCallback: callback,
} }
go tcpListener.start() }
return tcpListener, nil
go hub.start()
return hub, nil
} }
func (this *TCPHub) Close() { func (this *TCPHub) Close() {
@ -44,7 +55,7 @@ func (this *TCPHub) Close() {
func (this *TCPHub) start() { func (this *TCPHub) start() {
this.accepting = true this.accepting = true
for this.accepting { for this.accepting {
conn, err := this.listener.AcceptTCP() conn, err := this.listener.Accept()
if err != nil { if err != nil {
if this.accepting { if this.accepting {
log.Warning("Listener: Failed to accept new TCP connection: ", err) log.Warning("Listener: Failed to accept new TCP connection: ", err)