forked from bia-pain-bache/BPB-Worker-Panel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.js
181 lines (167 loc) · 9.08 KB
/
handlers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { fetchWarpConfigs } from '../protocols/warp';
import { isDomain, resolveDNS } from '../helpers/helpers';
import { initializeParams, panelVersion } from '../helpers/init';
import { Authenticate } from '../authentication/auth';
import { renderErrorPage } from '../pages/error';
export async function getDataset(request, env) {
await initializeParams(request, env);
let proxySettings, warpConfigs;
if (typeof env.bpb !== 'object') {
return {kvNotFound: true, proxySettings: null, warpConfigs: null}
}
try {
proxySettings = await env.bpb.get("proxySettings", {type: 'json'});
warpConfigs = await env.bpb.get('warpConfigs', {type: 'json'});
} catch (error) {
console.log(error);
throw new Error(`An error occurred while getting KV - ${error}`);
}
if (!proxySettings) {
proxySettings = await updateDataset(request, env);
const { error, configs } = await fetchWarpConfigs(env, proxySettings);
if (error) throw new Error(`An error occurred while getting Warp configs - ${error}`);
warpConfigs = configs;
}
if (panelVersion !== proxySettings.panelVersion) proxySettings = await updateDataset(request, env);
return {kvNotFound: false, proxySettings, warpConfigs}
}
export async function updateDataset (request, env) {
await initializeParams(request, env);
let newSettings = request.method === 'POST' ? await request.formData() : null;
const isReset = newSettings?.get('resetSettings') === 'true';
let currentSettings;
if (!isReset) {
try {
currentSettings = await env.bpb.get("proxySettings", {type: 'json'});
} catch (error) {
console.log(error);
throw new Error(`An error occurred while getting current KV settings - ${error}`);
}
} else {
await env.bpb.delete('warpConfigs');
newSettings = null;
}
const validateField = (field) => {
const fieldValue = newSettings?.get(field);
if (fieldValue === undefined) return null;
if (fieldValue === 'true') return true;
if (fieldValue === 'false') return false;
return fieldValue;
}
const remoteDNS = validateField('remoteDNS') ?? currentSettings?.remoteDNS ?? 'https://8.8.8.8/dns-query';
const enableIPv6 = validateField('enableIPv6') ?? currentSettings?.enableIPv6 ?? true;
const url = new URL(remoteDNS);
const remoteDNSServer = url.hostname;
const isServerDomain = isDomain(remoteDNSServer);
let resolvedRemoteDNS = {};
if (isServerDomain) {
try {
const resolvedDomain = await resolveDNS(remoteDNSServer);
resolvedRemoteDNS = {
server: remoteDNSServer,
staticIPs: enableIPv6 ? [...resolvedDomain.ipv4, ...resolvedDomain.ipv6] : resolvedDomain.ipv4
};
} catch (error) {
console.log(error);
throw new Error(`An error occurred while resolving remote DNS server, please try agian! - ${error}`);
}
}
const proxySettings = {
remoteDNS: remoteDNS,
resolvedRemoteDNS: resolvedRemoteDNS,
localDNS: validateField('localDNS') ?? currentSettings?.localDNS ?? '8.8.8.8',
vlessTrojanFakeDNS: validateField('vlessTrojanFakeDNS') ?? currentSettings?.vlessTrojanFakeDNS ?? false,
proxyIP: validateField('proxyIP')?.replaceAll(' ', '') ?? currentSettings?.proxyIP ?? '',
outProxy: validateField('outProxy') ?? currentSettings?.outProxy ?? '',
outProxyParams: extractChainProxyParams(validateField('outProxy')) ?? currentSettings?.outProxyParams ?? {},
cleanIPs: validateField('cleanIPs')?.replaceAll(' ', '') ?? currentSettings?.cleanIPs ?? '',
enableIPv6: enableIPv6,
customCdnAddrs: validateField('customCdnAddrs')?.replaceAll(' ', '') ?? currentSettings?.customCdnAddrs ?? '',
customCdnHost: validateField('customCdnHost')?.trim() ?? currentSettings?.customCdnHost ?? '',
customCdnSni: validateField('customCdnSni')?.trim() ?? currentSettings?.customCdnSni ?? '',
bestVLESSTrojanInterval: validateField('bestVLESSTrojanInterval') ?? currentSettings?.bestVLESSTrojanInterval ?? '30',
vlessConfigs: validateField('vlessConfigs') ?? currentSettings?.vlessConfigs ?? true,
trojanConfigs: validateField('trojanConfigs') ?? currentSettings?.trojanConfigs ?? false,
ports: validateField('ports')?.split(',') ?? currentSettings?.ports ?? ['443'],
lengthMin: validateField('fragmentLengthMin') ?? currentSettings?.lengthMin ?? '100',
lengthMax: validateField('fragmentLengthMax') ?? currentSettings?.lengthMax ?? '200',
intervalMin: validateField('fragmentIntervalMin') ?? currentSettings?.intervalMin ?? '1',
intervalMax: validateField('fragmentIntervalMax') ?? currentSettings?.intervalMax ?? '1',
fragmentPackets: validateField('fragmentPackets') ?? currentSettings?.fragmentPackets ?? 'tlshello',
bypassLAN: validateField('bypass-lan') ?? currentSettings?.bypassLAN ?? false,
bypassIran: validateField('bypass-iran') ?? currentSettings?.bypassIran ?? false,
bypassChina: validateField('bypass-china') ?? currentSettings?.bypassChina ?? false,
bypassRussia: validateField('bypass-russia') ?? currentSettings?.bypassRussia ?? false,
blockAds: validateField('block-ads') ?? currentSettings?.blockAds ?? false,
blockPorn: validateField('block-porn') ?? currentSettings?.blockPorn ?? false,
blockUDP443: validateField('block-udp-443') ?? currentSettings?.blockUDP443 ?? false,
customBypassRules: validateField('customBypassRules')?.replaceAll(' ', '') ?? currentSettings?.customBypassRules ?? '',
customBlockRules: validateField('customBlockRules')?.replaceAll(' ', '') ?? currentSettings?.customBlockRules ?? '',
warpEndpoints: validateField('warpEndpoints')?.replaceAll(' ', '') ?? currentSettings?.warpEndpoints ?? 'engage.cloudflareclient.com:2408',
warpFakeDNS: validateField('warpFakeDNS') ?? currentSettings?.warpFakeDNS ?? false,
warpEnableIPv6: validateField('warpEnableIPv6') ?? currentSettings?.warpEnableIPv6 ?? true,
warpPlusLicense: validateField('warpPlusLicense') ?? currentSettings?.warpPlusLicense ?? '',
bestWarpInterval: validateField('bestWarpInterval') ?? currentSettings?.bestWarpInterval ?? '30',
hiddifyNoiseMode: validateField('hiddifyNoiseMode') ?? currentSettings?.hiddifyNoiseMode ?? 'm4',
nikaNGNoiseMode: validateField('nikaNGNoiseMode') ?? currentSettings?.nikaNGNoiseMode ?? 'quic',
noiseCountMin: validateField('noiseCountMin') ?? currentSettings?.noiseCountMin ?? '10',
noiseCountMax: validateField('noiseCountMax') ?? currentSettings?.noiseCountMax ?? '15',
noiseSizeMin: validateField('noiseSizeMin') ?? currentSettings?.noiseSizeMin ?? '5',
noiseSizeMax: validateField('noiseSizeMax') ?? currentSettings?.noiseSizeMax ?? '10',
noiseDelayMin: validateField('noiseDelayMin') ?? currentSettings?.noiseDelayMin ?? '1',
noiseDelayMax: validateField('noiseDelayMax') ?? currentSettings?.noiseDelayMax ?? '1',
panelVersion: panelVersion
};
try {
await env.bpb.put("proxySettings", JSON.stringify(proxySettings));
} catch (error) {
console.log(error);
throw new Error(`An error occurred while updating KV - ${error}`);
}
return proxySettings;
}
function extractChainProxyParams(chainProxy) {
let configParams = {};
if (!chainProxy) return {};
const url = new URL(chainProxy);
const protocol = url.protocol.slice(0, -1);
if (protocol === 'vless') {
const params = new URLSearchParams(url.search);
configParams = {
protocol: protocol,
uuid : url.username,
server : url.hostname,
port : url.port
};
params.forEach( (value, key) => {
configParams[key] = value;
});
} else {
configParams = {
protocol: protocol,
user : url.username,
pass : url.password,
server : url.host,
port : url.port
};
}
return JSON.stringify(configParams);
}
export async function updateWarpConfigs(request, env) {
const auth = await Authenticate(request, env);
if (!auth) return new Response('Unauthorized', { status: 401 });
if (request.method === 'POST') {
try {
const { kvNotFound, proxySettings } = await getDataset(request, env);
if (kvNotFound) return await renderErrorPage(request, env, 'KV Dataset is not properly set!', null, true);
const { error: warpPlusError } = await fetchWarpConfigs(env, proxySettings);
if (warpPlusError) return new Response(warpPlusError, { status: 400 });
return new Response('Warp configs updated successfully', { status: 200 });
} catch (error) {
console.log(error);
return new Response(`An error occurred while updating Warp configs! - ${error}`, { status: 500 });
}
} else {
return new Response('Unsupported request', { status: 405 });
}
}