Skip to content

Commit

Permalink
iOS apps compatiblity and bug fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
bia-pain-bache committed Jul 12, 2024
2 parents da7563d + d3d10ca commit 3c8e505
Showing 1 changed file with 87 additions and 41 deletions.
128 changes: 87 additions & 41 deletions _worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ let proxyIP = proxyIPs[Math.floor(Math.random() * proxyIPs.length)];

let dohURL = 'https://cloudflare-dns.com/dns-query';

let panelVersion = '2.4.4';
let panelVersion = '2.4.5';

if (!isValidUUID(userID)) {
throw new Error('uuid is not valid');
Expand Down Expand Up @@ -1190,6 +1190,8 @@ const getFragmentConfigs = async (env, hostName, client) => {

const getSingboxConfig = async (env, hostName) => {
let proxySettings = {};
let outboundDomains = [];
const domainRegex = /^(?!:\/\/)([a-zA-Z0-9-]{1,63}\.)*[a-zA-Z0-9][a-zA-Z0-9-]{0,62}\.[a-zA-Z]{2,11}$/;

try {
proxySettings = await env.bpb.get("proxySettings", {type: 'json'});
Expand Down Expand Up @@ -1228,9 +1230,12 @@ const getSingboxConfig = async (env, hostName) => {
config.outbounds.push(outbound);
config.outbounds[0].outbounds.push(remark);
config.outbounds[1].outbounds.push(remark);
if (domainRegex.test(outbound.server)) outboundDomains.push(outbound.server);
});
});

config.dns.rules[0].domain = [...new Set(outboundDomains)];

return config;
}

