-
Notifications
You must be signed in to change notification settings - Fork 19
/
nontls_worker.js
403 lines (344 loc) · 12 KB
/
nontls_worker.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Developed by Surfboardv2ray
// https://github.com/Surfboardv2ray/v2ray-refiner
// Version 1.2.1
// Change 'url.port' and 'const workerport' value if your config uses another port. Only one port will work at a time.
export default {
async fetch(request) {
let url = new URL(request.url);
if (url.pathname === '/' && request.method === "GET") {
// Serve the HTML page at the root URL for GET request
return handleRequest();
} else if (url.pathname === '/' && request.method === "POST") {
// Handle POST request to process the config refinement
return handleConfigRefinement(request);
} else {
// Proceed with the existing fetch logic for other paths
let realhostname = url.pathname.split('/')[1];
let realpathname = url.pathname.split('/')[2];
url.hostname = realhostname;
url.pathname = '/' + realpathname;
url.port = 80;
url.protocol = 'http';
let newRequest = new Request(url, request);
return fetch(newRequest);
}
}
};
async function handleRequest() {
const html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Non-TLS Config Refiner</title>
<style>
/* General Reset */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(to right, #fff, #f4f4f4);
color: #333;
padding: 50px 20px;
}
.container {
max-width: 900px;
margin: 0 auto;
padding: 40px;
background-color: #fff;
border-radius: 15px;
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
}
h1 {
text-align: center;
font-size: 36px;
color: #FF8C00;
margin-bottom: 20px;
}
h2 {
font-size: 24px;
color: #444;
margin-top: 40px;
}
label {
font-weight: bold;
font-size: 14px;
display: block;
margin-bottom: 8px;
color: #444;
}
input[type="text"], textarea {
width: 100%;
padding: 12px 16px;
margin-bottom: 20px;
border-radius: 10px;
border: 2px solid #ddd;
font-size: 16px;
transition: border 0.3s;
box-shadow: inset 0 1px 5px rgba(0,0,0,0.1);
}
input[type="text"]:focus, textarea:focus {
border-color: #FF8C00;
outline: none;
box-shadow: inset 0 1px 5px rgba(255, 140, 0, 0.3);
}
button {
width: 100%;
padding: 15px;
background-color: #FF8C00;
color: #fff;
font-size: 18px;
border: none;
border-radius: 10px;
cursor: pointer;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
transition: transform 0.2s ease, box-shadow 0.2s ease;
}
button:hover {
background-color: #e67e22;
transform: translateY(-2px);
box-shadow: 0 6px 10px rgba(0, 0, 0, 0.15);
}
button:active {
transform: translateY(2px);
box-shadow: none;
}
.error {
color: red;
font-size: 14px;
margin-bottom: 20px;
text-align: center;
}
.refined-box {
background-color: #f9f9f9;
padding: 15px;
border-radius: 10px;
border: 1px solid #ddd;
word-wrap: break-word;
box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.1);
}
.copy-icon {
display: inline-block;
margin-top: 10px;
font-size: 20px;
cursor: pointer;
color: #FF8C00;
transition: color 0.2s ease;
}
.copy-icon:hover {
color: #e67e22;
}
@media (max-width: 600px) {
.container {
padding: 20px;
}
h1 {
font-size: 28px;
}
button {
font-size: 16px;
}
input[type="text"], textarea {
font-size: 14px;
}
}
</style>
</head>
<body>
<div class="container">
<h1>Non-TLS Config Refiner</h1>
<form id="config-form">
<label for="config">Config (vless, vmess, or trojan):</label>
<textarea id="config" rows="6"></textarea>
<label for="hostname">Hostname pointing to Server IP:</label>
<input type="text" id="hostname" placeholder="Enter your server hostname">
<label for="clean-ip">Clean IP:</label>
<input type="text" id="clean-ip" value="162.159.141.134" placeholder="Enter Cloudflare clean IP">
<button type="submit">Refine Config</button>
<div class="error" id="error"></div>
</form>
<h2>Refined Config</h2>
<div id="refined-config" class="refined-box"></div>
<span id="copy-btn" class="copy-icon">📋 Copy</span>
</div>
<script>
document.getElementById("config-form").addEventListener("submit", async function(event) {
event.preventDefault();
const config = document.getElementById("config").value;
const hostname = document.getElementById("hostname").value;
const cleanIp = document.getElementById("clean-ip").value;
const errorDiv = document.getElementById("error");
const refinedConfigDiv = document.getElementById("refined-config");
errorDiv.textContent = "";
refinedConfigDiv.textContent = "";
// Input validation
if (!config && !hostname) {
errorDiv.textContent = "Please enter your Config and Hostname";
return;
} else if (!config) {
errorDiv.textContent = "Please enter your vmess, vless or trojan config";
return;
} else if (!hostname) {
errorDiv.textContent = "Please enter a hostname pointing to your Server IP Address.";
return;
} else if (!cleanIp) {
errorDiv.textContent = "Please input your Cloudflare Clean IP address.";
return;
}
// Send POST request to the worker for refinement
const response = await fetch("/", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
config,
hostname,
cleanIp
})
});
const result = await response.json();
if (result.error) {
errorDiv.textContent = result.error;
} else {
refinedConfigDiv.textContent = result.refinedConfig;
}
});
// Copy to clipboard functionality
document.getElementById("copy-btn").addEventListener("click", () => {
const refinedConfig = document.getElementById("refined-config").textContent;
if (refinedConfig) {
navigator.clipboard.writeText(refinedConfig).then(() => {
alert("Config copied to clipboard!");
}).catch(err => {
alert("Failed to copy: " + err);
});
}
});
</script>
</body>
</html>
`;
return new Response(html, {
headers: { 'Content-Type': 'text/html' },
});
}
// Function to handle POST request and refine config
async function handleConfigRefinement(request) {
const { config, hostname, cleanIp } = await request.json();
const url = new URL(request.url);
const workerUrl = url.hostname;
const workerPort = '80'; // Default port value
try {
// Check if the config starts with vmess://, vless://, or trojan://
if (config.startsWith('vmess://')) {
return handleVmessConfig(config, hostname, cleanIp, workerUrl, workerPort);
} else if (config.startsWith('vless://')) {
return handleVlessConfig(config, hostname, cleanIp, workerUrl, workerPort);
} else if (config.startsWith('trojan://')) {
return handleTrojanConfig(config, hostname, cleanIp, workerUrl, workerPort);
} else {
return new Response(JSON.stringify({ error: "Please enter a valid vmess, vless or trojan config" }), {
headers: { 'Content-Type': 'application/json' },
});
}
} catch (error) {
return new Response(JSON.stringify({ error: error.message }), {
headers: { 'Content-Type': 'application/json' },
});
}
}
function handleVmessConfig(config, hostname, cleanIp, workerUrl, workerPort) {
console.log("Original config:", config);
const base64Part = config.split('vmess://')[1];
console.log("Base64 part:", base64Part);
if (!base64Part) {
throw new Error("Invalid vmess config: No base64 part found.");
}
let decodedConfig;
try {
decodedConfig = atob(base64Part);
console.log("Decoded config:", decodedConfig);
} catch (e) {
throw new Error("Invalid vmess config: Base64 decoding failed.");
}
let jsonConfig;
try {
jsonConfig = JSON.parse(decodedConfig);
console.log("Parsed JSON config:", jsonConfig);
} catch (e) {
throw new Error("Invalid vmess config: JSON parsing failed.");
}
if (!jsonConfig.port || !jsonConfig.ps || !jsonConfig.id) {
throw new Error("Invalid vmess config: Missing required fields (port, ps, id).");
}
const port = jsonConfig.port.toString();
console.log("Config port:", port);
if (port !== workerPort) {
throw new Error(`The config port must be ${workerPort}`);
}
const refinedConfig = {
v: "2",
ps: jsonConfig.ps,
add: cleanIp, // Use Clean IP provided by the user
port: "443",
id: jsonConfig.id,
aid: "0",
scy: "auto",
net: "ws",
type: "none",
host: workerUrl,
path: `/${hostname}${jsonConfig.path || ''}`,
tls: "tls",
sni: workerUrl,
alpn: "h2,http/1.1",
fp: "chrome"
};
console.log("Refined config object:", refinedConfig);
let refinedConfigBase64;
try {
refinedConfigBase64 = btoa(JSON.stringify(refinedConfig));
console.log("Refined config base64:", refinedConfigBase64);
} catch (e) {
throw new Error("Failed to encode the refined configuration to base64.");
}
return new Response(JSON.stringify({ refinedConfig: `vmess://${refinedConfigBase64}` }), {
headers: { 'Content-Type': 'application/json' },
});
}
function handleVlessConfig(config, hostname, cleanIp, workerUrl, workerPort) {
const parts = config.split('vless://')[1].split('@');
const uuid = parts[0];
const hostAndPort = parts[1].split('#')[0]; // Get everything before the #
const [host, portPath] = hostAndPort.split(':'); // Separate host and port
const port = portPath.split('?')[0]; // Get port
const path = portPath.split('path=')[1]?.split('&')[0] || ''; // Extract path if it exists
const alias = parts[1].split('#')[1]; // Extract the alias after #
if (port !== workerPort) {
throw new Error(`The config port must be ${workerPort}`);
}
const refinedConfig = `vless://${uuid}@${cleanIp}:443?encryption=none&security=tls&sni=${workerUrl}&alpn=h2%2Chttp%2F1.1&fp=chrome&allowInsecure=1&type=ws&host=${workerUrl}&path=%2F${hostname}${path}#${alias}`;
return new Response(JSON.stringify({ refinedConfig }), {
headers: { 'Content-Type': 'application/json' },
});
}
function handleTrojanConfig(config, hostname, cleanIp, workerUrl, workerPort) {
const parts = config.split('trojan://')[1].split('@');
const uuid = parts[0];
const hostAndPort = parts[1].split('#')[0]; // Get everything before the #
const [host, portPath] = hostAndPort.split(':'); // Separate host and port
const port = portPath.split('?')[0]; // Get port
const path = portPath.split('path=')[1]?.split('&')[0] || ''; // Extract path if it exists
const alias = parts[1].split('#')[1]; // Extract the alias after #
if (port !== workerPort) {
throw new Error(`The config port must be ${workerPort}`);
}
const refinedConfig = `trojan://${uuid}@${cleanIp}:443??encryption=none&security=tls&sni=${workerUrl}&alpn=h2%2Chttp%2F1.1&fp=chrome&allowInsecure=1&type=ws&host=${workerUrl}&path=%2F${hostname}${path}#${alias}`;
return new Response(JSON.stringify({ refinedConfig }), {
headers: { 'Content-Type': 'application/json' },
});
}