安装完 Linux 系统后的第一步就是换软件源,OpenTUNA 已于 2023 年 12 月 20 日起停运,清华大学开源软件镜像站 的 URL 太长,所以产生了做短链接跳转的想法。 Cloudflare Workers 代码如下:
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 let mirrors = { '163' : 'mirrors.163.com' , 'aliyun' : 'mirrors.aliyun.com' , 'tuna' : 'mirrors.tuna.tsinghua.edu.cn' , 'ustc' : 'mirrors.ustc.edu.cn' , 'npm' : 'registry.npmmirror.com' , 'py' : 'pypi.tuna.tsinghua.edu.cn' }; export default { async fetch (request ) { const Url = new URL (request.url ); const paths = Url .pathname .split ('/' ); if (mirrors.hasOwnProperty (paths[1 ])) { Url .hostname = mirrors[paths[1 ]]; Url .pathname = '/' + paths.slice (2 ).join ('/' ); } else { Url .hostname = mirrors["tuna" ]; } return new Response (null , { status : 301 , headers : { 'Location' : Url .toString (), }, }); } };
将 Workers 添加到自定义域,例如 m.foo.bar,在软件源里可以使用这个域名。 例如 Debian 的 apt 源:
1 deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
可以写成:
1 2 3 4 # 默认使用 tuna deb https://m.foo.bar/debian/ bookworm main contrib non-free non-free-firmware # 如果想强制 163 deb https://m.foo.bar/163/debian/ bookworm main contrib non-free non-free-firmware
写完了才发现已经有大佬做了短链接 :
1 2 https://z.tuna.dev/ 比如说这里还有清华站的短链 https://m.tuna.dev/
之后有空的话,或许可以继续改写这个脚本,比如在重定向前先判断目标镜像站是否有对应的镜像链接,如果没有则切换到另一个。