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