Skip to content

Commit

Permalink
Merge pull request jason5ng32#157 from jason5ng32/dev
Browse files Browse the repository at this point in the history
Add DNS Resolver
  • Loading branch information
jason5ng32 authored Apr 9, 2024
2 parents 539d823 + b362e5f commit c9366f5
Show file tree
Hide file tree
Showing 13 changed files with 663 additions and 44 deletions.
98 changes: 98 additions & 0 deletions api/dnsresolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// api/dnsresolver.js
import { Resolver } from 'dns';
import { promisify } from 'util';

// 普通 DNS 服务器列表
const dnsServers = {
'Google': '8.8.8.8',
'Cloudflare': '1.1.1.1',
'OpenDNS': '208.67.222.222',
'Quad9': '9.9.9.9',
'ControlD': '76.76.2.0',
'AdGuard': '94.140.14.14',
'Quad 101': '101.101.101.101',
'AliDNS': '223.5.5.5',
'DNSPod': '119.29.29.29',
'114DNS': '114.114.114.114',
'China Unicom': '123.123.123.123',
};

// DNS-over-HTTPS 服务列表
const dohServers = {
'Google': 'https://dns.google/resolve?',
'Cloudflare': 'https://cloudflare-dns.com/dns-query?ct=application/dns-json&',
'AliDNS': 'https://dns.alidns.com/resolve?',
};

const resolveDns = async (hostname, name, server) => {
const resolver = new Resolver();
resolver.setServers([server]);
const resolve4Async = promisify(resolver.resolve4.bind(resolver));
try {
const addresses = await resolve4Async(hostname);
return { [name]: addresses };
} catch (error) {
console.log(error.message);
return { [name]: `Error: No addresses found` };
}
};

const resolveDoh = async (hostname, name, url) => {
try {
const response = await fetch(`${url}name=${hostname}&type=A`, {
headers: { 'Accept': 'application/dns-json' }
});
const data = await response.json();
const addresses = data.Answer ? data.Answer.map(answer => answer.data) : ['Error: No addresses found'];
return { [name]: addresses };
} catch (error) {
console.log(error.message);
return { [name]: `Error: No addresses found` };
}
};

const dnsResolver = async (req, res) => {

// 限制请求方法
if (req.method !== 'GET') {
return res.status(405).json({ message: 'Method Not Allowed' });
}

// 限制只能从指定域名访问
const allowedDomains = ['localhost', ...(process.env.ALLOWED_DOMAINS || '').split(',')];
const referer = req.headers.referer;
if (referer) {
const domain = new URL(referer).hostname;
if (!allowedDomains.includes(domain)) {
return res.status(403).json({ error: 'Access denied' });
}
} else {
return res.status(403).json({ error: 'What are you doing?' });
}

const { hostname } = req.query;

if (!hostname) {
return res.status(400).send({ error: 'Missing hostname parameter' });
}

const dnsPromises = Object.entries(dnsServers).map(([name, ip]) => resolveDns(hostname, name, ip));
const dohPromises = Object.entries(dohServers).map(([name, url]) => resolveDoh(hostname, name, url));

try {
// 并行执行所有 DNS 和 DoH 查询

const result_dns = await Promise.all(dnsPromises);
const result_doh = await Promise.all(dohPromises);

res.json({
hostname,
result_dns,
result_doh
});
} catch (error) {
res.status(500).send({ error: error.message });
}
};

