github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/net/dns/doh_test.go (about)

     1  package dns
     2  
     3  import (
     4  	"context"
     5  	"net/netip"
     6  	"testing"
     7  
     8  	"github.com/Asutorufa/yuhaiin/pkg/net/netapi"
     9  	"github.com/Asutorufa/yuhaiin/pkg/net/proxy/socks5"
    10  	"github.com/Asutorufa/yuhaiin/pkg/protos/config/dns"
    11  	"github.com/Asutorufa/yuhaiin/pkg/utils/assert"
    12  	"golang.org/x/net/dns/dnsmessage"
    13  )
    14  
    15  func TestDOH(t *testing.T) {
    16  	netapi.Bootstrap = &netapi.SystemResolver{}
    17  	s, err := netip.ParsePrefix("223.5.5.5/24")
    18  	assert.NoError(t, err)
    19  	s5Dialer := socks5.Dial("127.0.0.1", "1080", "", "")
    20  
    21  	configMap := map[string]Config{
    22  		"google": {
    23  			Type:   dns.Type_doh,
    24  			Host:   "dns.google",
    25  			Subnet: s,
    26  			Dialer: s5Dialer,
    27  		},
    28  		"iijJP": {
    29  			Type:       dns.Type_doh,
    30  			Host:       "103.2.57.5",
    31  			Servername: "public.dns.iij.jp",
    32  			Subnet:     s,
    33  		},
    34  		"cloudflare": {
    35  			Type:   dns.Type_doh,
    36  			Host:   "cloudflare-dns.com",
    37  			Subnet: s,
    38  			Dialer: s5Dialer,
    39  		},
    40  		"quad9": {
    41  			Type:   dns.Type_doh,
    42  			Host:   "9.9.9.9",
    43  			Subnet: s,
    44  		},
    45  		"a.passcloud": {
    46  			Type:   dns.Type_doh,
    47  			Host:   "a.passcloud.xyz",
    48  			Subnet: s,
    49  		},
    50  		"adguard": {
    51  			Type: dns.Type_doh,
    52  			Host: "https://unfiltered.adguard-dns.com/dns-query",
    53  		},
    54  		"ali": {
    55  			Type:   dns.Type_doh,
    56  			Host:   "223.5.5.5",
    57  			Subnet: s,
    58  		},
    59  		"dns.pub": {
    60  			Type:   dns.Type_doh,
    61  			Host:   "dns.pub",
    62  			Subnet: s,
    63  		},
    64  		"sm2.dnspub": {
    65  			Type:   dns.Type_doh,
    66  			Host:   "sm2.doh.pub",
    67  			Subnet: s,
    68  		},
    69  		"360": {
    70  			Type:   dns.Type_doh,
    71  			Host:   "doh.360.cn",
    72  			Subnet: s,
    73  		},
    74  		"dnssb": {
    75  			Type:   dns.Type_doh,
    76  			Host:   "doh.dns.sb",
    77  			Subnet: s,
    78  			Dialer: s5Dialer,
    79  		},
    80  		"opendns": {
    81  			Type:   dns.Type_doh,
    82  			Host:   "doh.opendns.com",
    83  			Subnet: s,
    84  			Dialer: s5Dialer,
    85  		},
    86  		"tuna": {
    87  			Type: dns.Type_doh,
    88  			Host: "https://101.6.6.6:8443/dns-query",
    89  		},
    90  	}
    91  
    92  	d, err := New(configMap["dns.pub"])
    93  	assert.NoError(t, err)
    94  
    95  	t.Log(d.LookupIP(context.TODO(), "plasma"))
    96  	t.Log(d.LookupIP(context.TODO(), "fonts.gstatic.com"))
    97  	// t.Log(d.LookupIP(context.TODO(), "dc.services.visualstudio.com")) // -> will error, but not found reason
    98  	t.Log(d.LookupIP(context.TODO(), "i2.hdslb.com"))
    99  	t.Log(d.LookupIP(context.TODO(), "www.baidu.com"))
   100  	// t.Log(d.LookupIP(context.TODO(), "push.services.mozilla.com"))
   101  	// t.Log(d.LookupIP(context.TODO(), "www.google.com"))
   102  	// t.Log(d.LookupIP(context.TODO(), "www.pixiv.net"))
   103  	// t.Log(d.LookupIP(context.TODO(), "s1.hdslb.com"))
   104  	// t.Log(d.LookupIP(context.TODO(), "dns.nextdns.io"))
   105  	// t.Log(d.LookupIP(context.TODO(), "bilibili.com"))
   106  	// t.Log(d.LookupIP(context.TODO(), "test-ipv6.com"))
   107  	// t.Log(d.LookupIP(context.TODO(), "ss1.bdstatic.com"))
   108  	// t.Log(d.LookupIP(context.TODO(), "www.twitter.com"))
   109  	// t.Log(d.LookupIP(context.TODO(), "www.facebook.com"))
   110  	// t.Log(d.LookupIP(context.TODO(), "yahoo.co.jp"))
   111  	// t.Log(d.LookupIP(context.TODO(), "115-235-111-150.dhost.00cdn.com"))
   112  
   113  	t.Log(d.Raw(context.TODO(), dnsmessage.Question{
   114  		Name: dnsmessage.MustNewName("www.google.com."),
   115  		Type: dnsmessage.TypeA,
   116  	}))
   117  	t.Log(d.Raw(context.TODO(), dnsmessage.Question{
   118  		Name: dnsmessage.MustNewName("www.google.com."),
   119  		Type: dnsmessage.TypeA,
   120  	}))
   121  }
   122  
   123  func TestGetURL(t *testing.T) {
   124  	dnsQStr := "https://8.8.8.8/dns-query"
   125  	assert.Equal(t, getUrlAndHost("https://8.8.8.8"), dnsQStr)
   126  	assert.Equal(t, getUrlAndHost("8.8.8.8"), dnsQStr)
   127  
   128  	t.Log(getUrlAndHost("https://"))
   129  	t.Log(getUrlAndHost("/dns-query"))
   130  }