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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package v2binding
 
import (
    "google.golang.org/protobuf/types/known/anypb"
 
    core "github.com/v2fly/v2ray-core/v5"
    "github.com/v2fly/v2ray-core/v5/app/commander"
    "github.com/v2fly/v2ray-core/v5/app/dispatcher"
    "github.com/v2fly/v2ray-core/v5/app/instman"
    "github.com/v2fly/v2ray-core/v5/app/instman/command"
    "github.com/v2fly/v2ray-core/v5/app/proxyman"
    "github.com/v2fly/v2ray-core/v5/app/router"
    "github.com/v2fly/v2ray-core/v5/common/net"
    "github.com/v2fly/v2ray-core/v5/common/serial"
    _ "github.com/v2fly/v2ray-core/v5/main/distro/all"
    "github.com/v2fly/v2ray-core/v5/proxy/blackhole"
    "github.com/v2fly/v2ray-core/v5/proxy/dokodemo"
)
 
type bindingInstance struct {
    started  bool
    instance *core.Instance
}
 
var binding bindingInstance
 
func (b *bindingInstance) startAPIInstance() {
    bindConfig := &core.Config{
        App: []*anypb.Any{
            serial.ToTypedMessage(&instman.Config{}),
            serial.ToTypedMessage(&commander.Config{
                Tag: "api",
                Service: []*anypb.Any{
                    serial.ToTypedMessage(&command.Config{}),
                },
            }),
            serial.ToTypedMessage(&router.Config{
                Rule: []*router.RoutingRule{
                    {
                        InboundTag: []string{"api"},
                        TargetTag: &router.RoutingRule_Tag{
                            Tag: "api",
                        },
                    },
                },
            }),
        },
        Inbound: []*core.InboundHandlerConfig{
            {
                Tag: "api",
                ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
                    PortRange: net.SinglePortRange(10999),
                    Listen:    net.NewIPOrDomain(net.AnyIP),
                }),
                ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
                    Address:  net.NewIPOrDomain(net.LocalHostIP),
                    Port:     uint32(10999),
                    Networks: []net.Network{net.Network_TCP},
                }),
            },
        },
        Outbound: []*core.OutboundHandlerConfig{
            {
                Tag:           "default-outbound",
                ProxySettings: serial.ToTypedMessage(&blackhole.Config{}),
            },
        },
    }
    bindConfig = withDefaultApps(bindConfig)
 
    instance, err := core.New(bindConfig)
    if err != nil {
        panic(err)
    }
    err = instance.Start()
    if err != nil {
        panic(err)
    }
    b.instance = instance
}
 
func withDefaultApps(config *core.Config) *core.Config {
    config.App = append(config.App, serial.ToTypedMessage(&dispatcher.Config{}))
    config.App = append(config.App, serial.ToTypedMessage(&proxyman.InboundConfig{}))
    config.App = append(config.App, serial.ToTypedMessage(&proxyman.OutboundConfig{}))
    return config
}
 
func StartAPIInstance(basedir string) {
    if binding.started {
        return
    }
    binding.started = true
    binding.startAPIInstance()
}