diff --git a/_worker.js b/_worker.js index 69e79ced6..c8cffd11b 100644 --- a/_worker.js +++ b/_worker.js @@ -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.3'; +let panelVersion = '2.4.4'; if (!isValidUUID(userID)) { throw new Error('uuid is not valid'); @@ -98,7 +98,9 @@ export default { } if (!isAuth) return Response.redirect(`${url.origin}/login`, 302); - if (! await env.bpb.get('proxySettings')) await updateDataset(env); + const proxySettings = await env.bpb.get("proxySettings", {type: 'json'}); + const isUpdated = panelVersion === proxySettings.panelVersion; + if (!proxySettings || !isUpdated) await updateDataset(env); const fragConfs = await getFragmentConfigs(env, host, 'nekoray'); const homePage = await renderHomePage(env, host, fragConfs); @@ -1022,7 +1024,7 @@ const getFragmentConfigs = async (env, hostName, client) => { proxyIP, outProxy, outProxyParams, - ports = ['443'] + ports } = proxySettings; const resolved = await resolveDNS(hostName); @@ -1167,7 +1169,7 @@ const getFragmentConfigs = async (env, hostName, client) => { } bestFragment.observatory.subjectSelector = ["frag"]; - bestFragment.observatory.probeInterval = '1m'; + bestFragment.observatory.probeInterval = '30s'; bestFragment.routing.balancers[0].selector = ["frag"]; if (client === 'nekoray') { @@ -1234,132 +1236,183 @@ const getSingboxConfig = async (env, hostName) => { const getWarpConfigs = async (env, client) => { let proxySettings = {}; - let xrayOutbounds = [], singboxOutbounds = []; - + let xrayWarpConfigs = []; + let xrayWarpConfig = structuredClone(xrayConfigTemp); + let xrayWarpBestPing = structuredClone(xrayConfigTemp); + let xrayWoWConfigTemp = structuredClone(xrayConfigTemp); + let singboxWarpConfig = structuredClone(singboxConfigTemp); + try { proxySettings = await env.bpb.get("proxySettings", {type: 'json'}); } catch (error) { console.log(error); throw new Error(`An error occurred while getting fragment configs - ${error}`); } - - const { - remoteDNS, - localDNS, - blockAds, - bypassIran, - blockPorn, - bypassLAN, - wowEndpoint, - warpEndpoints - } = proxySettings; - - const ipv6Regex = /\[(.*?)\]/; - const portRegex = /[^:]*$/; - let xrayWoWConfig = structuredClone(xrayConfigTemp); - let singboxWarpConfig = structuredClone(singboxConfigTemp); + + const {remoteDNS, localDNS, blockAds, bypassIran, blockPorn, bypassLAN, wowEndpoint, warpEndpoints} = proxySettings; + const {xray: xrayWarpOutbounds, singbox: singboxWarpOutbounds} = await buildWarpOutbounds(remoteDNS, localDNS, blockAds, bypassIran, blockPorn, bypassLAN, warpEndpoints) + const {xray: xrayWoWOutbounds, singbox: singboxWoWOutbounds} = await buildWoWOutbounds(remoteDNS, localDNS, blockAds, bypassIran, blockPorn, bypassLAN, wowEndpoint); + singboxWarpConfig.outbounds[0].outbounds = ['💦 Warp Best Ping 🚀']; singboxWarpConfig.outbounds[1].tag = '💦 Warp Best Ping 🚀'; + xrayWarpConfig.dns = await buildDNSObject(remoteDNS, localDNS, blockAds, bypassIran, blockPorn); + xrayWarpConfig.routing.rules = buildRoutingRules(localDNS, blockAds, bypassIran, blockPorn, bypassLAN, false, false); + xrayWarpConfig.outbounds.splice(0,1); + xrayWarpConfig.routing.rules[xrayWarpConfig.routing.rules.length - 1].outboundTag = 'warp'; + delete xrayWarpConfig.observatory; + delete xrayWarpConfig.routing.balancers; + xrayWarpBestPing.remarks = '💦 BPB - Warp Best Ping 🚀' + 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.observatory.subjectSelector = ['warp']; + xrayWoWConfigTemp.dns = await buildDNSObject(remoteDNS, localDNS, blockAds, bypassIran, blockPorn); + xrayWoWConfigTemp.routing.rules = buildRoutingRules(localDNS, blockAds, bypassIran, blockPorn, bypassLAN, false, false); + xrayWoWConfigTemp.outbounds.splice(0,1); + delete xrayWoWConfigTemp.observatory; + delete xrayWoWConfigTemp.routing.balancers; + + xrayWarpOutbounds.forEach((outbound, index) => { + xrayWarpConfigs.push({ + ...xrayWarpConfig, + remarks: `💦 BPB - Warp ${index + 1} 🇮🇷`, + outbounds: [{...outbound, tag: 'warp'}, ...xrayWarpConfig.outbounds] + }); + }); + + xrayWoWOutbounds.forEach((outbound, index) => { + if (outbound.tag.includes('warp-out')) { + let xrayWoWConfig = structuredClone(xrayWoWConfigTemp); + xrayWoWConfig.remarks = `💦 BPB - WoW ${index/2 + 1} 🌍`; + xrayWoWConfig.outbounds = [{...xrayWoWOutbounds[index]}, {...xrayWoWOutbounds[index + 1]}, ...xrayWoWConfig.outbounds]; + xrayWoWConfig.routing.rules[xrayWoWConfig.routing.rules.length - 1].outboundTag = outbound.tag; + xrayWarpConfigs.push(xrayWoWConfig); + } + }); - for (let i = 0; i < 2; i++) { - let wgConfig = await fetchWgConfig(); - let xrayOutbound = structuredClone(xrayWgOutboundTemp); - let singboxOutbound = structuredClone(singboxWgOutboundTemp); - xrayOutbound.settings.address = [ - `${wgConfig.account.config.interface.addresses.v4}/32`, - `${wgConfig.account.config.interface.addresses.v6}/128` - ]; + xrayWarpBestPing.outbounds = [...xrayWarpOutbounds, ...xrayWarpBestPing.outbounds]; + xrayWarpConfigs.push(xrayWarpBestPing); + + singboxWarpOutbounds.forEach((outbound, index) => { + singboxWarpConfig.outbounds.push(outbound); + singboxWarpConfig.outbounds[0].outbounds.push(outbound.tag); + singboxWarpConfig.outbounds[1].outbounds.push(outbound.tag); + }); - if (wowEndpoint) xrayOutbound.settings.peers[0].endpoint = wowEndpoint; - xrayOutbound.settings.peers[0].publicKey = wgConfig.account.config.peers[0].public_key; - xrayOutbound.settings.reserved = base64ToDecimal(wgConfig.account.config.client_id); - xrayOutbound.settings.secretKey = wgConfig.privateKey; - xrayOutbound.tag = i === 1 ? 'warp-ir' : 'warp-out'; - - if (i === 1) { - delete xrayOutbound.streamSettings; - } else { - xrayOutbound.streamSettings.sockopt.dialerProxy = 'warp-ir'; + singboxWoWOutbounds.forEach((outbound, index) => { + if (outbound.tag.includes('WoW')) { + singboxWarpConfig.outbounds.push(singboxWoWOutbounds[index], singboxWoWOutbounds[index + 1]); + singboxWarpConfig.outbounds[0].outbounds.push(outbound.tag); } + }); - xrayOutbounds.push(xrayOutbound); + return client === 'singbox' + ? singboxWarpConfig + : xrayWarpConfigs; +} - singboxOutbound.local_address = [ - `${wgConfig.account.config.interface.addresses.v4}/32`, - `${wgConfig.account.config.interface.addresses.v6}/128` - ]; +const buildWarpOutbounds = async (remoteDNS, localDNS, blockAds, bypassIran, blockPorn, bypassLAN, warpEndpoints) => { + let wgConfig = await fetchWgConfig(); + let xrayOutboundTemp = structuredClone(xrayWgOutboundTemp); + let singboxOutbound = structuredClone(singboxWgOutboundTemp); + let xrayOutbounds = []; + let singboxOutbounds = []; + const ipv6Regex = /\[(.*?)\]/; + const portRegex = /[^:]*$/; - if (wowEndpoint) { - singboxOutbound.server = wowEndpoint.includes('[') ? wowEndpoint.match(ipv6Regex)[1] : wowEndpoint.split(':')[0]; - singboxOutbound.server_port = wowEndpoint.includes('[') ? +wowEndpoint.match(portRegex)[0] : +wowEndpoint.split(':')[1]; - } + xrayOutboundTemp.settings.address = [ + `${wgConfig.account.config.interface.addresses.v4}/32`, + `${wgConfig.account.config.interface.addresses.v6}/128` + ]; + + xrayOutboundTemp.settings.peers[0].publicKey = wgConfig.account.config.peers[0].public_key; + xrayOutboundTemp.settings.reserved = base64ToDecimal(wgConfig.account.config.client_id); + xrayOutboundTemp.settings.secretKey = wgConfig.privateKey; + delete xrayOutboundTemp.streamSettings; + + singboxOutbound.local_address = [ + `${wgConfig.account.config.interface.addresses.v4}/32`, + `${wgConfig.account.config.interface.addresses.v6}/128` + ]; - singboxOutbound.peer_public_key = wgConfig.account.config.peers[0].public_key; - singboxOutbound.reserved = wgConfig.account.config.client_id; - singboxOutbound.private_key = wgConfig.privateKey; - singboxOutbound.tag = i === 1 ? '💦 Warp-ir' : '💦 WoW 🌍'; + singboxOutbound.peer_public_key = wgConfig.account.config.peers[0].public_key; + singboxOutbound.reserved = wgConfig.account.config.client_id; + singboxOutbound.private_key = wgConfig.privateKey; + delete singboxOutbound.detour; + + warpEndpoints.split(',').forEach( (endpoint, index) => { + let xrayOutbound = structuredClone(xrayOutboundTemp); + xrayOutbound.settings.peers[0].endpoint = endpoint; + xrayOutbound.tag = `warp-${index + 1}`; + xrayOutbounds.push(xrayOutbound); - if (i === 0) { - singboxOutbound.detour = '💦 Warp-ir'; - singboxWarpConfig.outbounds[0].outbounds.push('💦 WoW 🌍'); - } else { - delete singboxOutbound.detour; - } + singboxOutbounds.push({ + ...singboxOutbound, + server: endpoint.includes('[') ? endpoint.match(ipv6Regex)[1] : endpoint.split(':')[0], + server_port: endpoint.includes('[') ? +endpoint.match(portRegex)[0] : +endpoint.split(':')[1], + tag: `💦 Warp ${index + 1} 🇮🇷` + }); + }) + + return {xray: xrayOutbounds, singbox: singboxOutbounds}; +} - singboxOutbounds.push(singboxOutbound); - } +const buildWoWOutbounds = async (remoteDNS, localDNS, blockAds, bypassIran, blockPorn, bypassLAN, wowEndpoint) => { + let xrayOutbounds = []; + let singboxOutbounds = []; + const ipv6Regex = /\[(.*?)\]/; + const portRegex = /[^:]*$/; + const wgConfigs = [await fetchWgConfig(), await fetchWgConfig()]; - xrayWoWConfig.remarks = '💦 BPB - WoW 🌍'; - xrayWoWConfig.dns = await buildDNSObject(remoteDNS, localDNS, blockAds, bypassIran, blockPorn); - xrayWoWConfig.routing.rules = buildRoutingRules(localDNS, blockAds, bypassIran, blockPorn, bypassLAN, false, false); - xrayWoWConfig.outbounds.splice(0,1); - delete xrayWoWConfig.observatory; - delete xrayWoWConfig.routing.balancers; - xrayWoWConfig.outbounds = [...xrayOutbounds, ...xrayWoWConfig.outbounds]; - xrayWoWConfig.routing.rules[xrayWoWConfig.routing.rules.length - 1].outboundTag = 'warp-out'; + wowEndpoint.split(',').forEach( (endpoint, index) => { + + for (let i = 0; i < 2; i++) { + let xrayOutbound = structuredClone(xrayWgOutboundTemp); + let singboxOutbound = structuredClone(singboxWgOutboundTemp); + xrayOutbound.settings.address = [ + `${wgConfigs[i].account.config.interface.addresses.v4}/32`, + `${wgConfigs[i].account.config.interface.addresses.v6}/128` + ]; - let xrayWarpConfigs = []; - let xrayWarpOutbounds = []; + xrayOutbound.settings.peers[0].endpoint = endpoint; + xrayOutbound.settings.peers[0].publicKey = wgConfigs[i].account.config.peers[0].public_key; + xrayOutbound.settings.reserved = base64ToDecimal(wgConfigs[i].account.config.client_id); + xrayOutbound.settings.secretKey = wgConfigs[i].privateKey; + xrayOutbound.tag = i === 1 ? `warp-ir_${index + 1}` : `warp-out_${index + 1}`; + + if (i === 1) { + delete xrayOutbound.streamSettings; + } else { + xrayOutbound.streamSettings.sockopt.dialerProxy = `warp-ir_${index + 1}`; + } - warpEndpoints.split(',').forEach((endpoint, index) => { - let xrayWarpConfig = structuredClone(xrayWoWConfig); - xrayWarpConfig.outbounds.splice(0,1); - xrayWarpConfig.outbounds[0].settings.peers[0].endpoint = endpoint; - xrayWarpConfig.outbounds[0].tag = 'warp'; - xrayWarpConfig.routing.rules[xrayWarpConfig.routing.rules.length - 1].outboundTag = 'warp'; - xrayWarpConfig.remarks = `💦 BPB - Warp ${index + 1} 🇮🇷`; - xrayWarpConfigs.push(xrayWarpConfig); - let xrayWarpOutbound = structuredClone(xrayWarpConfig.outbounds[0]); - xrayWarpOutbound.tag = `warp_${index + 1}`; - xrayWarpOutbounds.push(xrayWarpOutbound); - - let singboxWarpOutbound = structuredClone(singboxOutbounds[singboxOutbounds.length - 1]); - singboxWarpOutbound.server = endpoint.includes('[') ? endpoint.match(ipv6Regex)[1] : endpoint.split(':')[0]; - singboxWarpOutbound.server_port = endpoint.includes('[') ? +endpoint.match(portRegex)[0] : +endpoint.split(':')[1]; - singboxWarpOutbound.tag = `💦 Warp ${index + 1} 🇮🇷`; - singboxWarpOutbound - singboxOutbounds.push(singboxWarpOutbound); - singboxWarpConfig.outbounds[0].outbounds.push(singboxWarpOutbound.tag); - singboxWarpConfig.outbounds[1].outbounds.push(singboxWarpOutbound.tag); - }); - - let xrayWarpBestPing = structuredClone(xrayConfigTemp); - xrayWarpBestPing.remarks = '💦 BPB - Warp Best Ping 🚀'; - 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.outbounds = [...xrayWarpOutbounds, ...xrayWarpBestPing.outbounds]; - xrayWarpBestPing.routing.balancers[0].selector = 'warp'; - xrayWarpBestPing.observatory.subjectSelector = ['warp']; + xrayOutbounds.push(xrayOutbound); + + singboxOutbound.local_address = [ + `${wgConfigs[i].account.config.interface.addresses.v4}/32`, + `${wgConfigs[i].account.config.interface.addresses.v6}/128` + ]; + + singboxOutbound.server = endpoint.includes('[') ? endpoint.match(ipv6Regex)[1] : endpoint.split(':')[0]; + singboxOutbound.server_port = endpoint.includes('[') ? +endpoint.match(portRegex)[0] : +endpoint.split(':')[1]; + singboxOutbound.peer_public_key = wgConfigs[i].account.config.peers[0].public_key; + singboxOutbound.reserved = wgConfigs[i].account.config.client_id; + singboxOutbound.private_key = wgConfigs[i].privateKey; + singboxOutbound.tag = i === 1 ? `warp-ir_${index + 1}` : `💦 WoW ${index + 1} 🌍`; + + if (i === 0) { + singboxOutbound.detour = `warp-ir_${index + 1}`; + } else { + delete singboxOutbound.detour; + } + + singboxOutbounds.push(singboxOutbound); + } - singboxWarpConfig.dns.servers[0].address = remoteDNS; - singboxWarpConfig.dns.servers[1].address = localDNS; - singboxWarpConfig.dns.rules[0].domain = 'engage.cloudflareclient.com'; - singboxWarpConfig.outbounds = [...singboxWarpConfig.outbounds, ...singboxOutbounds]; + }) - return client === 'singbox' - ? singboxWarpConfig - : [{...xrayWoWConfig}, {...xrayWarpBestPing}, ...xrayWarpConfigs]; + return {xray: xrayOutbounds, singbox: singboxOutbounds}; } const fetchWgConfig = async () => { @@ -1521,26 +1574,55 @@ const base64ToDecimal = (base64) => { } const updateDataset = async (env, Settings) => { + let currentProxySettings = {}; + + try { + currentProxySettings = await env.bpb.get("proxySettings", {type: 'json'}); + } catch (error) { + console.log(error); + throw new Error(`An error occurred while getting current values - ${error}`); + } + + const { + remoteDNS: currentRemoteDNS, + localDNS: currentLocalDNS, + lengthMin: currentLengthMin, + lengthMax: currentLengthMax, + intervalMin: currentIntervalMin, + intervalMax: currentIntervalMax, + blockAds: currentBlockAds, + bypassIran: currentBypassIran, + blockPorn: currentBlockPorn, + bypassLAN: currentBypassLAN, + cleanIPs: currentCleanIPs, + proxyIP: currentProxyIP, + outProxy: currentOutProxy, + outProxyParams: currentOutProxyParams, + wowEndpoint: currentWoWEndpoint, + warpEndpoints: currentWarpEndpoints, + ports: currentPorts + } = currentProxySettings; const vlessConfig = Settings?.get('outProxy'); const proxySettings = { - remoteDNS: Settings?.get('remoteDNS') || 'https://94.140.14.14/dns-query', - localDNS: Settings?.get('localDNS') || '8.8.8.8', - lengthMin: Settings?.get('fragmentLengthMin') || '100', - lengthMax: Settings?.get('fragmentLengthMax') || '200', - intervalMin: Settings?.get('fragmentIntervalMin') || '5', - intervalMax: Settings?.get('fragmentIntervalMax') || '10', - blockAds: Settings?.get('block-ads') || false, - bypassIran: Settings?.get('bypass-iran') || false, - blockPorn: Settings?.get('block-porn') || false, - bypassLAN: Settings?.get('bypass-lan') || false, - cleanIPs: Settings?.get('cleanIPs')?.replaceAll(' ', '') || '', - proxyIP: Settings?.get('proxyIP') || '', - ports: Settings?.getAll('ports[]') || ['443'], - outProxy: vlessConfig || '', - outProxyParams: vlessConfig ? await extractVlessParams(vlessConfig) : '', - wowEndpoint: Settings?.get('wowEndpoint')?.replaceAll(' ', '') || 'engage.cloudflareclient.com:2408', - warpEndpoints: Settings?.get('warpEndpoints')?.replaceAll(' ', '') || 'engage.cloudflareclient.com:2408' + remoteDNS: Settings?.get('remoteDNS') || currentRemoteDNS || 'https://94.140.14.14/dns-query', + localDNS: Settings?.get('localDNS') || currentLocalDNS || '8.8.8.8', + lengthMin: Settings?.get('fragmentLengthMin') || currentLengthMin || '100', + lengthMax: Settings?.get('fragmentLengthMax') || currentLengthMax || '200', + intervalMin: Settings?.get('fragmentIntervalMin') || currentIntervalMin || '5', + intervalMax: Settings?.get('fragmentIntervalMax') || currentIntervalMax || '10', + blockAds: Settings?.get('block-ads') || currentBlockAds || false, + bypassIran: Settings?.get('bypass-iran') || currentBypassIran || false, + blockPorn: Settings?.get('block-porn') || currentBlockPorn || false, + bypassLAN: Settings?.get('bypass-lan') || currentBypassLAN || false, + cleanIPs: Settings?.get('cleanIPs')?.replaceAll(' ', '') || currentCleanIPs || '', + proxyIP: Settings?.get('proxyIP') || currentProxyIP || '', + ports: Settings?.getAll('ports[]') || currentPorts || ['443'], + outProxy: vlessConfig || currentOutProxy || '', + outProxyParams: vlessConfig ? await extractVlessParams(vlessConfig) : currentOutProxyParams || '', + wowEndpoint: Settings?.get('wowEndpoint')?.replaceAll(' ', '') || currentWoWEndpoint || 'engage.cloudflareclient.com:2408', + warpEndpoints: Settings?.get('warpEndpoints')?.replaceAll(' ', '') || currentWarpEndpoints || 'engage.cloudflareclient.com:2408', + panelVersion: panelVersion }; try { @@ -1664,22 +1746,22 @@ const renderHomePage = async (env, hostName, fragConfigs) => { } const { - remoteDNS = '', - localDNS = '', - lengthMin = '', - lengthMax = '', - intervalMin = '', - intervalMax = '', - blockAds = false, - bypassIran = false, - blockPorn = false, - bypassLAN = false, - cleanIPs = '', - proxyIP = '', - outProxy = '', - ports = ['443'], - wowEndpoint = 'engage.cloudflareclient.com:2408', - warpEndpoints = 'engage.cloudflareclient.com:2408' + remoteDNS, + localDNS, + lengthMin, + lengthMax, + intervalMin, + intervalMax, + blockAds, + bypassIran, + blockPorn, + bypassLAN, + cleanIPs, + proxyIP, + outProxy, + ports, + wowEndpoint, + warpEndpoints } = proxySettings; const genCustomConfRow = async (configs) => { @@ -2041,9 +2123,9 @@ const renderHomePage = async (env, hostName, fragConfigs) => { `} -