Hunter0x7c7
2022-08-11 b8230139fb40edea387617b6accd8371e37eda58
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//go:build !confonly
// +build !confonly
 
package taggedimpl
 
import (
    "context"
 
    core "github.com/v2fly/v2ray-core/v5"
    "github.com/v2fly/v2ray-core/v5/common/net"
    "github.com/v2fly/v2ray-core/v5/common/session"
    "github.com/v2fly/v2ray-core/v5/features/routing"
    "github.com/v2fly/v2ray-core/v5/transport/internet/tagged"
)
 
func DialTaggedOutbound(ctx context.Context, dest net.Destination, tag string) (net.Conn, error) {
    var dispatcher routing.Dispatcher
    if core.FromContext(ctx) == nil {
        return nil, newError("Instance context variable is not in context, dial denied. ")
    }
    if err := core.RequireFeatures(ctx, func(dispatcherInstance routing.Dispatcher) {
        dispatcher = dispatcherInstance
    }); err != nil {
        return nil, newError("Required Feature dispatcher not resolved").Base(err)
    }
 
    content := new(session.Content)
    content.SkipDNSResolve = true
 
    ctx = session.ContextWithContent(ctx, content)
    ctx = session.SetForcedOutboundTagToContext(ctx, tag)
 
    r, err := dispatcher.Dispatch(ctx, dest)
    if err != nil {
        return nil, err
    }
    var readerOpt net.ConnectionOption
    if dest.Network == net.Network_TCP {
        readerOpt = net.ConnectionOutputMulti(r.Reader)
    } else {
        readerOpt = net.ConnectionOutputMultiUDP(r.Reader)
    }
    return net.NewConnection(net.ConnectionInputMulti(r.Writer), readerOpt), nil
}
 
func init() {
    tagged.Dialer = DialTaggedOutbound
}