mirror of https://github.com/v2ray/v2ray-core
				
				
				
			Merge pull request #15 from cxjava/error
Add more error check and declare the field name in composite literal.pull/18/head
						commit
						1e90d25e76
					
				
							
								
								
									
										6
									
								
								id.go
								
								
								
								
							
							
						
						
									
										6
									
								
								id.go
								
								
								
								
							| 
						 | 
				
			
			@ -32,7 +32,11 @@ func NewID(id string) (ID, error) {
 | 
			
		|||
	md5hash.Write([]byte("c48619fe-8f02-49e0-b9e9-edf763e17e21"))
 | 
			
		||||
	cmdKey := md5.Sum(nil)
 | 
			
		||||
 | 
			
		||||
	return ID{id, idBytes, cmdKey[:]}, nil
 | 
			
		||||
	return ID{
 | 
			
		||||
		String: id,
 | 
			
		||||
		Bytes:  idBytes,
 | 
			
		||||
		cmdKey: cmdKey[:],
 | 
			
		||||
	}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (v ID) TimeRangeHash(rangeSec int) ([]byte, int64) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -125,7 +125,10 @@ type Socks5UserPassResponse struct {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
func NewSocks5UserPassResponse(status byte) Socks5UserPassResponse {
 | 
			
		||||
	return Socks5UserPassResponse{socksVersion, status}
 | 
			
		||||
	return Socks5UserPassResponse{
 | 
			
		||||
		version: socksVersion,
 | 
			
		||||
		status:  status,
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func WriteUserPassResponse(writer io.Writer, response Socks5UserPassResponse) error {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -20,18 +20,20 @@ type Address struct {
 | 
			
		|||
func IPAddress(ip []byte, port uint16) Address {
 | 
			
		||||
	// TODO: check IP length
 | 
			
		||||
	return Address{
 | 
			
		||||
		AddrTypeIP,
 | 
			
		||||
		net.IP(ip),
 | 
			
		||||
		"",
 | 
			
		||||
		port}
 | 
			
		||||
		Type:   AddrTypeIP,
 | 
			
		||||
		IP:     net.IP(ip),
 | 
			
		||||
		Domain: "",
 | 
			
		||||
		Port:   port,
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func DomainAddress(domain string, port uint16) Address {
 | 
			
		||||
	return Address{
 | 
			
		||||
		AddrTypeDomain,
 | 
			
		||||
		nil,
 | 
			
		||||
		domain,
 | 
			
		||||
		port}
 | 
			
		||||
		Type:   AddrTypeDomain,
 | 
			
		||||
		IP:     nil,
 | 
			
		||||
		Domain: domain,
 | 
			
		||||
		Port:   port,
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (addr Address) IsIPv4() bool {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -49,16 +49,14 @@ func (server *SocksServer) Listen(port uint16) error {
 | 
			
		|||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (server *SocksServer) AcceptConnections(listener net.Listener) error {
 | 
			
		||||
func (server *SocksServer) AcceptConnections(listener net.Listener) {
 | 
			
		||||
	for server.accepting {
 | 
			
		||||
		connection, err := listener.Accept()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Error("Error on accepting socks connection: %v", err)
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		go server.HandleConnection(connection)
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (server *SocksServer) HandleConnection(connection net.Conn) error {
 | 
			
		||||
| 
						 | 
				
			
			@ -79,15 +77,21 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error {
 | 
			
		|||
 | 
			
		||||
	if !auth.HasAuthMethod(expectedAuthMethod) {
 | 
			
		||||
		authResponse := socksio.NewAuthenticationResponse(socksio.AuthNoMatchingMethod)
 | 
			
		||||
		socksio.WriteAuthentication(connection, authResponse)
 | 
			
		||||
 | 
			
		||||
		err = socksio.WriteAuthentication(connection, authResponse)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Error("Error on socksio write authentication: %v", err)
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		log.Warning("Client doesn't support allowed any auth methods.")
 | 
			
		||||
		return ErrorAuthenticationFailed
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	authResponse := socksio.NewAuthenticationResponse(expectedAuthMethod)
 | 
			
		||||
	socksio.WriteAuthentication(connection, authResponse)
 | 
			
		||||
 | 
			
		||||
	err = socksio.WriteAuthentication(connection, authResponse)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Error("Error on socksio write authentication: %v", err)
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	if server.config.AuthMethod == JsonAuthMethodUserPass {
 | 
			
		||||
		upRequest, err := socksio.ReadUserPassRequest(reader)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
| 
						 | 
				
			
			@ -99,7 +103,11 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error {
 | 
			
		|||
			status = byte(0xFF)
 | 
			
		||||
		}
 | 
			
		||||
		upResponse := socksio.NewSocks5UserPassResponse(status)
 | 
			
		||||
		socksio.WriteUserPassResponse(connection, upResponse)
 | 
			
		||||
		err = socksio.WriteUserPassResponse(connection, upResponse)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Error("Error on socksio write user pass response: %v", err)
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		if status != byte(0) {
 | 
			
		||||
			return ErrorInvalidUser
 | 
			
		||||
		}
 | 
			
		||||
| 
						 | 
				
			
			@ -116,7 +124,11 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error {
 | 
			
		|||
	if request.Command == socksio.CmdBind || request.Command == socksio.CmdUdpAssociate {
 | 
			
		||||
		response := socksio.NewSocks5Response()
 | 
			
		||||
		response.Error = socksio.ErrorCommandNotSupported
 | 
			
		||||
		socksio.WriteResponse(connection, response)
 | 
			
		||||
		err = socksio.WriteResponse(connection, response)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Error("Error on socksio write response: %v", err)
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		log.Warning("Unsupported socks command %d", request.Command)
 | 
			
		||||
		return ErrorCommandNotSupported
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -132,8 +144,11 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error {
 | 
			
		|||
	case socksio.AddrTypeDomain:
 | 
			
		||||
		response.Domain = request.Domain
 | 
			
		||||
	}
 | 
			
		||||
	socksio.WriteResponse(connection, response)
 | 
			
		||||
 | 
			
		||||
	err = socksio.WriteResponse(connection, response)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Error("Error on socksio write response: %v", err)
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	ray := server.vPoint.NewInboundConnectionAccepted(request.Destination())
 | 
			
		||||
	input := ray.InboundInput()
 | 
			
		||||
	output := ray.InboundOutput()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -16,7 +16,10 @@ type VMessUser struct {
 | 
			
		|||
 | 
			
		||||
func (u *VMessUser) ToUser() (core.User, error) {
 | 
			
		||||
	id, err := core.NewID(u.Id)
 | 
			
		||||
	return core.User{id}, err
 | 
			
		||||
	return core.User{
 | 
			
		||||
		Id:    id,
 | 
			
		||||
		Email: "",
 | 
			
		||||
	}, err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type VMessInboundConfig struct {
 | 
			
		||||
| 
						 | 
				
			
			@ -49,8 +52,9 @@ func (config VNextConfig) ToVNextServer() VNextServer {
 | 
			
		|||
		panic(log.Error("Unable to parse VNext IP: %s", config.Address))
 | 
			
		||||
	}
 | 
			
		||||
	return VNextServer{
 | 
			
		||||
		v2net.IPAddress(ip, config.Port),
 | 
			
		||||
		users}
 | 
			
		||||
		Address: v2net.IPAddress(ip, config.Port),
 | 
			
		||||
		Users:   users,
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type VMessOutboundConfig struct {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue