mirror of
				https://github.com/v2fly/v2ray-core.git
				synced 2025-11-03 19:29:22 +00:00 
			
		
		
		
	format code
This commit is contained in:
		
							parent
							
								
									4046ee968c
								
							
						
					
					
						commit
						9a88e8696a
					
				@ -9,7 +9,7 @@ import (
 | 
			
		||||
type SocksServerFactory struct {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (factory SocksServerFactory) Create(dispatcher app.PacketDispatcher, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) {
 | 
			
		||||
func (this SocksServerFactory) Create(dispatcher app.PacketDispatcher, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) {
 | 
			
		||||
	return NewSocksServer(dispatcher, rawConfig.(*json.SocksConfig)), nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -11,7 +11,7 @@ import (
 | 
			
		||||
 | 
			
		||||
var udpAddress v2net.Address
 | 
			
		||||
 | 
			
		||||
func (server *SocksServer) ListenUDP(port uint16) error {
 | 
			
		||||
func (this *SocksServer) ListenUDP(port uint16) error {
 | 
			
		||||
	addr := &net.UDPAddr{
 | 
			
		||||
		IP:   net.IP{0, 0, 0, 0},
 | 
			
		||||
		Port: int(port),
 | 
			
		||||
@ -22,17 +22,17 @@ func (server *SocksServer) ListenUDP(port uint16) error {
 | 
			
		||||
		log.Error("Socks failed to listen UDP on port %d: %v", port, err)
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	udpAddress = v2net.IPAddress(server.config.IP(), port)
 | 
			
		||||
	udpAddress = v2net.IPAddress(this.config.IP(), port)
 | 
			
		||||
 | 
			
		||||
	go server.AcceptPackets(conn)
 | 
			
		||||
	go this.AcceptPackets(conn)
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (server *SocksServer) getUDPAddr() v2net.Address {
 | 
			
		||||
func (this *SocksServer) getUDPAddr() v2net.Address {
 | 
			
		||||
	return udpAddress
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (server *SocksServer) AcceptPackets(conn *net.UDPConn) error {
 | 
			
		||||
func (this *SocksServer) AcceptPackets(conn *net.UDPConn) error {
 | 
			
		||||
	for {
 | 
			
		||||
		buffer := alloc.NewBuffer()
 | 
			
		||||
		nBytes, addr, err := conn.ReadFromUDP(buffer.Value)
 | 
			
		||||
@ -60,12 +60,12 @@ func (server *SocksServer) AcceptPackets(conn *net.UDPConn) error {
 | 
			
		||||
 | 
			
		||||
		udpPacket := v2net.NewPacket(request.Destination(), request.Data, false)
 | 
			
		||||
		log.Info("Send packet to %s with %d bytes", udpPacket.Destination().String(), request.Data.Len())
 | 
			
		||||
		go server.handlePacket(conn, udpPacket, addr, request.Address)
 | 
			
		||||
		go this.handlePacket(conn, udpPacket, addr, request.Address)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (server *SocksServer) handlePacket(conn *net.UDPConn, packet v2net.Packet, clientAddr *net.UDPAddr, targetAddr v2net.Address) {
 | 
			
		||||
	ray := server.dispatcher.DispatchToOutbound(packet)
 | 
			
		||||
func (this *SocksServer) handlePacket(conn *net.UDPConn, packet v2net.Packet, clientAddr *net.UDPAddr, targetAddr v2net.Address) {
 | 
			
		||||
	ray := this.dispatcher.DispatchToOutbound(packet)
 | 
			
		||||
	close(ray.InboundInput())
 | 
			
		||||
 | 
			
		||||
	for data := range ray.InboundOutput() {
 | 
			
		||||
 | 
			
		||||
@ -44,11 +44,11 @@ type VMessRequest struct {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Destination is the final destination of this request.
 | 
			
		||||
func (request *VMessRequest) Destination() v2net.Destination {
 | 
			
		||||
	if request.Command == CmdTCP {
 | 
			
		||||
		return v2net.NewTCPDestination(request.Address)
 | 
			
		||||
func (this *VMessRequest) Destination() v2net.Destination {
 | 
			
		||||
	if this.Command == CmdTCP {
 | 
			
		||||
		return v2net.NewTCPDestination(this.Address)
 | 
			
		||||
	} else {
 | 
			
		||||
		return v2net.NewUDPDestination(request.Address)
 | 
			
		||||
		return v2net.NewUDPDestination(this.Address)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -65,7 +65,7 @@ func NewVMessRequestReader(vUserSet user.UserSet) *VMessRequestReader {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Read reads a VMessRequest from a byte stream.
 | 
			
		||||
func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
 | 
			
		||||
func (this *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
 | 
			
		||||
	buffer := alloc.NewSmallBuffer()
 | 
			
		||||
 | 
			
		||||
	nBytes, err := v2net.ReadAllBytes(reader, buffer.Value[:config.IDBytesLen])
 | 
			
		||||
@ -73,7 +73,7 @@ func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	userObj, timeSec, valid := r.vUserSet.GetUser(buffer.Value[:nBytes])
 | 
			
		||||
	userObj, timeSec, valid := this.vUserSet.GetUser(buffer.Value[:nBytes])
 | 
			
		||||
	if !valid {
 | 
			
		||||
		return nil, proxyerrors.InvalidAuthentication
 | 
			
		||||
	}
 | 
			
		||||
@ -155,35 +155,35 @@ func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ToBytes returns a VMessRequest in the form of byte array.
 | 
			
		||||
func (request *VMessRequest) ToBytes(idHash user.CounterHash, randomRangeInt64 user.RandomInt64InRange, buffer *alloc.Buffer) (*alloc.Buffer, error) {
 | 
			
		||||
func (this *VMessRequest) ToBytes(idHash user.CounterHash, randomRangeInt64 user.RandomInt64InRange, buffer *alloc.Buffer) (*alloc.Buffer, error) {
 | 
			
		||||
	if buffer == nil {
 | 
			
		||||
		buffer = alloc.NewSmallBuffer().Clear()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	counter := randomRangeInt64(time.Now().Unix(), 30)
 | 
			
		||||
	hash := idHash.Hash(request.User.ID().Bytes[:], counter)
 | 
			
		||||
	hash := idHash.Hash(this.User.ID().Bytes[:], counter)
 | 
			
		||||
 | 
			
		||||
	buffer.Append(hash)
 | 
			
		||||
 | 
			
		||||
	encryptionBegin := buffer.Len()
 | 
			
		||||
 | 
			
		||||
	buffer.AppendBytes(request.Version)
 | 
			
		||||
	buffer.Append(request.RequestIV)
 | 
			
		||||
	buffer.Append(request.RequestKey)
 | 
			
		||||
	buffer.Append(request.ResponseHeader)
 | 
			
		||||
	buffer.AppendBytes(request.Command)
 | 
			
		||||
	buffer.Append(request.Address.PortBytes())
 | 
			
		||||
	buffer.AppendBytes(this.Version)
 | 
			
		||||
	buffer.Append(this.RequestIV)
 | 
			
		||||
	buffer.Append(this.RequestKey)
 | 
			
		||||
	buffer.Append(this.ResponseHeader)
 | 
			
		||||
	buffer.AppendBytes(this.Command)
 | 
			
		||||
	buffer.Append(this.Address.PortBytes())
 | 
			
		||||
 | 
			
		||||
	switch {
 | 
			
		||||
	case request.Address.IsIPv4():
 | 
			
		||||
	case this.Address.IsIPv4():
 | 
			
		||||
		buffer.AppendBytes(addrTypeIPv4)
 | 
			
		||||
		buffer.Append(request.Address.IP())
 | 
			
		||||
	case request.Address.IsIPv6():
 | 
			
		||||
		buffer.Append(this.Address.IP())
 | 
			
		||||
	case this.Address.IsIPv6():
 | 
			
		||||
		buffer.AppendBytes(addrTypeIPv6)
 | 
			
		||||
		buffer.Append(request.Address.IP())
 | 
			
		||||
	case request.Address.IsDomain():
 | 
			
		||||
		buffer.AppendBytes(addrTypeDomain, byte(len(request.Address.Domain())))
 | 
			
		||||
		buffer.Append([]byte(request.Address.Domain()))
 | 
			
		||||
		buffer.Append(this.Address.IP())
 | 
			
		||||
	case this.Address.IsDomain():
 | 
			
		||||
		buffer.AppendBytes(addrTypeDomain, byte(len(this.Address.Domain())))
 | 
			
		||||
		buffer.Append([]byte(this.Address.Domain()))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	encryptionEnd := buffer.Len()
 | 
			
		||||
@ -195,7 +195,7 @@ func (request *VMessRequest) ToBytes(idHash user.CounterHash, randomRangeInt64 u
 | 
			
		||||
	buffer.AppendBytes(byte(fnvHash>>24), byte(fnvHash>>16), byte(fnvHash>>8), byte(fnvHash))
 | 
			
		||||
	encryptionEnd += 4
 | 
			
		||||
 | 
			
		||||
	aesStream, err := v2crypto.NewAesEncryptionStream(request.User.ID().CmdKey(), user.Int64Hash(counter))
 | 
			
		||||
	aesStream, err := v2crypto.NewAesEncryptionStream(this.User.ID().CmdKey(), user.Int64Hash(counter))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@ -31,7 +31,7 @@ func NewVMessInboundHandler(dispatcher app.PacketDispatcher, clients user.UserSe
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (handler *VMessInboundHandler) Listen(port uint16) error {
 | 
			
		||||
func (this *VMessInboundHandler) Listen(port uint16) error {
 | 
			
		||||
	listener, err := net.ListenTCP("tcp", &net.TCPAddr{
 | 
			
		||||
		IP:   []byte{0, 0, 0, 0},
 | 
			
		||||
		Port: int(port),
 | 
			
		||||
@ -41,20 +41,20 @@ func (handler *VMessInboundHandler) Listen(port uint16) error {
 | 
			
		||||
		log.Error("Unable to listen tcp port %d: %v", port, err)
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	handler.accepting = true
 | 
			
		||||
	go handler.AcceptConnections(listener)
 | 
			
		||||
	this.accepting = true
 | 
			
		||||
	go this.AcceptConnections(listener)
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (handler *VMessInboundHandler) AcceptConnections(listener *net.TCPListener) error {
 | 
			
		||||
	for handler.accepting {
 | 
			
		||||
func (this *VMessInboundHandler) AcceptConnections(listener *net.TCPListener) error {
 | 
			
		||||
	for this.accepting {
 | 
			
		||||
		retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
 | 
			
		||||
			connection, err := listener.AcceptTCP()
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				log.Error("Failed to accpet connection: %s", err.Error())
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
			go handler.HandleConnection(connection)
 | 
			
		||||
			go this.HandleConnection(connection)
 | 
			
		||||
			return nil
 | 
			
		||||
		})
 | 
			
		||||
 | 
			
		||||
@ -62,11 +62,11 @@ func (handler *VMessInboundHandler) AcceptConnections(listener *net.TCPListener)
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (handler *VMessInboundHandler) HandleConnection(connection *net.TCPConn) error {
 | 
			
		||||
func (this *VMessInboundHandler) HandleConnection(connection *net.TCPConn) error {
 | 
			
		||||
	defer connection.Close()
 | 
			
		||||
 | 
			
		||||
	connReader := v2net.NewTimeOutReader(16, connection)
 | 
			
		||||
	requestReader := protocol.NewVMessRequestReader(handler.clients)
 | 
			
		||||
	requestReader := protocol.NewVMessRequestReader(this.clients)
 | 
			
		||||
 | 
			
		||||
	request, err := requestReader.Read(connReader)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
@ -77,7 +77,7 @@ func (handler *VMessInboundHandler) HandleConnection(connection *net.TCPConn) er
 | 
			
		||||
	log.Access(connection.RemoteAddr().String(), request.Address.String(), log.AccessAccepted, "")
 | 
			
		||||
	log.Debug("VMessIn: Received request for %s", request.Address.String())
 | 
			
		||||
 | 
			
		||||
	ray := handler.dispatcher.DispatchToOutbound(v2net.NewPacket(request.Destination(), nil, true))
 | 
			
		||||
	ray := this.dispatcher.DispatchToOutbound(v2net.NewPacket(request.Destination(), nil, true))
 | 
			
		||||
	input := ray.InboundInput()
 | 
			
		||||
	output := ray.InboundOutput()
 | 
			
		||||
	var readFinish, writeFinish sync.Mutex
 | 
			
		||||
@ -139,7 +139,7 @@ func handleOutput(request *protocol.VMessRequest, writer io.Writer, output <-cha
 | 
			
		||||
type VMessInboundHandlerFactory struct {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (factory *VMessInboundHandlerFactory) Create(dispatcher app.PacketDispatcher, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) {
 | 
			
		||||
func (this *VMessInboundHandlerFactory) Create(dispatcher app.PacketDispatcher, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) {
 | 
			
		||||
	config := rawConfig.(config.Inbound)
 | 
			
		||||
 | 
			
		||||
	allowedClients := user.NewTimedUserSet()
 | 
			
		||||
 | 
			
		||||
@ -52,8 +52,8 @@ func pickVNext(serverList []*config.OutboundTarget) (v2net.Destination, config.U
 | 
			
		||||
	return vNext.Destination, vNextUser
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (handler *VMessOutboundHandler) Dispatch(firstPacket v2net.Packet, ray ray.OutboundRay) error {
 | 
			
		||||
	vNextList := handler.vNextList
 | 
			
		||||
func (this *VMessOutboundHandler) Dispatch(firstPacket v2net.Packet, ray ray.OutboundRay) error {
 | 
			
		||||
	vNextList := this.vNextList
 | 
			
		||||
	vNextAddress, vNextUser := pickVNext(vNextList)
 | 
			
		||||
 | 
			
		||||
	command := protocol.CmdTCP
 | 
			
		||||
@ -187,7 +187,7 @@ func handleResponse(conn net.Conn, request *protocol.VMessRequest, output chan<-
 | 
			
		||||
type VMessOutboundHandlerFactory struct {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (factory *VMessOutboundHandlerFactory) Create(rawConfig interface{}) (connhandler.OutboundConnectionHandler, error) {
 | 
			
		||||
func (this *VMessOutboundHandlerFactory) Create(rawConfig interface{}) (connhandler.OutboundConnectionHandler, error) {
 | 
			
		||||
	vOutConfig := rawConfig.(config.Outbound)
 | 
			
		||||
	return NewVMessOutboundHandler(vOutConfig.Targets()), nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -21,18 +21,18 @@ type directRay struct {
 | 
			
		||||
	Output chan *alloc.Buffer
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (ray *directRay) OutboundInput() <-chan *alloc.Buffer {
 | 
			
		||||
	return ray.Input
 | 
			
		||||
func (this *directRay) OutboundInput() <-chan *alloc.Buffer {
 | 
			
		||||
	return this.Input
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (ray *directRay) OutboundOutput() chan<- *alloc.Buffer {
 | 
			
		||||
	return ray.Output
 | 
			
		||||
func (this *directRay) OutboundOutput() chan<- *alloc.Buffer {
 | 
			
		||||
	return this.Output
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (ray *directRay) InboundInput() chan<- *alloc.Buffer {
 | 
			
		||||
	return ray.Input
 | 
			
		||||
func (this *directRay) InboundInput() chan<- *alloc.Buffer {
 | 
			
		||||
	return this.Input
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (ray *directRay) InboundOutput() <-chan *alloc.Buffer {
 | 
			
		||||
	return ray.Output
 | 
			
		||||
func (this *directRay) InboundOutput() <-chan *alloc.Buffer {
 | 
			
		||||
	return this.Output
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user