-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathsingbox_full_config.html
More file actions
477 lines (437 loc) · 17.8 KB
/
singbox_full_config.html
File metadata and controls
477 lines (437 loc) · 17.8 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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
<html>
<head>
<meta charset="UTF-8" />
<title>Sing-box 完整配置生成器</title>
<style>
body {
font-family: sans-serif;
margin: 20px;
}
.header-link {
float: right;
margin-bottom: 20px;
}
.header-link a {
display: flex;
align-items: center;
text-decoration: none;
color: #000;
}
.header-link img {
margin-right: 5px;
}
textarea {
width: 100%;
height: 200px;
margin-bottom: 10px;
padding: 10px;
box-sizing: border-box;
border: 1px solid #ccc;
border-radius: 4px;
clear: both;
}
#output {
height: 400px;
}
button {
margin: 10px 0;
padding: 10px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
p {
margin-top: 20px;
margin-bottom: 5px;
font-weight: bold;
}
.version-select {
margin-bottom: 15px;
}
.version-select label {
font-weight: bold;
margin-right: 10px;
}
.version-select select {
padding: 8px 12px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 14px;
}
.status {
margin: 10px 0;
padding: 10px;
border-radius: 4px;
display: none;
}
.status.error {
background-color: #f8d7da;
color: #721c24;
display: block;
}
.status.success {
background-color: #d4edda;
color: #155724;
display: block;
}
.template-section {
margin: 15px 0;
border: 1px solid #ddd;
border-radius: 4px;
}
.template-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px 15px;
background-color: #f8f9fa;
cursor: pointer;
border-radius: 4px 4px 0 0;
}
.template-header:hover {
background-color: #e9ecef;
}
.template-header-left {
display: flex;
align-items: center;
gap: 10px;
}
.template-header h3 {
margin: 0;
font-size: 14px;
}
.template-toggle {
font-size: 12px;
color: #666;
}
.template-content {
display: none;
padding: 15px;
border-top: 1px solid #ddd;
}
.template-content.expanded {
display: block;
}
#templateInput {
height: 300px;
font-family: monospace;
font-size: 12px;
}
.template-actions {
margin-bottom: 10px;
display: flex;
gap: 10px;
}
.template-actions button {
margin: 0;
padding: 6px 12px;
font-size: 12px;
}
.template-actions .btn-secondary {
background-color: #6c757d;
}
.template-actions .btn-secondary:hover {
background-color: #545b62;
}
.template-actions .btn-warning {
background-color: #ffc107;
color: #212529;
}
.template-actions .btn-warning:hover {
background-color: #e0a800;
}
</style>
<script src="test.js"></script>
<script src="https://cdn.jsdelivr.net/npm/js-yaml@4.1.0/dist/js-yaml.min.js"></script>
</head>
<body>
<div style="margin-bottom: 20px; overflow: hidden;">
<div style="float: left;">
<a href="index.html" style="text-decoration: none; color: #007bff;">
<< 返回首页
</a>
<a href="url_to_qrcode.html" style="text-decoration: none; color: #007bff; margin-left: 20px;">
二维码生成器
</a>
<a href="singbox_ss_uri.html" style="text-decoration: none; color: #007bff; margin-left: 20px;">
Sing-box JSON转URI
</a>
</div>
<div class="header-link">
<a href="https://github.com/wynemo/v2ray-to-sing-box" target="_blank">
<img src="https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png" alt="GitHub" width="30" height="30">
v2ray-to-sing-box
</a>
</div>
</div>
<h2>Sing-box 完整配置生成器</h2>
<p style="font-weight: normal; color: #666;">
输入 Clash YAML 或 Base64 编码的 v2ray 订阅,生成包含 DNS、路由规则、出站分组的完整 Sing-box 配置。
节点会自动按地区(港/台/日/新/美)分组。
</p>
<div class="version-select">
<label for="version">选择版本:</label>
<select id="version" onchange="onVersionChange()">
<option value="1.11">Sing-box 1.11</option>
<option value="1.12">Sing-box 1.12</option>
</select>
</div>
<div class="template-section">
<div class="template-header" onclick="toggleTemplate()">
<div class="template-header-left">
<h3>配置模板</h3>
<span class="template-toggle" id="templateToggle">点击展开编辑</span>
</div>
</div>
<div class="template-content" id="templateContent">
<div class="template-actions">
<button class="btn-secondary" onclick="loadDefaultTemplate()">重置为默认模板</button>
<button class="btn-warning" onclick="validateTemplate()">验证 JSON 格式</button>
</div>
<textarea id="templateInput" placeholder="加载中..."></textarea>
<p style="font-weight: normal; color: #666; font-size: 12px; margin-top: 5px;">
提示:模板中的 outbounds 数组会自动添加您输入的节点,并按地区分组(港/台/日/新/美)。
</p>
</div>
</div>
<textarea
id="input"
placeholder="支持以下格式:
1. Clash YAML 配置
2. Base64 编码的 v2ray 订阅
3. 多行 URI(每行一个节点,支持 ss/ssr/vmess/vless/trojan/hysteria/hysteria2/tuic/wireguard)"
></textarea>
<button onclick="convert()">生成完整配置</button>
<button onclick="copyOutput()" style="background-color: #28a745;">复制结果</button>
<div id="status" class="status"></div>
<p>完整 Sing-box 配置</p>
<textarea id="output" readonly></textarea>
<script>
// 缓存已加载的版本配置
const versionCache = {};
// 模板展开/折叠
function toggleTemplate() {
const content = document.getElementById("templateContent");
const toggle = document.getElementById("templateToggle");
if (content.classList.contains("expanded")) {
content.classList.remove("expanded");
toggle.textContent = "点击展开编辑";
} else {
content.classList.add("expanded");
toggle.textContent = "点击收起";
}
}
// 版本变化时加载对应模板
async function onVersionChange() {
await loadDefaultTemplate();
}
// 加载默认模板
async function loadDefaultTemplate() {
const version = document.getElementById("version").value;
const templateInput = document.getElementById("templateInput");
try {
templateInput.value = "加载中...";
const response = await fetch(`${version}/sing-box.json`);
if (!response.ok) {
throw new Error(`无法加载版本 ${version} 的配置模板`);
}
const template = await response.json();
templateInput.value = JSON.stringify(template, null, 2);
showStatus(`已加载 Sing-box ${version} 默认模板`);
} catch (error) {
templateInput.value = "";
showStatus(`加载模板失败: ${error.message}`, true);
}
}
// 验证模板 JSON 格式
function validateTemplate() {
const templateInput = document.getElementById("templateInput");
try {
JSON.parse(templateInput.value);
showStatus("JSON 格式正确");
} catch (error) {
showStatus(`JSON 格式错误: ${error.message}`, true);
}
}
// 页面加载时自动加载默认模板
window.addEventListener("DOMContentLoaded", function() {
loadDefaultTemplate();
});
function isValidSubYAML(str) {
if (typeof str !== "string") return false;
try {
const { proxies } = jsyaml.load(str);
return !!proxies;
} catch {
return false;
}
}
function isValidBase64(str) {
try {
// 添加 padding(如果需要)
const padded = str + '='.repeat((4 - str.length % 4) % 4);
const decoded = atob(padded);
// 检查解码后的内容是否包含 URI 协议标识
return /^(ss|ssr|vmess|vless|trojan|hysteria|hysteria2|tuic|wireguard):\/\//i.test(decoded);
} catch (err) {
return false;
}
}
function base64Decode(str) {
try {
// 自动添加缺失的 padding
const padded = str + '='.repeat((4 - str.length % 4) % 4);
const decoded = atob(padded);
return decodeURIComponent(escape(decoded));
} catch (err) {
try {
const padded = str + '='.repeat((4 - str.length % 4) % 4);
return atob(padded);
} catch (err2) {
return str;
}
}
}
function showStatus(message, isError = false) {
const status = document.getElementById("status");
status.textContent = message;
status.className = "status " + (isError ? "error" : "success");
}
function hideStatus() {
document.getElementById("status").className = "status";
}
async function loadVersionConfig(version) {
if (versionCache[version]) {
return versionCache[version];
}
try {
// 加载配置模板
const templateResponse = await fetch(`${version}/sing-box.json`);
if (!templateResponse.ok) {
throw new Error(`无法加载版本 ${version} 的配置模板`);
}
const template = await templateResponse.json();
// 加载处理脚本
const scriptResponse = await fetch(`${version}/sing-box.js`);
if (!scriptResponse.ok) {
throw new Error(`无法加载版本 ${version} 的处理脚本`);
}
const scriptText = await scriptResponse.text();
// 创建独立的函数作用域来执行脚本
const processConfigFunc = new Function(
scriptText + '; return { processConfig, getTags };'
)();
versionCache[version] = {
template,
processConfig: processConfigFunc.processConfig
};
return versionCache[version];
} catch (error) {
throw new Error(`加载版本 ${version} 失败: ${error.message}`);
}
}
async function convert() {
hideStatus();
try {
const input = document.getElementById("input").value.trim();
if (!input) {
showStatus("请输入订阅内容", true);
return;
}
const version = document.getElementById("version").value;
// 获取用户编辑的模板
const templateInput = document.getElementById("templateInput").value.trim();
if (!templateInput) {
showStatus("请先加载配置模板", true);
return;
}
let userTemplate;
try {
userTemplate = JSON.parse(templateInput);
} catch (error) {
showStatus(`模板 JSON 格式错误: ${error.message}`, true);
return;
}
// 加载版本配置(仅用于获取 processConfig 函数)
const versionConfig = await loadVersionConfig(version);
// 解析输入
let lines = [];
let proxies = [];
if (isValidSubYAML(input)) {
proxies = jsyaml.load(input).proxies;
} else if (isValidBase64(input)) {
const decodedText = base64Decode(input);
lines = decodedText.split("\n").filter((v) => v);
} else {
lines = input.split("\n").filter((v) => v);
}
// 解析 URI 为代理对象
for (let line of lines) {
line = line.trim();
if (!line) continue;
const schema = line.split("://")[0];
const protocol = protocolForClash[schema.toLowerCase()];
if (!protocol) {
console.log(`未支持的协议:${schema}`);
continue;
}
try {
const proxy = protocol.parse(line);
proxies.push(proxy);
} catch (error) {
console.log("解析错误:", error);
}
}
if (proxies.length === 0) {
showStatus("未能解析出任何有效节点", true);
return;
}
// 转换为 sing-box 格式
const singbox_proxies = [];
const protocolForSingBoxMap = protocolForSingBox();
for (let proxy of proxies) {
try {
const _proxy = protocolForSingBoxMap[proxy.type](proxy);
singbox_proxies.push(_proxy);
} catch (error) {
console.log("转换 SingBox 节点失败", error);
}
}
if (singbox_proxies.length === 0) {
showStatus("节点转换失败,请检查输入格式", true);
return;
}
// 使用版本脚本处理配置(使用用户编辑的模板)
const fullConfig = versionConfig.processConfig(
singbox_proxies,
userTemplate
);
document.getElementById("output").value = JSON.stringify(
fullConfig,
null,
2
);
showStatus(`成功生成配置,共 ${singbox_proxies.length} 个节点`);
} catch (error) {
console.error("转换失败:", error);
showStatus("转换失败:" + error.message, true);
}
}
function copyOutput() {
const output = document.getElementById("output");
if (!output.value) {
showStatus("没有可复制的内容", true);
return;
}
output.select();
document.execCommand("copy");
showStatus("已复制到剪贴板");
}
</script>
</body>
</html>