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
package outbound_test
 
import (
    "context"
    "testing"
    _ "unsafe"
 
    "google.golang.org/protobuf/types/known/anypb"
 
    core "github.com/v2fly/v2ray-core/v5"
    "github.com/v2fly/v2ray-core/v5/app/policy"
    . "github.com/v2fly/v2ray-core/v5/app/proxyman/outbound"
    "github.com/v2fly/v2ray-core/v5/app/stats"
    "github.com/v2fly/v2ray-core/v5/common/net"
    "github.com/v2fly/v2ray-core/v5/common/serial"
    "github.com/v2fly/v2ray-core/v5/features/outbound"
    "github.com/v2fly/v2ray-core/v5/proxy/freedom"
    "github.com/v2fly/v2ray-core/v5/transport/internet"
)
 
func TestInterfaces(t *testing.T) {
    _ = (outbound.Handler)(new(Handler))
    _ = (outbound.Manager)(new(Manager))
}
 
//go:linkname toContext github.com/v2fly/v2ray-core/v5.toContext
func toContext(ctx context.Context, v *core.Instance) context.Context
 
func TestOutboundWithoutStatCounter(t *testing.T) {
    config := &core.Config{
        App: []*anypb.Any{
            serial.ToTypedMessage(&stats.Config{}),
            serial.ToTypedMessage(&policy.Config{
                System: &policy.SystemPolicy{
                    Stats: &policy.SystemPolicy_Stats{
                        InboundUplink: true,
                    },
                },
            }),
        },
    }
 
    v, _ := core.New(config)
    v.AddFeature((outbound.Manager)(new(Manager)))
    ctx := toContext(context.Background(), v)
    h, _ := NewHandler(ctx, &core.OutboundHandlerConfig{
        Tag:           "tag",
        ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
    })
    conn, _ := h.(*Handler).Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146))
    _, ok := conn.(*internet.StatCouterConnection)
    if ok {
        t.Errorf("Expected conn to not be StatCouterConnection")
    }
}
 
func TestOutboundWithStatCounter(t *testing.T) {
    config := &core.Config{
        App: []*anypb.Any{
            serial.ToTypedMessage(&stats.Config{}),
            serial.ToTypedMessage(&policy.Config{
                System: &policy.SystemPolicy{
                    Stats: &policy.SystemPolicy_Stats{
                        OutboundUplink:   true,
                        OutboundDownlink: true,
                    },
                },
            }),
        },
    }
 
    v, _ := core.New(config)
    v.AddFeature((outbound.Manager)(new(Manager)))
    ctx := toContext(context.Background(), v)
    h, _ := NewHandler(ctx, &core.OutboundHandlerConfig{
        Tag:           "tag",
        ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
    })
    conn, _ := h.(*Handler).Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146))
    _, ok := conn.(*internet.StatCouterConnection)
    if !ok {
        t.Errorf("Expected conn to be StatCouterConnection")
    }
}