Refactor log

This commit is contained in:
世界
2022-07-12 15:17:29 +08:00
parent b47f3adbb3
commit 4fc763cfa2
46 changed files with 760 additions and 457 deletions

View File

@@ -18,7 +18,7 @@ type Block struct {
myOutboundAdapter
}
func NewBlock(logger log.Logger, tag string) *Block {
func NewBlock(logger log.ContextLogger, tag string) *Block {
return &Block{
myOutboundAdapter{
protocol: C.TypeBlock,
@@ -30,23 +30,23 @@ func NewBlock(logger log.Logger, tag string) *Block {
}
func (h *Block) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
h.logger.WithContext(ctx).Info("blocked connection to ", destination)
h.logger.InfoContext(ctx, "blocked connection to ", destination)
return nil, io.EOF
}
func (h *Block) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
h.logger.WithContext(ctx).Info("blocked packet connection to ", destination)
h.logger.InfoContext(ctx, "blocked packet connection to ", destination)
return nil, io.EOF
}
func (h *Block) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
conn.Close()
h.logger.WithContext(ctx).Info("blocked connection to ", metadata.Destination)
h.logger.InfoContext(ctx, "blocked connection to ", metadata.Destination)
return nil
}
func (h *Block) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
conn.Close()
h.logger.WithContext(ctx).Info("blocked packet connection to ", metadata.Destination)
h.logger.InfoContext(ctx, "blocked packet connection to ", metadata.Destination)
return nil
}

View File

