forked from xmdhs/clash2sfa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrontend.html
291 lines (266 loc) · 11.3 KB
/
frontend.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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="[[index . 0]] ([[index . 1]])" />
<title>clash 订阅链接转 sing-box</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/picocss/1.5.7/pico.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/3.3.4/vue.global.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pako/2.1.0/pako.min.js"
integrity="sha512-g2TeAWw5GPnX7z0Kn8nFbYfeHcvAu/tx6d6mrLe/90mkCxO+RcptyYpksUz35EO337F83bZwcmUyHiHamspkfg=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/base64.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.11/clipboard.min.js"></script>
<style type="text/css">
header {
border-bottom: 1px solid #e5e5e5;
margin-bottom: 30px;
}
</style>
</head>
<body>
<div id="app">
<header>
<nav class="container-fluid">
<ul>
<li><strong>clash 订阅链接转 sing-box</strong></li>
</ul>
<ul>
</ul>
</nav>
</header>
<main class="container">
<p>将 clash 格式的订阅链接转换为 sing-box 的工具,转换后的链接可用于安卓版本的 <a
href="https://sing-box.sagernet.org/installation/clients/sfa/">SFA</a>,ios 版本的未测试。</p>
<p>在此页面粘贴由本工具生成的订阅链接(仅参数保存在链接内可用),可解析到下面的选项中</p>
<input placeholder="订阅链接,多个链接使用 | 分割" v-model="sub" />
<div class="grid">
<input placeholder="url test 中包含的节点名(可用正则)" v-model="include" />
<input placeholder="url test 中排除的节点名(可用正则)" v-model="exclude" />
</div>
<br />
<details>
<summary>配置文件模板</summary>
<hr />
<span>可通过设置 urltest / selector 的 outbounds,来自定义生成配置文件后其中包含的节点。</span>
<span>类似:</span>
<pre>{
"tag": "selector",
"type": "selector",
"outbounds": [
"include: 新加坡",
"exclude: 香港"
]
}</pre>
<span>均可以使用正则筛选</span>
<br />
<label>
<input type="checkbox" role="switch" v-model="checkbox">
配置文件模板直链
</label>
<label>
<input type="checkbox" role="switch" v-model="disableUrlTest">
禁用默认生成的 urltest 和 selector
</label>
<label for="addTag">
<input type="checkbox" id="addTag" role="switch" v-model="addTag">
生成的节点中添加订阅地址的域名,用以区分不同订阅的节点
</label>
<label for="switch">
<input type="checkbox" id="switch" name="switch" role="switch" v-model="saveType">
{{ saveTypeText }}
</label>
<textarea style="resize: none;height: 25em;" v-model="config" v-show="!checkbox"></textarea>
<input v-model="configurl" v-show="checkbox" />
</details>
<progress v-show="inFetch"></progress>
<button v-show="!inFetch" @click="click">获取 sing-box 格式订阅链接</button>
<input type="text" ref="inputRef" v-if="newsub" placeholder="" v-model="newsub">
</main>
</div>
</body>
<script type="module">
Vue.createApp({
setup(props, context) {
const sub = Vue.ref('');
const newsub = Vue.ref('');
const include = Vue.ref('');
const exclude = Vue.ref('');
const config = Vue.ref('加载中');
const checkbox = Vue.ref(false)
const configurl = Vue.ref('');
const saveType = Vue.ref(true);
const saveTypeText = Vue.ref('参数保存在链接内');
const inFetch = Vue.ref(false)
const inputRef = Vue.ref(null)
const addTag = Vue.ref(false)
const disableUrlTest = Vue.ref(false)
Vue.watch(saveType, v => {
if (v) {
saveTypeText.value = "参数保存在链接内"
} else {
saveTypeText.value = "参数保存在服务端"
}
})
Vue.watch(checkbox, v => {
if (v) {
config.value = oldConfig
} else {
configurl.value = ""
}
})
let oldConfig = "";
(async () => {
const f = await fetch("/config")
config.value = await f.text()
oldConfig = config.value
})();
function saveParameter() {
const subUrl = new URL(new URL(location.href).origin)
subUrl.pathname = "/sub"
const c = config.value != oldConfig ? config.value : ""
if (c != "") {
const compressed = pako.deflate(config.value);
const base64String = Base64.fromUint8Array(compressed, true)
subUrl.searchParams.set("config", base64String)
}
configurl.value && subUrl.searchParams.set("configurl", configurl.value)
include.value && subUrl.searchParams.set("include", include.value)
exclude.value && subUrl.searchParams.set("exclude", exclude.value)
addTag.value && subUrl.searchParams.set("addTag", "true")
disableUrlTest.value && subUrl.searchParams.set("disableUrlTest", "true")
subUrl.searchParams.set("sub", sub.value)
return subUrl.toString()
}
async function saveServer() {
const u = new URL(new URL(location.href).origin)
u.pathname = "/put"
const c = config.value != oldConfig ? config.value : ""
const text = await (await fetch(u.toString(), {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
"Sub": sub.value,
"Include": include.value,
"Exclude": exclude.value,
"Config": c,
"ConfigUrl": configurl.value,
"AddTag": addTag.value,
"DisableUrlTest": disableUrlTest.value
})
})).text()
const subUrl = new URL(new URL(location.href).origin)
subUrl.pathname = "/sub"
subUrl.searchParams.set("id", text)
return subUrl.toString()
}
function catchSome(f, onfail) {
const nf = async (...a) => {
try {
return await f(...a);
} catch (e) {
if (onfail) {
onfail()
}
console.warn(e)
alert(String(e))
}
}
return nf
}
const click = catchSome(async () => {
if (sub.value == "") {
return ""
}
if (inFetch.value) {
return
}
newsub.value = ""
inFetch.value = true
const subURL = await (async () => {
if (saveType.value) {
return saveParameter()
} else {
return await saveServer()
}
})()
const f = await fetch(subURL)
if (!f.ok) {
const msg = await f.text()
newsub.value = msg
console.warn(msg)
inFetch.value = false
alert("错误 " + msg)
return
}
inFetch.value = false
newsub.value = subURL
await Vue.nextTick()
inputRef.value.scrollIntoView({ behavior: "smooth" })
inputRef.value.select()
document.execCommand('copy', true);
alert("已复制到粘贴板")
}, () => {
inFetch.value = false
})
document.addEventListener('paste', async (event) => {
const items = event.clipboardData && event.clipboardData.items;
for (const v of items) {
if (v.kind == "file") continue
v.getAsString(v => {
try {
const u = new URL(v)
if (u.pathname != "/sub") {
return
}
if (!confirm("解析粘贴的订阅链接?")) {
return
}
const c = u.searchParams.get("config")
if (c && c != "") {
const d = pako.inflate(Base64.toUint8Array(c), { to: "string" });
config.value = d
}
const cu = u.searchParams.get("configurl")
if (cu && cu != "") {
configurl.value = cu
checkbox.value = true
config.value = oldConfig
} else {
configurl.value = ""
}
include.value = u.searchParams.get("include") || include.value
exclude.value = u.searchParams.get("exclude") || exclude.value
sub.value = u.searchParams.get("sub") || sub.value
addTag.value = u.searchParams.get("addTag") === "true"
disableUrlTest.value = u.searchParams.get("disableUrlTest") === "true"
} catch (error) {
console.log(error)
return
}
})
}
});
return {
sub,
config,
include,
exclude,
newsub,
click,
checkbox,
configurl,
saveType,
saveTypeText,
inFetch,
inputRef,
addTag,
disableUrlTest
}
},
}).mount('#app')
</script>
</html>