export default dnsResolver;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"svgmap": "^2.10.1",
"vue": "^3.4.21",
"vue-i18n": "^9.10.2",
"vue-router": "^4.3.0",
"vuex": "^4.1.0"
},
"devDependencies": {
Expand Down
4 changes: 4 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import ipsbHandler from './api/ipsb.js';
import cfHander from './api/cfradar.js';
import recaptchaHandler from './api/recaptcha.js';
import validateConfigs from './api/configs.js';
import dnsResolver from './api/dnsresolver.js';

dotenv.config();

Expand All @@ -27,6 +28,9 @@ app.get('/api/ipsb', ipsbHandler);
app.get('/api/cfradar', cfHander);
app.get('/api/recaptcha', recaptchaHandler);

// DNS Resolver
app.get('/api/dnsresolver', dnsResolver);

// 使用查询参数处理所有配置请求
app.get('/api/configs', validateConfigs);

Expand Down
34 changes: 6 additions & 28 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
<Connectivity ref="connectivityRef" />
<WebRTC ref="webRTCRef" />
<DNSLeaks ref="dnsLeaksRef" />
<RuleTest ref="ruleTestRef" />
<SpeedTest ref="speedTestRef" />
<GlobalLatency ref="globalLatencyRef" />
<MTRtest ref="mtrtestRef" />
<AdvancedTools ref="advancedToolsRef" />
<QueryIP ref="queryIPRef" />
<HelpModal ref="helpModalRef" />
<!-- Info Mask BTN-->
Expand All @@ -47,13 +45,11 @@ import Connectivity from './components/connectivity.vue'
import WebRTC from './components/webrtc.vue'
import DNSLeaks from './components/dnsleaks.vue'
import SpeedTest from './components/speedtest.vue'
import GlobalLatency from './components/globallatency.vue'
import MTRtest from './components/mtrtest.vue'
import Footer from './components/footer.vue'
import QueryIP from './components/queryip.vue'
import HelpModal from './components/help.vue'
import PWA from './components/pwa.vue'
import RuleTest from './components/ruletest.vue'
import AdvancedTools from './components/advancedtools.vue'
import { mappingKeys, navigateCards, keyMap } from "./shortcut.js";
import { ref, computed, watch } from 'vue';
Expand All @@ -67,6 +63,7 @@ export default {
const store = useStore();
const isDarkMode = computed(() => store.state.isDarkMode);
const isMobile = computed(() => store.state.isMobile);
const configs = computed(() => store.state.configs);
const shouldRefreshEveryThing = computed(() => store.state.shouldRefreshEveryThing);
const shouldRefresh = ref(false);
Expand All @@ -78,6 +75,7 @@ export default {
isDarkMode,
isMobile,
shouldRefresh,
configs,
};
},
Expand All @@ -88,13 +86,11 @@ export default {
WebRTC,
DNSLeaks,
SpeedTest,
GlobalLatency,
MTRtest,
Footer,
QueryIP,
HelpModal,
PWA,
RuleTest,
AdvancedTools,
},
name: 'App',
data() {
Expand All @@ -105,7 +101,6 @@ export default {
originipDataCards: [],
originstunServers: [],
originleakTest: [],
originRuleTests: [],
alertToShow: false,
alertStyle: "",
alertMessage: "",
Expand Down Expand Up @@ -209,7 +204,6 @@ export default {
this.originipDataCards = JSON.parse(JSON.stringify(this.$refs.IPCheckRef.ipDataCards));
this.originstunServers = JSON.parse(JSON.stringify(this.$refs.webRTCRef.stunServers));
this.originleakTest = JSON.parse(JSON.stringify(this.$refs.dnsLeaksRef.leakTest));
this.originRuleTests = JSON.parse(JSON.stringify(this.$refs.ruleTestRef.ruleTests));
this.infoMask();
this.alertStyle = "text-warning";
this.alertMessage = this.$t('alert.maskedInfoMessage_1');
Expand Down Expand Up @@ -245,9 +239,6 @@ export default {
this.$refs.dnsLeaksRef.leakTest.forEach((server) => {
server.ip = "12.34.56.78";
});
this.$refs.ruleTestRef.ruleTests.forEach((test) => {
test.ip = "8.8.8.8";
});
this.infoMaskLevel = 1;
} else if (this.infoMaskLevel === 1) {
this.$refs.IPCheckRef.ipDataCards.forEach((card) => {
Expand All @@ -266,9 +257,6 @@ export default {
this.$refs.dnsLeaksRef.leakTest.forEach((server) => {
server.geo = "United States";
});
this.$refs.ruleTestRef.ruleTests.forEach((test) => {
test.country_code = "US";
});
this.infoMaskLevel = 2;
}
},
Expand All @@ -278,7 +266,6 @@ export default {
this.$refs.IPCheckRef.ipDataCards = JSON.parse(JSON.stringify(this.originipDataCards));
this.$refs.webRTCRef.stunServers = JSON.parse(JSON.stringify(this.originstunServers));
this.$refs.dnsLeaksRef.leakTest = JSON.parse(JSON.stringify(this.originleakTest));
this.$refs.ruleTestRef.ruleTests = JSON.parse(JSON.stringify(this.originRuleTests));
this.infoMaskLevel = 0;
},
Expand Down Expand Up @@ -404,15 +391,6 @@ export default {
},
description: this.$t('shortcutKeys.RefreshWebRTC'),
},
{
keys: "r",
action: () => {
this.scrollToElement("RuleTest", 80);
this.$refs.ruleTestRef.checkAllRuleTest(true);
this.$trackEvent('ShortCut', 'ShortCut', 'WebRTC');
},
description: this.$t('shortcutKeys.RefreshRuleTests'),
},
{
keys: "d",
action: () => {
Expand All @@ -434,7 +412,7 @@ export default {
{
keys: "m",
action: () => {
if (this.$refs.IPCheckRef.isEnvBingMapKey) {
if (this.configs.bingMap) {
window.scrollTo({ top: 0, behavior: "smooth" });
this.$refs.IPCheckRef.toggleMaps();
};
Expand Down
Loading

0 comments on commit c9366f5

Please sign in to comment.