Skip to content

Commit

Permalink
Bug fixes, refactored.
Browse files Browse the repository at this point in the history
  • Loading branch information
bia-pain-bache committed Nov 1, 2024
1 parent e852e2c commit 84d4f1b
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 136 deletions.
6 changes: 3 additions & 3 deletions src/authentication/auth.js
Original file line number Diff line number Diff line change
@@ -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' })
Expand Down
8 changes: 2 additions & 6 deletions src/cores-configs/clash.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 2 additions & 6 deletions src/cores-configs/normalConfigs.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
8 changes: 2 additions & 6 deletions src/cores-configs/sing-box.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 3 additions & 6 deletions src/cores-configs/xray.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 = [];
Expand Down
10 changes: 0 additions & 10 deletions src/helpers/config.js

This file was deleted.

4 changes: 1 addition & 3 deletions src/helpers/helpers.js
Original file line number Diff line number Diff line change
@@ -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`;

Expand Down
21 changes: 21 additions & 0 deletions src/helpers/init.js
Original file line number Diff line number Diff line change
@@ -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 };
5 changes: 3 additions & 2 deletions src/kv/handlers.js
Original file line number Diff line number Diff line change
@@ -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}
Expand All @@ -29,6 +29,7 @@ export async function getDataset(env) {
}

export async function updateDataset (env, newSettings, resetSettings) {
await initializeParams(env);
let currentSettings;
if (!resetSettings) {
try {
Expand Down
6 changes: 3 additions & 3 deletions src/pages/errorPage.js
Original file line number Diff line number Diff line change
@@ -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 `
<!DOCTYPE html>
<html lang="en">
Expand Down
9 changes: 4 additions & 5 deletions src/pages/homePage.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -72,6 +70,7 @@ export function renderHomePage (request, env, hostName, proxySettings, isPassSet
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="timestamp" content=${Date.now()}>
<title>BPB Panel ${panelVersion}</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,[email protected],100..700,0..1,-50..200" />
Expand Down
6 changes: 3 additions & 3 deletions src/pages/loginPage.js
Original file line number Diff line number Diff line change
@@ -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 `
<!DOCTYPE html>
<html lang="en">
Expand Down
8 changes: 2 additions & 6 deletions src/protocols/trojan.js
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
9 changes: 3 additions & 6 deletions src/protocols/vless.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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);

Expand Down
Loading

0 comments on commit 84d4f1b

Please sign in to comment.