mirror of https://github.com/v2ray/v2ray-core
				
				
				
			lazy evaluation of access log
							parent
							
								
									eec0bb4db4
								
							
						
					
					
						commit
						4dd771170c
					
				| 
						 | 
				
			
			@ -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,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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))
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue