|
| 1 | +import assert from 'node:assert' |
| 2 | +import dns from '../src/lib/dns/index.js' |
| 3 | +import matchUtil from '../src/utils/util.match.js' |
| 4 | + |
| 5 | +const presetIp = '100.100.100.100' |
| 6 | +const preSetIpList = matchUtil.domainMapRegexply({ |
| 7 | + 'xxx.com': [ |
| 8 | + presetIp |
| 9 | + ] |
| 10 | +}) |
| 11 | + |
| 12 | +// 境外DNS测试 |
| 13 | +const dnsProviders = dns.initDNS({ |
| 14 | + // https |
| 15 | + cloudflare: { |
| 16 | + type: 'https', |
| 17 | + server: 'https://1.1.1.1/dns-query', |
| 18 | + cacheSize: 1000, |
| 19 | + }, |
| 20 | + quad9: { |
| 21 | + server: 'https://9.9.9.9/dns-query', |
| 22 | + cacheSize: 1000, |
| 23 | + forSNI: true, |
| 24 | + }, |
| 25 | + rubyfish: { |
| 26 | + server: 'https://rubyfish.cn/dns-query', |
| 27 | + cacheSize: 1000, |
| 28 | + }, |
| 29 | + py233: { |
| 30 | + server: ' https://i.233py.com/dns-query', |
| 31 | + cacheSize: 1000, |
| 32 | + }, |
| 33 | + |
| 34 | + // tls |
| 35 | + cloudflareTLS: { |
| 36 | + type: 'tls', |
| 37 | + server: '1.1.1.1', |
| 38 | + servername: 'cloudflare-dns.com', |
| 39 | + cacheSize: 1000, |
| 40 | + }, |
| 41 | + quad9TLS: { |
| 42 | + server: 'tls://9.9.9.9', |
| 43 | + servername: 'dns.quad9.net', |
| 44 | + cacheSize: 1000, |
| 45 | + }, |
| 46 | +}, preSetIpList) |
| 47 | + |
| 48 | + |
| 49 | +const hasPresetHostname = 'xxx.com' |
| 50 | +const noPresetHostname = 'yyy.com' |
| 51 | + |
| 52 | +const hostname1 = 'github.com' |
| 53 | +const hostname2 = 'api.github.com' |
| 54 | +const hostname3 = 'hk.docmirror.cn' |
| 55 | +const hostname4 = 'github.docmirror.cn' |
| 56 | +const hostname5 = 'gh.docmirror.top' |
| 57 | +const hostname6 = 'gh2.docmirror.top' |
| 58 | + |
| 59 | +let ip |
| 60 | + |
| 61 | + |
| 62 | +console.log('\n--------------- test ForSNI ---------------\n') |
| 63 | +console.log(`===> test ForSNI: ${dnsProviders.ForSNI.dnsName}`, '\n\n') |
| 64 | +assert.strictEqual(dnsProviders.ForSNI, dnsProviders.quad9) |
| 65 | + |
| 66 | + |
| 67 | +console.log('\n--------------- test PreSet ---------------\n') |
| 68 | +ip = await dnsProviders.PreSet.lookup(hasPresetHostname) |
| 69 | +console.log(`===> test PreSet: ${hasPresetHostname} ->`, ip, '\n\n') |
| 70 | +console.log('\n\n') |
| 71 | +assert.strictEqual(ip, presetIp) // 预设过IP,等于预设的IP |
| 72 | + |
| 73 | +ip = await dnsProviders.PreSet.lookup(noPresetHostname) |
| 74 | +console.log(`===> test PreSet: ${noPresetHostname} ->`, ip, '\n\n') |
| 75 | +console.log('\n\n') |
| 76 | +assert.strictEqual(ip, noPresetHostname) // 未预设IP,等于域名自己 |
| 77 | + |
| 78 | + |
| 79 | +console.log('\n--------------- test https ---------------\n') |
| 80 | +ip = await dnsProviders.cloudflare.lookup(hasPresetHostname) |
| 81 | +assert.strictEqual(ip, presetIp) // test preset |
| 82 | +console.log('\n\n') |
| 83 | + |
| 84 | +assert.strictEqual(dnsProviders.cloudflare.dnsType, 'HTTPS') |
| 85 | +ip = await dnsProviders.cloudflare.lookup(hostname1) |
| 86 | +console.log(`===> test cloudflare: ${hostname1} ->`, ip, '\n\n') |
| 87 | + |
| 88 | +assert.strictEqual(dnsProviders.quad9.dnsType, 'HTTPS') |
| 89 | +ip = await dnsProviders.quad9.lookup(hostname1) |
| 90 | +console.log(`===> test quad9: ${hostname1} ->`, ip, '\n\n') |
| 91 | + |
| 92 | +assert.strictEqual(dnsProviders.rubyfish.dnsType, 'HTTPS') |
| 93 | +ip = await dnsProviders.rubyfish.lookup(hostname1) |
| 94 | +console.log(`===> test rubyfish: ${hostname1} ->`, ip, '\n\n') |
| 95 | + |
| 96 | +assert.strictEqual(dnsProviders.py233.dnsType, 'HTTPS') |
| 97 | +ip = await dnsProviders.py233.lookup(hostname1) |
| 98 | +console.log(`===> test py233: ${hostname1} ->`, ip, '\n\n') |
| 99 | + |
| 100 | + |
| 101 | +console.log('\n--------------- test TLS ---------------\n') |
| 102 | +ip = await dnsProviders.cloudflareTLS.lookup(hasPresetHostname) |
| 103 | +assert.strictEqual(ip, presetIp) // test preset |
| 104 | +console.log('\n\n') |
| 105 | + |
| 106 | +assert.strictEqual(dnsProviders.cloudflareTLS.dnsType, 'TLS') |
| 107 | +ip = await dnsProviders.cloudflareTLS.lookup(hostname1) |
| 108 | +console.log(`===> test cloudflareTLS: ${hostname1} ->`, ip, '\n\n') |
| 109 | + |
| 110 | +assert.strictEqual(dnsProviders.quad9TLS.dnsType, 'TLS') |
| 111 | +ip = await dnsProviders.quad9TLS.lookup(hostname1) |
| 112 | +console.log(`===> test quad9TLS: ${hostname1} ->`, ip, '\n\n') |
0 commit comments