-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
217 lines (195 loc) · 6.03 KB
/
index.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
const config = {
//控制 HTTP referer header,如果你想创建一个隐藏 HTTP Referer header 的匿名链接,请设置为“on”。
no_ref: "on",
//首页主题,默认主题为空。 要使用其它主题,请填写 "theme/name" 。
theme:"",
//是否允许API请求的跨源资源共享。
cors: "on",
//相同的长url是否使用同一个短url。
unique_link:true,
//允许用户自定义短url。
custom_link:true,
//输入 Google 安全浏览 API 密钥以在重定向前启用 url 安全检查。
safe_browsing_api_key: ""
}
const html404 = `<!DOCTYPE html>
<body>
<h1>404 Not Found.</h1>
<p>The url you visit is not found.</p>
<a href="https://github.com/xramas/Urlcreate/" target="_self">Fork me on GitHub</a>
</body>`
let response_header={
"content-type": "text/html;charset=UTF-8",
}
if (config.cors=="on"){
response_header={
"content-type": "text/html;charset=UTF-8",
"Access-Control-Allow-Origin":"*",
"Access-Control-Allow-Methods": "POST",
}
}
async function randomString(len) {
len = len || 6;
let $chars = 'ABCDEFGHKLMNOPRSTUVWXYZabcdehikmnorstuvwxz023456789'; /****去除了:I\J\Q\f\g\j\l\p\q\y****/
let maxPos = $chars.length;
let result = '';
for (i = 0; i < len; i++) {
result += $chars.charAt(Math.floor(Math.random() * maxPos));
}
return result;
}
async function sha512(url){
url = new TextEncoder().encode(url)
const url_digest = await crypto.subtle.digest(
{
name: "SHA-512",
},
url, //您要散列为 ArrayBuffer 的数据
)
const hashArray = Array.from(new Uint8Array(url_digest)); //将缓冲区转换为字节数组
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
//console.log(hashHex)
return hashHex
}
async function checkURL(URL){
let str=URL;
let Expression=/http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/;
let objExp=new RegExp(Expression);
if(objExp.test(str)==true){
if (str[0] == 'h')
return true;
else
return false;
}else{
return false;
}
}
async function save_url(URL){
let random_key=await randomString()
let is_exist=await LINKS.get(random_key)
console.log(is_exist)
if (is_exist == null)
return await LINKS.put(random_key, URL),random_key
else
save_url(URL)
}
async function is_url_exist(url_sha512){
let is_exist = await LINKS.get(url_sha512)
console.log(is_exist)
if (is_exist == null) {
return false
}else{
return is_exist
}
}
async function is_url_safe(url){
let raw = JSON.stringify({"client":{"clientId":"Url-Shorten-Worker","clientVersion":"1.0.7"},"threatInfo":{"threatTypes":["MALWARE","SOCIAL_ENGINEERING","POTENTIALLY_HARMFUL_APPLICATION","UNWANTED_SOFTWARE"],"platformTypes":["ANY_PLATFORM"],"threatEntryTypes":["URL"],"threatEntries":[{"url":url}]}});
let requestOptions = {
method: 'POST',
body: raw,
redirect: 'follow'
};
result = await fetch("https://safebrowsing.googleapis.com/v4/threatMatches:find?key=AIzaSyB9hnLmYPihUefkSl9Mnxui35NDCJVw650", requestOptions)
result = await result.json()
console.log(result)
if (Object.keys(result).length === 0){
return true
}else{
return false
}
}
async function handleRequest(request) {
console.log(request)
if (request.method === "POST") {
let req=await request.json()
console.log(req["url"])
if(!await checkURL(req["url"])){
return new Response(`{"status":500,"key":": Error: Url illegal."}`, {
headers: response_header,
})}
let stat,random_key
if (config.unique_link){
let url_sha512 = await sha512(req["url"])
let url_key = await is_url_exist(url_sha512)
if(url_key){
random_key = url_key
}else{
stat,random_key=await save_url(req["url"])
if (typeof(stat) == "undefined"){
console.log(await LINKS.put(url_sha512,random_key))
}
}
}else{
stat,random_key=await save_url(req["url"])
}
console.log(stat)
if (typeof(stat) == "undefined"){
return new Response(`{"status":200,"key":"/`+random_key+`"}`, {
headers: response_header,
})
}else{
return new Response(`{"status":200,"key":": Error:Reach the KV write limitation."}`, {
headers: response_header,
})}
}else if(request.method === "OPTIONS"){
return new Response(``, {
headers: response_header,
})
}
const requestURL = new URL(request.url)
const path = requestURL.pathname.split("/")[1]
const params = requestURL.search;
console.log(path)
if(!path){
const html= await fetch("https://xramas.github.io/Pages/Urlcreate/"+config.theme+"/index.html")
return new Response(await html.text(), {
headers: {
"content-type": "text/html;charset=UTF-8",
},
})
}
const value = await LINKS.get(path);
let location ;
if(params) {
location = value + params
} else {
location = value
}
console.log(value)
if (location) {
if (config.safe_browsing_api_key){
if(!(await is_url_safe(location))){
let warning_page = await fetch("https://xramas.github.io/Pages/Urlcreate/safe-browsing.html")
warning_page =await warning_page.text()
warning_page = warning_page.replace(/{Replace}/gm, location)
return new Response(warning_page, {
headers: {
"content-type": "text/html;charset=UTF-8",
},
})
}
}
if (config.no_ref=="on"){
let no_ref= await fetch("https://xramas.github.io/Pages/Urlcreate/no-ref.html")
no_ref=await no_ref.text()
no_ref=no_ref.replace(/{Replace}/gm, location)
return new Response(no_ref, {
headers: {
"content-type": "text/html;charset=UTF-8",
},
})
}else{
return Response.redirect(location, 302)
}
}
// If request not in kv, return 404
return new Response(html404, {
headers: {
"content-type": "text/html;charset=UTF-8",
},
status: 404
})
}
addEventListener("fetch", async event => {
event.respondWith(handleRequest(event.request))
})