2015-10-09 11:43:27 -04:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
2015-10-13 15:55:12 -04:00
|
|
|
|
|
|
|
"github.com/v2ray/v2ray-core/common/platform"
|
2015-10-09 11:43:27 -04:00
|
|
|
)
|
|
|
|
|
2015-10-11 08:46:12 -04:00
|
|
|
// AccessStatus is the status of an access request from clients.
|
2015-10-09 11:43:27 -04:00
|
|
|
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
|
2015-10-10 17:41:45 -04:00
|
|
|
file *os.File
|
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *fileAccessLogger) close() {
|
|
|
|
logger.file.Close()
|
2015-10-09 11:43:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *fileAccessLogger) Log(from, to string, status AccessStatus, reason string) {
|
2015-10-11 10:06:27 -04:00
|
|
|
select {
|
|
|
|
case logger.queue <- &accessLog{
|
2015-10-09 11:43:27 -04:00
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
Status: status,
|
|
|
|
Reason: reason,
|
2015-10-11 10:06:27 -04:00
|
|
|
}:
|
|
|
|
default:
|
|
|
|
// We don't expect this to happen, but don't want to block main thread as well.
|
2015-10-09 11:43:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2015-10-13 15:45:09 -04:00
|
|
|
log.Printf("Unable to create or open file (%s): %v%s", path, err, platform.LineSeparator())
|
2015-10-09 11:43:27 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &fileAccessLogger{
|
|
|
|
queue: make(chan *accessLog, 16),
|
|
|
|
logger: log.New(file, "", log.Ldate|log.Ltime),
|
2015-10-10 17:41:45 -04:00
|
|
|
file: file,
|
2015-10-09 11:43:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var accessLoggerInstance accessLogger = &noOpAccessLogger{}
|
|
|
|
|
2015-10-11 08:46:12 -04:00
|
|
|
// InitAccessLogger initializes the access logger to write into the give file.
|
2015-10-09 11:43:27 -04:00
|
|
|
func InitAccessLogger(file string) {
|
|
|
|
logger := newFileAccessLogger(file)
|
|
|
|
if logger != nil {
|
|
|
|
go logger.(*fileAccessLogger).Run()
|
|
|
|
accessLoggerInstance = logger
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-11 08:46:12 -04:00
|
|
|
// Access writes an access log.
|
2015-10-09 11:43:27 -04:00
|
|
|
func Access(from, to string, status AccessStatus, reason string) {
|
|
|
|
accessLoggerInstance.Log(from, to, status, reason)
|
|
|
|
}
|