diff --git a/src/authentication/auth.js b/src/authentication/auth.js index ee6e4f9b5..aa4d558e9 100644 --- a/src/authentication/auth.js +++ b/src/authentication/auth.js @@ -1,10 +1,10 @@ import { SignJWT, jwtVerify } from 'jose'; import nacl from 'tweetnacl'; -import { configs } from '../helpers/config.js'; -let userID = configs.userID; +import { initializeParams, userID } from "../helpers/init.js"; + export async function generateJWTToken (env, secretKey) { - userID = env.UUID || userID; + await initializeParams(env); const secret = new TextEncoder().encode(secretKey); return await new SignJWT({ userID }) .setProtectedHeader({ alg: 'HS256' }) diff --git a/src/cores-configs/clash.js b/src/cores-configs/clash.js index 98e363547..dafc1ec98 100644 --- a/src/cores-configs/clash.js +++ b/src/cores-configs/clash.js @@ -1,8 +1,5 @@ import { getConfigAddresses, extractWireguardParams, generateRemark, randomUpperCase, getRandomPath, isIPv6 } from './helpers.js'; -import { configs } from '../helpers/config.js'; -let userID = configs.userID; -let trojanPassword = configs.userID; -const defaultHttpsPorts = configs.defaultHttpsPorts; +import { initializeParams, userID, trojanPassword, defaultHttpsPorts } from "../helpers/init.js"; async function buildClashDNS (proxySettings, isWarp) { const { @@ -315,9 +312,8 @@ export async function getClashWarpConfig(proxySettings, warpConfigs) { } export async function getClashNormalConfig (env, hostName, proxySettings) { + await initializeParams(env); let chainProxy; - userID = env.UUID || userID; - trojanPassword = env.TROJAN_PASS || trojanPassword; const { cleanIPs, proxyIP, diff --git a/src/cores-configs/normalConfigs.js b/src/cores-configs/normalConfigs.js index 88f41fb1d..daf7436cd 100644 --- a/src/cores-configs/normalConfigs.js +++ b/src/cores-configs/normalConfigs.js @@ -1,12 +1,8 @@ import { getConfigAddresses, generateRemark, randomUpperCase, getRandomPath } from './helpers.js'; -import { configs } from '../helpers/config.js'; -let userID = configs.userID; -let trojanPassword = configs.userID; -const defaultHttpsPorts = configs.defaultHttpsPorts; +import { initializeParams, userID, trojanPassword, defaultHttpsPorts } from "../helpers/init.js"; export async function getNormalConfigs(env, hostName, proxySettings, client) { - userID = env.UUID || userID; - trojanPassword = env.TROJAN_PASS || trojanPassword; + await initializeParams(env); const { cleanIPs, proxyIP, diff --git a/src/cores-configs/sing-box.js b/src/cores-configs/sing-box.js index c4b959406..d2e24dead 100644 --- a/src/cores-configs/sing-box.js +++ b/src/cores-configs/sing-box.js @@ -1,8 +1,5 @@ import { getConfigAddresses, extractWireguardParams, generateRemark, randomUpperCase, getRandomPath } from './helpers.js'; -import { configs } from '../helpers/config.js'; -let userID = configs.userID; -let trojanPassword = configs.userID; -const defaultHttpsPorts = configs.defaultHttpsPorts; +import { initializeParams, userID, trojanPassword, defaultHttpsPorts } from "../helpers/init.js"; function buildSingBoxDNS (proxySettings, isChain, isWarp) { const { @@ -550,9 +547,8 @@ export async function getSingBoxWarpConfig (proxySettings, warpConfigs, client) } export async function getSingBoxCustomConfig(env, hostName, proxySettings, isFragment) { + await initializeParams(env); let chainProxyOutbound; - userID = env.UUID || userID; - trojanPassword = env.TROJAN_PASS || trojanPassword; const { cleanIPs, ports, diff --git a/src/cores-configs/xray.js b/src/cores-configs/xray.js index ac1147914..3f8e9f081 100644 --- a/src/cores-configs/xray.js +++ b/src/cores-configs/xray.js @@ -1,9 +1,7 @@ import { resolveDNS, isDomain } from '../helpers/helpers.js'; import { getConfigAddresses, extractWireguardParams, base64ToDecimal, generateRemark, randomUpperCase, getRandomPath } from './helpers.js'; -import { configs } from '../helpers/config.js'; -let userID = configs.userID; -let trojanPassword = configs.userID; -const defaultHttpsPorts = configs.defaultHttpsPorts; +import { initializeParams, userID, trojanPassword, defaultHttpsPorts } from "../helpers/init.js"; + async function buildXrayDNS (proxySettings, outboundAddrs, domainToStaticIPs, isWorkerLess, isBalancer, isWarp) { const { @@ -635,8 +633,7 @@ async function buildXrayWorkerLessConfig(proxySettings) { } export async function getXrayCustomConfigs(env, hostName, proxySettings, isFragment) { - userID = env.UUID || userID; - trojanPassword = env.TROJAN_PASS || trojanPassword; + await initializeParams(env); let configs = []; let outbounds = []; let protocols = []; diff --git a/src/helpers/config.js b/src/helpers/config.js deleted file mode 100644 index aa5e9e322..000000000 --- a/src/helpers/config.js +++ /dev/null @@ -1,10 +0,0 @@ -const proxyIPs = ['bpb.yousef.isegaro.com']; -export const configs = { - userID: '89b3cbba-e6ac-485a-9481-976a0415eab9', - dohURL: 'https://cloudflare-dns.com/dns-query', - proxyIP: proxyIPs[Math.floor(Math.random() * proxyIPs.length)], - trojanPassword: 'bpb-trojan', - defaultHttpPorts: ['80', '8080', '2052', '2082', '2086', '2095', '8880'], - defaultHttpsPorts: ['443', '8443', '2053', '2083', '2087', '2096'], - panelVersion: '2.7.2' -}; diff --git a/src/helpers/helpers.js b/src/helpers/helpers.js index 102286f0d..76e289e1e 100644 --- a/src/helpers/helpers.js +++ b/src/helpers/helpers.js @@ -1,12 +1,10 @@ -import { configs } from './config.js'; -const { dohURL } = configs; - export function isValidUUID(uuid) { const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[4][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; return uuidRegex.test(uuid); } export async function resolveDNS (domain) { + const dohURL = 'https://cloudflare-dns.com/dns-query'; const dohURLv4 = `${dohURL}?name=${encodeURIComponent(domain)}&type=A`; const dohURLv6 = `${dohURL}?name=${encodeURIComponent(domain)}&type=AAAA`; diff --git a/src/helpers/init.js b/src/helpers/init.js new file mode 100644 index 000000000..f740bbad4 --- /dev/null +++ b/src/helpers/init.js @@ -0,0 +1,21 @@ +import { isValidUUID } from './helpers.js'; +const proxyIPs = ['bpb.yousef.isegaro.com']; +let userID, dohURL, proxyIP, trojanPassword, defaultHttpPorts, defaultHttpsPorts, panelVersion; + +function initParams(env) { + userID = env.UUID || '89b3cbba-e6ac-485a-9481-976a0415eab9'; + if (!isValidUUID(userID)) throw new Error(`Invalid UUID: ${userID}`); + dohURL = env.DOH_URL || 'https://cloudflare-dns.com/dns-query'; + proxyIP = env.PROXYIP || proxyIPs[Math.floor(Math.random() * proxyIPs.length)]; + trojanPassword = env.TROJAN_PASS || 'bpb-trojan'; + defaultHttpPorts = ['80', '8080', '2052', '2082', '2086', '2095', '8880']; + defaultHttpsPorts = ['443', '8443', '2053', '2083', '2087', '2096']; + panelVersion = '2.7.2'; +} + +export function initializeParams(env) { + initParams(env); + return Promise.resolve(); +} + +export { userID, dohURL, proxyIP, trojanPassword, defaultHttpPorts, defaultHttpsPorts, panelVersion }; diff --git a/src/kv/handlers.js b/src/kv/handlers.js index 10c29cc9c..0196f1cbe 100644 --- a/src/kv/handlers.js +++ b/src/kv/handlers.js @@ -1,9 +1,9 @@ import { fetchWgConfig } from '../protocols/warp.js'; import { isDomain, resolveDNS } from '../helpers/helpers.js'; -import { configs } from '../helpers/config.js'; -const { panelVersion } = configs; +import { initializeParams, panelVersion } from '../helpers/init.js'; export async function getDataset(env) { + await initializeParams(env); let proxySettings, warpConfigs; if (typeof env.bpb !== 'object') { return {kvNotFound: true, proxySettings: null, warpConfigs: null} @@ -29,6 +29,7 @@ export async function getDataset(env) { } export async function updateDataset (env, newSettings, resetSettings) { + await initializeParams(env); let currentSettings; if (!resetSettings) { try { diff --git a/src/pages/errorPage.js b/src/pages/errorPage.js index 4b39aed09..8b583abb3 100644 --- a/src/pages/errorPage.js +++ b/src/pages/errorPage.js @@ -1,7 +1,7 @@ -import { configs } from '../helpers/config.js'; -const panelVersion = configs.panelVersion; +import { initializeParams, panelVersion } from "../helpers/init.js"; -export function renderErrorPage (message, error, refer) { +export async function renderErrorPage (env, message, error, refer) { + await initializeParams(env); return ` diff --git a/src/pages/homePage.js b/src/pages/homePage.js index 1eb9352b3..19d913f8b 100644 --- a/src/pages/homePage.js +++ b/src/pages/homePage.js @@ -1,9 +1,7 @@ -import { configs } from '../helpers/config.js'; -const { defaultHttpPorts, defaultHttpsPorts, panelVersion } = configs; -let userID = configs.userID; +import { initializeParams, userID, defaultHttpPorts, defaultHttpsPorts, panelVersion } from "../helpers/init.js"; -export function renderHomePage (request, env, hostName, proxySettings, isPassSet) { - userID = env.UUID || userID; +export async function renderHomePage (request, env, hostName, proxySettings, isPassSet) { + await initializeParams(env); const { remoteDNS, localDNS, @@ -72,6 +70,7 @@ export function renderHomePage (request, env, hostName, proxySettings, isPassSet + BPB Panel ${panelVersion} diff --git a/src/pages/loginPage.js b/src/pages/loginPage.js index a24bd3812..facc22a95 100644 --- a/src/pages/loginPage.js +++ b/src/pages/loginPage.js @@ -1,7 +1,7 @@ -import { configs } from '../helpers/config.js'; -const panelVersion = configs.panelVersion; +import { initializeParams, panelVersion } from "../helpers/init.js"; -export function renderLoginPage () { +export async function renderLoginPage (env) { + await initializeParams(env); return ` diff --git a/src/protocols/trojan.js b/src/protocols/trojan.js index d8303fc3c..4664487f1 100644 --- a/src/protocols/trojan.js +++ b/src/protocols/trojan.js @@ -1,13 +1,9 @@ import { connect } from 'cloudflare:sockets'; -import { configs } from '../helpers/config.js'; import sha256 from 'js-sha256'; -// https://www.nslookup.io/domains/bpb.yousef.isegaro.com/dns-records/ -let proxyIP = configs.proxyIP -let trojanPassword = configs.trojanPassword; +import { initializeParams, trojanPassword, proxyIP } from "../helpers/init.js"; export async function trojanOverWSHandler(request, env) { - proxyIP = env.PROXYIP || proxyIP; - trojanPassword = env.TROJAN_PASS || trojanPassword; + await initializeParams(env); const webSocketPair = new WebSocketPair(); const [client, webSocket] = Object.values(webSocketPair); webSocket.accept(); diff --git a/src/protocols/vless.js b/src/protocols/vless.js index 325529ba3..4cd6b8efd 100644 --- a/src/protocols/vless.js +++ b/src/protocols/vless.js @@ -1,9 +1,7 @@ import { connect } from 'cloudflare:sockets'; -import { configs } from '../helpers/config.js'; import { isValidUUID } from '../helpers/helpers.js'; -let proxyIP = configs.proxyIP; -let userID = configs.userID; -const dohURL = configs.dohURL; +import { initializeParams, userID, dohURL, proxyIP } from "../helpers/init.js"; + /** * Handles VLESS over WebSocket requests by creating a WebSocket pair, accepting the WebSocket connection, and processing the VLESS header. * @param {import("@cloudflare/workers-types").Request} request The incoming request object. @@ -12,8 +10,7 @@ const dohURL = configs.dohURL; export async function vlessOverWSHandler(request, env) { /** @type {import("@cloudflare/workers-types").WebSocket[]} */ // @ts-ignore - userID = env.UUID || userID; - proxyIP = env.PROXYIP || proxyIP; + await initializeParams(env); const webSocketPair = new WebSocketPair(); const [client, webSocket] = Object.values(webSocketPair); diff --git a/src/worker.js b/src/worker.js index d7229bfe2..521220542 100644 --- a/src/worker.js +++ b/src/worker.js @@ -11,8 +11,7 @@ import { getXrayCustomConfigs, getXrayWarpConfigs } from './cores-configs/xray.j import { getSingBoxCustomConfig, getSingBoxWarpConfig } from './cores-configs/sing-box.js'; import { getClashNormalConfig, getClashWarpConfig } from './cores-configs/clash.js'; import { getNormalConfigs } from './cores-configs/normalConfigs.js'; -import { configs } from './helpers/config.js'; -import { isValidUUID } from './helpers/helpers.js'; +import { initializeParams, userID } from './helpers/init.js'; export default { async fetch(request, env) { @@ -20,16 +19,32 @@ export default { const upgradeHeader = request.headers.get('Upgrade'); const url = new URL(request.url); if (!upgradeHeader || upgradeHeader !== 'websocket') { - const userID = env.UUID || configs.userID; - if (!isValidUUID(userID)) throw new Error(`Invalid UUID: ${userID}`); + await initializeParams(env); const hostName = request.headers.get('Host'); const searchParams = new URLSearchParams(url.search); const client = searchParams.get('app'); + const clientResponseHeader = { + 'Content-Type': 'text/plain;charset=utf-8', + 'Cache-Control': 'no-store, no-cache, must-revalidate, proxy-revalidate', + 'CDN-Cache-Control': 'no-store' + }; + const pageResponseHeader = { + 'Content-Type': 'text/html;charset=utf-8', + 'Access-Control-Allow-Origin': url.origin, + 'Access-Control-Allow-Methods': 'GET, POST', + 'Access-Control-Allow-Headers': 'Content-Type, Authorization', + 'X-Content-Type-Options': 'nosniff', + 'X-Frame-Options': 'DENY', + 'Referrer-Policy': 'strict-origin-when-cross-origin', + 'Cache-Control': 'no-store, no-cache, must-revalidate, proxy-revalidate, no-transform', + 'CDN-Cache-Control': 'no-store' + }; const { kvNotFound, proxySettings, warpConfigs } = await getDataset(env); if (kvNotFound) { - const errorPage = renderErrorPage('KV Dataset is not properly set!', null, true); + const errorPage = await renderErrorPage(env, 'KV Dataset is not properly set!', null, true); return new Response(errorPage, { status: 200, headers: {'Content-Type': 'text/html'}}); - } + } + switch (url.pathname) { case '/update-warp': @@ -57,11 +72,7 @@ export default { const BestPingSFA = await getSingBoxCustomConfig(env, hostName, proxySettings, false); return new Response(JSON.stringify(BestPingSFA, null, 4), { status: 200, - headers: { - 'Content-Type': 'application/json;charset=utf-8', - 'Cache-Control': 'no-store, no-cache, must-revalidate, proxy-revalidate', - 'CDN-Cache-Control': 'no-store' - } + headers: clientResponseHeader }); } @@ -69,11 +80,7 @@ export default { const BestPingClash = await getClashNormalConfig(env, hostName, proxySettings); return new Response(JSON.stringify(BestPingClash, null, 4), { status: 200, - headers: { - 'Content-Type': 'application/json;charset=utf-8', - 'Cache-Control': 'no-store, no-cache, must-revalidate, proxy-revalidate', - 'CDN-Cache-Control': 'no-store' - } + headers: clientResponseHeader }); } @@ -81,22 +88,14 @@ export default { const xrayFullConfigs = await getXrayCustomConfigs(env, hostName, proxySettings, false); return new Response(JSON.stringify(xrayFullConfigs, null, 4), { status: 200, - headers: { - 'Content-Type': 'application/json;charset=utf-8', - 'Cache-Control': 'no-store, no-cache, must-revalidate, proxy-revalidate', - 'CDN-Cache-Control': 'no-store' - } + headers: clientResponseHeader }); } const normalConfigs = await getNormalConfigs(env, hostName, proxySettings, client); return new Response(normalConfigs, { status: 200, - headers: { - 'Content-Type': 'text/plain;charset=utf-8', - 'Cache-Control': 'no-store, no-cache, must-revalidate, proxy-revalidate', - 'CDN-Cache-Control': 'no-store' - } + headers: clientResponseHeader }); case `/fragsub/${userID}`: @@ -106,11 +105,7 @@ export default { return new Response(JSON.stringify(fragConfigs, null, 4), { status: 200, - headers: { - 'Content-Type': 'application/json;charset=utf-8', - 'Cache-Control': 'no-store, no-cache, must-revalidate, proxy-revalidate', - 'CDN-Cache-Control': 'no-store' - } + headers: clientResponseHeader }); case `/warpsub/${userID}`: @@ -118,11 +113,7 @@ export default { const clashWarpConfig = await getClashWarpConfig(proxySettings, warpConfigs); return new Response(JSON.stringify(clashWarpConfig, null, 4), { status: 200, - headers: { - 'Content-Type': 'application/json;charset=utf-8', - 'Cache-Control': 'no-store, no-cache, must-revalidate, proxy-revalidate', - 'CDN-Cache-Control': 'no-store' - } + headers: clientResponseHeader }); } @@ -130,22 +121,14 @@ export default { const singboxWarpConfig = await getSingBoxWarpConfig(proxySettings, warpConfigs, client); return new Response(JSON.stringify(singboxWarpConfig, null, 4), { status: 200, - headers: { - 'Content-Type': 'application/json;charset=utf-8', - 'Cache-Control': 'no-store, no-cache, must-revalidate, proxy-revalidate', - 'CDN-Cache-Control': 'no-store' - } + headers: clientResponseHeader }); } const warpConfig = await getXrayWarpConfigs(proxySettings, warpConfigs, client); return new Response(JSON.stringify(warpConfig, null, 4), { status: 200, - headers: { - 'Content-Type': 'application/json;charset=utf-8', - 'Cache-Control': 'no-store, no-cache, must-revalidate, proxy-revalidate', - 'CDN-Cache-Control': 'no-store' - } + headers: clientResponseHeader }); case '/panel': @@ -164,25 +147,15 @@ export default { 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, env, hostName, proxySettings, isPassSet); + const homePage = await renderHomePage(request, env, hostName, proxySettings, isPassSet); return new Response(homePage, { status: 200, - headers: { - 'Content-Type': 'text/html', - 'Access-Control-Allow-Origin': url.origin, - 'Access-Control-Allow-Methods': 'GET, POST', - 'Access-Control-Allow-Headers': 'Content-Type, Authorization', - 'X-Content-Type-Options': 'nosniff', - 'X-Frame-Options': 'DENY', - 'Referrer-Policy': 'strict-origin-when-cross-origin', - 'Cache-Control': 'no-store, no-cache, must-revalidate, proxy-revalidate', - 'CDN-Cache-Control': 'no-store' - } + headers: pageResponseHeader }); case '/login': if (typeof env.bpb !== 'object') { - const errorPage = renderErrorPage('KV Dataset is not properly set!', null, true); + const errorPage = await renderErrorPage(env, 'KV Dataset is not properly set!', null, true); return new Response(errorPage, { status: 200, headers: {'Content-Type': 'text/html'}}); } @@ -213,18 +186,10 @@ export default { } } - const loginPage = renderLoginPage(); + const loginPage = await renderLoginPage(env); return new Response(loginPage, { status: 200, - headers: { - 'Content-Type': 'text/html', - 'Access-Control-Allow-Origin': url.origin, - 'Access-Control-Allow-Methods': 'GET, POST', - 'Access-Control-Allow-Headers': 'Content-Type, Authorization', - 'X-Content-Type-Options': 'nosniff', - 'X-Frame-Options': 'DENY', - 'Referrer-Policy': 'strict-origin-when-cross-origin' - } + headers: pageResponseHeader }); case '/logout': @@ -252,7 +217,6 @@ export default { }); default: - // return new Response('Not found', { status: 404 }); url.hostname = 'www.speedtest.net'; url.protocol = 'https:'; request = new Request(url, request); @@ -264,7 +228,7 @@ export default { : await vlessOverWSHandler(request, env); } } catch (err) { - const errorPage = renderErrorPage('Something went wrong!', err, false); + const errorPage = await renderErrorPage(env, 'Something went wrong!', err, false); return new Response(errorPage, { status: 200, headers: {'Content-Type': 'text/html'}}); } }