mirror of
https://github.com/SagerNet/sing-box.git
synced 2026-04-14 12:48:28 +10:00
Refactor log
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ import (
|
||||
|
||||
type myOutboundAdapter struct {
|
||||
protocol string
|
||||
logger log.Logger
|
||||
logger log.ContextLogger
|
||||
tag string
|
||||
network []string
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user