mirror of
https://github.com/SagerNet/sing-box.git
synced 2025-04-04 04:17:36 +03:00
Inbound rule support
This commit is contained in:
parent
9f4c0ff624
commit
7c57eb70e8
34 changed files with 622 additions and 247 deletions
23
log/log.go
23
log/log.go
|
@ -3,10 +3,27 @@ package log
|
|||
import (
|
||||
"context"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/sagernet/sing-box/option"
|
||||
)
|
||||
|
||||
type Logger interface {
|
||||
logrus.FieldLogger
|
||||
WithContext(ctx context.Context) *logrus.Entry
|
||||
Trace(args ...interface{})
|
||||
Debug(args ...interface{})
|
||||
Info(args ...interface{})
|
||||
Print(args ...interface{})
|
||||
Warn(args ...interface{})
|
||||
Warning(args ...interface{})
|
||||
Error(args ...interface{})
|
||||
Fatal(args ...interface{})
|
||||
Panic(args ...interface{})
|
||||
WithContext(ctx context.Context) Logger
|
||||
WithPrefix(prefix string) Logger
|
||||
Close() error
|
||||
}
|
||||
|
||||
func NewLogger(options option.LogOption) (Logger, error) {
|
||||
if options.Disabled {
|
||||
return NewNopLogger(), nil
|
||||
}
|
||||
return NewLogrusLogger(options)
|
||||
}
|
||||
|
|
65
log/logrus.go
Normal file
65
log/logrus.go
Normal file
|
@ -0,0 +1,65 @@
|
|||
package log
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
"github.com/sagernet/sing-box/option"
|
||||
"github.com/sagernet/sing/common"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
F "github.com/sagernet/sing/common/format"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var _ Logger = (*logrusLogger)(nil)
|
||||
|
||||
type logrusLogger struct {
|
||||
abstractLogrusLogger
|
||||
output *os.File
|
||||
}
|
||||
|
||||
type abstractLogrusLogger interface {
|
||||
logrus.Ext1FieldLogger
|
||||
WithContext(ctx context.Context) *logrus.Entry
|
||||
}
|
||||
|
||||
func NewLogrusLogger(options option.LogOption) (*logrusLogger, error) {
|
||||
logger := logrus.New()
|
||||
logger.SetLevel(logrus.TraceLevel)
|
||||
logger.Formatter.(*logrus.TextFormatter).ForceColors = true
|
||||
logger.AddHook(new(logrusHook))
|
||||
var output *os.File
|
||||
var err error
|
||||
if options.Level != "" {
|
||||
logger.Level, err = logrus.ParseLevel(options.Level)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if options.Output != "" {
|
||||
output, err = os.OpenFile(options.Output, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
|
||||
if err != nil {
|
||||
return nil, E.Extend(err, "open log output")
|
||||
}
|
||||
logger.SetOutput(output)
|
||||
}
|
||||
return &logrusLogger{logger, output}, nil
|
||||
}
|
||||
|
||||
func (l *logrusLogger) WithContext(ctx context.Context) Logger {
|
||||
return &logrusLogger{l.abstractLogrusLogger.WithContext(ctx), nil}
|
||||
}
|
||||
|
||||
func (l *logrusLogger) WithPrefix(prefix string) Logger {
|
||||
if entry, isEntry := l.abstractLogrusLogger.(*logrus.Entry); isEntry {
|
||||
loadedPrefix := entry.Data["prefix"]
|
||||
if loadedPrefix != "" {
|
||||
prefix = F.ToString(loadedPrefix, prefix)
|
||||
}
|
||||
}
|
||||
return &logrusLogger{l.WithField("prefix", prefix), nil}
|
||||
}
|
||||
|
||||
func (l *logrusLogger) Close() error {
|
||||
return common.Close(common.PtrOrNil(l.output))
|
||||
}
|
|
@ -6,13 +6,13 @@ import (
|
|||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type Hook struct{}
|
||||
type logrusHook struct{}
|
||||
|
||||
func (h *Hook) Levels() []logrus.Level {
|
||||
func (h *logrusHook) Levels() []logrus.Level {
|
||||
return logrus.AllLevels
|
||||
}
|
||||
|
||||
func (h *Hook) Fire(entry *logrus.Entry) error {
|
||||
func (h *logrusHook) Fire(entry *logrus.Entry) error {
|
||||
if prefix, loaded := entry.Data["prefix"]; loaded {
|
||||
prefixStr := prefix.(string)
|
||||
delete(entry.Data, "prefix")
|
||||
|
@ -20,7 +20,7 @@ func (h *Hook) Fire(entry *logrus.Entry) error {
|
|||
}
|
||||
var idCtx *idContext
|
||||
if entry.Context != nil {
|
||||
idCtx = entry.Context.Value(idType).(*idContext)
|
||||
idCtx, _ = entry.Context.Value(idType).(*idContext)
|
||||
}
|
||||
if idCtx != nil {
|
||||
var color aurora.Color
|
50
log/nop.go
Normal file
50
log/nop.go
Normal file
|
@ -0,0 +1,50 @@
|
|||
package log
|
||||
|
||||
import "context"
|
||||
|
||||
var _ Logger = (*nopLogger)(nil)
|
||||
|
||||
type nopLogger struct{}
|
||||
|
||||
func NewNopLogger() Logger {
|
||||
return (*nopLogger)(nil)
|
||||
}
|
||||
|
||||
func (l *nopLogger) Trace(args ...interface{}) {
|
||||
}
|
||||
|
||||
func (l *nopLogger) Debug(args ...interface{}) {
|
||||
}
|
||||
|
||||
func (l *nopLogger) Info(args ...interface{}) {
|
||||
}
|
||||
|
||||
func (l *nopLogger) Print(args ...interface{}) {
|
||||
}
|
||||
|
||||
func (l *nopLogger) Warn(args ...interface{}) {
|
||||
}
|
||||
|
||||
func (l *nopLogger) Warning(args ...interface{}) {
|
||||
}
|
||||
|
||||
func (l *nopLogger) Error(args ...interface{}) {
|
||||
}
|
||||
|
||||
func (l *nopLogger) Fatal(args ...interface{}) {
|
||||
}
|
||||
|
||||
func (l *nopLogger) Panic(args ...interface{}) {
|
||||
}
|
||||
|
||||
func (l *nopLogger) WithContext(ctx context.Context) Logger {
|
||||
return l
|
||||
}
|
||||
|
||||
func (l *nopLogger) WithPrefix(prefix string) Logger {
|
||||
return l
|
||||
}
|
||||
|
||||
func (l *nopLogger) Close() error {
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue