forked from sunsetsonwheels/webstore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CloudflareWorker.js
137 lines (132 loc) · 4.41 KB
/
CloudflareWorker.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
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request).catch((err) => {
return new Response(err.message)
}))
})
const html = `
<html><head></head><body>
<input type="url" placeholder="url" id="url" style="width: 80%; display: block;">
<input type="submit" id="submit" value="submit"/>
<div id="res"></div>
<a id="a" href=""></a>
<div>Note: This tool is only for direct URL links</div>
<script>
document.getElementById('submit').onclick=function(){
let url = document.getElementById('url').value;
console.log('url: '+url);
let a = document.getElementById('a');
let div = document.getElementById('res');
if(!url || !url.startsWith('http')){
div.textContent="Link is illegal: "+url;
a.style="display:none";
}else{
div.textContent="";
let res = (new URL(window.location.href)).origin+'?url='+encodeURIComponent(url);
a.textContent=res;
a.href=res;
a.style="";
}
}
</script>
</body></html>`;
/**
* Respond to the request
* @param {Request} request
*/
async function handleRequest(request) {
if (request.method === 'OPTIONS' && request.headers.has('access-control-request-headers')) {
return new Response(null, {
status: 204,
headers: new Headers({
'access-control-allow-origin': '*',
'access-control-allow-methods': 'GET,POST,PUT,PATCH,TRACE,DELETE,HEAD,OPTIONS',
'access-control-allow-headers': request.headers.get('access-control-request-headers'),
'access-control-max-age': '1728000'
}),
})
}
let req_url = new URL(request.url);
if (req_url.pathname.startsWith('/ajax/')) { //ajax
let url = req_url.pathname.slice(6).replace(/^(https?):\/+/, '$1://');
if (!url) return new Response("Only For Ajax");
let res = await fetch(url, {
method: request.method,
headers: request.headers,
body: request.body
});
let h = new Headers(res.headers);
h.set('access-control-allow-origin', '*');
h.set('access-control-expose-headers', '*');
return new Response(res.body, {
status: res.status,
headers: h
});
} else if (req_url.pathname === '/') { //download
let url = req_url.searchParams.get('url');
if (!url) return new Response(html, {
status: 200,
headers: {
'Content-Type': 'text/html; charset=utf-8'
}
});
let res;
let type;
if (request.headers.get('Range')) {
res = await fetch(url, {
headers: {
'Range': request.headers.get('Range')
}
});
} else {
res = await fetch(url, {
method: request.method,
headers: request.headers,
body: request.body
});
}
let h = new Headers(res.headers);
h.set('set-cookie', '');
h.set('access-control-allow-origin', '*');
h.set('access-control-expose-headers', '*');
return new Response(res.body, {
status: res.status,
headers: h,
})
} else if (req_url.pathname === '/manifest') {
let url = req_url.searchParams.get('url');
if (!url) return new Response(html, {
status: 200,
headers: {
'Content-Type': 'text/html; charset=utf-8'
}
});
let res = await fetch(url, {
method: request.method,
headers: request.headers,
body: request.body
});
return new Response(JSON.stringify(await res.json()), {
status: 200,
headers: {
'Content-Type': 'application/x-web-app-manifest+json',
'access-control-allow-origin': '*',
'access-control-expose-headers': '*',
},
})
} else {
return new Response("400 --", {
status: 400,
headers: {
'Content-Type': 'text/html; charset=utf-8'
}
});
}
}
String.prototype.format = function () {
let s = this;
for (let i = 0; i < arguments.length; i++) {
const reg = new RegExp("\\{" + i + "\\}", "gm");
s = s.replace(reg, arguments[i]);
}
return s;
};