| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- 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/environment"
- "github.com/v2fly/v2ray-core/v5/common/environment/deferredpersistentstorage"
- "github.com/v2fly/v2ray-core/v5/common/environment/envctx"
- "github.com/v2fly/v2ray-core/v5/common/environment/filesystemimpl"
- "github.com/v2fly/v2ray-core/v5/common/environment/systemnetworkimpl"
- "github.com/v2fly/v2ray-core/v5/common/environment/transientstorageimpl"
- "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)
- defaultNetworkImpl := systemnetworkimpl.NewSystemNetworkDefault()
- defaultFilesystemImpl := filesystemimpl.NewDefaultFileSystemDefaultImpl()
- deferredPersistentStorageImpl := deferredpersistentstorage.NewDeferredPersistentStorage(ctx)
- rootEnv := environment.NewRootEnvImpl(ctx,
- transientstorageimpl.NewScopedTransientStorageImpl(), defaultNetworkImpl.Dialer(), defaultNetworkImpl.Listener(),
- defaultFilesystemImpl, deferredPersistentStorageImpl)
- proxyEnvironment := rootEnv.ProxyEnvironment("o")
- ctx = envctx.ContextWithEnvironment(ctx, proxyEnvironment)
- 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)
- defaultNetworkImpl := systemnetworkimpl.NewSystemNetworkDefault()
- defaultFilesystemImpl := filesystemimpl.NewDefaultFileSystemDefaultImpl()
- deferredPersistentStorageImpl := deferredpersistentstorage.NewDeferredPersistentStorage(ctx)
- rootEnv := environment.NewRootEnvImpl(ctx,
- transientstorageimpl.NewScopedTransientStorageImpl(), defaultNetworkImpl.Dialer(), defaultNetworkImpl.Listener(),
- defaultFilesystemImpl, deferredPersistentStorageImpl)
- proxyEnvironment := rootEnv.ProxyEnvironment("o")
- ctx = envctx.ContextWithEnvironment(ctx, proxyEnvironment)
- 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")
- }
- }
|