lazy evaluation of access log

pull/73/head
v2ray 2016-01-18 12:31:27 +01:00
parent eec0bb4db4
commit 4dd771170c
3 changed files with 13 additions and 8 deletions

View File

@ -1,5 +1,9 @@
package log package log
import (
"github.com/v2ray/v2ray-core/common/serial"
)
// AccessStatus is the status of an access request from clients. // AccessStatus is the status of an access request from clients.
type AccessStatus string type AccessStatus string
@ -13,14 +17,14 @@ var (
) )
type accessLog struct { type accessLog struct {
From string From serial.String
To string To serial.String
Status AccessStatus Status AccessStatus
Reason string Reason serial.String
} }
func (this *accessLog) String() 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. // 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. // 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{ accessLoggerInstance.Log(&accessLog{
From: from, From: from,
To: to, To: to,

View File

@ -19,7 +19,7 @@ func TestAccessLog(t *testing.T) {
_, err := os.Stat(filename) _, err := os.Stat(filename)
assert.Error(err).IsNil() 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) <-time.After(2 * time.Second)
accessLoggerInstance.(*fileLogWriter).close() accessLoggerInstance.(*fileLogWriter).close()

View File

@ -12,6 +12,7 @@ import (
"github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net" v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/common/retry" "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"
"github.com/v2ray/v2ray-core/proxy/internal" "github.com/v2ray/v2ray-core/proxy/internal"
"github.com/v2ray/v2ray-core/proxy/vmess" "github.com/v2ray/v2ray-core/proxy/vmess"
@ -95,11 +96,11 @@ func (this *VMessInboundHandler) HandleConnection(connection *net.TCPConn) error
request, err := requestReader.Read(connReader) request, err := requestReader.Read(connReader)
if err != nil { 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) log.Warning("VMessIn: Invalid request from ", connection.RemoteAddr(), ": ", err)
return 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) log.Debug("VMessIn: Received request for ", request.Address)
ray := this.space.PacketDispatcher().DispatchToOutbound(v2net.NewPacket(request.Destination(), nil, true)) ray := this.space.PacketDispatcher().DispatchToOutbound(v2net.NewPacket(request.Destination(), nil, true))