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