Expand All @@ -1241,6 +1246,8 @@ const getWarpConfigs = async (env, client) => {
let xrayWarpBestPing = structuredClone(xrayConfigTemp);
let xrayWoWConfigTemp = structuredClone(xrayConfigTemp);
let singboxWarpConfig = structuredClone(singboxConfigTemp);
let outboundDomains = [];
const domainRegex = /^(?!:\/\/)([a-zA-Z0-9-]{1,63}\.)*[a-zA-Z0-9][a-zA-Z0-9-]{0,62}\.[a-zA-Z]{2,11}$/;

try {
proxySettings = await env.bpb.get("proxySettings", {type: 'json'});
Expand All @@ -1265,7 +1272,7 @@ const getWarpConfigs = async (env, client) => {
xrayWarpBestPing.dns = await buildDNSObject(remoteDNS, localDNS, blockAds, bypassIran, blockPorn);
xrayWarpBestPing.routing.rules = buildRoutingRules(localDNS, blockAds, bypassIran, blockPorn, bypassLAN, false, true);
xrayWarpBestPing.outbounds.splice(0,1);
xrayWarpBestPing.routing.balancers[0].selector = 'warp';
xrayWarpBestPing.routing.balancers[0].selector = ['warp'];
xrayWarpBestPing.observatory.subjectSelector = ['warp'];
xrayWoWConfigTemp.dns = await buildDNSObject(remoteDNS, localDNS, blockAds, bypassIran, blockPorn);
xrayWoWConfigTemp.routing.rules = buildRoutingRules(localDNS, blockAds, bypassIran, blockPorn, bypassLAN, false, false);
Expand Down Expand Up @@ -1298,14 +1305,18 @@ const getWarpConfigs = async (env, client) => {
singboxWarpConfig.outbounds.push(outbound);
singboxWarpConfig.outbounds[0].outbounds.push(outbound.tag);
singboxWarpConfig.outbounds[1].outbounds.push(outbound.tag);
if (domainRegex.test(outbound.server)) outboundDomains.push(outbound.server);
});

singboxWoWOutbounds.forEach((outbound, index) => {
if (outbound.tag.includes('WoW')) {
singboxWarpConfig.outbounds.push(singboxWoWOutbounds[index], singboxWoWOutbounds[index + 1]);
singboxWarpConfig.outbounds[0].outbounds.push(outbound.tag);
if (domainRegex.test(outbound.server)) outboundDomains.push(outbound.server);
}
});

singboxWarpConfig.dns.rules[0].domain = [...new Set(outboundDomains)];

return client === 'singbox'
? singboxWarpConfig
Expand Down Expand Up @@ -1476,12 +1487,12 @@ const buildDNSObject = async (remoteDNS, localDNS, blockAds, bypassIran, blockPo
}

if (blockAds) {
dnsObject.hosts["geosite:category-ads-all"] = "127.0.0.1";
dnsObject.hosts["geosite:category-ads-ir"] = "127.0.0.1";
dnsObject.hosts["geosite:category-ads-all"] = ["127.0.0.1"];
dnsObject.hosts["geosite:category-ads-ir"] = ["127.0.0.1"];
}

if (blockPorn) {
dnsObject.hosts["geosite:category-porn"] = "127.0.0.1";
dnsObject.hosts["geosite:category-porn"] = ["127.0.0.1"];
}

if (!bypassIran || localDNS === 'localhost' || isWorkerLess) {
Expand Down Expand Up @@ -1584,24 +1595,25 @@ const updateDataset = async (env, Settings) => {
}

const vlessConfig = Settings?.get('outProxy');

const proxySettings = {
remoteDNS: Settings?.get('remoteDNS') || currentProxySettings?.remoteDNS || 'https://94.140.14.14/dns-query',
localDNS: Settings?.get('localDNS') || currentProxySettings?.localDNS || '8.8.8.8',
lengthMin: Settings?.get('fragmentLengthMin') || currentProxySettings?.lengthMin || '100',
lengthMax: Settings?.get('fragmentLengthMax') || currentProxySettings?.lengthMax || '200',
intervalMin: Settings?.get('fragmentIntervalMin') || currentProxySettings?.intervalMin || '5',
intervalMax: Settings?.get('fragmentIntervalMax') || currentProxySettings?.intervalMax || '10',
blockAds: Settings?.get('block-ads') || currentProxySettings?.blockAds || false,
bypassIran: Settings?.get('bypass-iran') || currentProxySettings?.bypassIran || false,
blockPorn: Settings?.get('block-porn') || currentProxySettings?.blockPorn || false,
bypassLAN: Settings?.get('bypass-lan') || currentProxySettings?.bypassLAN || false,
cleanIPs: Settings?.get('cleanIPs')?.replaceAll(' ', '') || currentProxySettings?.cleanIPs || '',
proxyIP: Settings?.get('proxyIP') || currentProxySettings?.proxyIP || '',
ports: Settings?.getAll('ports[]') || currentProxySettings?.ports || ['443'],
outProxy: vlessConfig || currentProxySettings?.outProxy || '',
remoteDNS: Settings ? Settings.get('remoteDNS') : currentProxySettings?.remoteDNS || 'https://94.140.14.14/dns-query',
localDNS: Settings ? Settings.get('localDNS') : currentProxySettings?.localDNS || '8.8.8.8',
lengthMin: Settings ? Settings.get('fragmentLengthMin') : currentProxySettings?.lengthMin || '100',
lengthMax: Settings ? Settings.get('fragmentLengthMax') : currentProxySettings?.lengthMax || '200',
intervalMin: Settings ? Settings.get('fragmentIntervalMin') : currentProxySettings?.intervalMin || '5',
intervalMax: Settings ? Settings.get('fragmentIntervalMax') : currentProxySettings?.intervalMax || '10',
blockAds: Settings ? Settings.get('block-ads') : currentProxySettings?.blockAds || false,
bypassIran: Settings ? Settings.get('bypass-iran') : currentProxySettings?.bypassIran || false,
blockPorn: Settings ? Settings.get('block-porn') : currentProxySettings?.blockPorn || false,
bypassLAN: Settings ? Settings.get('bypass-lan') : currentProxySettings?.bypassLAN || false,
cleanIPs: Settings ? Settings.get('cleanIPs')?.replaceAll(' ', '') : currentProxySettings?.cleanIPs || '',
proxyIP: Settings ? Settings.get('proxyIP') : currentProxySettings?.proxyIP || '',
ports: Settings ? Settings.getAll('ports[]') : currentProxySettings?.ports || ['443'],
outProxy: Settings ? vlessConfig : currentProxySettings?.outProxy || '',
outProxyParams: vlessConfig ? await extractVlessParams(vlessConfig) : currentProxySettings?.outProxyParams || '',
wowEndpoint: Settings?.get('wowEndpoint')?.replaceAll(' ', '') || currentProxySettings?.wowEndpoint || 'engage.cloudflareclient.com:2408',
warpEndpoints: Settings?.get('warpEndpoints')?.replaceAll(' ', '') || currentProxySettings?.warpEndpoints || 'engage.cloudflareclient.com:2408',
wowEndpoint: Settings ? Settings.get('wowEndpoint')?.replaceAll(' ', '') : currentProxySettings?.wowEndpoint || 'engage.cloudflareclient.com:2408',
warpEndpoints: Settings ? Settings.get('warpEndpoints')?.replaceAll(' ', '') : currentProxySettings?.warpEndpoints || 'engage.cloudflareclient.com:2408',
panelVersion: panelVersion
};

Expand Down Expand Up @@ -1778,7 +1790,7 @@ const renderHomePage = async (env, hostName, fragConfigs) => {
let id = `port-${port}`;
let portBlock = `
<div class="routing" style="grid-template-columns: 1fr 2fr; margin-right: 10px;">
<input type="checkbox" id=${id} name=${id} onchange="handlePortChange(event)" value="true" ${ports.includes(port) ? 'checked' : ''}>
<input type="checkbox" id=${id} name=${port} onchange="handlePortChange(event)" value="true" ${ports.includes(port) ? 'checked' : ''}>
<label style="margin-bottom: 3px;" for=${id}>${port}</label>
</div>`;
defaultHttpPorts.includes(port) ? httpPortsBlock += portBlock : httpsPortsBlock += portBlock;
Expand Down Expand Up @@ -2106,11 +2118,11 @@ const renderHomePage = async (env, hostName, fragConfigs) => {
<h2>WARP ENDPOINTS ⚙️</h2>
<div class="form-control">
<label for="wowEndpoint">✨ WoW Endpoints</label>
<input type="text" id="wowEndpoint" name="wowEndpoint" value="${wowEndpoint.replaceAll(",", " , ")}">
<input type="text" id="wowEndpoint" name="wowEndpoint" value="${wowEndpoint.replaceAll(",", " , ")}" required>
</div>
<div class="form-control">
<label for="warpEndpoints">✨ Warp Endpoints</label>
<input type="text" id="warpEndpoints" name="warpEndpoints" value="${warpEndpoints.replaceAll(",", " , ")}">
<input type="text" id="warpEndpoints" name="warpEndpoints" value="${warpEndpoints.replaceAll(",", " , ")}" required>
</div>
<div class="form-control">
<label>🔎 Scanner Script</label>
Expand Down Expand Up @@ -2178,6 +2190,10 @@ const renderHomePage = async (env, hostName, fragConfigs) => {
<span class="material-symbols-outlined symbol">verified</span>
<span>Nekoray (Sing-Box)</span>
</div>
<div>
<span class="material-symbols-outlined symbol">verified</span>
<span>Karing</span>
</div>
</td>
<td>
<button onclick="copyToClipboard('https://${hostName}/sub/${userID}?app=singbox#BPB-Normal', false)">
Expand Down Expand Up @@ -2347,7 +2363,9 @@ const renderHomePage = async (env, hostName, fragConfigs) => {
<script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
<script>
let activePortsNo = ${ports.length};
const defaultHttpsPorts = ['443', '8443', '2053', '2083', '2087', '2096'];
let activePortsNo = ${ports.length};
let activeHttpsPortsNo = ${ports.filter(port => defaultHttpsPorts.includes(port)).length};
document.addEventListener('DOMContentLoaded', () => {
const configForm = document.getElementById('configForm');
const modal = document.getElementById('myModal');
Expand Down Expand Up @@ -2413,12 +2431,29 @@ const renderHomePage = async (env, hostName, fragConfigs) => {
});
const handlePortChange = (event) => {
event.target.checked ? activePortsNo++ : activePortsNo--;
if(event.target.checked) {
activePortsNo++
defaultHttpsPorts.includes(event.target.name) && activeHttpsPortsNo++;
} else {
activePortsNo--;
defaultHttpsPorts.includes(event.target.name) && activeHttpsPortsNo--;
}
if (activePortsNo === 0) {
event.preventDefault();
event.target.checked = !event.target.checked;
alert("⛔ At least one port should be selected! 🫤");
activePortsNo = 1;
defaultHttpsPorts.includes(event.target.name) && activeHttpsPortsNo++;
return false;
}
if (activeHttpsPortsNo === 0) {
event.preventDefault();
event.target.checked = !event.target.checked;
alert("⛔ At least one TLS(https) port should be selected! 🫤");
activeHttpsPortsNo = 1;
return false;
}
}
Expand Down Expand Up @@ -2464,7 +2499,7 @@ const renderHomePage = async (env, hostName, fragConfigs) => {
const intervalMax = getValue('fragmentIntervalMax');
const proxyIP = document.getElementById('proxyIP').value?.trim();
const cleanIP = document.getElementById('cleanIPs');
const wowEndpoint = document.getElementById('warpEndpoints').value?.replaceAll(' ', '').split(',');
const wowEndpoint = document.getElementById('wowEndpoint').value?.replaceAll(' ', '').split(',');
const warpEndpoints = document.getElementById('warpEndpoints').value?.replaceAll(' ', '').split(',');
const cleanIPs = cleanIP.value?.split(',');
const chainProxy = document.getElementById('outProxy').value?.trim();
Expand All @@ -2474,7 +2509,7 @@ const renderHomePage = async (env, hostName, fragConfigs) => {
const validSecurityType = /security=(tls|none|reality)/.test(chainProxy);
const validTransmission = /type=(tcp|grpc|ws)/.test(chainProxy);
const validIPDomain = /^((?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,})|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|\\[(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}\\]|\\[(?:[a-fA-F0-9]{1,4}:){1,7}:\\]|\\[(?:[a-fA-F0-9]{1,4}:){1,6}:[a-fA-F0-9]{1,4}\\]|\\[(?:[a-fA-F0-9]{1,4}:){1,5}(?::[a-fA-F0-9]{1,4}){1,2}\\]|\\[(?:[a-fA-F0-9]{1,4}:){1,4}(?::[a-fA-F0-9]{1,4}){1,3}\\]|\\[(?:[a-fA-F0-9]{1,4}:){1,3}(?::[a-fA-F0-9]{1,4}){1,4}\\]|\\[(?:[a-fA-F0-9]{1,4}:){1,2}(?::[a-fA-F0-9]{1,4}){1,5}\\]|\\[[a-fA-F0-9]{1,4}:(?::[a-fA-F0-9]{1,4}){1,6}\\]|\\[:(?::[a-fA-F0-9]{1,4}){1,7}\\]|\\[\\](?:::[a-fA-F0-9]{1,4}){1,7}\\])$/i;
const checkedPorts = Array.from(document.querySelectorAll('input[name^="port-"]:checked')).map(input => input.name.split('-')[1]);
const checkedPorts = Array.from(document.querySelectorAll('input[id^="port-"]:checked')).map(input => input.id.split('-')[1]);
const validEndpoint = /^(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,}|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|\\[(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}\\]|\\[(?:[a-fA-F0-9]{1,4}:){1,7}:\\]|\\[(?:[a-fA-F0-9]{1,4}:){1,6}:[a-fA-F0-9]{1,4}\\]|\\[(?:[a-fA-F0-9]{1,4}:){1,5}(?::[a-fA-F0-9]{1,4}){1,2}\\]|\\[(?:[a-fA-F0-9]{1,4}:){1,4}(?::[a-fA-F0-9]{1,4}){1,3}\\]|\\[(?:[a-fA-F0-9]{1,4}:){1,3}(?::[a-fA-F0-9]{1,4}){1,4}\\]|\\[(?:[a-fA-F0-9]{1,4}:){1,2}(?::[a-fA-F0-9]{1,4}){1,5}\\]|\\[[a-fA-F0-9]{1,4}:(?::[a-fA-F0-9]{1,4}){1,6}\\]|\\[:(?::[a-fA-F0-9]{1,4}){1,7}\\]|\\[::(?::[a-fA-F0-9]{1,4}){0,7}\\]):(?:[0-9]{1,5})$/;
checkedPorts.forEach(port => formData.append('ports[]', port));
Expand Down Expand Up @@ -2987,34 +3022,41 @@ const singboxConfigTemp = {
dns: {
servers: [
{
tag: "dns-remote",
address: "https://8.8.8.8/dns-query",
address_resolver: "dns-direct"
address_resolver: "dns-direct",
strategy: "prefer_ipv4",
tag: "dns-remote"
},
{
tag: "dns-direct",
address: "8.8.8.8",
address_resolver: "dns-local",
detour: "direct"
strategy: "prefer_ipv4",
detour: "direct",
tag: "dns-direct"
},
{
tag: "dns-local",
address: "local",
detour: "direct"
tag: "dns-local"
},
{
tag: "dns-block",
address: "rcode://success"
address: "rcode://success",
tag: "dns-block"
}
],
rules: [
{
domain_suffix: [".ir"],
domain_suffix: ".ir",
server: "dns-direct"
},
{
outbound: "direct",
server: "dns-direct"
disable_cache: true,
rule_set: [
"geosite-category-ads-all",
"geosite-malware",
"geosite-phishing",
"geosite-cryptominers"
],
server: "dns-block"
}
],
independent_cache: true
Expand All @@ -3031,8 +3073,9 @@ const singboxConfigTemp = {
{
type: "tun",
tag: "tun-in",
mtu: 9000,
inet4_address: "172.19.0.1/28",
inet6_address: "fdfe:dcba:9876::1/126",
mtu: 9000,
auto_route: true,
strict_route: true,
endpoint_independent_nat: true,
Expand Down Expand Up @@ -3089,7 +3132,7 @@ const singboxConfigTemp = {
{
network: "udp",
port: 443,
port_range: [],
protocol: "quic",
outbound: "block"
},
{
Expand Down Expand Up @@ -3180,6 +3223,9 @@ const singboxConfigTemp = {
final: "proxy"
},
experimental: {
cache_file: {
enabled: true
},
clash_api: {
external_controller: "0.0.0.0:9090",
external_ui: "yacd",
Expand Down

0 comments on commit 3c8e505

Please sign in to comment.