Synocommunity Package 使用Cloudflare Workers反向代理
Synocommunity 第三方应用
Synocommunity提供了非常丰富和有用的第三方应用,具体可以去该网站查看。
在中国由于众所周知的原因,访问和下载packages.synocommunity.com里面的内容非常慢。安装、更新应用一直无法正常访问。最简单的想法就是使用镜像网站,发现并没有,所以就通过Cloudflare Workers自己搭建了一个。
搭建过程非常简单,我找到了一个专门用来反向代理的GitHub项目,然后修改了配置就可以使用了。
你可以使用我搭建的 https://synocommunity-packages.tuzhihao.com
直接配置在你的群晖使用,参考下图:
之后你就可以在社群里面找到丰富的插件,按需下载:
自行搭建
脚本
当然你可以自己搭建一个,创建一个wokers,在线编辑,填入如下脚本内容。
// copy from https://raw.githubusercontent.com/aD4wn/Workers-Proxy/master/src/index.js
// Website you intended to retrieve for users.
const upstream = 'packages.synocommunity.com'
// Custom pathname for the upstream website.
const upstream_path = '/'
// Website you intended to retrieve for users using mobile devices.
const upstream_mobile = 'packages.synocommunity.com'
// Countries and regions where you wish to suspend your service.
const blocked_region = []
// IP addresses which you wish to block from using your service.
const blocked_ip_address = []
// Whether to use HTTPS protocol for upstream address.
const https = true
// Whether to disable cache.
const disable_cache = true
// Replace texts.
const replace_dict = {
'$upstream': '$custom_domain'
}
const replace_content_type = [
'text/html',
'application/json'
]
addEventListener('fetch', event => {
event.respondWith(fetchAndApply(event.request));
})
async function fetchAndApply(request) {
const region = (request.headers.get('cf-ipcountry') || "").toUpperCase();
const ip_address = request.headers.get('cf-connecting-ip') || "";
const user_agent = request.headers.get('user-agent') || "";
let response = null;
let url = new URL(request.url);
let url_hostname = url.hostname;
if (https == true) {
url.protocol = 'https:';
} else {
url.protocol = 'http:';
}
if (await device_status(user_agent)) {
var upstream_domain = upstream;
} else {
var upstream_domain = upstream_mobile;
}
url.host = upstream_domain;
if (url.pathname == '/') {
url.pathname = upstream_path;
} else {
url.pathname = upstream_path + url.pathname;
}
if (blocked_region.includes(region)) {
response = new Response('Access denied: WorkersProxy is not available in your region yet.', {
status: 403
});
} else if (blocked_ip_address.includes(ip_address)) {
response = new Response('Access denied: Your IP address is blocked by WorkersProxy.', {
status: 403
});
} else {
let method = request.method;
let request_headers = request.headers;
let new_request_headers = new Headers(request_headers);
new_request_headers.set('Host', upstream_domain);
new_request_headers.set('Referer', url.protocol + '//' + url_hostname);
let original_response = await fetch(url.href, {
method: method,
headers: new_request_headers
})
let original_response_clone = original_response.clone();
let original_text = null;
let response_headers = original_response.headers;
let new_response_headers = new Headers(response_headers);
let status = original_response.status;
if (disable_cache) {
new_response_headers.set('Cache-Control', 'no-store');
}
new_response_headers.set('access-control-allow-origin', '*');
new_response_headers.set('access-control-allow-credentials', true);
new_response_headers.delete('content-security-policy');
new_response_headers.delete('content-security-policy-report-only');
new_response_headers.delete('clear-site-data');
if(new_response_headers.get("x-pjax-url")) {
new_response_headers.set("x-pjax-url", response_headers.get("x-pjax-url").replace("//" + upstream_domain, "//" + url_hostname));
}
const content_type = new_response_headers.get('content-type');
if (check_replace_response_text(content_type)) {
console.log('replace');
original_text = await replace_response_text(original_response_clone, upstream_domain, url_hostname);
} else {
original_text = original_response_clone.body
}
response = new Response(original_text, {
status,
headers: new_response_headers
})
}
return response;
}
function check_replace_response_text(content_type) {
if(content_type == null){
return false;
}
return replace_content_type.some((v) => content_type.includes(v));
}
async function replace_response_text(response, upstream_domain, host_name) {
let text = await response.text()
var i, j;
for (i in replace_dict) {
j = replace_dict[i]
if (i == '$upstream') {
i = upstream_domain
} else if (i == '$custom_domain') {
i = host_name
}
if (j == '$upstream') {
j = upstream_domain
} else if (j == '$custom_domain') {
j = host_name
}
console.log(i, j);
let re = new RegExp(i, 'g')
text = text.replace(re, j);
}
return text;
}
async function device_status(user_agent_info) {
var agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
var flag = true;
for (var v = 0; v < agents.length; v++) {
if (user_agent_info.indexOf(agents[v]) > 0) {
flag = false;
break;
}
}
return flag;
}
检查是否搭建成功
访问如下地址(host替换为你自己的):
https://synocommunity-packages.tuzhihao.com/?build=42962&language=enu&arch=apollolake
检查返回的json里面的link地址是否已经被替换,成功替换就代表搭建成功,恭喜🎉