Hunter0x7c7
2022-08-11 a82f9cb69f63aaeba40c024960deda7d75b9fece
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package scenarios
 
import (
    "testing"
    "time"
 
    "golang.org/x/sync/errgroup"
    "google.golang.org/protobuf/types/known/anypb"
 
    core "github.com/v2fly/v2ray-core/v5"
    "github.com/v2fly/v2ray-core/v5/app/log"
    "github.com/v2fly/v2ray-core/v5/app/proxyman"
    "github.com/v2fly/v2ray-core/v5/common"
    clog "github.com/v2fly/v2ray-core/v5/common/log"
    "github.com/v2fly/v2ray-core/v5/common/net"
    "github.com/v2fly/v2ray-core/v5/common/protocol"
    "github.com/v2fly/v2ray-core/v5/common/serial"
    "github.com/v2fly/v2ray-core/v5/common/uuid"
    "github.com/v2fly/v2ray-core/v5/proxy/dokodemo"
    "github.com/v2fly/v2ray-core/v5/proxy/freedom"
    "github.com/v2fly/v2ray-core/v5/proxy/vmess"
    "github.com/v2fly/v2ray-core/v5/proxy/vmess/inbound"
    "github.com/v2fly/v2ray-core/v5/proxy/vmess/outbound"
    "github.com/v2fly/v2ray-core/v5/testing/servers/tcp"
    "github.com/v2fly/v2ray-core/v5/testing/servers/udp"
)
 
func TestDokodemoTCP(t *testing.T) {
    tcpServer := tcp.Server{
        MsgProcessor: xor,
    }
    dest, err := tcpServer.Start()
    common.Must(err)
    defer tcpServer.Close()
 
    userID := protocol.NewID(uuid.New())
    serverPort := tcp.PickPort()
    serverConfig := &core.Config{
        App: []*anypb.Any{
            serial.ToTypedMessage(&log.Config{
                Error: &log.LogSpecification{Level: clog.Severity_Debug, Type: log.LogType_Console},
            }),
        },
        Inbound: []*core.InboundHandlerConfig{
            {
                ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
                    PortRange: net.SinglePortRange(serverPort),
                    Listen:    net.NewIPOrDomain(net.LocalHostIP),
                }),
                ProxySettings: serial.ToTypedMessage(&inbound.Config{
                    User: []*protocol.User{
                        {
                            Account: serial.ToTypedMessage(&vmess.Account{
                                Id: userID.String(),
                            }),
                        },
                    },
                }),
            },
        },
        Outbound: []*core.OutboundHandlerConfig{
            {
                ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
            },
        },
    }
 
    clientPort := uint32(tcp.PickPort())
    clientPortRange := uint32(5)
    clientConfig := &core.Config{
        App: []*anypb.Any{
            serial.ToTypedMessage(&log.Config{
                Error: &log.LogSpecification{Level: clog.Severity_Debug, Type: log.LogType_Console},
            }),
        },
        Inbound: []*core.InboundHandlerConfig{
            {
                ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
                    PortRange: &net.PortRange{From: clientPort, To: clientPort + clientPortRange},
                    Listen:    net.NewIPOrDomain(net.LocalHostIP),
                }),
                ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
                    Address: net.NewIPOrDomain(dest.Address),
                    Port:    uint32(dest.Port),
                    NetworkList: &net.NetworkList{
                        Network: []net.Network{net.Network_TCP},
                    },
                }),
            },
        },
        Outbound: []*core.OutboundHandlerConfig{
            {
                ProxySettings: serial.ToTypedMessage(&outbound.Config{
                    Receiver: []*protocol.ServerEndpoint{
                        {
                            Address: net.NewIPOrDomain(net.LocalHostIP),
                            Port:    uint32(serverPort),
                            User: []*protocol.User{
                                {
                                    Account: serial.ToTypedMessage(&vmess.Account{
                                        Id: userID.String(),
                                    }),
                                },
                            },
                        },
                    },
                }),
            },
        },
    }
 
    servers, err := InitializeServerConfigs(serverConfig, clientConfig)
    common.Must(err)
    defer CloseAllServers(servers)
 
    for port := clientPort; port <= clientPort+clientPortRange; port++ {
        if err := testTCPConn(net.Port(port), 1024, time.Second*2)(); err != nil {
            t.Error(err)
        }
    }
}
 
func TestDokodemoUDP(t *testing.T) {
    udpServer := udp.Server{
        MsgProcessor: xor,
    }
    dest, err := udpServer.Start()
    common.Must(err)
    defer udpServer.Close()
 
    userID := protocol.NewID(uuid.New())
    serverPort := tcp.PickPort()
    serverConfig := &core.Config{
        Inbound: []*core.InboundHandlerConfig{
            {
                ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
                    PortRange: net.SinglePortRange(serverPort),
                    Listen:    net.NewIPOrDomain(net.LocalHostIP),
                }),
                ProxySettings: serial.ToTypedMessage(&inbound.Config{
                    User: []*protocol.User{
                        {
                            Account: serial.ToTypedMessage(&vmess.Account{
                                Id: userID.String(),
                            }),
                        },
                    },
                }),
            },
        },
        Outbound: []*core.OutboundHandlerConfig{
            {
                ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
            },
        },
    }
 
    clientPort := uint32(udp.PickPort())
    clientPortRange := uint32(5)
    clientConfig := &core.Config{
        Inbound: []*core.InboundHandlerConfig{
            {
                ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
                    PortRange: &net.PortRange{From: clientPort, To: clientPort + clientPortRange},
                    Listen:    net.NewIPOrDomain(net.LocalHostIP),
                }),
                ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
                    Address: net.NewIPOrDomain(dest.Address),
                    Port:    uint32(dest.Port),
                    NetworkList: &net.NetworkList{
                        Network: []net.Network{net.Network_UDP},
                    },
                }),
            },
        },
        Outbound: []*core.OutboundHandlerConfig{
            {
                ProxySettings: serial.ToTypedMessage(&outbound.Config{
                    Receiver: []*protocol.ServerEndpoint{
                        {
                            Address: net.NewIPOrDomain(net.LocalHostIP),
                            Port:    uint32(serverPort),
                            User: []*protocol.User{
                                {
                                    Account: serial.ToTypedMessage(&vmess.Account{
                                        Id: userID.String(),
                                    }),
                                },
                            },
                        },
                    },
                }),
            },
        },
    }
 
    servers, err := InitializeServerConfigs(serverConfig, clientConfig)
    common.Must(err)
    defer CloseAllServers(servers)
 
    var errg errgroup.Group
    for port := clientPort; port <= clientPort+clientPortRange; port++ {
        errg.Go(testUDPConn(net.Port(port), 1024, time.Second*5))
    }
    if err := errg.Wait(); err != nil {
        t.Error(err)
    }
}