Contents
hide
介绍
- https://github.com/qkqpttgf/OneManager-php/blob/master/readme_cn.md
- 目的就是为了在国内访问OneDrive能够更快些
- https://hostloc.com/thread-880704-1-2.html
实现
CF+反代 – 可以用worker进行反代,但腾讯其实就没啥意义了
Shell
x
89
89
1
// odd, 单日
2
const SingleDay = 'https://od-xyze3m3.herokuapp.com/'
3
// even, 双日
4
const DoubleDay = 'https://od-xyze3m3.herokuapp.com/'
5
6
//const SingleDay = 'https://153xxxxx0.cn-hongkong.fc.aliyuncs.com/2016-08-15/proxy/onedrive/xxx/'
7
//const DoubleDay = 'https://153xxxxx0.cn-hongkong.fc.aliyuncs.com/2016-08-15/proxy/onedrive/xxx/'
8
9
// CF proxy all, 一切给CF代理,true/false
10
const CFproxy = true
11
12
// Used in cloudflare workers, odd or even days point to 2 heroku account.
13
14
// 由于heroku不绑卡不能自定义域名,就算绑卡后https也不方便
15
// 另外免费套餐每月550小时,有些人不够用
16
// 于是在CF Workers使用此代码,分单双日拉取不同heroku帐号下的相同网页
17
// 只改上面,下面不用动
18
19
addEventListener('fetch', event => {
20
let url=new URL(event.request.url);
21
if (url.protocol == 'http:') {
22
url.protocol = 'https:'
23
event.respondWith( Response.redirect(url.href) )
24
} else {
25
let response = null;
26
let nd = new Date();
27
if (nd.getDate()%2) {
28
host = SingleDay
29
} else {
30
host = DoubleDay
31
}
32
if (host.substr(0, 7)!='http://'&&host.substr(0, 8)!='https://') host = 'http://' + host;
33
34
response = fetchAndApply(host, event.request);
35
36
event.respondWith( response );
37
}
38
})
39
40
async function fetchAndApply(host, request) {
41
let f_url = new URL(request.url);
42
let a_url = new URL(host);
43
let replace_path = a_url.pathname;
44
if (replace_path.substr(replace_path.length-1)!='/') replace_path += '/';
45
let replaced_path = '/';
46
let query = f_url.search;
47
let path = f_url.pathname;
48
if (host.substr(host.length-1)=='/') path = path.substr(1);
49
f_url.href = host + path + query;
50
51
let response = null;
52
if (!CFproxy) {
53
response = await fetch(f_url, request);
54
} else {
55
let method = request.method;
56
let body = request.body;
57
let request_headers = request.headers;
58
let new_request_headers = new Headers(request_headers);
59
new_request_headers.set('Host', f_url.host);
60
new_request_headers.set('Referer', request.url);
61
62
response = await fetch(f_url.href, {
63
method: method,
64
body: body,
65
headers: new_request_headers
66
});
67
}
68
69
let out_headers = new Headers(response.headers);
70
if (out_headers.get('Content-Disposition')=='attachment') out_headers.delete('Content-Disposition');
71
let out_body = null;
72
let contentType = out_headers.get('Content-Type');
73
if (contentType.includes("application/text")) {
74
out_body = await response.text();
75
while (out_body.includes(replace_path)) out_body = out_body.replace(replace_path, replaced_path);
76
} else if (contentType.includes("text/html")) {
77
out_body = await response.text();
78
while (replace_path!='/'&&out_body.includes(replace_path)) out_body = out_body.replace(replace_path, replaced_path);
79
} else {
80
out_body = await response.body;
81
}
82
83
let out_response = new Response(out_body, {
84
status: response.status,
85
headers: out_headers
86
})
87
88
return out_response;
89
}