1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-25 00:45:24 +00:00

Access log

This commit is contained in:
V2Ray 2015-10-09 17:43:27 +02:00
parent 09c76de65f
commit fc80d5e279
7 changed files with 107 additions and 0 deletions

77
common/log/access.go Normal file
View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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