forked from HoldOnBro/WinRoute2ClashRules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
400 lines (351 loc) · 15.8 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://lib.sinaapp.com/js/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
.copy-btn {
margin-left: auto;
margin-bottom: 10px;
display: none;
}
.download-btn {
margin-left: auto;
margin-bottom: 10px;
}
.output-label {
display: flex;
align-items: center;
}
.button-group {
display: flex;
justify-content: flex-end;
margin-top: 10px;
}
.button-group button {
margin-left: 5px;
}
#sstapRuleContainer:not(.show) {
display: none;
}
.form-group textarea {
resize: vertical;
}
</style>
</head>
<body>
<div class="container mt-5">
<h2 class="text-center">Route to CIDR Converter</h2>
<div class="form-group mt-4">
<div class="row">
<div class="col-md-6">
<div class="d-flex flex-column">
<div class="d-flex align-items-center justify-content-between mb-2">
<label for="gameName">输入游戏名称(英文名,中文名):</label>
</div>
<input type="text" id="gameName" class="form-control" placeholder="somegame,某游戏"
oninput="updateSSTapRule();" />
</div>
</div>
<div class="col-md-6">
<div class="d-flex flex-column">
<div class="d-flex align-items-center justify-content-between mb-2">
<label for="gatewayIP">虚拟网卡网关IP:</label>
<div class="form-check d-flex align-items-center">
<input class="form-check-input" type="checkbox" id="autoExtractCheckbox" checked
onchange="toggleAutoExtract();" />
<label class="form-check-label ml-2" for="autoExtractCheckbox" data-toggle="tooltip"
data-placement="top" title="默认选中,自动使用路由表中规则数量最多的网关 IP 地址">
自动提取 <i class="fas fa-question-circle"></i>
</label>
</div>
</div>
<input type="text" id="gatewayIP" class="form-control" placeholder="172.19.83.1/在链路上"
onblur="validateGatewayIP();" disabled="true" />
</div>
</div>
</div>
</div>
<div class="form-group mt-4">
<label for="input">粘贴路由表至此:</label>
<textarea id="input" class="form-control" rows="10"></textarea>
</div>
<div class="form-group text-center mt-4">
<div class="button-group d-flex justify-content-center">
<button onclick="convertToClash()" class="btn btn-primary">转换为 Clash 规则</button>
<button onclick="convertToSSTap()" class="btn btn-primary">转换为 SSTap 规则</button>
<button onclick="convertToNetch()" class="btn btn-primary">转换为 Netch 规则</button>
</div>
</div>
<div id="sstapRuleContainer" class="form-group mt-4 collapse">
<label for="sstapRule" class="tooltip-label">
SSTap 规则头:
<i class="fas fa-question-circle" data-toggle="tooltip" data-placement="top"
title="格式如下(分行显示为方便查看): #CSGO(规则英文名), 反恐精英:全球攻势(规则中文名), 0(动作类型 0.代理 1.直连), 0(和上一条相同 动作类型 0.代理 1.直连), 1(未知), 0(未知), 1(是否可读写 0.可读写 1.只可读), 0(DNS代理类型 0.自动 1.直连 2.代理), By-HoldOnBro(备注)"></i>
</label>
<textarea id="sstapRule" class="form-control" rows="1"></textarea>
</div>
<div class="form-group mt-4">
<div class="row">
<div class="col-6 output-label text-right">
<label for="output" class="mr-2">转换得到规则如下:</label>
</div>
<div class="col-6">
<div class="button-group d-flex justify-content-end">
<button onclick="copyToClipboard()" id="copyButton" class="btn btn-secondary copy-btn">复制</button>
<button onclick="showDownloadDialog()" id="downloadButton" class="btn btn-success download-btn"
style="display: none;">下载</button>
</div>
</div>
<div class="col-12">
<textarea id="output" class="form-control" rows="10" readonly></textarea>
</div>
</div>
</div>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js"></script>
<script>
const defaultText = `以下为示例:
IPv4 路由表
===========================================================================
活动路由:
网络目标 网络掩码 网关 接口 跃点数
0.0.0.0 0.0.0.0 192.168.123.1 192.168.123.10 25
12.25.94.0 255.255.255.0 172.19.83.1 172.19.83.237 100
14.17.17.0 255.255.255.0 172.19.83.1 172.19.83.237 100
14.17.37.0 255.255.255.0 172.19.83.1 172.19.83.237 100
14.17.44.0 255.255.255.0 172.19.83.1 172.19.83.237 100
14.17.45.0 255.255.255.0 172.19.83.1 172.19.83.237 100
14.17.108.0 255.255.255.0 172.19.83.1 172.19.83.237 100
......`;
let gameName = "";
let selectedGatewayIP = "";
window.addEventListener('DOMContentLoaded', function () {
const inputElement = document.getElementById('input');
inputElement.placeholder = defaultText;
const sstapRuleElement = document.getElementById('sstapRule');
const gameNameElement = document.getElementById('gameName');
const gatewayIPElement = document.getElementById('gatewayIP');
sstapRuleElement.addEventListener('input', function () {
const sstapRuleParts = sstapRuleElement.value.split(",");
if (sstapRuleParts.length >= 2) {
gameName = sstapRuleParts[0].substring(1) + "," + sstapRuleParts[1];
gameNameElement.value = gameName;
}
});
});
function convertToCIDR(ip, netmask) {
let binaryStr = '';
for (let octet of netmask.split('.')) {
binaryStr += Number.parseInt(octet).toString(2).padStart(8, '0');
}
const netmaskCidr = binaryStr.split('1').length - 1;
return ip + '/' + netmaskCidr;
}
function isValidIPv4(ip) {
const octets = ip.split(".");
if (octets.length !== 4) return false;
for (let octet of octets) {
let num = Number.parseInt(octet);
if (Number.isNaN(num) || num < 0 || num > 255) return false;
}
return true;
}
function isReservedIPv4(ip) {
const reservedRanges = [
{ range: '0.0.0.0/8' },
{ range: '10.0.0.0/8' },
{ range: '100.64.0.0/10' },
{ range: '127.0.0.0/8' },
{ range: '169.254.0.0/16' },
{ range: '192.0.0.0/24' },
{ range: '192.0.2.0/24' },
{ range: '192.88.99.0/24' },
{ range: '192.168.0.0/16' },
{ range: '198.18.0.0/15' },
{ range: '198.51.100.0/24' },
{ range: '203.0.113.0/24' },
{ range: '224.0.0.0/4' },
{ range: '233.252.0.0/24' },
{ range: '240.0.0.0/4' }
];
const ipParts = ip.split('.');
const ipAddress = (ipParts[0] << 24) + (ipParts[1] << 16) + (ipParts[2] << 8) + parseInt(ipParts[3]);
for (let i = 0; i < reservedRanges.length; i++) {
const rangeParts = reservedRanges[i].range.split('/');
const rangeAddress = (rangeParts[0].split('.')[0] << 24) + (rangeParts[0].split('.')[1] << 16) + (rangeParts[0].split('.')[2] << 8) + parseInt(rangeParts[0].split('.')[3]);
const rangeMask = 0xffffffff << (32 - parseInt(rangeParts[1]));
if ((ipAddress & rangeMask) === (rangeAddress & rangeMask)) {
return true;
}
}
return false;
}
function isValidSSTapRule(rule) {
// The expected format is a string starting with '#' followed by a series of comma-separated fields.
// Some fields must be specific numbers.
const regex = /^#([^,]+),([^,]+),(0|1),(0|1),(0|1),(0|1),(0|1),(0|1|2),([^,]+)$/;
return regex.test(rule);
}
function hasChinese(str) {
return /[\u4E00-\u9FA5]/.test(str);
}
function validateGameName() {
const gameNameElement = document.getElementById('gameName');
const gameNames = gameNameElement.value.split(',');
if (gameNames.length !== 2 || gameNames[0].trim() === "" || gameNames[1].trim() === "") {
gameNameElement.value = gameNameElement.placeholder;
}
}
function validateGatewayIP() {
const gatewayIPElement = document.getElementById('gatewayIP');
const gatewayIP = gatewayIPElement.value.trim();
if (gatewayIP !== "在链路上" && !isValidIPv4(gatewayIP)) {
gatewayIPElement.value = "172.19.83.1/在链路上";
}
}
function updateSSTapRule() {
const gameNameElement = document.getElementById('gameName');
const sstapRuleElement = document.getElementById('sstapRule');
const gameNames = gameNameElement.value.split(',');
if (gameNames.length === 2 && gameNames[0].trim() !== "" && gameNames[1].trim() !== "") {
const sstapRuleParts = sstapRuleElement.value.split(",");
if (sstapRuleParts.length >= 9) {
sstapRuleParts[0] = "#" + gameNames[0].trim();
sstapRuleParts[1] = gameNames[1].trim();
sstapRuleElement.value = sstapRuleParts.join(",");
}
}
}
function extractGatewayIP() {
if (document.getElementById('autoExtractCheckbox').checked) {
const inputElement = document.getElementById('input');
const gatewayIPCounts = new Map();
const routes = inputElement.value.split("\n");
for (let route of routes) {
const elements = route.trim().split(/\s+/);
if (elements.length >= 3 && isValidIPv4(elements[0])) {
const gatewayIP = elements[2];
gatewayIPCounts.set(gatewayIP, (gatewayIPCounts.get(gatewayIP) || 0) + 1);
}
}
if (gatewayIPCounts.size > 0) {
const sortedGatewayIPs = Array.from(gatewayIPCounts.keys()).sort((a, b) => gatewayIPCounts.get(b) - gatewayIPCounts.get(a));
selectedGatewayIP = sortedGatewayIPs[0];
}
} else {
const gatewayIPElement = document.getElementById('gatewayIP');
const gatewayIP = gatewayIPElement.value.trim();
selectedGatewayIP = gatewayIP;
}
}
function convertToClash() {
validateGameName();
extractGatewayIP();
const inputElement = document.getElementById("input");
const outputElement = document.getElementById("output");
const copyButton = document.getElementById("copyButton");
const downloadButton = document.getElementById("downloadButton");
const sstapRuleContainer = document.getElementById('sstapRuleContainer');
let routes = inputElement.value.split("\n");
let results = ["payload:"];
for (let route of routes) {
let elements = route.trim().split(/\s+/);
if (elements.length >= 3 && isValidIPv4(elements[0]) && elements[2] === selectedGatewayIP) {
const ipCidr = convertToCIDR(elements[0], elements[1]);
results.push(" - " + ipCidr);
}
}
outputElement.value = results.join("\n");
copyButton.style.display = "block";
downloadButton.style.display = "block";
downloadButton.dataset.fileExtension = "rules";
sstapRuleContainer.classList.remove('show');
}
function convertToSSTap() {
validateGameName();
extractGatewayIP();
updateSSTapRule();
const inputElement = document.getElementById("input");
const outputElement = document.getElementById("output");
const gameNameElement = document.getElementById('gameName');
const sstapRuleContainer = document.getElementById('sstapRuleContainer');
const sstapRuleElement = document.getElementById('sstapRule');
const copyButton = document.getElementById("copyButton");
const downloadButton = document.getElementById("downloadButton");
let routes = inputElement.value.split("\n");
let results = [];
for (let route of routes) {
let elements = route.trim().split(/\s+/);
if (elements.length >= 3 && isValidIPv4(elements[0]) && elements[2] === selectedGatewayIP) {
const ipCidr = convertToCIDR(elements[0], elements[1]);
results.push(ipCidr);
}
}
const gameNames = gameNameElement.value.split(',');
if (isValidSSTapRule(sstapRule.value)) {
sstapRuleElement.value = sstapRule.value;
} else {
sstapRuleElement.value = "#" + gameNames[0].trim() + "," + gameNames[1].trim() + ",0,0,1,0,1,0,By-HoldOnBro";
}
outputElement.value = sstapRuleElement.value + "\n" + results.join("\n");
copyButton.style.display = "block";
downloadButton.style.display = "block";
downloadButton.dataset.fileExtension = "rules";
sstapRuleContainer.classList.add('show');
}
function convertToNetch() {
validateGameName();
extractGatewayIP();
const inputElement = document.getElementById("input");
const outputElement = document.getElementById("output");
const copyButton = document.getElementById("copyButton");
const downloadButton = document.getElementById("downloadButton");
const sstapRuleContainer = document.getElementById('sstapRuleContainer');
let routes = inputElement.value.split("\n");
let results = [];
for (let route of routes) {
let elements = route.trim().split(/\s+/);
if (elements.length >= 3 && isValidIPv4(elements[0]) && elements[2] === selectedGatewayIP) {
const ipCidr = convertToCIDR(elements[0], elements[1]);
results.push(ipCidr);
}
}
outputElement.value = results.join("\n");
copyButton.style.display = "block";
downloadButton.style.display = "block";
downloadButton.dataset.fileExtension = "txt";
sstapRuleContainer.classList.remove('show');
}
function copyToClipboard() {
const outputElement = document.getElementById("output");
outputElement.select();
document.execCommand("copy");
}
function showDownloadDialog() {
const outputElement = document.getElementById("output");
const downloadButton = document.getElementById("downloadButton");
const gameNameElement = document.getElementById('gameName');
const gameNames = gameNameElement.value.split(',');
let fileName = (gameNames.length === 2 && gameNames[0].trim() !== "" && gameNames[1].trim() !== "") ? gameNames[0].trim() : "somegame";
fileName += "." + downloadButton.dataset.fileExtension;
const fileContents = outputElement.value;
const blob = new Blob([fileContents], { type: "text/plain;charset=utf-8" });
saveAs(blob, fileName);
}
function toggleAutoExtract() {
const autoExtractCheckbox = document.getElementById('autoExtractCheckbox');
const gatewayIPElement = document.getElementById('gatewayIP');
const gatewayIP = gatewayIPElement.value.trim();
if (autoExtractCheckbox.checked) {
gatewayIPElement.disabled = true;
gatewayIPElement.value = "";
} else {
gatewayIPElement.disabled = false;
gatewayIPElement.value = selectedGatewayIP;
}
}
</script>
</body>
</html>