-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathv2ray_proxy_worker.js1
More file actions
358 lines (296 loc) · 10.6 KB
/
Copy pathv2ray_proxy_worker.js1
File metadata and controls
358 lines (296 loc) · 10.6 KB
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
// V2Ray Proxy with Cloudflare Workers
// This script creates a proxy that connects to a V2Ray server with WebSocket + TLS
export default {
async fetch(request) {
return handleRequest(request);
}
};
async function handleRequest(request) {
const headers = new Headers();
headers.set("Access-Control-Allow-Origin", "*");
headers.set("Access-Control-Allow-Methods", "GET, POST");
headers.set("Access-Control-Allow-Headers", "Content-Type");
// Handle WebSocket connections
if (request.headers.get("Upgrade") === "websocket") {
return handleWebSocket(request);
}
// Handle OPTIONS request (preflight)
if (request.method === "OPTIONS") {
return new Response(null, {
headers,
status: 204
});
}
// Handle GET request - serve the configuration UI
if (request.method === "GET") {
return new Response(renderHTML(), {
headers: { "content-type": "text/html;charset=UTF-8" }
});
}
// Handle POST request - process the config generation
else if (request.method === "POST") {
const formData = await request.formData();
const config = formData.get("config");
const cleanIp = formData.get("cleanIp");
const workerHost = new URL(request.url).hostname;
try {
const refinedConfig = await refineConfig(config, cleanIp, workerHost);
return new Response(JSON.stringify({ refinedConfig }), {
headers: { "content-type": "application/json", ...headers }
});
} catch (error) {
return new Response(JSON.stringify({ error: error.message }), {
headers: { "content-type": "application/json", ...headers },
status: 400
});
}
}
return new Response("Method not allowed", { status: 405 });
}
// Handle WebSocket connections by forwarding them to the V2Ray server
async function handleWebSocket(request) {
const url = new URL(request.url);
// Extract the target hostname from the path
const targetHost = url.pathname.replace(/^\/|\/$/, "");
if (!targetHost) {
return new Response("Invalid WebSocket request", { status: 400 });
}
// Create a new URL to the target V2Ray server
const newUrl = new URL(`wss://${targetHost}`);
// Forward the WebSocket connection
return fetch(new Request(newUrl, request));
}
// UI for configuring the proxy
function renderHTML() {
return `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>V2Ray WebSocket+TLS Proxy</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
margin: 0;
padding: 0;
background: #f5f5f5;
color: #333;
}
.container {
max-width: 800px;
margin: 40px auto;
padding: 20px;
background: white;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
h1 {
text-align: center;
color: #2196F3;
font-weight: bold;
}
label {
font-size: 16px;
margin-top: 10px;
display: block;
font-weight: 600;
color: #555;
}
input, textarea {
width: 100%;
padding: 8px;
margin: 8px 0;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 16px;
background: #fafafa;
transition: all 0.3s;
}
input:focus, textarea:focus {
border-color: #2196F3;
outline: none;
box-shadow: 0 0 5px rgba(33, 150, 243, 0.5);
}
textarea {
resize: vertical;
min-height: 120px;
}
button {
padding: 12px 20px;
margin: 10px 0;
font-size: 16px;
color: white;
background: linear-gradient(145deg, #2196F3, #1976D2);
border: none;
border-radius: 4px;
cursor: pointer;
text-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
transition: transform 0.2s, box-shadow 0.2s;
}
button:hover {
transform: translateY(-2px);
box-shadow: 0 6px 8px rgba(0, 0, 0, 0.2);
}
button:active {
transform: translateY(0);
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.output-box {
position: relative;
}
.copy-btn {
position: absolute;
top: 10px;
right: 10px;
padding: 8px 12px;
font-size: 14px;
background: linear-gradient(145deg, #2196F3, #1976D2);
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
transition: transform 0.2s, box-shadow 0.2s;
}
.copy-btn:hover {
transform: translateY(-2px);
box-shadow: 0 6px 8px rgba(0, 0, 0, 0.2);
}
.copy-btn:active {
transform: translateY(0);
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
@media (max-width: 768px) {
.container {
margin: 20px;
padding: 15px;
}
button, .copy-btn {
width: 100%;
}
}
</style>
</head>
<body>
<div class="container">
<h1>V2Ray WebSocket+TLS Proxy</h1>
<label for="config">Enter your V2Ray config (VMess, VLESS, or Trojan):</label>
<textarea id="config" placeholder="vmess://... or vless://... or trojan://..."></textarea>
<label for="clean-ip">Cloudflare Clean IP Address:</label>
<input type="text" id="clean-ip" value="104.18.6.41" placeholder="Enter a Cloudflare clean IP">
<button id="refine-btn">Generate Proxy Config</button>
<h3>Proxy Configuration:</h3>
<div class="output-box">
<textarea id="refined-config" readonly></textarea>
<button class="copy-btn" id="copy-btn">Copy</button>
</div>
</div>
<script>
document.getElementById('refine-btn').addEventListener('click', async () => {
const config = document.getElementById('config').value.trim();
const cleanIp = document.getElementById('clean-ip').value.trim();
try {
const formData = new FormData();
formData.append('config', config);
formData.append('cleanIp', cleanIp);
const response = await fetch(window.location.href, {
method: 'POST',
body: formData
});
const result = await response.json();
if (response.ok) {
document.getElementById('refined-config').value = result.refinedConfig;
} else {
document.getElementById('refined-config').value = 'Error: ' + result.error;
}
} catch (error) {
document.getElementById('refined-config').value = 'Error: ' + error.message;
}
});
document.getElementById('copy-btn').addEventListener('click', () => {
const refinedConfig = document.getElementById('refined-config');
refinedConfig.select();
document.execCommand('copy');
alert('Config copied to clipboard!');
});
</script>
</body>
</html>
`;
}
// Process and refine the V2Ray configuration
async function refineConfig(config, cleanIp, workerHost) {
const allowedPorts = ['443', '8443', '2053', '2083', '2087', '2096'];
if (config.startsWith('vmess://')) {
return refineVmess(config, cleanIp, workerHost, allowedPorts);
} else if (config.startsWith('vless://')) {
return refineVless(config, cleanIp, workerHost, allowedPorts);
} else if (config.startsWith('trojan://')) {
return refineTrojan(config, cleanIp, workerHost, allowedPorts);
} else {
throw new Error("Invalid config format. Please enter a valid VMess, VLESS, or Trojan config with WebSocket+TLS.");
}
}
// Process VMess configuration
function refineVmess(config, cleanIp, workerHost, allowedPorts) {
const base64Data = config.slice(8); // Remove 'vmess://'
const decodedString = atob(base64Data); // Decode base64 string
const decoded = JSON.parse(decodedString);
if (decoded.net !== 'ws') throw new Error('Network must be WebSocket (ws)');
if (decoded.tls !== 'tls') throw new Error('Security must be TLS');
// Check if the input port is allowed
if (!allowedPorts.includes(String(decoded.port))) {
throw new Error('Config must use a Cloudflare TLS Port (443, 8443, 2053, 2083, 2087, or 2096)');
}
// Preserve the original "host" and "sni" values
const originalHost = decoded.host || '';
const originalSni = decoded.sni || decoded.host || '';
// Set the port to 443 regardless of input config
decoded.port = 443;
decoded.add = cleanIp; // Set clean IP for "address"
decoded.host = workerHost; // New worker host
decoded.sni = workerHost; // New worker SNI
const originalPath = decoded.path || '';
decoded.path = `/${originalSni}${originalPath}`; // Concatenated path with original SNI
const newConfig = 'vmess://' + btoa(JSON.stringify(decoded));
return newConfig;
}
// Process VLESS configuration
function refineVless(config, cleanIp, workerHost, allowedPorts) {
const url = new URL(config);
if (url.searchParams.get('type') !== 'ws') throw new Error('Network must be WebSocket (ws)');
if (url.searchParams.get('security') !== 'tls') throw new Error('Security must be TLS');
// Check if the input port is allowed
if (!allowedPorts.includes(url.port)) {
throw new Error('Config must use a Cloudflare TLS Port (443, 8443, 2053, 2083, 2087, or 2096)');
}
// Set the port to 443 regardless of input config
url.port = 443;
url.hostname = cleanIp; // Set clean IP for "address"
const originalHost = url.searchParams.get('host') || ''; // Original host
const originalPath = url.searchParams.get('path') || ''; // Original path
url.searchParams.set('host', workerHost);
url.searchParams.set('sni', workerHost);
url.searchParams.set('path', `/${originalHost}${originalPath}`); // Concatenated path
return url.toString();
}
// Process Trojan configuration
function refineTrojan(config, cleanIp, workerHost, allowedPorts) {
const url = new URL(config);
if (url.searchParams.get('type') !== 'ws') throw new Error('Network must be WebSocket (ws)');
if (url.searchParams.get('security') !== 'tls') throw new Error('Security must be TLS');
// Check if the input port is allowed
if (!allowedPorts.includes(url.port)) {
throw new Error('Config must use a Cloudflare TLS Port (443, 8443, 2053, 2083, 2087, or 2096)');
}
// Set the port to 443 regardless of input config
url.port = 443;
url.hostname = cleanIp; // Set clean IP for "address"
const originalHost = url.searchParams.get('host') || ''; // Original host
const originalPath = url.searchParams.get('path') || ''; // Original path
url.searchParams.set('host', workerHost);
url.searchParams.set('sni', workerHost);
url.searchParams.set('path', `/${originalHost}${originalPath}`); // Concatenated path
return url.toString();
}