⚠️ 注意:本项目仅适合短时间、小流量下载文件。长期、大流量使用可能被 Cloudflare 判定为滥用,或被要求升级 Pro 版本。
1. 获取 OneDrive 域名
- 在 OneDrive 中任选 1 个文件 → 点击「分享」→「复制链接」
- 得到类似链接:<code>https://test-my.sharepoint.com/:i:/g/personal/xxx/yyy?e=zzz</code>
- 仅复制域名(不含协议):<code>test-my.sharepoint.com</code>
2. 部署 Cloudflare Worker
- 登录 Cloudflare Dashboard → Workers →「创建服务」
- 删除默认代码,粘贴下方内容,并将 <code>test-my.sharepoint.com</code> 替换成你刚才复制的域名:
// ===== 只需改这里 =====
const upstream = 'test-my.sharepoint.com' // 你的域名
const upstream_mobile = 'test-my.sharepoint.com' // 同上
// =======================
const upstream_path = '/'
const blocked_region = ['KP', 'SY', 'PK', 'CU']
const blocked_ip_address = ['0.0.0.0', '127.0.0.1']
const https = true
const disable_cache = false
const replace_dict = {
'$upstream' : '$custom_domain',
'//sunpma.com' : ''
}
const not_found_html_content = <code>404</code>
addEventListener('fetch', event => {
event.respondWith(fetchAndApply(event.request))
})
async function fetchAndApply(req) {
const region = req.headers.get('cf-ipcountry')?.toUpperCase()
const ip = req.headers.get('cf-connecting-ip')
const ua = req.headers.get('user-agent')
const url = new URL(req.url)
const urlHost = url.hostname
// ---------- 路径过滤 ----------
if (url.pathname === '/personal' || !url.pathname.includes('/personal/')) {
return new Response(not_found_html_content, {
status : 404,
headers: { 'Content-Type': 'text/html; charset=UTF-8' }
})
}
url.protocol = https ? 'https:' : 'http:'
url.host = (await device_status(ua)) ? upstream : upstream_mobile
url.pathname = upstream_path + (url.pathname === '/' ? '' : url.pathname)
if (blocked_region.includes(region)) {
return new Response('Access denied: WorkersProxy is not available in your region yet.', { status: 403 })
}
if (blocked_ip_address.includes(ip)) {
return new Response('Access denied: Your IP address is blocked by WorkersProxy.', { status: 403 })
}
const newHdr = new Headers(req.headers)
newHdr.set('Host', url.host)
newHdr.set('Referer', <code>${url.protocol}//${urlHost}</code>)
const res = await fetch(url.href, { method: req.method, headers: newHdr })
const resHdr = new Headers(res.headers)
const contentTp = resHdr.get('content-type') || ''
if (disable_cache) resHdr.set('Cache-Control', 'no-store')
resHdr.set('access-control-allow-origin', '*')
resHdr.set('access-control-allow-credentials', 'true')
resHdr.delete('content-security-policy')
resHdr.delete('content-security-policy-report-only')
resHdr.delete('clear-site-data')
if (resHdr.get('x-pjax-url')) {
resHdr.set('x-pjax-url',
resHdr.get('x-pjax-url').replace(<code>//${upstream}</code>, <code>//${urlHost}</code>))
}
let body = res.body
if (contentTp.includes('text/html') && contentTp.includes('UTF-8')) {
body = await replace_response_text(await res.text(), upstream, urlHost)
}
return new Response(body, { status: res.status, headers: resHdr })
}
async function replace_response_text(text, up, host) {
for (const [k, v] of Object.entries(replace_dict)) {
const key = k === '$upstream' ? up : (k === '$custom_domain' ? host : k)
const val = v === '$upstream' ? up : (v === '$custom_domain' ? host : v)
text = text.replace(new RegExp(key, 'g'), val)
}
return text
}
async function device_status(ua) {
const mobile = ['Android', 'iPhone', 'SymbianOS', 'Windows Phone', 'iPad', 'iPod']
return !mobile.some(m => ua.includes(m))
}
<aside>
💡
至此,Cloudflare Workers 反代节点已搭建完成。轻量场景下可显著改善 OneDrive 直链速度;若出现 100 GB 级持续高流量,请及时分流或升级至 Cloudflare Pro,避免被平台限流。祝使用愉快!
</aside>
Comments NOTHING