2020-11-25 11:01:53 +00:00
|
|
|
package log
|
|
|
|
|
2020-12-04 01:36:16 +00:00
|
|
|
//go:generate go run github.com/xtls/xray-core/common/errors/errorgen
|
2020-11-25 11:01:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-09-11 14:45:47 +00:00
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2020-11-25 11:01:53 +00:00
|
|
|
"sync"
|
|
|
|
|
2020-12-04 01:36:16 +00:00
|
|
|
"github.com/xtls/xray-core/common"
|
2024-06-29 18:32:57 +00:00
|
|
|
"github.com/xtls/xray-core/common/errors"
|
2020-12-04 01:36:16 +00:00
|
|
|
"github.com/xtls/xray-core/common/log"
|
2020-11-25 11:01:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Instance is a log.Handler that handles logs.
|
|
|
|
type Instance struct {
|
|
|
|
sync.RWMutex
|
|
|
|
config *Config
|
|
|
|
accessLogger log.Handler
|
|
|
|
errorLogger log.Handler
|
|
|
|
active bool
|
2021-03-06 16:29:17 +00:00
|
|
|
dns bool
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new log.Instance based on the given config.
|
|
|
|
func New(ctx context.Context, config *Config) (*Instance, error) {
|
|
|
|
g := &Instance{
|
|
|
|
config: config,
|
|
|
|
active: false,
|
2021-03-06 16:29:17 +00:00
|
|
|
dns: config.EnableDnsLog,
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
log.RegisterHandler(g)
|
|
|
|
|
2024-07-11 22:20:06 +00:00
|
|
|
// Start logger instantly on initialization
|
|
|
|
// Other modules would log during initialization
|
2020-11-25 11:01:53 +00:00
|
|
|
if err := g.startInternal(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-06-29 18:32:57 +00:00
|
|
|
errors.LogDebug(ctx, "Logger started")
|
2020-11-25 11:01:53 +00:00
|
|
|
return g, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Instance) initAccessLogger() error {
|
|
|
|
handler, err := createHandler(g.config.AccessLogType, HandlerCreatorOptions{
|
|
|
|
Path: g.config.AccessLogPath,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
g.accessLogger = handler
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Instance) initErrorLogger() error {
|
|
|
|
handler, err := createHandler(g.config.ErrorLogType, HandlerCreatorOptions{
|
|
|
|
Path: g.config.ErrorLogPath,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
g.errorLogger = handler
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type implements common.HasType.
|
|
|
|
func (*Instance) Type() interface{} {
|
|
|
|
return (*Instance)(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Instance) startInternal() error {
|
|
|
|
g.Lock()
|
|
|
|
defer g.Unlock()
|
|
|
|
|
|
|
|
if g.active {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
g.active = true
|
|
|
|
|
|
|
|
if err := g.initAccessLogger(); err != nil {
|
2024-06-29 18:32:57 +00:00
|
|
|
return errors.New("failed to initialize access logger").Base(err).AtWarning()
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
if err := g.initErrorLogger(); err != nil {
|
2024-06-29 18:32:57 +00:00
|
|
|
return errors.New("failed to initialize error logger").Base(err).AtWarning()
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start implements common.Runnable.Start().
|
|
|
|
func (g *Instance) Start() error {
|
|
|
|
return g.startInternal()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle implements log.Handler.
|
|
|
|
func (g *Instance) Handle(msg log.Message) {
|
|
|
|
g.RLock()
|
|
|
|
defer g.RUnlock()
|
|
|
|
|
|
|
|
if !g.active {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-11 14:45:47 +00:00
|
|
|
var Msg log.Message
|
|
|
|
if g.config.MaskAddress != "" {
|
|
|
|
Msg = &MaskedMsgWrapper{Message: msg, config: g.config}
|
|
|
|
} else {
|
|
|
|
Msg = msg
|
|
|
|
}
|
|
|
|
|
2020-11-25 11:01:53 +00:00
|
|
|
switch msg := msg.(type) {
|
|
|
|
case *log.AccessMessage:
|
|
|
|
if g.accessLogger != nil {
|
2024-09-11 14:45:47 +00:00
|
|
|
g.accessLogger.Handle(Msg)
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
2021-03-06 16:29:17 +00:00
|
|
|
case *log.DNSLog:
|
|
|
|
if g.dns && g.accessLogger != nil {
|
2024-09-11 14:45:47 +00:00
|
|
|
g.accessLogger.Handle(Msg)
|
2021-03-06 16:29:17 +00:00
|
|
|
}
|
2020-11-25 11:01:53 +00:00
|
|
|
case *log.GeneralMessage:
|
|
|
|
if g.errorLogger != nil && msg.Severity <= g.config.ErrorLogLevel {
|
2024-09-11 14:45:47 +00:00
|
|
|
g.errorLogger.Handle(Msg)
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
// Swallow
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements common.Closable.Close().
|
|
|
|
func (g *Instance) Close() error {
|
2024-06-29 18:32:57 +00:00
|
|
|
errors.LogDebug(context.Background(), "Logger closing")
|
2020-11-25 11:01:53 +00:00
|
|
|
|
|
|
|
g.Lock()
|
|
|
|
defer g.Unlock()
|
|
|
|
|
|
|
|
if !g.active {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
g.active = false
|
|
|
|
|
|
|
|
common.Close(g.accessLogger)
|
|
|
|
g.accessLogger = nil
|
|
|
|
|
|
|
|
common.Close(g.errorLogger)
|
|
|
|
g.errorLogger = nil
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-11 14:45:47 +00:00
|
|
|
// MaskedMsgWrapper is to wrap the string() method to mask IP addresses in the log.
|
|
|
|
type MaskedMsgWrapper struct {
|
|
|
|
log.Message
|
|
|
|
config *Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MaskedMsgWrapper) String() string {
|
|
|
|
str := m.Message.String()
|
|
|
|
|
|
|
|
ipv4Regex := regexp.MustCompile(`(\d{1,3}\.){3}\d{1,3}`)
|
|
|
|
ipv6Regex := regexp.MustCompile(`((?:[\da-fA-F]{0,4}:[\da-fA-F]{0,4}){2,7})(?:[\/\\%](\d{1,3}))?`)
|
|
|
|
|
|
|
|
// Process ipv4
|
|
|
|
maskedMsg := ipv4Regex.ReplaceAllStringFunc(str, func(ip string) string {
|
|
|
|
parts := strings.Split(ip, ".")
|
|
|
|
switch m.config.MaskAddress {
|
|
|
|
case "half":
|
|
|
|
return fmt.Sprintf("%s.%s.*.*", parts[0], parts[1])
|
|
|
|
case "quarter":
|
|
|
|
return fmt.Sprintf("%s.*.*.*", parts[0])
|
|
|
|
case "full":
|
|
|
|
return "[Masked IPv4]"
|
|
|
|
default:
|
|
|
|
return ip
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// process ipv6
|
|
|
|
maskedMsg = ipv6Regex.ReplaceAllStringFunc(maskedMsg, func(ip string) string {
|
|
|
|
parts := strings.Split(ip, ":")
|
|
|
|
switch m.config.MaskAddress {
|
|
|
|
case "half":
|
|
|
|
if len(parts) >= 2 {
|
|
|
|
return fmt.Sprintf("%s:%s::/32", parts[0], parts[1])
|
|
|
|
}
|
|
|
|
case "quarter":
|
|
|
|
if len(parts) >= 1 {
|
|
|
|
return fmt.Sprintf("%s::/16", parts[0])
|
|
|
|
}
|
|
|
|
case "full":
|
|
|
|
return "Masked IPv6" // Do not use [Masked IPv6] like ipv4, or you will get "[[Masked IPv6]]" (v6 address already has [])
|
|
|
|
default:
|
|
|
|
return ip
|
|
|
|
}
|
|
|
|
return ip
|
|
|
|
})
|
|
|
|
|
|
|
|
return maskedMsg
|
|
|
|
}
|
|
|
|
|
2020-11-25 11:01:53 +00:00
|
|
|
func init() {
|
|
|
|
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
|
|
return New(ctx, config.(*Config))
|
|
|
|
}))
|
|
|
|
}
|