1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2026-04-23 05:59:11 -04:00

lazy evaluation of log fields

This commit is contained in:
v2ray
2016-01-18 12:24:33 +01:00
parent 8f20933457
commit eec0bb4db4
25 changed files with 124 additions and 117 deletions

View File

@@ -74,7 +74,7 @@ func (this *DokodemoDoor) ListenUDP(port v2net.Port) error {
Zone: "",
})
if err != nil {
log.Error("Dokodemo failed to listen on port %d: %v", port, err)
log.Error("Dokodemo failed to listen on port ", port, ": ", err)
return err
}
this.udpMutex.Lock()
@@ -97,7 +97,7 @@ func (this *DokodemoDoor) handleUDPPackets() {
buffer.Slice(0, nBytes)
if err != nil {
buffer.Release()
log.Error("Dokodemo failed to read from UDP: %v", err)
log.Error("Dokodemo failed to read from UDP: ", err)
return
}
@@ -124,7 +124,7 @@ func (this *DokodemoDoor) ListenTCP(port v2net.Port) error {
Zone: "",
})
if err != nil {
log.Error("Dokodemo failed to listen on port %d: %v", port, err)
log.Error("Dokodemo failed to listen on port ", port, ": ", err)
return err
}
this.tcpMutex.Lock()
@@ -144,7 +144,7 @@ func (this *DokodemoDoor) AcceptTCPConnections() {
}
connection, err := this.tcpListener.AcceptTCP()
if err != nil {
log.Error("Dokodemo failed to accept new connections: %v", err)
log.Error("Dokodemo failed to accept new connections: ", err)
return err
}
go this.HandleTCPConnection(connection)

View File

@@ -17,7 +17,7 @@ type FreedomConnection struct {
}
func (this *FreedomConnection) Dispatch(firstPacket v2net.Packet, ray ray.OutboundRay) error {
log.Info("Freedom: Opening connection to %s", firstPacket.Destination().String())
log.Info("Freedom: Opening connection to ", firstPacket.Destination())
var conn net.Conn
err := retry.Timed(5, 100).On(func() error {
@@ -30,7 +30,7 @@ func (this *FreedomConnection) Dispatch(firstPacket v2net.Packet, ray ray.Outbou
})
if err != nil {
close(ray.OutboundOutput())
log.Error("Freedom: Failed to open connection: %s : %v", firstPacket.Destination().String(), err)
log.Error("Freedom: Failed to open connection to ", firstPacket.Destination(), ": ", err)
return err
}
defer conn.Close()
@@ -60,7 +60,7 @@ func (this *FreedomConnection) Dispatch(firstPacket v2net.Packet, ray ray.Outbou
defer close(output)
response, err := v2net.ReadFrom(conn, nil)
log.Info("Freedom receives %d bytes from %s", response.Len(), conn.RemoteAddr().String())
log.Info("Freedom receives ", response.Len(), " bytes from ", conn.RemoteAddr())
if response.Len() > 0 {
output <- response
} else {

View File

@@ -68,7 +68,7 @@ func (this *HttpProxyServer) accept() {
}
tcpConn, err := this.tcpListener.AcceptTCP()
if err != nil {
log.Error("Failed to accept HTTP connection: %v", err)
log.Error("Failed to accept HTTP connection: ", err)
return err
}
go this.handleConnection(tcpConn)
@@ -106,10 +106,10 @@ func (this *HttpProxyServer) handleConnection(conn *net.TCPConn) {
request, err := http.ReadRequest(reader)
if err != nil {
log.Warning("Failed to read http request: %v", err)
log.Warning("Failed to read http request: ", err)
return
}
log.Info("Request to Method [%s] Host [%s] with URL [%s]", request.Method, request.Host, request.URL.String())
log.Info("Request to Method [", request.Method, "] Host [", request.Host, "] with URL [", request.URL, "]")
defaultPort := v2net.Port(80)
if strings.ToLower(request.URL.Scheme) == "https" {
defaultPort = v2net.Port(443)
@@ -120,7 +120,7 @@ func (this *HttpProxyServer) handleConnection(conn *net.TCPConn) {
}
dest, err := parseHost(host, defaultPort)
if err != nil {
log.Warning("Malformed proxy host (%s): %v", host, err)
log.Warning("Malformed proxy host (", host, "): ", err)
return
}
if strings.ToUpper(request.Method) == "CONNECT" {
@@ -222,7 +222,7 @@ func (this *HttpProxyServer) handlePlainHTTP(request *http.Request, dest v2net.D
requestBuffer := alloc.NewBuffer().Clear() // Don't release this buffer as it is passed into a Packet.
request.Write(requestBuffer)
log.Debug("Request to remote:\n%s", string(requestBuffer.Value))
log.Debug("Request to remote:\n", string(requestBuffer.Value))
packet := v2net.NewPacket(dest, requestBuffer, true)
ray := this.space.PacketDispatcher().DispatchToOutbound(packet)

View File

@@ -41,7 +41,7 @@ func init() {
} else if rawConfig.AuthMethod == AuthMethodUserPass {
socksConfig.AuthType = AuthTypePassword
} else {
log.Error("Socks: Unknown auth method: %s", rawConfig.AuthMethod)
log.Error("Socks: Unknown auth method: ", rawConfig.AuthMethod)
return nil, internal.ErrorBadConfiguration
}

View File

@@ -49,7 +49,7 @@ func ReadAuthentication(reader io.Reader) (auth Socks5AuthenticationRequest, aut
return
}
if nBytes < 2 {
log.Info("Socks: expected 2 bytes read, but only %d bytes read", nBytes)
log.Warning("Socks: expected 2 bytes read, but only ", nBytes, " bytes read")
err = transport.CorruptedPacket
return
}
@@ -65,20 +65,20 @@ func ReadAuthentication(reader io.Reader) (auth Socks5AuthenticationRequest, aut
auth.version = buffer.Value[0]
if auth.version != socksVersion {
log.Warning("Socks: Unknown protocol version %d", auth.version)
log.Warning("Socks: Unknown protocol version ", auth.version)
err = proxy.InvalidProtocolVersion
return
}
auth.nMethods = buffer.Value[1]
if auth.nMethods <= 0 {
log.Info("Socks: Zero length of authentication methods")
log.Warning("Socks: Zero length of authentication methods")
err = transport.CorruptedPacket
return
}
if nBytes-2 != int(auth.nMethods) {
log.Info("Socks: Unmatching number of auth methods, expecting %d, but got %d", auth.nMethods, nBytes)
log.Warning("Socks: Unmatching number of auth methods, expecting ", auth.nMethods, ", but got ", nBytes)
err = transport.CorruptedPacket
return
}
@@ -227,7 +227,7 @@ func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
}
if nBytes != int(domainLength) {
log.Info("Socks: Unable to read domain with %d bytes, expecting %d bytes", nBytes, domainLength)
log.Warning("Socks: Unable to read domain with ", nBytes, " bytes, expecting ", domainLength, " bytes")
err = transport.CorruptedPacket
return
}
@@ -242,7 +242,7 @@ func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
return
}
default:
log.Info("Socks: Unexpected address type %d", request.AddrType)
log.Warning("Socks: Unexpected address type ", request.AddrType)
err = transport.CorruptedPacket
return
}

View File

@@ -83,7 +83,7 @@ func ReadUDPRequest(packet []byte) (*Socks5UDPRequest, error) {
}
dataBegin = 5 + domainLength + 2
default:
log.Warning("Unknown address type %d", addrType)
log.Warning("Unknown address type ", addrType)
return nil, ErrorUnknownAddressType
}

View File

@@ -63,7 +63,7 @@ func (this *SocksServer) Listen(port v2net.Port) error {
Zone: "",
})
if err != nil {
log.Error("Socks: failed to listen on port %d: %v", port, err)
log.Error("Socks: failed to listen on port ", port, ": ", err)
return err
}
this.accepting = true
@@ -87,7 +87,7 @@ func (this *SocksServer) AcceptConnections() {
}
connection, err := this.tcpListener.AcceptTCP()
if err != nil {
log.Error("Socks: failed to accept new connection %v", err)
log.Error("Socks: failed to accept new connection: ", err)
return err
}
go this.HandleConnection(connection)
@@ -103,7 +103,7 @@ func (this *SocksServer) HandleConnection(connection *net.TCPConn) error {
auth, auth4, err := protocol.ReadAuthentication(reader)
if err != nil && err != protocol.Socks4Downgrade {
log.Error("Socks: failed to read authentication: %v", err)
log.Error("Socks: failed to read authentication: ", err)
return err
}
@@ -124,7 +124,7 @@ func (this *SocksServer) handleSocks5(reader *v2net.TimeOutReader, writer io.Wri
authResponse := protocol.NewAuthenticationResponse(protocol.AuthNoMatchingMethod)
err := protocol.WriteAuthentication(writer, authResponse)
if err != nil {
log.Error("Socks: failed to write authentication: %v", err)
log.Error("Socks: failed to write authentication: ", err)
return err
}
log.Warning("Socks: client doesn't support allowed any auth methods.")
@@ -134,13 +134,13 @@ func (this *SocksServer) handleSocks5(reader *v2net.TimeOutReader, writer io.Wri
authResponse := protocol.NewAuthenticationResponse(expectedAuthMethod)
err := protocol.WriteAuthentication(writer, authResponse)
if err != nil {
log.Error("Socks: failed to write authentication: %v", err)
log.Error("Socks: failed to write authentication: ", err)
return err
}
if this.config.AuthType == AuthTypePassword {
upRequest, err := protocol.ReadUserPassRequest(reader)
if err != nil {
log.Error("Socks: failed to read username and password: %v", err)
log.Error("Socks: failed to read username and password: ", err)
return err
}
status := byte(0)
@@ -150,18 +150,18 @@ func (this *SocksServer) handleSocks5(reader *v2net.TimeOutReader, writer io.Wri
upResponse := protocol.NewSocks5UserPassResponse(status)
err = protocol.WriteUserPassResponse(writer, upResponse)
if err != nil {
log.Error("Socks: failed to write user pass response: %v", err)
log.Error("Socks: failed to write user pass response: ", err)
return err
}
if status != byte(0) {
log.Warning("Socks: Invalid user account: %s", upRequest.AuthDetail())
log.Warning("Socks: Invalid user account: ", upRequest.AuthDetail())
return proxy.InvalidAuthentication
}
}
request, err := protocol.ReadRequest(reader)
if err != nil {
log.Error("Socks: failed to read request: %v", err)
log.Error("Socks: failed to read request: ", err)
return err
}
@@ -180,10 +180,10 @@ func (this *SocksServer) handleSocks5(reader *v2net.TimeOutReader, writer io.Wri
_, err = writer.Write(responseBuffer.Value)
responseBuffer.Release()
if err != nil {
log.Error("Socks: failed to write response: %v", err)
log.Error("Socks: failed to write response: ", err)
return err
}
log.Warning("Socks: Unsupported socks command %d", request.Command)
log.Warning("Socks: Unsupported socks command ", request.Command)
return UnsupportedSocksCommand
}
@@ -199,12 +199,12 @@ func (this *SocksServer) handleSocks5(reader *v2net.TimeOutReader, writer io.Wri
_, err = writer.Write(responseBuffer.Value)
responseBuffer.Release()
if err != nil {
log.Error("Socks: failed to write response: %v", err)
log.Error("Socks: failed to write response: ", err)
return err
}
dest := request.Destination()
log.Info("Socks: TCP Connect request to %s", dest.String())
log.Info("Socks: TCP Connect request to ", dest)
packet := v2net.NewPacket(dest, nil, true)
this.transport(reader, writer, packet)
@@ -233,7 +233,7 @@ func (this *SocksServer) handleUDP(reader *v2net.TimeOutReader, writer io.Writer
responseBuffer.Release()
if err != nil {
log.Error("Socks: failed to write response: %v", err)
log.Error("Socks: failed to write response: ", err)
return err
}
@@ -260,7 +260,7 @@ func (this *SocksServer) handleSocks4(reader io.Reader, writer io.Writer, auth p
responseBuffer.Release()
if result == protocol.Socks4RequestRejected {
log.Warning("Socks: Unsupported socks 4 command %d", auth.Command)
log.Warning("Socks: Unsupported socks 4 command ", auth.Command)
return UnsupportedSocksCommand
}

View File

@@ -17,7 +17,7 @@ func (this *SocksServer) ListenUDP(port v2net.Port) error {
}
conn, err := net.ListenUDP("udp", addr)
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 ", port, ": ", err)
return err
}
this.udpMutex.Lock()
@@ -40,15 +40,15 @@ func (this *SocksServer) AcceptPackets() error {
nBytes, addr, err := this.udpConn.ReadFromUDP(buffer.Value)
this.udpMutex.RUnlock()
if err != nil {
log.Error("Socks: failed to read UDP packets: %v", err)
log.Error("Socks: failed to read UDP packets: ", err)
buffer.Release()
continue
}
log.Info("Socks: Client UDP connection from %v", addr)
log.Info("Socks: Client UDP connection from ", addr)
request, err := protocol.ReadUDPRequest(buffer.Value[:nBytes])
buffer.Release()
if err != nil {
log.Error("Socks: failed to parse UDP request: %v", err)
log.Error("Socks: failed to parse UDP request: ", err)
continue
}
if request.Data == nil || request.Data.Len() == 0 {
@@ -62,7 +62,7 @@ func (this *SocksServer) AcceptPackets() error {
}
udpPacket := v2net.NewPacket(request.Destination(), request.Data, false)
log.Info("Socks: Send packet to %s with %d bytes", udpPacket.Destination().String(), request.Data.Len())
log.Info("Socks: Send packet to ", udpPacket.Destination(), " with ", request.Data.Len(), " bytes")
go this.handlePacket(udpPacket, addr, request.Address, request.Port)
}
return nil
@@ -79,7 +79,7 @@ func (this *SocksServer) handlePacket(packet v2net.Packet, clientAddr *net.UDPAd
Port: port,
Data: data,
}
log.Info("Socks: 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 ", data.Len(), " bytes from ", targetAddr, " to ", clientAddr)
udpMessage := alloc.NewSmallBuffer().Clear()
response.Write(udpMessage)
@@ -94,7 +94,7 @@ func (this *SocksServer) handlePacket(packet v2net.Packet, clientAddr *net.UDPAd
udpMessage.Release()
response.Data.Release()
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 (", nBytes, " bytes) to ", clientAddr, ": ", err)
}
}
}

View File

@@ -55,7 +55,7 @@ func (this *VMessInboundHandler) Listen(port v2net.Port) error {
Zone: "",
})
if err != nil {
log.Error("Unable to listen tcp port %d: %v", port, err)
log.Error("Unable to listen tcp port ", port, ": ", err)
return err
}
this.accepting = true
@@ -76,7 +76,7 @@ func (this *VMessInboundHandler) AcceptConnections() error {
}
connection, err := this.listener.AcceptTCP()
if err != nil {
log.Error("Failed to accpet connection: %s", err.Error())
log.Error("Failed to accpet connection: ", err)
return err
}
go this.HandleConnection(connection)
@@ -96,11 +96,11 @@ func (this *VMessInboundHandler) HandleConnection(connection *net.TCPConn) error
request, err := requestReader.Read(connReader)
if err != nil {
log.Access(connection.RemoteAddr().String(), "", log.AccessRejected, err.Error())
log.Warning("VMessIn: Invalid request from (%s): %v", connection.RemoteAddr().String(), err)
log.Warning("VMessIn: Invalid request from ", connection.RemoteAddr(), ": ", err)
return err
}
log.Access(connection.RemoteAddr().String(), request.Address.String(), log.AccessAccepted, "")
log.Debug("VMessIn: Received request for %s", request.Address.String())
log.Debug("VMessIn: Received request for ", request.Address)
ray := this.space.PacketDispatcher().DispatchToOutbound(v2net.NewPacket(request.Destination(), nil, true))
input := ray.InboundInput()
@@ -118,7 +118,7 @@ func (this *VMessInboundHandler) HandleConnection(connection *net.TCPConn) error
aesStream, err := v2crypto.NewAesEncryptionStream(responseKey[:], responseIV[:])
if err != nil {
log.Error("VMessIn: Failed to create AES decryption stream: %v", err)
log.Error("VMessIn: Failed to create AES decryption stream: ", err)
close(input)
return err
}
@@ -152,7 +152,7 @@ func handleInput(request *protocol.VMessRequest, reader io.Reader, input chan<-
aesStream, err := v2crypto.NewAesDecryptionStream(request.RequestKey, request.RequestIV)
if err != nil {
log.Error("VMessIn: Failed to create AES decryption stream: %v", err)
log.Error("VMessIn: Failed to create AES decryption stream: ", err)
return
}
requestReader := v2crypto.NewCryptionReader(aesStream, reader)

View File

@@ -67,13 +67,13 @@ func startCommunicate(request *protocol.VMessRequest, dest v2net.Destination, ra
Port: int(dest.Port()),
})
if err != nil {
log.Error("Failed to open %s: %v", dest.String(), err)
log.Error("Failed to open ", dest, ": ", err)
if ray != nil {
close(ray.OutboundOutput())
}
return err
}
log.Info("VMessOut: Tunneling request to %s via %s", request.Address.String(), dest.String())
log.Info("VMessOut: Tunneling request to ", request.Address, " via ", dest)
defer conn.Close()
@@ -97,7 +97,7 @@ func handleRequest(conn net.Conn, request *protocol.VMessRequest, firstPacket v2
defer finish.Unlock()
aesStream, err := v2crypto.NewAesEncryptionStream(request.RequestKey[:], request.RequestIV[:])
if err != nil {
log.Error("VMessOut: Failed to create AES encryption stream: %v", err)
log.Error("VMessOut: Failed to create AES encryption stream: ", err)
return
}
encryptRequestWriter := v2crypto.NewCryptionWriter(aesStream, conn)
@@ -106,7 +106,7 @@ func handleRequest(conn net.Conn, request *protocol.VMessRequest, firstPacket v2
defer buffer.Release()
buffer, err = request.ToBytes(protocol.NewRandomTimestampGenerator(protocol.Timestamp(time.Now().Unix()), 30), buffer)
if err != nil {
log.Error("VMessOut: Failed to serialize VMess request: %v", err)
log.Error("VMessOut: Failed to serialize VMess request: ", err)
return
}
@@ -129,7 +129,7 @@ func handleRequest(conn net.Conn, request *protocol.VMessRequest, firstPacket v2
_, err = conn.Write(buffer.Value)
if err != nil {
log.Error("VMessOut: Failed to write VMess request: %v", err)
log.Error("VMessOut: Failed to write VMess request: ", err)
return
}
@@ -151,14 +151,14 @@ func handleResponse(conn net.Conn, request *protocol.VMessRequest, output chan<-
aesStream, err := v2crypto.NewAesDecryptionStream(responseKey[:], responseIV[:])
if err != nil {
log.Error("VMessOut: Failed to create AES encryption stream: %v", err)
log.Error("VMessOut: Failed to create AES encryption stream: ", err)
return
}
decryptResponseReader := v2crypto.NewCryptionReader(aesStream, conn)
buffer, err := v2net.ReadFrom(decryptResponseReader, nil)
if err != nil {
log.Error("VMessOut: Failed to read VMess response (%d bytes): %v", buffer.Len(), err)
log.Error("VMessOut: Failed to read VMess response (", buffer.Len(), " bytes): ", err)
buffer.Release()
return
}
@@ -166,7 +166,7 @@ func handleResponse(conn net.Conn, request *protocol.VMessRequest, output chan<-
log.Warning("VMessOut: unexepcted response header. The connection is probably hijacked.")
return
}
log.Info("VMessOut received %d bytes from %s", buffer.Len()-4, conn.RemoteAddr().String())
log.Info("VMessOut received ", buffer.Len()-4, " bytes from ", conn.RemoteAddr())
responseBegin := 4
if buffer.Value[2] != 0 {

View File

@@ -71,7 +71,7 @@ func (this *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
nBytes, err := v2net.ReadAllBytes(reader, buffer.Value[:vmess.IDBytesLen])
if err != nil {
log.Debug("VMess: Failed to read request ID (%d bytes): %v", nBytes, err)
log.Debug("VMess: Failed to read request ID (", nBytes, " bytes): ", err)
return nil, err
}
@@ -85,7 +85,7 @@ func (this *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
iv := timestampHash.Sum(nil)
aesStream, err := v2crypto.NewAesDecryptionStream(userObj.ID.CmdKey(), iv)
if err != nil {
log.Debug("VMess: Failed to create AES stream: %v", err)
log.Debug("VMess: Failed to create AES stream: ", err)
return nil, err
}
@@ -93,7 +93,7 @@ func (this *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
nBytes, err = v2net.ReadAllBytes(decryptor, buffer.Value[:41])
if err != nil {
log.Debug("VMess: Failed to read request header (%d bytes): %v", nBytes, err)
log.Debug("VMess: Failed to read request header (", nBytes, " bytes): ", err)
return nil, err
}
bufferLen := nBytes
@@ -104,7 +104,7 @@ func (this *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
}
if request.Version != Version {
log.Warning("VMess: Invalid protocol version %d", request.Version)
log.Warning("VMess: Invalid protocol version ", request.Version)
return nil, proxy.InvalidProtocolVersion
}
@@ -120,7 +120,7 @@ func (this *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
nBytes, err = v2net.ReadAllBytes(decryptor, buffer.Value[41:45]) // 4 bytes
bufferLen += 4
if err != nil {
log.Debug("VMess: Failed to read target IPv4 (%d bytes): %v", nBytes, err)
log.Debug("VMess: Failed to read target IPv4 (", nBytes, " bytes): ", err)
return nil, err
}
request.Address = v2net.IPAddress(buffer.Value[41:45])
@@ -128,14 +128,14 @@ func (this *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
nBytes, err = v2net.ReadAllBytes(decryptor, buffer.Value[41:57]) // 16 bytes
bufferLen += 16
if err != nil {
log.Debug("VMess: Failed to read target IPv6 (%d bytes): %v", nBytes, err)
log.Debug("VMess: Failed to read target IPv6 (", nBytes, " bytes): ", nBytes, err)
return nil, err
}
request.Address = v2net.IPAddress(buffer.Value[41:57])
case addrTypeDomain:
nBytes, err = v2net.ReadAllBytes(decryptor, buffer.Value[41:42])
if err != nil {
log.Debug("VMess: Failed to read target domain (%d bytes): %v", nBytes, err)
log.Debug("VMess: Failed to read target domain (", nBytes, " bytes): ", nBytes, err)
return nil, err
}
domainLength := int(buffer.Value[41])
@@ -144,7 +144,7 @@ func (this *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
}
nBytes, err = v2net.ReadAllBytes(decryptor, buffer.Value[42:42+domainLength])
if err != nil {
log.Debug("VMess: Failed to read target domain (%d bytes): %v", nBytes, err)
log.Debug("VMess: Failed to read target domain (", nBytes, " bytes): ", nBytes, err)
return nil, err
}
bufferLen += 1 + domainLength
@@ -154,7 +154,7 @@ func (this *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
nBytes, err = v2net.ReadAllBytes(decryptor, buffer.Value[bufferLen:bufferLen+4])
if err != nil {
log.Debug("VMess: Failed to read checksum (%d bytes): %v", nBytes, err)
log.Debug("VMess: Failed to read checksum (", nBytes, " bytes): ", nBytes, err)
return nil, err
}