Xray-core/testing/scenarios/dns_test.go

108 lines
2.6 KiB
Go
Raw Permalink Normal View History

2020-11-25 14:01:53 +03:00
package scenarios
import (
"fmt"
"testing"
"time"
2020-12-04 04:36:16 +03:00
"github.com/xtls/xray-core/app/dns"
"github.com/xtls/xray-core/app/proxyman"
"github.com/xtls/xray-core/app/router"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/serial"
"github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/proxy/blackhole"
"github.com/xtls/xray-core/proxy/freedom"
"github.com/xtls/xray-core/proxy/socks"
"github.com/xtls/xray-core/testing/servers/tcp"
2022-05-18 10:29:01 +03:00
xproxy "golang.org/x/net/proxy"
2020-11-25 14:01:53 +03:00
)
func TestResolveIP(t *testing.T) {
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
common.Must(err)
defer tcpServer.Close()
serverPort := tcp.PickPort()
serverConfig := &core.Config{
App: []*serial.TypedMessage{
serial.ToTypedMessage(&dns.Config{
StaticHosts: []*dns.Config_HostMapping{
{
Type: dns.DomainMatchingType_Full,
Domain: "google.com",
Ip: [][]byte{dest.Address.IP()},
},
2020-11-25 14:01:53 +03:00
},
}),
serial.ToTypedMessage(&router.Config{
DomainStrategy: router.Config_IpIfNonMatch,
Rule: []*router.RoutingRule{
{
Geoip: []*router.GeoIP{
2020-11-25 14:01:53 +03:00
{
Cidr: []*router.CIDR{
{
Ip: []byte{127, 0, 0, 0},
Prefix: 8,
},
},
2020-11-25 14:01:53 +03:00
},
},
TargetTag: &router.RoutingRule_Tag{
Tag: "direct",
},
},
},
}),
},
Inbound: []*core.InboundHandlerConfig{
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(serverPort)}},
Listen: net.NewIPOrDomain(net.LocalHostIP),
2020-11-25 14:01:53 +03:00
}),
ProxySettings: serial.ToTypedMessage(&socks.ServerConfig{
AuthType: socks.AuthType_NO_AUTH,
Accounts: map[string]string{
"Test Account": "Test Password",
},
Address: net.NewIPOrDomain(net.LocalHostIP),
UdpEnabled: false,
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
{
ProxySettings: serial.ToTypedMessage(&blackhole.Config{}),
},
{
Tag: "direct",
ProxySettings: serial.ToTypedMessage(&freedom.Config{
DomainStrategy: freedom.Config_USE_IP,
}),
},
},
}
servers, err := InitializeServerConfigs(serverConfig)
common.Must(err)
defer CloseAllServers(servers)
{
noAuthDialer, err := xproxy.SOCKS5("tcp", net.TCPDestination(net.LocalHostIP, serverPort).NetAddr(), nil, xproxy.Direct)
common.Must(err)
conn, err := noAuthDialer.Dial("tcp", fmt.Sprintf("google.com:%d", dest.Port))
common.Must(err)
defer conn.Close()
if err := testTCPConn2(conn, 1024, time.Second*5)(); err != nil {
t.Error(err)
}
}
}