1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-29 05:27:02 -05:00
v2fly/testing/scenarios/dns_test.go

103 lines
2.6 KiB
Go
Raw Normal View History

2017-01-27 15:19:46 -05:00
package scenarios
import (
"fmt"
2021-09-07 08:43:28 -04:00
"github.com/v2fly/v2ray-core/v4/app/router/routercommon"
2021-06-19 09:36:54 -04:00
"google.golang.org/protobuf/types/known/anypb"
2017-01-27 15:19:46 -05:00
"testing"
2019-02-03 13:46:53 -05:00
"time"
2017-01-27 15:19:46 -05:00
xproxy "golang.org/x/net/proxy"
2021-02-16 15:31:50 -05:00
core "github.com/v2fly/v2ray-core/v4"
"github.com/v2fly/v2ray-core/v4/app/dns"
"github.com/v2fly/v2ray-core/v4/app/proxyman"
"github.com/v2fly/v2ray-core/v4/app/router"
"github.com/v2fly/v2ray-core/v4/common"
"github.com/v2fly/v2ray-core/v4/common/net"
"github.com/v2fly/v2ray-core/v4/common/serial"
"github.com/v2fly/v2ray-core/v4/proxy/blackhole"
"github.com/v2fly/v2ray-core/v4/proxy/freedom"
"github.com/v2fly/v2ray-core/v4/proxy/socks"
"github.com/v2fly/v2ray-core/v4/testing/servers/tcp"
2017-01-27 15:19:46 -05:00
)
func TestResolveIP(t *testing.T) {
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
2019-02-02 16:19:30 -05:00
common.Must(err)
2017-01-27 15:19:46 -05:00
defer tcpServer.Close()
2018-03-01 05:53:39 -05:00
serverPort := tcp.PickPort()
2017-01-27 15:19:46 -05:00
serverConfig := &core.Config{
2021-06-19 09:36:54 -04:00
App: []*anypb.Any{
2017-01-27 15:19:46 -05:00
serial.ToTypedMessage(&dns.Config{
Hosts: map[string]*net.IPOrDomain{
"google.com": net.NewIPOrDomain(dest.Address),
2017-01-27 15:19:46 -05:00
},
}),
serial.ToTypedMessage(&router.Config{
2021-09-07 08:43:28 -04:00
DomainStrategy: router.DomainStrategy_IpIfNonMatch,
2017-01-27 15:19:46 -05:00
Rule: []*router.RoutingRule{
{
2021-09-07 08:43:28 -04:00
Cidr: []*routercommon.CIDR{
2017-01-27 15:19:46 -05:00
{
Ip: []byte{127, 0, 0, 0},
Prefix: 8,
},
},
2018-11-07 19:01:53 -05:00
TargetTag: &router.RoutingRule_Tag{
Tag: "direct",
},
2017-01-27 15:19:46 -05:00
},
},
}),
},
Inbound: []*core.InboundHandlerConfig{
2017-01-27 15:19:46 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(serverPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
2017-01-27 15:19:46 -05:00
}),
ProxySettings: serial.ToTypedMessage(&socks.ServerConfig{
AuthType: socks.AuthType_NO_AUTH,
Accounts: map[string]string{
"Test Account": "Test Password",
},
Address: net.NewIPOrDomain(net.LocalHostIP),
2017-01-27 15:19:46 -05:00
UdpEnabled: false,
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-01-27 15:19:46 -05:00
{
ProxySettings: serial.ToTypedMessage(&blackhole.Config{}),
},
{
Tag: "direct",
ProxySettings: serial.ToTypedMessage(&freedom.Config{
DomainStrategy: freedom.Config_USE_IP,
}),
},
},
}
2017-05-17 18:39:30 -04:00
servers, err := InitializeServerConfigs(serverConfig)
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-03 13:46:53 -05:00
defer CloseAllServers(servers)
2017-01-27 15:19:46 -05:00
{
noAuthDialer, err := xproxy.SOCKS5("tcp", net.TCPDestination(net.LocalHostIP, serverPort).NetAddr(), nil, xproxy.Direct)
2019-02-02 16:19:30 -05:00
common.Must(err)
2017-01-27 15:19:46 -05:00
conn, err := noAuthDialer.Dial("tcp", fmt.Sprintf("google.com:%d", dest.Port))
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-03 13:46:53 -05:00
defer conn.Close()
2017-01-27 15:19:46 -05:00
2019-02-03 13:46:53 -05:00
if err := testTCPConn2(conn, 1024, time.Second*5)(); err != nil {
t.Error(err)
}
2017-01-27 15:19:46 -05:00
}
}