|  | @@ -3,7 +3,6 @@ package inbound
 | 
											
												
													
														|  |  import (
 |  |  import (
 | 
											
												
													
														|  |  	"crypto/md5"
 |  |  	"crypto/md5"
 | 
											
												
													
														|  |  	"io"
 |  |  	"io"
 | 
											
												
													
														|  | -	"net"
 |  | 
 | 
											
												
													
														|  |  	"sync"
 |  |  	"sync"
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  	"github.com/v2ray/v2ray-core/app"
 |  |  	"github.com/v2ray/v2ray-core/app"
 | 
											
										
											
												
													
														|  | @@ -11,12 +10,12 @@ import (
 | 
											
												
													
														|  |  	v2crypto "github.com/v2ray/v2ray-core/common/crypto"
 |  |  	v2crypto "github.com/v2ray/v2ray-core/common/crypto"
 | 
											
												
													
														|  |  	"github.com/v2ray/v2ray-core/common/log"
 |  |  	"github.com/v2ray/v2ray-core/common/log"
 | 
											
												
													
														|  |  	v2net "github.com/v2ray/v2ray-core/common/net"
 |  |  	v2net "github.com/v2ray/v2ray-core/common/net"
 | 
											
												
													
														|  | -	"github.com/v2ray/v2ray-core/common/retry"
 |  | 
 | 
											
												
													
														|  |  	"github.com/v2ray/v2ray-core/common/serial"
 |  |  	"github.com/v2ray/v2ray-core/common/serial"
 | 
											
												
													
														|  |  	"github.com/v2ray/v2ray-core/proxy"
 |  |  	"github.com/v2ray/v2ray-core/proxy"
 | 
											
												
													
														|  |  	"github.com/v2ray/v2ray-core/proxy/internal"
 |  |  	"github.com/v2ray/v2ray-core/proxy/internal"
 | 
											
												
													
														|  |  	"github.com/v2ray/v2ray-core/proxy/vmess"
 |  |  	"github.com/v2ray/v2ray-core/proxy/vmess"
 | 
											
												
													
														|  |  	"github.com/v2ray/v2ray-core/proxy/vmess/protocol"
 |  |  	"github.com/v2ray/v2ray-core/proxy/vmess/protocol"
 | 
											
												
													
														|  | 
 |  | +	"github.com/v2ray/v2ray-core/transport/listener"
 | 
											
												
													
														|  |  )
 |  |  )
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  // Inbound connection handler that handles messages in VMess format.
 |  |  // Inbound connection handler that handles messages in VMess format.
 | 
											
										
											
												
													
														|  | @@ -26,7 +25,7 @@ type VMessInboundHandler struct {
 | 
											
												
													
														|  |  	clients       protocol.UserSet
 |  |  	clients       protocol.UserSet
 | 
											
												
													
														|  |  	user          *vmess.User
 |  |  	user          *vmess.User
 | 
											
												
													
														|  |  	accepting     bool
 |  |  	accepting     bool
 | 
											
												
													
														|  | -	listener      *net.TCPListener
 |  | 
 | 
											
												
													
														|  | 
 |  | +	listener      *listener.TCPListener
 | 
											
												
													
														|  |  	features      *FeaturesConfig
 |  |  	features      *FeaturesConfig
 | 
											
												
													
														|  |  	listeningPort v2net.Port
 |  |  	listeningPort v2net.Port
 | 
											
												
													
														|  |  }
 |  |  }
 | 
											
										
											
												
													
														|  | @@ -38,8 +37,8 @@ func (this *VMessInboundHandler) Port() v2net.Port {
 | 
											
												
													
														|  |  func (this *VMessInboundHandler) Close() {
 |  |  func (this *VMessInboundHandler) Close() {
 | 
											
												
													
														|  |  	this.accepting = false
 |  |  	this.accepting = false
 | 
											
												
													
														|  |  	if this.listener != nil {
 |  |  	if this.listener != nil {
 | 
											
												
													
														|  | -		this.listener.Close()
 |  | 
 | 
											
												
													
														|  |  		this.Lock()
 |  |  		this.Lock()
 | 
											
												
													
														|  | 
 |  | +		this.listener.Close()
 | 
											
												
													
														|  |  		this.listener = nil
 |  |  		this.listener = nil
 | 
											
												
													
														|  |  		this.Unlock()
 |  |  		this.Unlock()
 | 
											
												
													
														|  |  	}
 |  |  	}
 | 
											
										
											
												
													
														|  | @@ -59,45 +58,19 @@ func (this *VMessInboundHandler) Listen(port v2net.Port) error {
 | 
											
												
													
														|  |  	}
 |  |  	}
 | 
											
												
													
														|  |  	this.listeningPort = port
 |  |  	this.listeningPort = port
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -	listener, err := net.ListenTCP("tcp", &net.TCPAddr{
 |  | 
 | 
											
												
													
														|  | -		IP:   []byte{0, 0, 0, 0},
 |  | 
 | 
											
												
													
														|  | -		Port: int(port),
 |  | 
 | 
											
												
													
														|  | -		Zone: "",
 |  | 
 | 
											
												
													
														|  | -	})
 |  | 
 | 
											
												
													
														|  | 
 |  | +	tcpListener, err := listener.ListenTCP(port, this.HandleConnection)
 | 
											
												
													
														|  |  	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
 | 
											
												
													
														|  |  	}
 |  |  	}
 | 
											
												
													
														|  |  	this.accepting = true
 |  |  	this.accepting = true
 | 
											
												
													
														|  |  	this.Lock()
 |  |  	this.Lock()
 | 
											
												
													
														|  | -	this.listener = listener
 |  | 
 | 
											
												
													
														|  | 
 |  | +	this.listener = tcpListener
 | 
											
												
													
														|  |  	this.Unlock()
 |  |  	this.Unlock()
 | 
											
												
													
														|  | -	go this.AcceptConnections()
 |  | 
 | 
											
												
													
														|  |  	return nil
 |  |  	return nil
 | 
											
												
													
														|  |  }
 |  |  }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -func (this *VMessInboundHandler) AcceptConnections() error {
 |  | 
 | 
											
												
													
														|  | -	for this.accepting {
 |  | 
 | 
											
												
													
														|  | -		retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
 |  | 
 | 
											
												
													
														|  | -			this.Lock()
 |  | 
 | 
											
												
													
														|  | -			defer this.Unlock()
 |  | 
 | 
											
												
													
														|  | -			if !this.accepting {
 |  | 
 | 
											
												
													
														|  | -				return nil
 |  | 
 | 
											
												
													
														|  | -			}
 |  | 
 | 
											
												
													
														|  | -			connection, err := this.listener.AcceptTCP()
 |  | 
 | 
											
												
													
														|  | -			if err != nil {
 |  | 
 | 
											
												
													
														|  | -				log.Error("Failed to accpet connection: ", err)
 |  | 
 | 
											
												
													
														|  | -				return err
 |  | 
 | 
											
												
													
														|  | -			}
 |  | 
 | 
											
												
													
														|  | -			go this.HandleConnection(connection)
 |  | 
 | 
											
												
													
														|  | -			return nil
 |  | 
 | 
											
												
													
														|  | -		})
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -	}
 |  | 
 | 
											
												
													
														|  | -	return nil
 |  | 
 | 
											
												
													
														|  | -}
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -func (this *VMessInboundHandler) HandleConnection(connection *net.TCPConn) error {
 |  | 
 | 
											
												
													
														|  | 
 |  | +func (this *VMessInboundHandler) HandleConnection(connection *listener.TCPConn) {
 | 
											
												
													
														|  |  	defer connection.Close()
 |  |  	defer connection.Close()
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  	connReader := v2net.NewTimeOutReader(16, connection)
 |  |  	connReader := v2net.NewTimeOutReader(16, connection)
 | 
											
										
											
												
													
														|  | @@ -107,7 +80,7 @@ func (this *VMessInboundHandler) HandleConnection(connection *net.TCPConn) error
 | 
											
												
													
														|  |  	if err != nil {
 |  |  	if err != nil {
 | 
											
												
													
														|  |  		log.Access(connection.RemoteAddr(), serial.StringLiteral(""), log.AccessRejected, serial.StringLiteral(err.Error()))
 |  |  		log.Access(connection.RemoteAddr(), serial.StringLiteral(""), log.AccessRejected, serial.StringLiteral(err.Error()))
 | 
											
												
													
														|  |  		log.Warning("VMessIn: Invalid request from ", connection.RemoteAddr(), ": ", err)
 |  |  		log.Warning("VMessIn: Invalid request from ", connection.RemoteAddr(), ": ", err)
 | 
											
												
													
														|  | -		return err
 |  | 
 | 
											
												
													
														|  | 
 |  | +		return
 | 
											
												
													
														|  |  	}
 |  |  	}
 | 
											
												
													
														|  |  	log.Access(connection.RemoteAddr(), request.Address, log.AccessAccepted, serial.StringLiteral(""))
 |  |  	log.Access(connection.RemoteAddr(), request.Address, log.AccessAccepted, serial.StringLiteral(""))
 | 
											
												
													
														|  |  	log.Debug("VMessIn: Received request for ", request.Address)
 |  |  	log.Debug("VMessIn: Received request for ", request.Address)
 | 
											
										
											
												
													
														|  | @@ -130,7 +103,7 @@ func (this *VMessInboundHandler) HandleConnection(connection *net.TCPConn) error
 | 
											
												
													
														|  |  	if err != nil {
 |  |  	if err != nil {
 | 
											
												
													
														|  |  		log.Error("VMessIn: Failed to create AES decryption stream: ", err)
 |  |  		log.Error("VMessIn: Failed to create AES decryption stream: ", err)
 | 
											
												
													
														|  |  		close(input)
 |  |  		close(input)
 | 
											
												
													
														|  | -		return err
 |  | 
 | 
											
												
													
														|  | 
 |  | +		return
 | 
											
												
													
														|  |  	}
 |  |  	}
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  	responseWriter := v2crypto.NewCryptionWriter(aesStream, connection)
 |  |  	responseWriter := v2crypto.NewCryptionWriter(aesStream, connection)
 | 
											
										
											
												
													
														|  | @@ -151,8 +124,6 @@ func (this *VMessInboundHandler) HandleConnection(connection *net.TCPConn) error
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  	connection.CloseWrite()
 |  |  	connection.CloseWrite()
 | 
											
												
													
														|  |  	readFinish.Lock()
 |  |  	readFinish.Lock()
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -	return nil
 |  | 
 | 
											
												
													
														|  |  }
 |  |  }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  func handleInput(request *protocol.VMessRequest, reader io.Reader, input chan<- *alloc.Buffer, finish *sync.Mutex) {
 |  |  func handleInput(request *protocol.VMessRequest, reader io.Reader, input chan<- *alloc.Buffer, finish *sync.Mutex) {
 |