github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/testing/scenarios/dns_test.go (about)

     1  package scenarios
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	xproxy "golang.org/x/net/proxy"
     9  	"google.golang.org/protobuf/types/known/anypb"
    10  
    11  	core "github.com/v2fly/v2ray-core/v5"
    12  	"github.com/v2fly/v2ray-core/v5/app/dns"
    13  	"github.com/v2fly/v2ray-core/v5/app/proxyman"
    14  	"github.com/v2fly/v2ray-core/v5/app/router"
    15  	"github.com/v2fly/v2ray-core/v5/app/router/routercommon"
    16  	"github.com/v2fly/v2ray-core/v5/common"
    17  	"github.com/v2fly/v2ray-core/v5/common/net"
    18  	"github.com/v2fly/v2ray-core/v5/common/serial"
    19  	"github.com/v2fly/v2ray-core/v5/proxy/blackhole"
    20  	"github.com/v2fly/v2ray-core/v5/proxy/freedom"
    21  	"github.com/v2fly/v2ray-core/v5/proxy/socks"
    22  	"github.com/v2fly/v2ray-core/v5/testing/servers/tcp"
    23  )
    24  
    25  func TestResolveIP(t *testing.T) {
    26  	tcpServer := tcp.Server{
    27  		MsgProcessor: xor,
    28  	}
    29  	dest, err := tcpServer.Start()
    30  	common.Must(err)
    31  	defer tcpServer.Close()
    32  
    33  	serverPort := tcp.PickPort()
    34  	serverConfig := &core.Config{
    35  		App: []*anypb.Any{
    36  			serial.ToTypedMessage(&dns.Config{
    37  				Hosts: map[string]*net.IPOrDomain{
    38  					"google.com": net.NewIPOrDomain(dest.Address),
    39  				},
    40  			}),
    41  			serial.ToTypedMessage(&router.Config{
    42  				DomainStrategy: router.DomainStrategy_IpIfNonMatch,
    43  				Rule: []*router.RoutingRule{
    44  					{
    45  						Cidr: []*routercommon.CIDR{
    46  							{
    47  								Ip:     []byte{127, 0, 0, 0},
    48  								Prefix: 8,
    49  							},
    50  						},
    51  						TargetTag: &router.RoutingRule_Tag{
    52  							Tag: "direct",
    53  						},
    54  					},
    55  				},
    56  			}),
    57  		},
    58  		Inbound: []*core.InboundHandlerConfig{
    59  			{
    60  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
    61  					PortRange: net.SinglePortRange(serverPort),
    62  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
    63  				}),
    64  				ProxySettings: serial.ToTypedMessage(&socks.ServerConfig{
    65  					AuthType: socks.AuthType_NO_AUTH,
    66  					Accounts: map[string]string{
    67  						"Test Account": "Test Password",
    68  					},
    69  					Address:    net.NewIPOrDomain(net.LocalHostIP),
    70  					UdpEnabled: false,
    71  				}),
    72  			},
    73  		},
    74  		Outbound: []*core.OutboundHandlerConfig{
    75  			{
    76  				ProxySettings: serial.ToTypedMessage(&blackhole.Config{}),
    77  			},
    78  			{
    79  				Tag: "direct",
    80  				ProxySettings: serial.ToTypedMessage(&freedom.Config{
    81  					DomainStrategy: freedom.Config_USE_IP,
    82  				}),
    83  			},
    84  		},
    85  	}
    86  
    87  	servers, err := InitializeServerConfigs(serverConfig)
    88  	common.Must(err)
    89  	defer CloseAllServers(servers)
    90  
    91  	{
    92  		noAuthDialer, err := xproxy.SOCKS5("tcp", net.TCPDestination(net.LocalHostIP, serverPort).NetAddr(), nil, xproxy.Direct)
    93  		common.Must(err)
    94  		conn, err := noAuthDialer.Dial("tcp", fmt.Sprintf("google.com:%d", dest.Port))
    95  		common.Must(err)
    96  		defer conn.Close()
    97  
    98  		if err := testTCPConn2(conn, 1024, time.Second*5)(); err != nil {
    99  			t.Error(err)
   100  		}
   101  	}
   102  }