Skip to content

Commit

Permalink
Revised Xray Fake DNS.
Browse files Browse the repository at this point in the history
  • Loading branch information
bia-pain-bache committed Oct 24, 2024
1 parent ec63b14 commit bc479c8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 68 deletions.
49 changes: 20 additions & 29 deletions _worker.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 23 additions & 39 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ export default {
});

case '/panel':
const pwd = await env.bpb.get('pwd');
const isAuth = await Authenticate(request, env);
if (request.method === 'POST') {
if (!isAuth) return new Response('Unauthorized or expired session!', { status: 401 });
Expand All @@ -176,9 +175,10 @@ export default {
? await updateDataset(env, null, true)
: await updateDataset(env, formData);

return new Response('Success', { status: 200 });
}

return new Response('Success', { status: 200 });
}

const pwd = await env.bpb.get('pwd');
if (pwd && !isAuth) return Response.redirect(`${url.origin}/login`, 302);
const isPassSet = pwd?.length >= 8;
const homePage = renderHomePage(request, settings, host, isPassSet);
Expand Down Expand Up @@ -1411,17 +1411,17 @@ function renderHomePage (request, proxySettings, hostName, isPassSet) {
const cfCountry = regionNames.of(request.cf.country);

allPorts.forEach(port => {
let id = `port-${port}`;
const id = `port-${port}`;
const isChecked = ports.includes(port) ? 'checked' : '';
let portBlock = `
const portBlock = `
<div class="routing" style="grid-template-columns: 1fr 2fr; margin-right: 10px;">
<input type="checkbox" id=${id} name=${port} onchange="handlePortChange(event)" value="true" ${isChecked}>
<label style="margin-bottom: 3px;" for=${id}>${port}</label>
</div>`;
defaultHttpsPorts.includes(port) ? httpsPortsBlock += portBlock : httpPortsBlock += portBlock;
});

const html = `
return `
<!DOCTYPE html>
<html lang="en">
<head>
Expand Down Expand Up @@ -2870,8 +2870,6 @@ function renderHomePage (request, proxySettings, hostName, isPassSet) {
</script>
</body>
</html>`;

return html;
}

function renderLoginPage () {
Expand Down Expand Up @@ -3251,6 +3249,7 @@ async function buildXrayDNS (proxySettings, outboundAddrs, domainToStaticIPs, is
tag: "dns",
};

isFakeDNS && dnsObject.servers.unshift("fakedns");
isOutboundRule && dnsObject.servers.push({
address: localDNS,
domains: outboundRules
Expand All @@ -3269,24 +3268,10 @@ async function buildXrayDNS (proxySettings, outboundAddrs, domainToStaticIPs, is
localDNSServer.expectIPs.push(ip);
}
});

dnsObject.servers.push(localDNSServer);
}

if (isFakeDNS) {
if ((isBypass || isOutboundRule) && !isWorkerLess) {
dnsObject.servers.unshift({
address: "fakedns",
domains: [
...localDNSServer.domains,
...outboundRules
]
});
} else {
dnsObject.servers.unshift("fakedns");
}
}

return dnsObject;
}

Expand Down Expand Up @@ -4380,6 +4365,7 @@ function buildSingBoxDNS (proxySettings, isChain, isWarp) {
let fakeip;
const isFakeDNS = (vlessTrojanFakeDNS && !isWarp) || (warpFakeDNS && isWarp);
const isIPv6 = (enableIPv6 && !isWarp) || (warpEnableIPv6 && isWarp);
const isBypass = bypassIran || bypassChina || bypassRussia;
const geoRules = [
{ rule: bypassIran, type: 'direct', ruleSet: "geosite-ir" },
{ rule: bypassChina, type: 'direct', ruleSet: "geosite-cn" },
Expand Down Expand Up @@ -4449,6 +4435,7 @@ function buildSingBoxDNS (proxySettings, isChain, isWarp) {
rule && type === 'block' && blockRule.rule_set.push(ruleSet);
});

isBypass && rules.push(bypassRule);
rules.push(bypassRule, blockRule);
if (isFakeDNS) {
servers.push({
Expand Down Expand Up @@ -4608,30 +4595,27 @@ function buildSingBoxRoutingRules (proxySettings) {
download_detour: "direct"
};

let geositeDirectRule = createRule('direct');
let geoipDirectRule = createRule('direct');
let geositeBlockRule = createRule('block');
let geoipBlockRule = createRule('block');
let directRule = createRule('direct');;
let blockRule = createRule('block');
let ruleSets = [];

geoRules.forEach(({ rule, type, ruleSet }) => {
const { geosite, geoip, geositeURL, geoipURL } = ruleSet;
if (rule) {
type === 'direct'
? geositeDirectRule.rule_set.push(geosite)
: geositeBlockRule.rule_set.push(geosite);
ruleSets.push({...routingRuleSet, tag: geosite, url: geositeURL});
if (geoip) {
type === 'direct'
? geoipDirectRule.rule_set.push(geoip)
: geoipBlockRule.rule_set.push(geoip);
ruleSets.push({...routingRuleSet, tag: geoip, url: geoipURL});
if (type === 'direct') {
directRule.rule_set.unshift(geosite);
directRule.rule_set.push(geoip);
} else {
blockRule.rule_set.unshift(geosite);
geoip && blockRule.rule_set.push(geoip);
}
ruleSets.push({...routingRuleSet, tag: geosite, url: geositeURL});
geoip && ruleSets.push({...routingRuleSet, tag: geoip, url: geoipURL});
}
});

isBypass && rules.push(geositeDirectRule, geoipDirectRule);
rules.push(geositeBlockRule, geoipBlockRule);
isBypass && rules.push(directRule);
rules.push(blockRule);

blockUDP443 && rules.push({
network: "udp",
Expand Down

0 comments on commit bc479c8

Please sign in to comment.