From 4dd771170c9b640583b0f45a32def7b831029d58 Mon Sep 17 00:00:00 2001 From: v2ray Date: Mon, 18 Jan 2016 12:31:27 +0100 Subject: [PATCH] lazy evaluation of access log --- common/log/access.go | 14 +++++++++----- common/log/access_test.go | 2 +- proxy/vmess/inbound/inbound.go | 5 +++-- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/common/log/access.go b/common/log/access.go index 27a73c006..f41cace67 100644 --- a/common/log/access.go +++ b/common/log/access.go @@ -1,5 +1,9 @@ package log +import ( + "github.com/v2ray/v2ray-core/common/serial" +) + // AccessStatus is the status of an access request from clients. type AccessStatus string @@ -13,14 +17,14 @@ var ( ) type accessLog struct { - From string - To string + From serial.String + To serial.String Status AccessStatus - Reason string + Reason serial.String } func (this *accessLog) String() string { - return this.From + " " + string(this.Status) + " " + this.To + " " + this.Reason + return this.From.String() + " " + string(this.Status) + " " + this.To.String() + " " + this.Reason.String() } // InitAccessLogger initializes the access logger to write into the give file. @@ -35,7 +39,7 @@ func InitAccessLogger(file string) error { } // Access writes an access log. -func Access(from, to string, status AccessStatus, reason string) { +func Access(from, to serial.String, status AccessStatus, reason serial.String) { accessLoggerInstance.Log(&accessLog{ From: from, To: to, diff --git a/common/log/access_test.go b/common/log/access_test.go index 7c254fb8f..30aa00aa0 100644 --- a/common/log/access_test.go +++ b/common/log/access_test.go @@ -19,7 +19,7 @@ func TestAccessLog(t *testing.T) { _, err := os.Stat(filename) assert.Error(err).IsNil() - Access("test_from", "test_to", AccessAccepted, "test_reason") + Access(serial.StringLiteral("test_from"), serial.StringLiteral("test_to"), AccessAccepted, serial.StringLiteral("test_reason")) <-time.After(2 * time.Second) accessLoggerInstance.(*fileLogWriter).close() diff --git a/proxy/vmess/inbound/inbound.go b/proxy/vmess/inbound/inbound.go index a4f99f91a..d35259988 100644 --- a/proxy/vmess/inbound/inbound.go +++ b/proxy/vmess/inbound/inbound.go @@ -12,6 +12,7 @@ import ( "github.com/v2ray/v2ray-core/common/log" v2net "github.com/v2ray/v2ray-core/common/net" "github.com/v2ray/v2ray-core/common/retry" + "github.com/v2ray/v2ray-core/common/serial" "github.com/v2ray/v2ray-core/proxy" "github.com/v2ray/v2ray-core/proxy/internal" "github.com/v2ray/v2ray-core/proxy/vmess" @@ -95,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.Access(connection.RemoteAddr(), serial.StringLiteral(""), log.AccessRejected, serial.StringLiteral(err.Error())) log.Warning("VMessIn: Invalid request from ", connection.RemoteAddr(), ": ", err) return err } - log.Access(connection.RemoteAddr().String(), request.Address.String(), log.AccessAccepted, "") + log.Access(connection.RemoteAddr(), request.Address, log.AccessAccepted, serial.StringLiteral("")) log.Debug("VMessIn: Received request for ", request.Address) ray := this.space.PacketDispatcher().DispatchToOutbound(v2net.NewPacket(request.Destination(), nil, true))