惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
Microsoft Azure Blog
Microsoft Azure Blog
L
LangChain Blog
Y
Y Combinator Blog
Microsoft Security Blog
Microsoft Security Blog
宝玉的分享
宝玉的分享
B
Blog RSS Feed
MongoDB | Blog
MongoDB | Blog
Jina AI
Jina AI
D
Docker
B
Blog
Engineering at Meta
Engineering at Meta
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
I
InfoQ
G
Google Developers Blog
博客园 - Franky
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss
大猫的无限游戏
大猫的无限游戏
阮一峰的网络日志
阮一峰的网络日志
U
Unit 42

zhecydn的博客站

edgeone配置加速s3 cloudflare r2对象存储(水) – zhecydn的博客站 我的wp网站优化and美化(四) – zhecydn的博客站 b站首页回到旧版+搜索页回到旧版 – zhecydn的博客站 我的b站使用优化(弹幕+视频+动态优化) – zhecydn的博客站 spotify歌词汉化,同步,添加外部歌词源 – zhecydn的博客站 把中科院免费20G的s3存储桶挂载到openlist – zhecydn的博客站 腾讯云eo优选ip失败的过程+“优选”的探索 – zhecydn的博客站 用腾讯eo/阿里esa来干掉你的ddns访问端口,套cdn(lucky内网穿透补充篇) – zhecydn的博客站 (转)(水)主流nas系统体验账号官方免费申请地址 – zhecydn的博客站 如何下载b站以及网页上的视频文件到你的nas或者本地电脑里(附脚本使用教程) – zhecydn的博客站 关于我大半年没写东西都在干什么 – zhecydn的博客站 让小爱同学实现全屋智能,接入gpt,播放本地歌曲,开电脑 – zhecydn的博客站 r66s-一个不错的软路由入手选择 – zhecydn的博客站 8.21拍摄 – zhecydn的博客站 确诊抑郁症的这段时间 – zhecydn的博客站 使用内网穿透神器lucky来做ddns和https重定向以及ipv4访问 – zhecydn的博客站 关于小雅emby的一些美化和清理以及更新 – zhecydn的博客站 小雅影视库的进一步升级 xiaoya-tvbox – zhecydn的博客站 在绿联私有云dx4600上用docker部署motrix – zhecydn的博客站 eu4欧陆风云1.36.x版本汉化+双字节+dlc方法(steam版) – zhecydn的博客站 2023 – zhecydn的博客站 使用docker部署一个计数器 Moe-counter – zhecydn的博客站 用docker部署免费的思维导图和pdf转化项目 – zhecydn的博客站 博客一周年啦 – zhecydn的博客站 docker部署新的免费chatgpt项目 – zhecydn的博客站 我在vercel上面部署的网页被墙了 – zhecydn的博客站 腾讯云服务器优惠,低至58/年,老用户可用 – zhecydn的博客站 使用免费的cloudflared给绿联dx4600实现内网穿透 – zhecydn的博客站 使用nastool自动追番(1)nastool和qbittorrent的安装 – zhecydn的博客站 新疆农大社会实践自动填写(需手动提交) – zhecydn的博客站
最近用cf worker搭的一些好玩的小项目 – zhecydn的博客站
2023-10-23 · via zhecydn的博客站

开始

最近玩了一些有意思的东西,然后流水账式的给大家分享一下

镜像站

我这里以wiki为例子

代码如下

// 反代目标网站.
const upstream = 'zh.wikipedia.org'

// 反代目标网站的移动版.
const upstream_mobile = 'zh.m.wikipedia.org'

// 访问区域黑名单(按需设置).
const blocked_region = ['TK']

// IP地址黑名单(按需设置).
const blocked_ip_address = ['0.0.0.0', '127.0.0.1']

// 路径替换.
const replace_dict = {
'$upstream': '$custom_domain',
'//archiveofourown.org': ''
}

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_host = url.host;

if (url.protocol == 'http:') {
    url.protocol = 'https:'
    response = Response.redirect(url.href);
    return response;
}

if (await device_status(user_agent)) {
    var upstream_domain = upstream;
} else {
    var upstream_domain = upstream_mobile;
}

url.host = upstream_domain;

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.href);

    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;

    new_response_headers.set('cache-control' ,'public, max-age=14400')
    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');

    const content_type = new_response_headers.get('content-type');
    if (content_type.includes('text/html') && content_type.includes('UTF-8')) {
        original_text = await replace_response_text(original_response_clone, upstream_domain, url_host);
    } else {
        original_text = original_response_clone.body
    }

    response = new Response(original_text, {
        status,
        headers: new_response_headers
    })
}
return response;
}

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
    }

    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;
}

总之就是cf worker那里填写对应的代码即可,我尝试了几个网站,不过成功的只有wiki

我搭建的wiki的镜像站,bili.zhecydn.link,大家可以尝试着访问看看

google和youtube等网站就无法进行镜像了,这里大家可以用其他的方式去搭镜像站

比如我就用宝塔的反向代理搭了一个google的镜像站,大家可以进行参考

网址的话不太方便透露,大家如果有兴趣就自己在服务器上搭一个试试看

随机壁纸api

这个呢,我之前做过一期在服务器上部署壁纸api的过程

但是我发现cf worker也可以做到这件事,因此我也把源码发出来给大家参考一下

addEventListener('fetch', event => {
    event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {

	var background_urls = [
'https://cdn.jsdelivr.net/gh/cypicbed/cypic@master/img/mt1.jpg',
'https://cdn.jsdelivr.net/gh/cypicbed/cypic@master/img/mt2.jpg',
'https://cdn.jsdelivr.net/gh/cypicbed/cypic@master/img/mt3.jpg',
'https://cdn.jsdelivr.net/gh/cypicbed/cypic@master/img/mt4.jpg',
'https://cdn.jsdelivr.net/gh/cypicbed/cypic@master/img/mt5.jpg',
'https://img.400511.net/2022/02/24/Ne5Wu0HV.jpg'
 /*需要随机到的图片的链接*/
    ]
	var index = Math.floor((Math.random()*background_urls.length));
	res = await fetch(background_urls[index])
    return new Response(res.body, {
        headers: { 'content-type': 'image/jpeg' },
    })
}

使用的话替换var background urls下面的图片链接为自己的就ok了,我自己搭建的就不给大家分享了,大家自行搭建吧

b站港澳台代理

这里我不是很想多讲,我只讲一下方案吧,其实我也没验证过是否可行

如果有成功的朋友可以跟我私底下来讲讲

前面我们讲过镜像站了,所以这里我们可以尝试使用cdn反代api.bilibili.com

然后把这个worker项目绑定到自定义域名上

最后优选ip,把dns改到优选的ip再去访问,我这里没有合适的优选ip,感觉超麻烦就没弄了,而且我也有对应地区的服务器

所以我就停在了这一步没有再进行尝试

其他

其实我以前的图床站就是用cloudflare的worker搭建的

虽然现在我早就不用了,但是我觉得大家如果需要的话也可以搭建一个,使用起来还是很方便的

结语

最近还是有在玩一些好玩的小玩意的,今天给大家分享一下