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

@ -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

View File

@ -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{

View File

@ -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{

View File

@ -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
}

View File

@ -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

View File

@ -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,
})
}

View File

@ -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")
}

View File

@ -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
}
}

View File

@ -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

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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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

View File

@ -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
}
}

View File

@ -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)