| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- package route
- import (
- "context"
- "net"
- "github.com/sagernet/sing-box/adapter"
- "github.com/sagernet/sing-box/common/dialer"
- C "github.com/sagernet/sing-box/constant"
- R "github.com/sagernet/sing-box/route/rule"
- "github.com/sagernet/sing/common"
- "github.com/sagernet/sing/common/buf"
- "github.com/sagernet/sing/common/bufio"
- E "github.com/sagernet/sing/common/exceptions"
- N "github.com/sagernet/sing/common/network"
- )
- // DialRoutePacketConnection dials a routed connected UDP packet connection for metadata.
- func (r *Router) DialRoutePacketConnection(ctx context.Context, metadata adapter.InboundContext) (N.PacketConn, error) {
- metadata.Network = N.NetworkUDP
- metadata.UDPConnect = true
- ctx = adapter.WithContext(ctx, &metadata)
- selectedRule, selectedOutbound, err := r.selectRoutedOutbound(ctx, &metadata, N.NetworkUDP)
- if err != nil {
- return nil, err
- }
- var remoteConn net.Conn
- if len(metadata.DestinationAddresses) > 0 || metadata.Destination.IsIP() {
- remoteConn, err = dialer.DialSerialNetwork(
- ctx,
- selectedOutbound,
- N.NetworkUDP,
- metadata.Destination,
- metadata.DestinationAddresses,
- metadata.NetworkStrategy,
- metadata.NetworkType,
- metadata.FallbackNetworkType,
- metadata.FallbackDelay,
- )
- } else {
- remoteConn, err = selectedOutbound.DialContext(ctx, N.NetworkUDP, metadata.Destination)
- }
- if err != nil {
- return nil, err
- }
- var packetConn N.PacketConn = bufio.NewUnbindPacketConn(remoteConn)
- for _, tracker := range r.trackers {
- packetConn = tracker.RoutedPacketConnection(ctx, packetConn, metadata, selectedRule, selectedOutbound)
- }
- if metadata.FakeIP {
- packetConn = bufio.NewNATPacketConn(bufio.NewNetPacketConn(packetConn), metadata.OriginDestination, metadata.Destination)
- }
- return packetConn, nil
- }
- func (r *Router) selectRoutedOutbound(
- ctx context.Context,
- metadata *adapter.InboundContext,
- network string,
- ) (adapter.Rule, adapter.Outbound, error) {
- selectedRule, _, buffers, packetBuffers, err := r.matchRule(ctx, metadata, false, false, nil, nil)
- if len(buffers) > 0 {
- buf.ReleaseMulti(buffers)
- }
- if len(packetBuffers) > 0 {
- N.ReleaseMultiPacketBuffer(packetBuffers)
- }
- if err != nil {
- return nil, nil, err
- }
- var selectedOutbound adapter.Outbound
- if selectedRule != nil {
- switch action := selectedRule.Action().(type) {
- case *R.RuleActionRoute:
- var loaded bool
- selectedOutbound, loaded = r.outbound.Outbound(action.Outbound)
- if !loaded {
- return nil, nil, E.New("outbound not found: ", action.Outbound)
- }
- case *R.RuleActionBypass:
- if action.Outbound != "" {
- var loaded bool
- selectedOutbound, loaded = r.outbound.Outbound(action.Outbound)
- if !loaded {
- return nil, nil, E.New("outbound not found: ", action.Outbound)
- }
- }
- case *R.RuleActionReject:
- if action.Method == C.RuleActionRejectMethodReply {
- return nil, nil, E.New("reject method `reply` is not supported for dialed connections")
- }
- return nil, nil, action.Error(ctx)
- case *R.RuleActionHijackDNS:
- return nil, nil, E.New("DNS hijack is not supported for dialed connections")
- }
- }
- if selectedOutbound == nil {
- selectedOutbound = r.outbound.Default()
- }
- if !common.Contains(selectedOutbound.Network(), network) {
- return nil, nil, E.New(network, " is not supported by outbound: ", selectedOutbound.Tag())
- }
- return selectedRule, selectedOutbound, nil
- }
|