@@ -7,31 +7,23 @@ import (
"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"
)
func New(router adapter.Router, logger log.Logger, index int, options option.Outbound) (adapter.Outbound, error) {
func New(router adapter.Router, logger log.ContextLogger, options option.Outbound) (adapter.Outbound, error) {
if common.IsEmpty(options) {
return nil, E.New("empty outbound config")
}
var tag string
if options.Tag != "" {
tag = options.Tag
} else {
tag = F.ToString(index)
}
outboundLogger := logger.WithPrefix(F.ToString("outbound/", options.Type, "[", tag, "]: "))
switch options.Type {
case C.TypeDirect:
return NewDirect(router, outboundLogger, options.Tag, options.DirectOptions), nil
return NewDirect(router, logger, options.Tag, options.DirectOptions), nil
case C.TypeBlock:
return NewBlock(outboundLogger, options.Tag), nil
return NewBlock(logger, options.Tag), nil
case C.TypeSocks:
return NewSocks(router, outboundLogger, options.Tag, options.SocksOptions)
return NewSocks(router, logger, options.Tag, options.SocksOptions)
case C.TypeHTTP:
return NewHTTP(router, outboundLogger, options.Tag, options.HTTPOptions), nil
return NewHTTP(router, logger, options.Tag, options.HTTPOptions), nil
case C.TypeShadowsocks:
return NewShadowsocks(router, outboundLogger, options.Tag, options.ShadowsocksOptions)
return NewShadowsocks(router, logger, options.Tag, options.ShadowsocksOptions)
default:
return nil, E.New("unknown outbound type: ", options.Type)
}

View File

@@ -18,7 +18,7 @@ import (
type myOutboundAdapter struct {
protocol string
logger log.Logger
logger log.ContextLogger
tag string
network []string
}

View File

@@ -23,7 +23,7 @@ type Direct struct {
overrideDestination M.Socksaddr
}
func NewDirect(router adapter.Router, logger log.Logger, tag string, options option.DirectOutboundOptions) *Direct {
func NewDirect(router adapter.Router, logger log.ContextLogger, tag string, options option.DirectOutboundOptions) *Direct {
outbound := &Direct{
myOutboundAdapter: myOutboundAdapter{
protocol: C.TypeDirect,
@@ -62,9 +62,9 @@ func (h *Direct) DialContext(ctx context.Context, network string, destination M.
}
switch network {
case C.NetworkTCP:
h.logger.WithContext(ctx).Info("outbound connection to ", destination)
h.logger.InfoContext(ctx, "outbound connection to ", destination)
case C.NetworkUDP:
h.logger.WithContext(ctx).Info("outbound packet connection to ", destination)
h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
}
return h.dialer.DialContext(ctx, network, destination)
}
@@ -73,7 +73,7 @@ func (h *Direct) ListenPacket(ctx context.Context, destination M.Socksaddr) (net
ctx, metadata := adapter.AppendContext(ctx)
metadata.Outbound = h.tag
metadata.Destination = destination
h.logger.WithContext(ctx).Info("outbound packet connection")
h.logger.InfoContext(ctx, "outbound packet connection")
return h.dialer.ListenPacket(ctx, destination)
}

View File

@@ -22,7 +22,7 @@ type HTTP struct {
client *http.Client
}
func NewHTTP(router adapter.Router, logger log.Logger, tag string, options option.HTTPOutboundOptions) *HTTP {
func NewHTTP(router adapter.Router, logger log.ContextLogger, tag string, options option.HTTPOutboundOptions) *HTTP {
return &HTTP{
myOutboundAdapter{
protocol: C.TypeHTTP,
@@ -38,7 +38,7 @@ func (h *HTTP) DialContext(ctx context.Context, network string, destination M.So
ctx, metadata := adapter.AppendContext(ctx)
metadata.Outbound = h.tag
metadata.Destination = destination
h.logger.WithContext(ctx).Info("outbound connection to ", destination)
h.logger.InfoContext(ctx, "outbound connection to ", destination)
return h.client.DialContext(ctx, network, destination)
}

View File

@@ -25,7 +25,7 @@ type Shadowsocks struct {
serverAddr M.Socksaddr
}
func NewShadowsocks(router adapter.Router, logger log.Logger, tag string, options option.ShadowsocksOutboundOptions) (*Shadowsocks, error) {
func NewShadowsocks(router adapter.Router, logger log.ContextLogger, tag string, options option.ShadowsocksOutboundOptions) (*Shadowsocks, error) {
method, err := shadowimpl.FetchMethod(options.Method, options.Password)
if err != nil {
return nil, err
@@ -49,14 +49,14 @@ func (h *Shadowsocks) DialContext(ctx context.Context, network string, destinati
metadata.Destination = destination
switch network {
case C.NetworkTCP:
h.logger.WithContext(ctx).Info("outbound connection to ", destination)
h.logger.InfoContext(ctx, "outbound connection to ", destination)
outConn, err := h.dialer.DialContext(ctx, C.NetworkTCP, h.serverAddr)
if err != nil {
return nil, err
}
return h.method.DialEarlyConn(outConn, destination), nil
case C.NetworkUDP:
h.logger.WithContext(ctx).Info("outbound packet connection to ", destination)
h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
outConn, err := h.dialer.DialContext(ctx, C.NetworkUDP, h.serverAddr)
if err != nil {
return nil, err
@@ -71,7 +71,7 @@ func (h *Shadowsocks) ListenPacket(ctx context.Context, destination M.Socksaddr)
ctx, metadata := adapter.AppendContext(ctx)
metadata.Outbound = h.tag
metadata.Destination = destination
h.logger.WithContext(ctx).Info("outbound packet connection to ", h.serverAddr)
h.logger.InfoContext(ctx, "outbound packet connection to ", h.serverAddr)
outConn, err := h.dialer.DialContext(ctx, "udp", h.serverAddr)
if err != nil {
return nil, err

View File

@@ -21,7 +21,7 @@ type Socks struct {
client *socks.Client
}
func NewSocks(router adapter.Router, logger log.Logger, tag string, options option.SocksOutboundOptions) (*Socks, error) {
func NewSocks(router adapter.Router, logger log.ContextLogger, tag string, options option.SocksOutboundOptions) (*Socks, error) {
detour := dialer.NewOutbound(router, options.OutboundDialerOptions)
var version socks.Version
var err error
@@ -50,9 +50,9 @@ func (h *Socks) DialContext(ctx context.Context, network string, destination M.S
metadata.Destination = destination
switch network {
case C.NetworkTCP:
h.logger.WithContext(ctx).Info("outbound connection to ", destination)
h.logger.InfoContext(ctx, "outbound connection to ", destination)
case C.NetworkUDP:
h.logger.WithContext(ctx).Info("outbound packet connection to ", destination)
h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
default:
panic("unknown network " + network)
}
@@ -63,7 +63,7 @@ func (h *Socks) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.
ctx, metadata := adapter.AppendContext(ctx)
metadata.Outbound = h.tag
metadata.Destination = destination
h.logger.WithContext(ctx).Info("outbound packet connection to ", destination)
h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
return h.client.ListenPacket(ctx, destination)
}