2018-01-10 06:22:37 -05:00
|
|
|
package outbound_test
|
|
|
|
|
|
|
|
import (
|
2020-06-27 00:28:53 -04:00
|
|
|
"context"
|
2018-01-10 06:22:37 -05:00
|
|
|
"testing"
|
2021-05-04 19:28:09 -04:00
|
|
|
_ "unsafe"
|
2018-01-10 06:22:37 -05:00
|
|
|
|
2022-09-06 15:22:10 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v5/common/environment/systemnetworkimpl"
|
|
|
|
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/environment"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/environment/envctx"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/environment/transientstorageimpl"
|
|
|
|
|
2021-10-28 06:34:19 -04:00
|
|
|
"google.golang.org/protobuf/types/known/anypb"
|
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
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"
|
2018-01-10 06:22:37 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestInterfaces(t *testing.T) {
|
2019-01-07 17:27:59 -05:00
|
|
|
_ = (outbound.Handler)(new(Handler))
|
|
|
|
_ = (outbound.Manager)(new(Manager))
|
2018-01-10 06:22:37 -05:00
|
|
|
}
|
2020-06-27 00:28:53 -04:00
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
//go:linkname toContext github.com/v2fly/v2ray-core/v5.toContext
|
2021-05-04 15:41:09 -04:00
|
|
|
func toContext(ctx context.Context, v *core.Instance) context.Context
|
2020-06-27 00:28:53 -04:00
|
|
|
|
|
|
|
func TestOutboundWithoutStatCounter(t *testing.T) {
|
|
|
|
config := &core.Config{
|
2021-06-19 09:36:54 -04:00
|
|
|
App: []*anypb.Any{
|
2020-06-27 00:28:53 -04:00
|
|
|
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)))
|
2021-05-04 15:41:09 -04:00
|
|
|
ctx := toContext(context.Background(), v)
|
2022-09-06 15:22:10 -04:00
|
|
|
defaultNetworkImpl := systemnetworkimpl.NewSystemNetworkDefault()
|
|
|
|
rootEnv := environment.NewRootEnvImpl(ctx, transientstorageimpl.NewScopedTransientStorageImpl(), defaultNetworkImpl.Dialer(), defaultNetworkImpl.Listener())
|
|
|
|
proxyEnvironment := rootEnv.ProxyEnvironment("o")
|
|
|
|
ctx = envctx.ContextWithEnvironment(ctx, proxyEnvironment)
|
2020-06-27 00:28:53 -04:00
|
|
|
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{
|
2021-06-19 09:36:54 -04:00
|
|
|
App: []*anypb.Any{
|
2020-06-27 00:28:53 -04:00
|
|
|
serial.ToTypedMessage(&stats.Config{}),
|
|
|
|
serial.ToTypedMessage(&policy.Config{
|
|
|
|
System: &policy.SystemPolicy{
|
|
|
|
Stats: &policy.SystemPolicy_Stats{
|
2020-08-30 09:17:22 -04:00
|
|
|
OutboundUplink: true,
|
2020-06-27 00:28:53 -04:00
|
|
|
OutboundDownlink: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
v, _ := core.New(config)
|
|
|
|
v.AddFeature((outbound.Manager)(new(Manager)))
|
2021-05-04 15:41:09 -04:00
|
|
|
ctx := toContext(context.Background(), v)
|
2022-09-06 15:22:10 -04:00
|
|
|
defaultNetworkImpl := systemnetworkimpl.NewSystemNetworkDefault()
|
|
|
|
rootEnv := environment.NewRootEnvImpl(ctx, transientstorageimpl.NewScopedTransientStorageImpl(), defaultNetworkImpl.Dialer(), defaultNetworkImpl.Listener())
|
|
|
|
proxyEnvironment := rootEnv.ProxyEnvironment("o")
|
|
|
|
ctx = envctx.ContextWithEnvironment(ctx, proxyEnvironment)
|
2020-06-27 00:28:53 -04:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|