-
Notifications
You must be signed in to change notification settings - Fork 6
/
cf-worker.js
162 lines (145 loc) · 5.71 KB
/
cf-worker.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
addEventListener('fetch', event => {
event.passThroughOnException()
event.respondWith(handleRequest(event))
})
/**
* Respond to the request
* @param {Request} request
*/
async function handleRequest(event) {
const { request } = event;
//请求头部、返回对象
let reqHeaders = new Headers(request.headers),
outBody, outStatus = 200, outStatusText = 'OK', outCt = null, outHeaders = new Headers({
"Access-Control-Allow-Origin": reqHeaders.get('Origin'),
"Access-Control-Allow-Methods": "GET, POST, PUT, PATCH, DELETE, OPTIONS",
"Access-Control-Allow-Headers": reqHeaders.get('Access-Control-Allow-Headers') || "Accept, Authorization, Cache-Control, Content-Type, DNT, If-Modified-Since, Keep-Alive, Origin, User-Agent, X-Requested-With, Token, x-access-token, Notion-Version"
});
try {
//取域名第一个斜杠后的所有信息为代理链接
let url = request.url.substr(8);
url = decodeURIComponent(url.substr(url.indexOf('/') + 1));
//需要忽略的代理
if (request.method == "OPTIONS" && reqHeaders.has('access-control-request-headers')) {
//输出提示
return new Response(null, PREFLIGHT_INIT)
}
else if(url.length < 3 || url.indexOf('.') == -1 || url == "favicon.ico" || url == "robots.txt") {
return Response.redirect('https://baidu.com', 301)
}
//阻断
else if (blocker.check(url)) {
return Response.redirect('https://baidu.com', 301)
}
else {
//补上前缀 http://
url = url.replace(/https:(\/)*/,'https://').replace(/http:(\/)*/, 'http://')
if (url.indexOf("://") == -1) {
url = "http://" + url;
}
//构建 fetch 参数
let fp = {
method: request.method,
headers: {}
}
//保留头部其它信息
let he = reqHeaders.entries();
const requrl = new URL(url);
// if(requrl.hostname.includes('.xunleix.com')){
// for (let h of he) {
// if (!['content-length', 'cf-ipcountry', 'x-real-ip', 'cf-connecting-ip', 'server'].includes(h[0])) {
// fp.headers[h[0]] = h[1]; // 其他头部正常添加
// }
// }
// fp.headers["User-Agent"] = 'AndroidDownloadManager/12 (Linux; U; Android 12; M2004J7AC Build/SP1A.210812.016)';
// }else{
// for (let h of he) {
// if (!['content-length'].includes(h[0])) {
// fp.headers[h[0]] = h[1];
// }
// }
// }
if(requrl.hostname=='api-pan.xunleix.com'){
for (let h of he) {
if (!['content-length', 'cf-ipcountry', 'x-real-ip', 'cf-connecting-ip', 'server'].includes(h[0])) {
fp.headers[h[0]] = h[1]; // 其他头部正常添加
}
}
}else{
for (let h of he) {
if (!['content-length'].includes(h[0])) {
fp.headers[h[0]] = h[1];
}
}
}
// 是否带 body
if (["POST", "PUT", "PATCH", "DELETE"].indexOf(request.method) >= 0) {
const ct = (reqHeaders.get('content-type') || "").toLowerCase();
if (ct.includes('application/json')) {
let requestJSON = await request.json()
console.log(typeof requestJSON)
fp.body = JSON.stringify(requestJSON);
} else if (ct.includes('application/text') || ct.includes('text/html')) {
fp.body = await request.text();
} else if (ct.includes('form')) {
fp.body = await request.formData();
} else {
fp.body = await request.blob();
}
}
// 发起 fetch
let fr = (await fetch(new URL(url), fp));
outCt = fr.headers.get('content-type');
if(outCt && (outCt.includes('application/text') || outCt.includes('text/html'))) {
try {
// 添加base
let newFr = new HTMLRewriter()
.on("head", {
element(element) {
element.prepend(`<base href="${url}" />`, {
html: true
})
},
})
.transform(fr)
fr = newFr
} catch(e) {
}
}
for (const [key, value] of fr.headers.entries()) {
outHeaders.set(key, value);
}
outStatus = fr.status;
outStatusText = fr.statusText;
outBody = fr.body;
}
} catch (err) {
outCt = "application/json";
outBody = JSON.stringify({
code: -1,
msg: JSON.stringify(err.stack) || err
});
}
//设置类型
if (outCt && outCt != "") {
outHeaders.set("content-type", outCt);
}
let response = new Response(outBody, {
status: outStatus,
statusText: outStatusText,
headers: outHeaders
})
return response;
// return new Response('OK', { status: 200 })
}
/**
* 阻断器
*/
const blocker = {
keys: [".m3u8", ".ts", ".acc", ".m4s", "photocall.tv", "googlevideo.com"],
check: function (url) {
url = url.toLowerCase();
let len = blocker.keys.filter(x => url.includes(x)).length;
return len != 0;
}
}