Browse Source

Access log

V2Ray 10 years ago
parent
commit
fc80d5e279

+ 77 - 0
common/log/access.go

@@ -0,0 +1,77 @@
+package log
+
+import (
+	"log"
+	"os"
+)
+
+type AccessStatus string
+
+const (
+	AccessAccepted = AccessStatus("accepted")
+	AccessRejected = AccessStatus("rejected")
+)
+
+type accessLogger interface {
+	Log(from, to string, status AccessStatus, reason string)
+}
+
+type noOpAccessLogger struct {
+}
+
+func (logger *noOpAccessLogger) Log(from, to string, status AccessStatus, reason string) {
+	// Swallow
+}
+
+type accessLog struct {
+	From   string
+	To     string
+	Status AccessStatus
+	Reason string
+}
+
+type fileAccessLogger struct {
+	queue  chan *accessLog
+	logger *log.Logger
+}
+
+func (logger *fileAccessLogger) Log(from, to string, status AccessStatus, reason string) {
+	logger.queue <- &accessLog{
+		From:   from,
+		To:     to,
+		Status: status,
+		Reason: reason,
+	}
+}
+
+func (logger *fileAccessLogger) Run() {
+	for entry := range logger.queue {
+		logger.logger.Println(entry.From + " " + string(entry.Status) + " " + entry.To + " " + entry.Reason)
+	}
+}
+
+func newFileAccessLogger(path string) accessLogger {
+	file, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
+	if err != nil {
+		log.Printf("Unable to create or open file (%s): %v\n", path, err)
+		return nil
+	}
+	return &fileAccessLogger{
+		queue:  make(chan *accessLog, 16),
+		logger: log.New(file, "", log.Ldate|log.Ltime),
+	}
+}
+
+var accessLoggerInstance accessLogger = &noOpAccessLogger{}
+
+func InitAccessLogger(file string) {
+	logger := newFileAccessLogger(file)
+	if logger != nil {
+		go logger.(*fileAccessLogger).Run()
+		accessLoggerInstance = logger
+	}
+}
+
+func Access(from, to string, status AccessStatus, reason string) {
+	accessLoggerInstance.Log(from, to, status, reason)
+}

+ 5 - 0
config/config.go

@@ -12,8 +12,13 @@ type ConnectionConfig interface {
 	Settings(configType Type) interface{}
 	Settings(configType Type) interface{}
 }
 }
 
 
+type LogConfig interface {
+	AccessLog() string
+}
+
 type PointConfig interface {
 type PointConfig interface {
 	Port() uint16
 	Port() uint16
+	LogConfig() LogConfig
 	InboundConfig() ConnectionConfig
 	InboundConfig() ConnectionConfig
 	OutboundConfig() ConnectionConfig
 	OutboundConfig() ConnectionConfig
 }
 }

+ 13 - 0
config/json/json.go

@@ -32,9 +32,18 @@ func (config *ConnectionConfig) Settings(configType config.Type) interface{} {
 	return configObj
 	return configObj
 }
 }
 
 
+type LogConfig struct {
+	AccessLogValue string `json:"access"`
+}
+
+func (config *LogConfig) AccessLog() string {
+	return config.AccessLogValue
+}
+
 // Config is the config for Point server.
 // Config is the config for Point server.
 type Config struct {
 type Config struct {
 	PortValue           uint16            `json:"port"` // Port of this Point server.
 	PortValue           uint16            `json:"port"` // Port of this Point server.
+	LogConfigValue      *LogConfig        `json:"log"`
 	InboundConfigValue  *ConnectionConfig `json:"inbound"`
 	InboundConfigValue  *ConnectionConfig `json:"inbound"`
 	OutboundConfigValue *ConnectionConfig `json:"outbound"`
 	OutboundConfigValue *ConnectionConfig `json:"outbound"`
 }
 }
@@ -43,6 +52,10 @@ func (config *Config) Port() uint16 {
 	return config.PortValue
 	return config.PortValue
 }
 }
 
 
+func (config *Config) LogConfig() config.LogConfig {
+	return config.LogConfigValue
+}
+
 func (config *Config) InboundConfig() config.ConnectionConfig {
 func (config *Config) InboundConfig() config.ConnectionConfig {
 	return config.InboundConfigValue
 	return config.InboundConfigValue
 }
 }

+ 2 - 0
proxy/vmess/vmessin.go

@@ -68,9 +68,11 @@ func (handler *VMessInboundHandler) HandleConnection(connection *net.TCPConn) er
 
 
 	request, err := requestReader.Read(connReader)
 	request, err := requestReader.Read(connReader)
 	if err != nil {
 	if err != nil {
+		log.Access(connection.RemoteAddr().String(), "", log.AccessRejected, "Invalid Auth")
 		log.Warning("VMessIn: Invalid request from (%s): %v", connection.RemoteAddr().String(), err)
 		log.Warning("VMessIn: Invalid request from (%s): %v", connection.RemoteAddr().String(), err)
 		return 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 %s", request.Address.String())
 
 
 	ray := handler.vPoint.DispatchToOutbound(v2net.NewPacket(request.Destination(), nil, true))
 	ray := handler.vPoint.DispatchToOutbound(v2net.NewPacket(request.Destination(), nil, true))

+ 3 - 0
release/config/vpoint_socks_vmess.json

@@ -1,5 +1,8 @@
 {
 {
   "port": 1080,
   "port": 1080,
+  "log": {
+    "access": ""
+  },
   "inbound": {
   "inbound": {
     "protocol": "socks",
     "protocol": "socks",
     "settings": {
     "settings": {

+ 3 - 0
release/config/vpoint_vmess_freedom.json

@@ -1,5 +1,8 @@
 {
 {
   "port": 27183,
   "port": 27183,
+  "log" : {
+    "access": "./access.log"
+  },
   "inbound": {
   "inbound": {
     "protocol": "vmess",
     "protocol": "vmess",
     "settings": {
     "settings": {

+ 4 - 0
release/server/main.go

@@ -55,6 +55,10 @@ func main() {
 		return
 		return
 	}
 	}
 
 
+	if config.LogConfig() != nil && len(config.LogConfig().AccessLog()) > 0 {
+		log.InitAccessLogger(config.LogConfig().AccessLog())
+	}
+
 	vPoint, err := core.NewPoint(config)
 	vPoint, err := core.NewPoint(config)
 	if err != nil {
 	if err != nil {
 		log.Error("Failed to create Point server: %v", err)
 		log.Error("Failed to create Point server: %v", err)