From eec0bb4db485e05bb8788b0eb8640ee4c2da20ed Mon Sep 17 00:00:00 2001 From: v2ray Date: Mon, 18 Jan 2016 12:24:33 +0100 Subject: [PATCH] lazy evaluation of log fields --- app/router/config_json.go | 2 +- app/router/rules/chinaip.go | 2 +- app/router/rules/chinasites.go | 2 +- app/router/rules/router_config.go | 12 +++++------ common/log/access.go | 2 +- common/log/log.go | 36 ++++++++++++++++++------------- common/log/log_test.go | 9 ++++---- common/net/address.go | 2 +- common/net/port_json.go | 10 ++++----- proxy/dokodemo/dokodemo.go | 8 +++---- proxy/freedom/freedom.go | 6 +++--- proxy/http/http.go | 10 ++++----- proxy/socks/config_json.go | 2 +- proxy/socks/protocol/socks.go | 12 +++++------ proxy/socks/protocol/udp.go | 2 +- proxy/socks/socks.go | 30 +++++++++++++------------- proxy/socks/udp.go | 14 ++++++------ proxy/vmess/inbound/inbound.go | 12 +++++------ proxy/vmess/outbound/outbound.go | 16 +++++++------- proxy/vmess/protocol/vmess.go | 18 ++++++++-------- release/server/main.go | 6 +++--- shell/point/config_json.go | 4 ++-- shell/point/inbound_detour.go | 4 ++-- shell/point/point.go | 14 ++++++------ testing/scenarios/server_env.go | 6 +++--- 25 files changed, 124 insertions(+), 117 deletions(-) diff --git a/app/router/config_json.go b/app/router/config_json.go index 79a9ababd..9aeb1fc15 100644 --- a/app/router/config_json.go +++ b/app/router/config_json.go @@ -19,7 +19,7 @@ func (this *Config) UnmarshalJSON(data []byte) error { } settings, err := CreateRouterConfig(jsonConfig.Strategy, []byte(jsonConfig.Settings)) if err != nil { - log.Error("Router: Failed to load router settings: %v", err) + log.Error("Router: Failed to load router settings: ", err) return err } this.Strategy = jsonConfig.Strategy diff --git a/app/router/rules/chinaip.go b/app/router/rules/chinaip.go index e3a2fd171..dbe56df43 100644 --- a/app/router/rules/chinaip.go +++ b/app/router/rules/chinaip.go @@ -13,7 +13,7 @@ func parseChinaIPRule(data []byte) (*Rule, error) { rawRule := new(JsonRule) err := json.Unmarshal(data, rawRule) if err != nil { - log.Error("Router: Invalid router rule: %v", err) + log.Error("Router: Invalid router rule: ", err) return nil, err } return &Rule{ diff --git a/app/router/rules/chinasites.go b/app/router/rules/chinasites.go index 66015b953..4b45a9a46 100644 --- a/app/router/rules/chinasites.go +++ b/app/router/rules/chinasites.go @@ -24,7 +24,7 @@ func parseChinaSitesRule(data []byte) (*Rule, error) { rawRule := new(JsonRule) err := json.Unmarshal(data, rawRule) if err != nil { - log.Error("Router: Invalid router rule: %v", err) + log.Error("Router: Invalid router rule: ", err) return nil, err } return &Rule{ diff --git a/app/router/rules/router_config.go b/app/router/rules/router_config.go index 0eed6d24b..72c4d0940 100644 --- a/app/router/rules/router_config.go +++ b/app/router/rules/router_config.go @@ -53,7 +53,7 @@ func parseFieldRule(msg json.RawMessage) (*Rule, error) { for _, ipStr := range *(rawFieldRule.IP) { cidrMatcher, err := NewCIDRMatcher(ipStr.String()) if err != nil { - log.Error("Router: Invalid IP range in router rule: %v", err) + log.Error("Router: Invalid IP range in router rule: ", err) return nil, err } conds.Add(cidrMatcher) @@ -78,14 +78,14 @@ func parseRule(msg json.RawMessage) *Rule { rawRule := new(JsonRule) err := json.Unmarshal(msg, rawRule) if err != nil { - log.Error("Router: Invalid router rule: %v", err) + log.Error("Router: Invalid router rule: ", err) return nil } if rawRule.Type == "field" { fieldrule, err := parseFieldRule(msg) if err != nil { - log.Error("Invalid field rule: %v", err) + log.Error("Invalid field rule: ", err) return nil } return fieldrule @@ -93,7 +93,7 @@ func parseRule(msg json.RawMessage) *Rule { if rawRule.Type == "chinaip" { chinaiprule, err := parseChinaIPRule(msg) if err != nil { - log.Error("Router: Invalid chinaip rule: %v", err) + log.Error("Router: Invalid chinaip rule: ", err) return nil } return chinaiprule @@ -101,12 +101,12 @@ func parseRule(msg json.RawMessage) *Rule { if rawRule.Type == "chinasites" { chinasitesrule, err := parseChinaSitesRule(msg) if err != nil { - log.Error("Invalid chinasites rule: %v", err) + log.Error("Invalid chinasites rule: ", err) return nil } return chinasitesrule } - log.Error("Unknown router rule type: %s", rawRule.Type) + log.Error("Unknown router rule type: ", rawRule.Type) return nil } diff --git a/common/log/access.go b/common/log/access.go index 2046bf8a5..27a73c006 100644 --- a/common/log/access.go +++ b/common/log/access.go @@ -27,7 +27,7 @@ func (this *accessLog) String() string { func InitAccessLogger(file string) error { logger, err := newFileLogWriter(file) if err != nil { - Error("Failed to create access logger on file (%s): %v", file, err) + Error("Failed to create access logger on file (", file, "): ", file, err) return err } accessLoggerInstance = logger diff --git a/common/log/log.go b/common/log/log.go index 117541021..e8de6834e 100644 --- a/common/log/log.go +++ b/common/log/log.go @@ -2,6 +2,8 @@ package log import ( "fmt" + + "github.com/v2ray/v2ray-core/common/serial" ) const ( @@ -13,16 +15,24 @@ const ( type errorLog struct { prefix string - format string values []interface{} } func (this *errorLog) String() string { - var data string - if len(this.values) == 0 { - data = this.format - } else { - data = fmt.Sprintf(this.format, this.values...) + data := "" + for _, value := range this.values { + switch typedVal := value.(type) { + case string: + data += typedVal + case *string: + data += *typedVal + case serial.String: + data += typedVal.String() + case error: + data += typedVal.Error() + default: + data += fmt.Sprintf("%v", value) + } } return this.prefix + data } @@ -64,7 +74,7 @@ func SetLogLevel(level LogLevel) { func InitErrorLogger(file string) error { logger, err := newFileLogWriter(file) if err != nil { - Error("Failed to create error logger on file (%s): %v", file, err) + Error("Failed to create error logger on file (", file, "): ", err) return err } streamLoggerInstance = logger @@ -72,37 +82,33 @@ func InitErrorLogger(file string) error { } // Debug outputs a debug log with given format and optional arguments. -func Debug(format string, v ...interface{}) { +func Debug(v ...interface{}) { debugLogger.Log(&errorLog{ prefix: "[Debug]", - format: format, values: v, }) } // Info outputs an info log with given format and optional arguments. -func Info(format string, v ...interface{}) { +func Info(v ...interface{}) { infoLogger.Log(&errorLog{ prefix: "[Info]", - format: format, values: v, }) } // Warning outputs a warning log with given format and optional arguments. -func Warning(format string, v ...interface{}) { +func Warning(v ...interface{}) { warningLogger.Log(&errorLog{ prefix: "[Warning]", - format: format, values: v, }) } // Error outputs an error log with given format and optional arguments. -func Error(format string, v ...interface{}) { +func Error(v ...interface{}) { errorLogger.Log(&errorLog{ prefix: "[Error]", - format: format, values: v, }) } diff --git a/common/log/log_test.go b/common/log/log_test.go index a7886a89e..15f921c9a 100644 --- a/common/log/log_test.go +++ b/common/log/log_test.go @@ -5,6 +5,7 @@ import ( "log" "testing" + "github.com/v2ray/v2ray-core/common/serial" v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) @@ -28,11 +29,11 @@ func TestStreamLogger(t *testing.T) { infoLogger = &stdOutLogWriter{ logger: log.New(buffer, "", 0), } - Info("Test %s Format", "Stream Logger") - assert.Bytes(buffer.Bytes()).Equals([]byte("[Info]Test Stream Logger Format\n")) + Info("Test ", "Stream Logger", " Format") + assert.StringLiteral(string(buffer.Bytes())).Equals("[Info]Test Stream Logger Format\n") buffer.Reset() errorLogger = infoLogger - Error("Test No Format") - assert.Bytes(buffer.Bytes()).Equals([]byte("[Error]Test No Format\n")) + Error("Test ", serial.StringLiteral("literal"), " Format") + assert.StringLiteral(string(buffer.Bytes())).Equals("[Error]Test literal Format\n") } diff --git a/common/net/address.go b/common/net/address.go index a754ac29c..d3eb9b857 100644 --- a/common/net/address.go +++ b/common/net/address.go @@ -46,7 +46,7 @@ func IPAddress(ip []byte) Address { } return &addr default: - log.Error("Invalid IP format: %v", ip) + log.Error("Invalid IP format: ", ip) return nil } } diff --git a/common/net/port_json.go b/common/net/port_json.go index 6b2999494..8d31baac5 100644 --- a/common/net/port_json.go +++ b/common/net/port_json.go @@ -20,7 +20,7 @@ func (this *PortRange) UnmarshalJSON(data []byte) error { err := json.Unmarshal(data, &maybeint) if err == nil { if maybeint <= 0 || maybeint >= 65535 { - log.Error("Invalid port [%s]", string(data)) + log.Error("Invalid port [", string(data), "]") return InvalidPortRange } this.From = Port(maybeint) @@ -35,7 +35,7 @@ func (this *PortRange) UnmarshalJSON(data []byte) error { if len(pair) == 1 { value, err := strconv.Atoi(pair[0]) if err != nil || value <= 0 || value >= 65535 { - log.Error("Invalid from port %s", pair[0]) + log.Error("Invalid from port ", pair[0]) return InvalidPortRange } this.From = Port(value) @@ -44,20 +44,20 @@ func (this *PortRange) UnmarshalJSON(data []byte) error { } else if len(pair) == 2 { from, err := strconv.Atoi(pair[0]) if err != nil || from <= 0 || from >= 65535 { - log.Error("Invalid from port %s", pair[0]) + log.Error("Invalid from port ", pair[0]) return InvalidPortRange } this.From = Port(from) to, err := strconv.Atoi(pair[1]) if err != nil || to <= 0 || to >= 65535 { - log.Error("Invalid to port %s", pair[1]) + log.Error("Invalid to port ", pair[1]) return InvalidPortRange } this.To = Port(to) if this.From > this.To { - log.Error("Invalid port range %d -> %d", this.From, this.To) + log.Error("Invalid port range ", this.From, " -> ", this.To) return InvalidPortRange } return nil diff --git a/proxy/dokodemo/dokodemo.go b/proxy/dokodemo/dokodemo.go index e7a2cf57c..ddeb9e707 100644 --- a/proxy/dokodemo/dokodemo.go +++ b/proxy/dokodemo/dokodemo.go @@ -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) diff --git a/proxy/freedom/freedom.go b/proxy/freedom/freedom.go index 6e95f84aa..c3e11f19f 100644 --- a/proxy/freedom/freedom.go +++ b/proxy/freedom/freedom.go @@ -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 { diff --git a/proxy/http/http.go b/proxy/http/http.go index c191e1b12..049a40300 100644 --- a/proxy/http/http.go +++ b/proxy/http/http.go @@ -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) diff --git a/proxy/socks/config_json.go b/proxy/socks/config_json.go index a3a47e44b..c9c82d9a1 100644 --- a/proxy/socks/config_json.go +++ b/proxy/socks/config_json.go @@ -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 } diff --git a/proxy/socks/protocol/socks.go b/proxy/socks/protocol/socks.go index 1557381c0..2d09453e3 100644 --- a/proxy/socks/protocol/socks.go +++ b/proxy/socks/protocol/socks.go @@ -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 } diff --git a/proxy/socks/protocol/udp.go b/proxy/socks/protocol/udp.go index 1bce02d7a..596e38faf 100644 --- a/proxy/socks/protocol/udp.go +++ b/proxy/socks/protocol/udp.go @@ -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 } diff --git a/proxy/socks/socks.go b/proxy/socks/socks.go index dd8c78ee2..1046ad4eb 100644 --- a/proxy/socks/socks.go +++ b/proxy/socks/socks.go @@ -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 } diff --git a/proxy/socks/udp.go b/proxy/socks/udp.go index ce922bcd9..6db478906 100644 --- a/proxy/socks/udp.go +++ b/proxy/socks/udp.go @@ -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) } } } diff --git a/proxy/vmess/inbound/inbound.go b/proxy/vmess/inbound/inbound.go index 18c004404..a4f99f91a 100644 --- a/proxy/vmess/inbound/inbound.go +++ b/proxy/vmess/inbound/inbound.go @@ -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) diff --git a/proxy/vmess/outbound/outbound.go b/proxy/vmess/outbound/outbound.go index 815b39b64..78206884a 100644 --- a/proxy/vmess/outbound/outbound.go +++ b/proxy/vmess/outbound/outbound.go @@ -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 { diff --git a/proxy/vmess/protocol/vmess.go b/proxy/vmess/protocol/vmess.go index 994af6220..4ab436601 100644 --- a/proxy/vmess/protocol/vmess.go +++ b/proxy/vmess/protocol/vmess.go @@ -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 } diff --git a/release/server/main.go b/release/server/main.go index ca9ec29f8..29631ad48 100644 --- a/release/server/main.go +++ b/release/server/main.go @@ -65,7 +65,7 @@ func main() { } config, err := point.LoadConfig(configFile) if err != nil { - log.Error("Failed to read config file (%s): %v", configFile, err) + log.Error("Failed to read config file (", configFile, "): ", configFile, err) return } @@ -75,13 +75,13 @@ func main() { vPoint, err := point.NewPoint(config) if err != nil { - log.Error("Failed to create Point server: %v", err) + log.Error("Failed to create Point server: ", err) return } err = vPoint.Start() if err != nil { - log.Error("Error starting Point server: %v", err) + log.Error("Error starting Point server: ", err) return } diff --git a/shell/point/config_json.go b/shell/point/config_json.go index 7dfb17e73..278025529 100644 --- a/shell/point/config_json.go +++ b/shell/point/config_json.go @@ -138,14 +138,14 @@ func JsonLoadConfig(file string) (*Config, error) { fixedFile := os.ExpandEnv(file) rawConfig, err := ioutil.ReadFile(fixedFile) if err != nil { - log.Error("Failed to read server config file (%s): %v", file, err) + log.Error("Failed to read server config file (", file, "): ", file, err) return nil, err } jsonConfig := &Config{} err = json.Unmarshal(rawConfig, jsonConfig) if err != nil { - log.Error("Failed to load server config: %v", err) + log.Error("Failed to load server config: ", err) return nil, err } diff --git a/shell/point/inbound_detour.go b/shell/point/inbound_detour.go index d727a5a81..367166b5b 100644 --- a/shell/point/inbound_detour.go +++ b/shell/point/inbound_detour.go @@ -28,7 +28,7 @@ func (this *InboundDetourHandler) Initialize() error { ichConfig := this.config.Settings ich, err := proxyrepo.CreateInboundConnectionHandler(this.config.Protocol, this.space, ichConfig) if err != nil { - log.Error("Failed to create inbound connection handler: %v", err) + log.Error("Failed to create inbound connection handler: ", err) return err } this.ich = append(this.ich, &InboundConnectionHandlerWithPort{ @@ -51,7 +51,7 @@ func (this *InboundDetourHandler) Start() error { err := retry.Timed(100 /* times */, 100 /* ms */).On(func() error { err := ich.handler.Listen(ich.port) if err != nil { - log.Error("Failed to start inbound detour on port %d: %v", ich.port, err) + log.Error("Failed to start inbound detour on port ", ich.port, ": ", err) return err } return nil diff --git a/shell/point/point.go b/shell/point/point.go index eb1503b36..1d9aff202 100644 --- a/shell/point/point.go +++ b/shell/point/point.go @@ -58,7 +58,7 @@ func NewPoint(pConfig *Config) (*Point, error) { ichConfig := pConfig.InboundConfig.Settings ich, err := proxyrepo.CreateInboundConnectionHandler(pConfig.InboundConfig.Protocol, vpoint.space.ForContext("vpoint-default-inbound"), ichConfig) if err != nil { - log.Error("Failed to create inbound connection handler: %v", err) + log.Error("Failed to create inbound connection handler: ", err) return nil, err } vpoint.ich = ich @@ -66,7 +66,7 @@ func NewPoint(pConfig *Config) (*Point, error) { ochConfig := pConfig.OutboundConfig.Settings och, err := proxyrepo.CreateOutboundConnectionHandler(pConfig.OutboundConfig.Protocol, vpoint.space.ForContext("vpoint-default-outbound"), ochConfig) if err != nil { - log.Error("Failed to create outbound connection handler: %v", err) + log.Error("Failed to create outbound connection handler: ", err) return nil, err } vpoint.och = och @@ -93,7 +93,7 @@ func NewPoint(pConfig *Config) (*Point, error) { for _, detourConfig := range outboundDetours { detourHandler, err := proxyrepo.CreateOutboundConnectionHandler(detourConfig.Protocol, vpoint.space.ForContext(detourConfig.Tag), detourConfig.Settings) if err != nil { - log.Error("Failed to create detour outbound connection handler: %v", err) + log.Error("Failed to create detour outbound connection handler: ", err) return nil, err } vpoint.odh[detourConfig.Tag] = detourHandler @@ -104,7 +104,7 @@ func NewPoint(pConfig *Config) (*Point, error) { if routerConfig != nil { r, err := router.CreateRouter(routerConfig.Strategy, routerConfig.Settings) if err != nil { - log.Error("Failed to create router: %v", err) + log.Error("Failed to create router: ", err) return nil, BadConfiguration } vpoint.router = r @@ -124,7 +124,7 @@ func (this *Point) Close() { // In the case of any errors, the state of the server is unpredicatable. func (this *Point) Start() error { if this.port <= 0 { - log.Error("Invalid port %d", this.port) + log.Error("Invalid port ", this.port) return BadConfiguration } @@ -133,7 +133,7 @@ func (this *Point) Start() error { if err != nil { return err } - log.Warning("Point server started on port %d", this.port) + log.Warning("Point server started on port ", this.port) return nil }) if err != nil { @@ -162,7 +162,7 @@ func (this *Point) DispatchToOutbound(context app.Context, packet v2net.Packet) if this.router != nil { if tag, err := this.router.TakeDetour(dest); err == nil { if handler, found := this.odh[tag]; found { - log.Info("Point: Taking detour [%s] for [%s]", tag, dest) + log.Info("Point: Taking detour [", tag, "] for [", dest, "]", tag, dest) dispatcher = handler } } diff --git a/testing/scenarios/server_env.go b/testing/scenarios/server_env.go index cad717e44..812f873b2 100644 --- a/testing/scenarios/server_env.go +++ b/testing/scenarios/server_env.go @@ -40,19 +40,19 @@ func InitializeServerSetOnce(testcase string) error { func InitializeServer(configFile string) error { config, err := point.LoadConfig(configFile) if err != nil { - log.Error("Failed to read config file (%s): %v", configFile, err) + log.Error("Failed to read config file (", configFile, "): ", configFile, err) return err } vPoint, err := point.NewPoint(config) if err != nil { - log.Error("Failed to create Point server: %v", err) + log.Error("Failed to create Point server: ", err) return err } err = vPoint.Start() if err != nil { - log.Error("Error starting Point server: %v", err) + log.Error("Error starting Point server: ", err) return err } runningServers = append(runningServers, vPoint)