mirror of https://github.com/v2ray/v2ray-core
				
				
				
			cleanup socks log
							parent
							
								
									0bfe78dd6e
								
							
						
					
					
						commit
						8f20933457
					
				| 
						 | 
					@ -49,7 +49,7 @@ func ReadAuthentication(reader io.Reader) (auth Socks5AuthenticationRequest, aut
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if nBytes < 2 {
 | 
						if nBytes < 2 {
 | 
				
			||||||
		log.Info("Socks expected 2 bytes read, but only %d bytes read", nBytes)
 | 
							log.Info("Socks: expected 2 bytes read, but only %d bytes read", nBytes)
 | 
				
			||||||
		err = transport.CorruptedPacket
 | 
							err = transport.CorruptedPacket
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					@ -65,20 +65,20 @@ func ReadAuthentication(reader io.Reader) (auth Socks5AuthenticationRequest, aut
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	auth.version = buffer.Value[0]
 | 
						auth.version = buffer.Value[0]
 | 
				
			||||||
	if auth.version != socksVersion {
 | 
						if auth.version != socksVersion {
 | 
				
			||||||
		log.Warning("Unknown protocol version %d", auth.version)
 | 
							log.Warning("Socks: Unknown protocol version %d", auth.version)
 | 
				
			||||||
		err = proxy.InvalidProtocolVersion
 | 
							err = proxy.InvalidProtocolVersion
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	auth.nMethods = buffer.Value[1]
 | 
						auth.nMethods = buffer.Value[1]
 | 
				
			||||||
	if auth.nMethods <= 0 {
 | 
						if auth.nMethods <= 0 {
 | 
				
			||||||
		log.Info("Zero length of authentication methods")
 | 
							log.Info("Socks: Zero length of authentication methods")
 | 
				
			||||||
		err = transport.CorruptedPacket
 | 
							err = transport.CorruptedPacket
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if nBytes-2 != int(auth.nMethods) {
 | 
						if nBytes-2 != int(auth.nMethods) {
 | 
				
			||||||
		log.Info("Unmatching number of auth methods, expecting %d, but got %d", auth.nMethods, nBytes)
 | 
							log.Info("Socks: Unmatching number of auth methods, expecting %d, but got %d", auth.nMethods, nBytes)
 | 
				
			||||||
		err = transport.CorruptedPacket
 | 
							err = transport.CorruptedPacket
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					@ -227,7 +227,7 @@ func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if nBytes != int(domainLength) {
 | 
							if nBytes != int(domainLength) {
 | 
				
			||||||
			log.Info("Unable to read domain with %d bytes, expecting %d bytes", nBytes, domainLength)
 | 
								log.Info("Socks: Unable to read domain with %d bytes, expecting %d bytes", nBytes, domainLength)
 | 
				
			||||||
			err = transport.CorruptedPacket
 | 
								err = transport.CorruptedPacket
 | 
				
			||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
| 
						 | 
					@ -242,7 +242,7 @@ func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
 | 
				
			||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	default:
 | 
						default:
 | 
				
			||||||
		log.Info("Unexpected address type %d", request.AddrType)
 | 
							log.Info("Socks: Unexpected address type %d", request.AddrType)
 | 
				
			||||||
		err = transport.CorruptedPacket
 | 
							err = transport.CorruptedPacket
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -63,7 +63,7 @@ func (this *SocksServer) Listen(port v2net.Port) error {
 | 
				
			||||||
		Zone: "",
 | 
							Zone: "",
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Error("Socks failed to listen on port %d: %v", port, err)
 | 
							log.Error("Socks: failed to listen on port %d: %v", port, err)
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	this.accepting = true
 | 
						this.accepting = true
 | 
				
			||||||
| 
						 | 
					@ -87,7 +87,7 @@ func (this *SocksServer) AcceptConnections() {
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			connection, err := this.tcpListener.AcceptTCP()
 | 
								connection, err := this.tcpListener.AcceptTCP()
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				log.Error("Socks failed to accept new connection %v", err)
 | 
									log.Error("Socks: failed to accept new connection %v", err)
 | 
				
			||||||
				return err
 | 
									return err
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			go this.HandleConnection(connection)
 | 
								go this.HandleConnection(connection)
 | 
				
			||||||
| 
						 | 
					@ -103,7 +103,7 @@ func (this *SocksServer) HandleConnection(connection *net.TCPConn) error {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	auth, auth4, err := protocol.ReadAuthentication(reader)
 | 
						auth, auth4, err := protocol.ReadAuthentication(reader)
 | 
				
			||||||
	if err != nil && err != protocol.Socks4Downgrade {
 | 
						if err != nil && err != protocol.Socks4Downgrade {
 | 
				
			||||||
		log.Error("Socks failed to read authentication: %v", err)
 | 
							log.Error("Socks: failed to read authentication: %v", err)
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -124,23 +124,23 @@ func (this *SocksServer) handleSocks5(reader *v2net.TimeOutReader, writer io.Wri
 | 
				
			||||||
		authResponse := protocol.NewAuthenticationResponse(protocol.AuthNoMatchingMethod)
 | 
							authResponse := protocol.NewAuthenticationResponse(protocol.AuthNoMatchingMethod)
 | 
				
			||||||
		err := protocol.WriteAuthentication(writer, authResponse)
 | 
							err := protocol.WriteAuthentication(writer, authResponse)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			log.Error("Socks failed to write authentication: %v", err)
 | 
								log.Error("Socks: failed to write authentication: %v", err)
 | 
				
			||||||
			return err
 | 
								return err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		log.Warning("Socks client doesn't support allowed any auth methods.")
 | 
							log.Warning("Socks: client doesn't support allowed any auth methods.")
 | 
				
			||||||
		return UnsupportedAuthMethod
 | 
							return UnsupportedAuthMethod
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	authResponse := protocol.NewAuthenticationResponse(expectedAuthMethod)
 | 
						authResponse := protocol.NewAuthenticationResponse(expectedAuthMethod)
 | 
				
			||||||
	err := protocol.WriteAuthentication(writer, authResponse)
 | 
						err := protocol.WriteAuthentication(writer, authResponse)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Error("Socks failed to write authentication: %v", err)
 | 
							log.Error("Socks: failed to write authentication: %v", err)
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if this.config.AuthType == AuthTypePassword {
 | 
						if this.config.AuthType == AuthTypePassword {
 | 
				
			||||||
		upRequest, err := protocol.ReadUserPassRequest(reader)
 | 
							upRequest, err := protocol.ReadUserPassRequest(reader)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			log.Error("Socks failed to read username and password: %v", err)
 | 
								log.Error("Socks: failed to read username and password: %v", err)
 | 
				
			||||||
			return err
 | 
								return err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		status := byte(0)
 | 
							status := byte(0)
 | 
				
			||||||
| 
						 | 
					@ -150,18 +150,18 @@ func (this *SocksServer) handleSocks5(reader *v2net.TimeOutReader, writer io.Wri
 | 
				
			||||||
		upResponse := protocol.NewSocks5UserPassResponse(status)
 | 
							upResponse := protocol.NewSocks5UserPassResponse(status)
 | 
				
			||||||
		err = protocol.WriteUserPassResponse(writer, upResponse)
 | 
							err = protocol.WriteUserPassResponse(writer, upResponse)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			log.Error("Socks failed to write user pass response: %v", err)
 | 
								log.Error("Socks: failed to write user pass response: %v", err)
 | 
				
			||||||
			return err
 | 
								return err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if status != byte(0) {
 | 
							if status != byte(0) {
 | 
				
			||||||
			log.Warning("Invalid user account: %s", upRequest.AuthDetail())
 | 
								log.Warning("Socks: Invalid user account: %s", upRequest.AuthDetail())
 | 
				
			||||||
			return proxy.InvalidAuthentication
 | 
								return proxy.InvalidAuthentication
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	request, err := protocol.ReadRequest(reader)
 | 
						request, err := protocol.ReadRequest(reader)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Error("Socks failed to read request: %v", err)
 | 
							log.Error("Socks: failed to read request: %v", err)
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -180,10 +180,10 @@ func (this *SocksServer) handleSocks5(reader *v2net.TimeOutReader, writer io.Wri
 | 
				
			||||||
		_, err = writer.Write(responseBuffer.Value)
 | 
							_, err = writer.Write(responseBuffer.Value)
 | 
				
			||||||
		responseBuffer.Release()
 | 
							responseBuffer.Release()
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			log.Error("Socks failed to write response: %v", err)
 | 
								log.Error("Socks: failed to write response: %v", err)
 | 
				
			||||||
			return err
 | 
								return err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		log.Warning("Unsupported socks command %d", request.Command)
 | 
							log.Warning("Socks: Unsupported socks command %d", request.Command)
 | 
				
			||||||
		return UnsupportedSocksCommand
 | 
							return UnsupportedSocksCommand
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -199,12 +199,12 @@ func (this *SocksServer) handleSocks5(reader *v2net.TimeOutReader, writer io.Wri
 | 
				
			||||||
	_, err = writer.Write(responseBuffer.Value)
 | 
						_, err = writer.Write(responseBuffer.Value)
 | 
				
			||||||
	responseBuffer.Release()
 | 
						responseBuffer.Release()
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Error("Socks failed to write response: %v", err)
 | 
							log.Error("Socks: failed to write response: %v", err)
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	dest := request.Destination()
 | 
						dest := request.Destination()
 | 
				
			||||||
	log.Info("TCP Connect request to %s", dest.String())
 | 
						log.Info("Socks: TCP Connect request to %s", dest.String())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	packet := v2net.NewPacket(dest, nil, true)
 | 
						packet := v2net.NewPacket(dest, nil, true)
 | 
				
			||||||
	this.transport(reader, writer, packet)
 | 
						this.transport(reader, writer, packet)
 | 
				
			||||||
| 
						 | 
					@ -233,7 +233,7 @@ func (this *SocksServer) handleUDP(reader *v2net.TimeOutReader, writer io.Writer
 | 
				
			||||||
	responseBuffer.Release()
 | 
						responseBuffer.Release()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Error("Socks failed to write response: %v", err)
 | 
							log.Error("Socks: failed to write response: %v", err)
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -260,7 +260,7 @@ func (this *SocksServer) handleSocks4(reader io.Reader, writer io.Writer, auth p
 | 
				
			||||||
	responseBuffer.Release()
 | 
						responseBuffer.Release()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if result == protocol.Socks4RequestRejected {
 | 
						if result == protocol.Socks4RequestRejected {
 | 
				
			||||||
		log.Warning("Unsupported socks 4 command %d", auth.Command)
 | 
							log.Warning("Socks: Unsupported socks 4 command %d", auth.Command)
 | 
				
			||||||
		return UnsupportedSocksCommand
 | 
							return UnsupportedSocksCommand
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -17,7 +17,7 @@ func (this *SocksServer) ListenUDP(port v2net.Port) error {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	conn, err := net.ListenUDP("udp", addr)
 | 
						conn, err := net.ListenUDP("udp", addr)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Error("Socks failed to listen UDP on port %d: %v", port, err)
 | 
							log.Error("Socks: failed to listen UDP on port %d: %v", port, err)
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	this.udpMutex.Lock()
 | 
						this.udpMutex.Lock()
 | 
				
			||||||
| 
						 | 
					@ -40,29 +40,29 @@ func (this *SocksServer) AcceptPackets() error {
 | 
				
			||||||
		nBytes, addr, err := this.udpConn.ReadFromUDP(buffer.Value)
 | 
							nBytes, addr, err := this.udpConn.ReadFromUDP(buffer.Value)
 | 
				
			||||||
		this.udpMutex.RUnlock()
 | 
							this.udpMutex.RUnlock()
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			log.Error("Socks failed to read UDP packets: %v", err)
 | 
								log.Error("Socks: failed to read UDP packets: %v", err)
 | 
				
			||||||
			buffer.Release()
 | 
								buffer.Release()
 | 
				
			||||||
			continue
 | 
								continue
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		log.Info("Client UDP connection from %v", addr)
 | 
							log.Info("Socks: Client UDP connection from %v", addr)
 | 
				
			||||||
		request, err := protocol.ReadUDPRequest(buffer.Value[:nBytes])
 | 
							request, err := protocol.ReadUDPRequest(buffer.Value[:nBytes])
 | 
				
			||||||
		buffer.Release()
 | 
							buffer.Release()
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			log.Error("Socks failed to parse UDP request: %v", err)
 | 
								log.Error("Socks: failed to parse UDP request: %v", err)
 | 
				
			||||||
			continue
 | 
								continue
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if request.Data == nil || request.Data.Len() == 0 {
 | 
							if request.Data == nil || request.Data.Len() == 0 {
 | 
				
			||||||
			continue
 | 
								continue
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if request.Fragment != 0 {
 | 
							if request.Fragment != 0 {
 | 
				
			||||||
			log.Warning("Dropping fragmented UDP packets.")
 | 
								log.Warning("Socks: Dropping fragmented UDP packets.")
 | 
				
			||||||
			// TODO handle fragments
 | 
								// TODO handle fragments
 | 
				
			||||||
			request.Data.Release()
 | 
								request.Data.Release()
 | 
				
			||||||
			continue
 | 
								continue
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		udpPacket := v2net.NewPacket(request.Destination(), request.Data, false)
 | 
							udpPacket := v2net.NewPacket(request.Destination(), request.Data, false)
 | 
				
			||||||
		log.Info("Send packet to %s with %d bytes", udpPacket.Destination().String(), request.Data.Len())
 | 
							log.Info("Socks: Send packet to %s with %d bytes", udpPacket.Destination().String(), request.Data.Len())
 | 
				
			||||||
		go this.handlePacket(udpPacket, addr, request.Address, request.Port)
 | 
							go this.handlePacket(udpPacket, addr, request.Address, request.Port)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
| 
						 | 
					@ -79,7 +79,7 @@ func (this *SocksServer) handlePacket(packet v2net.Packet, clientAddr *net.UDPAd
 | 
				
			||||||
			Port:     port,
 | 
								Port:     port,
 | 
				
			||||||
			Data:     data,
 | 
								Data:     data,
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		log.Info("Writing back UDP response with %d bytes from %s to %s", data.Len(), targetAddr.String(), clientAddr.String())
 | 
							log.Info("Socks: Writing back UDP response with %d bytes from %s to %s", data.Len(), targetAddr.String(), clientAddr.String())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		udpMessage := alloc.NewSmallBuffer().Clear()
 | 
							udpMessage := alloc.NewSmallBuffer().Clear()
 | 
				
			||||||
		response.Write(udpMessage)
 | 
							response.Write(udpMessage)
 | 
				
			||||||
| 
						 | 
					@ -94,7 +94,7 @@ func (this *SocksServer) handlePacket(packet v2net.Packet, clientAddr *net.UDPAd
 | 
				
			||||||
		udpMessage.Release()
 | 
							udpMessage.Release()
 | 
				
			||||||
		response.Data.Release()
 | 
							response.Data.Release()
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			log.Error("Socks failed to write UDP message (%d bytes) to %s: %v", nBytes, clientAddr.String(), err)
 | 
								log.Error("Socks: failed to write UDP message (%d bytes) to %s: %v", nBytes, clientAddr.String(), err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue