|
| 1 | +package group |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net" |
| 6 | + |
| 7 | + "github.com/sagernet/sing-box/adapter" |
| 8 | + "github.com/sagernet/sing-box/adapter/outbound" |
| 9 | + "github.com/sagernet/sing-box/constant" |
| 10 | + "github.com/sagernet/sing-box/log" |
| 11 | + "github.com/sagernet/sing-box/option" |
| 12 | + "github.com/sagernet/sing-box/protocol/group" |
| 13 | + "github.com/sagernet/sing/common/metadata" |
| 14 | + "github.com/sagernet/sing/common/network" |
| 15 | + "go.opentelemetry.io/otel" |
| 16 | + "go.opentelemetry.io/otel/attribute" |
| 17 | + "go.opentelemetry.io/otel/trace" |
| 18 | +) |
| 19 | + |
| 20 | +func RegisterSelector(registry *outbound.Registry) { |
| 21 | + outbound.Register[option.SelectorOutboundOptions](registry, constant.TypeSelector, NewSelector) |
| 22 | +} |
| 23 | + |
| 24 | +const tracerName = "github.com/getlantern/sing-box-extensions/protocol/group" |
| 25 | + |
| 26 | +type Selector struct { |
| 27 | + *group.Selector |
| 28 | +} |
| 29 | + |
| 30 | +func NewSelector(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.SelectorOutboundOptions) (adapter.Outbound, error) { |
| 31 | + outbound, err := group.NewSelector(ctx, router, logger, tag, options) |
| 32 | + if err != nil { |
| 33 | + return nil, err |
| 34 | + } |
| 35 | + |
| 36 | + return &Selector{outbound.(*group.Selector)}, nil |
| 37 | +} |
| 38 | + |
| 39 | +func (s *Selector) DialContext(ctx context.Context, network string, destination metadata.Socksaddr) (net.Conn, error) { |
| 40 | + ctx, span := otel.Tracer(tracerName).Start(ctx, "Selector.DialContext", trace.WithAttributes( |
| 41 | + attribute.String("network", network), |
| 42 | + attribute.StringSlice("supported_network_options", s.Network()), |
| 43 | + attribute.String("outbound", s.Now()), |
| 44 | + attribute.String("tag", s.Tag()), |
| 45 | + attribute.String("type", s.Type()), |
| 46 | + )) |
| 47 | + defer span.End() |
| 48 | + conn, err := s.Selector.DialContext(ctx, network, destination) |
| 49 | + span.RecordError(err) |
| 50 | + return conn, err |
| 51 | +} |
| 52 | + |
| 53 | +func (s *Selector) ListenPacket(ctx context.Context, destination metadata.Socksaddr) (net.PacketConn, error) { |
| 54 | + ctx, span := otel.Tracer(tracerName).Start(ctx, "Selector.ListenPacket", trace.WithAttributes( |
| 55 | + attribute.StringSlice("supported_network_options", s.Network()), |
| 56 | + attribute.String("outbound", s.Now()), |
| 57 | + attribute.String("tag", s.Tag()), |
| 58 | + attribute.String("type", s.Type()), |
| 59 | + )) |
| 60 | + defer span.End() |
| 61 | + conn, err := s.Selector.ListenPacket(ctx, destination) |
| 62 | + span.RecordError(err) |
| 63 | + return conn, err |
| 64 | +} |
| 65 | + |
| 66 | +func (s *Selector) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose network.CloseHandlerFunc) { |
| 67 | + ctx, span := otel.Tracer(tracerName).Start(ctx, "Selector.NewConnectionEx", trace.WithAttributes( |
| 68 | + attribute.String("inbound", s.Now()), |
| 69 | + attribute.String("outbound", metadata.Outbound), |
| 70 | + attribute.String("protocol", metadata.Protocol), |
| 71 | + attribute.String("tag", s.Tag()), |
| 72 | + attribute.String("type", s.Type()), |
| 73 | + )) |
| 74 | + defer span.End() |
| 75 | + |
| 76 | + s.Selector.NewConnectionEx(ctx, conn, metadata, func(it error) { |
| 77 | + span.RecordError(it) |
| 78 | + onClose(it) |
| 79 | + }) |
| 80 | +} |
| 81 | +func (s *Selector) NewPacketConnectionEx(ctx context.Context, conn network.PacketConn, metadata adapter.InboundContext, onClose network.CloseHandlerFunc) { |
| 82 | + ctx, span := otel.Tracer(tracerName).Start(ctx, "Selector.NewPacketConnectionEx", trace.WithAttributes( |
| 83 | + attribute.String("inbound", s.Now()), |
| 84 | + attribute.String("outbound", metadata.Outbound), |
| 85 | + attribute.String("protocol", metadata.Protocol), |
| 86 | + attribute.String("tag", s.Tag()), |
| 87 | + attribute.String("type", s.Type()), |
| 88 | + )) |
| 89 | + defer span.End() |
| 90 | + |
| 91 | + s.Selector.NewPacketConnectionEx(ctx, conn, metadata, func(it error) { |
| 92 | + span.RecordError(it) |
| 93 | + onClose(it) |
| 94 | + }) |
| 95 | +} |
0 commit comments