{"id":13775898,"url":"https://github.com/gxfxyz/unblockchn","last_synced_at":"2025-05-11T08:34:24.318Z","repository":{"id":43932888,"uuid":"156122226","full_name":"gxfxyz/unblockchn","owner":"gxfxyz","description":"Unblock CHN - Shadowsocks 回国解锁代理分流配置工具 (路由器 \u0026 Surge) - Unblock Youku 衍生工具","archived":false,"fork":false,"pushed_at":"2022-12-08T04:50:25.000Z","size":403,"stargazers_count":160,"open_issues_count":1,"forks_count":39,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-08-03T17:12:20.656Z","etag":null,"topics":["asuswrt-merlin","shadowsocks","surge"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"agpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gxfxyz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-11-04T20:32:35.000Z","updated_at":"2024-07-30T13:14:59.000Z","dependencies_parsed_at":"2023-01-24T13:00:12.787Z","dependency_job_id":null,"html_url":"https://github.com/gxfxyz/unblockchn","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gxfxyz%2Funblockchn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gxfxyz%2Funblockchn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gxfxyz%2Funblockchn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gxfxyz%2Funblockchn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gxfxyz","download_url":"https://codeload.github.com/gxfxyz/unblockchn/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225031361,"owners_count":17410049,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asuswrt-merlin","shadowsocks","surge"],"created_at":"2024-08-03T17:01:53.977Z","updated_at":"2024-11-17T11:30:32.700Z","avatar_url":"https://github.com/gxfxyz.png","language":"Python","funding_links":[],"categories":["\u003ca id=\"d03d494700077f6a65092985c06bf8e8\"\u003e\u003c/a\u003e工具"],"sub_categories":["\u003ca id=\"cb16466a31a167bb61f39e2a4a85f449\"\u003e\u003c/a\u003eShadowsocks"],"readme":"# Unblock CHN\n\nUnblock CHN 是一个帮助配置 Shadowsocks 回国解锁代理分流的命令行小工具，实现通过路由器或 Surge 自动分流，将国内一些网站的访问通过 Shadowsocks 代理回国，用以解除这些网站的海外访问限制，其它流量则会正常直连不走回国代理。\n\n当然前提是你需要有一台位于国内的 Shadowsocks 服务器，在国内的路由器上[部署 Shadowsocks 服务器端](https://github.com/gxfxyz/unblockchn/wiki/在华硕梅林固件（Asuswrt-Merlin）路由器上部署-Shadowsocks-服务器端（ss-server）)也是可行的。\n\n分流规则提取自 [Unblock Youku](https://github.com/uku/Unblock-Youku)，Unblock Youku 的规则中一般只包含站点用于检测的地址 ，不包含实际音视频流的地址，因此大部分情况下音视频流可以直连不用走代理，这样可以避免音视频变慢，也可以有效节约代理服务器的流量。\n\n### Unblock CHN 的功能分为两部分：\n\n* [路由器](#%E8%B7%AF%E7%94%B1%E5%99%A8)\n    + [原理](#%E5%8E%9F%E7%90%86)\n    + [安装](#%E5%AE%89%E8%A3%85)\n    + [使用](#%E4%BD%BF%E7%94%A8)\n        - [一键配置路由器](#%E4%B8%80%E9%94%AE%E9%85%8D%E7%BD%AE%E8%B7%AF%E7%94%B1%E5%99%A8)\n        - [查看代理状态](#%E6%9F%A5%E7%9C%8B%E4%BB%A3%E7%90%86%E7%8A%B6%E6%80%81)\n        - [关闭代理](#%E5%85%B3%E9%97%AD%E4%BB%A3%E7%90%86)\n        - [开启代理](#%E5%BC%80%E5%90%AF%E4%BB%A3%E7%90%86)\n        - [列出代理服务器](#%E5%88%97%E5%87%BA%E4%BB%A3%E7%90%86%E6%9C%8D%E5%8A%A1%E5%99%A8)\n        - [切换代理服务器](#%E5%88%87%E6%8D%A2%E4%BB%A3%E7%90%86%E6%9C%8D%E5%8A%A1%E5%99%A8)\n        - [检查 \u003cURL/IP/域名\u003e 是否走代理](#%E6%A3%80%E6%9F%A5-urlip%E5%9F%9F%E5%90%8D-%E6%98%AF%E5%90%A6%E8%B5%B0%E4%BB%A3%E7%90%86)\n        - [更新规则](#%E6%9B%B4%E6%96%B0%E8%A7%84%E5%88%99)\n        - [还原路由器为未配置状态](#%E8%BF%98%E5%8E%9F%E8%B7%AF%E7%94%B1%E5%99%A8%E4%B8%BA%E6%9C%AA%E9%85%8D%E7%BD%AE%E7%8A%B6%E6%80%81)\n        - [仅生成 ipset 和 dnsmasq 规则配置文件](#%E4%BB%85%E7%94%9F%E6%88%90-ipset-%E5%92%8C-dnsmasq-%E8%A7%84%E5%88%99%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6)\n        - [修改规则模板](#%E4%BF%AE%E6%94%B9%E8%A7%84%E5%88%99%E6%A8%A1%E6%9D%BF)\n    + [远程控制工具](#%E8%BF%9C%E7%A8%8B%E6%8E%A7%E5%88%B6%E5%B7%A5%E5%85%B7)\n        - [iOS 捷径（Shortcut）](#ios-%E6%8D%B7%E5%BE%84shortcut)\n        - [Alfred Workflow](#alfred-workflow)\n* [Surge](#surge)\n    + [现成配置](#%E7%8E%B0%E6%88%90%E9%85%8D%E7%BD%AE)\n        - [配置文件](#%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6)\n        - [RULESET 文件](#ruleset-%E6%96%87%E4%BB%B6)\n    + [安装](#%E5%AE%89%E8%A3%85-1)\n    + [使用](#%E4%BD%BF%E7%94%A8-1)\n        - [准备模板](#%E5%87%86%E5%A4%87%E6%A8%A1%E6%9D%BF)\n        - [生成配置文件](#%E7%94%9F%E6%88%90%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6)\n        - [指定目录](#%E6%8C%87%E5%AE%9A%E7%9B%AE%E5%BD%95)\n        - [生成基于 URL 正则表达式的规则](#%E7%94%9F%E6%88%90%E5%9F%BA%E4%BA%8E-url-%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E7%9A%84%E8%A7%84%E5%88%99)\n        - [生成 RULESET](#%E7%94%9F%E6%88%90-ruleset)\n\n---\n\n## 路由器\n\n目前仅支持[华硕梅林固件（Asuswrt-Merlin）](https://asuswrt.lostrealm.ca)，其它支持 ss-redir + iptables + ipset + dnsmasq 的路由器固件或许也可以使用，但可能需要修改部分配置或代码。\n\n### 原理\n\n1. 从 Unblock Youku 的 [urls.js](https://github.com/uku/Unblock-Youku/blob/master/shared/urls.js) 中提取分流规则。\n\n2. 根据分流规则生成 dnsmasq 和 ipset 规则，将需要回国代理的 IP 地址加入 chn ipset。\n\n3. 添加 iptables 规则，将属于 chn ipset 的请求转发到 Shadowsocks 透明代理工具 ss-redir 的端口，通过 Shadowsocks 代理回国。\n\nUnblock CHN 自动化以上过程，提供了一键配置路由器的命令和一些管理命令。\n\n### 安装\n\n1. 在路由器上插入一个 U 盘，用来安装 Unblock CHN 和相关程序。\n\n2. [在路由器上安装 Entware](https://github.com/gxfxyz/unblockchn/wiki/在华硕梅林固件（Asuswrt-Merlin）路由器上安装-Entware)。\n\n3. 安装依赖程序：\n\n```console\n# Python3\n$ opkg install python3\n\n# pip3\n$ opkg install python3-pip\n\n# Git\n$ opkg install git-http\n\n# Shadowsocks 透明代理工具 ss-redir\n$ opkg install shadowsocks-libev-ss-redir\n```\n\n4. 安装 Unblock CHN：\n\n```console\n# 进入 U 盘目录（例如 /tmp/mnt/sda1）\n$ cd /tmp/mnt/sda1\n\n# 安装 Unblock CHN\n$ git clone https://github.com/gxfxyz/unblockchn.git\n\n# 进入 Unblock CHN 目录\n$ cd unblockchn\n\n# 安装 Unblock CHN 依赖\n$ pip3 install -r requirements.txt\n```\n\n### 使用\n\n```console\n$ python3 unblockchn.py router --help\nusage: python3 unblockchn.py router [-h] {status,on,off,servers,switch,check,renew,setup,restore,create}\n\nUnblock CHN 路由器命令：\n  status                    查看代理状态\n  on                        开启代理\n  off                       关闭代理\n  servers [--json]          列出代理服务器 [--json: 输出 json 格式]\n  switch [name] [--auto]    切换代理服务器 [--auto: 自动选择延迟最低的代理服务器]\n  check \u003cURL/IP/域名\u003e       检查 \u003cURL/IP/域名\u003e 是否走代理\n  renew                     更新规则\n  setup [--no-ss]           一键配置路由器 [--no-ss: 跳过配置 ss-redir]\n  restore [--no-ss]         还原路由器为未配置状态 [--no-ss: 跳过还原 ss-redir]\n  create                    仅生成 ipset 和 dnsmasq 规则配置文件\n\npositional arguments:\n  {status,on,off,servers,switch,check,renew,setup,restore,create}\n\noptional arguments:\n  -h, --help            show this help message and exit\n```\n\n#### 一键配置路由器\n\n```console\n$ python3 unblockchn.py router setup\nShadowsocks 服务器地址：180.160.0.1\nShadowsocks 服务器端口：1111\nShadowsocks 密码：xxxxxxxxxx\nShadowsocks 加密方法：rc4-md5\n命名此代理服务器为：Shanghai\n✔ 保存 ss-redir 配置文件：/tmp/mnt/sda1/unblockchn/shadowsocks/Shanghai.json\n是否添加更多代理服务器？[y/N]：y\nShadowsocks 服务器地址：beijing.asuscomm.com\nShadowsocks 服务器端口：2222\nShadowsocks 密码：xxxxxxxxxx\nShadowsocks 加密方法：aes-128-cfb\n命名此代理服务器为：Beijing\n✔ 保存 ss-redir 配置文件：/tmp/mnt/sda1/unblockchn/shadowsocks/Beijing.json\n是否添加更多代理服务器？[y/N]：n\n0) 自动选择延迟最低的代理服务器\n1) Shanghai [180.160.0.1:1111]\n2) Beijing [beijing.asuscomm.com:2222]\n请选择要使用的代理服务器 [0-2]：1\n使用 Shanghai 代理服务器\n✔ 启动 ss-redir（Shanghai 代理服务器）：/opt/bin/ss-redir -c /tmp/mnt/sda1/unblockchn/shadowsocks/Shanghai.json -f /tmp/mnt/sda1/unblockchn/shadowsocks/ss-redir.pid\n✔ 保存 ss-redir 启动命令到路由器的 services-start 启动脚本中：/jffs/scripts/services-start\n✔ 生成 ipset 配置文件（configs 目录）：ipset.rules \u0026 ipset.headless.rules\n✔ 生成 dnsmasq 配置文件（configs 目录）：dnsmasq.conf.add\n✔ 复制：/tmp/mnt/sda1/unblockchn/configs/ipset.rules -\u003e /jffs/configs/ipset.rules\n✔ 复制：/tmp/mnt/sda1/unblockchn/configs/dnsmasq.conf.add -\u003e /jffs/configs/dnsmasq.conf.add\n✔ 加载 xt_set 模块：modprobe xt_set\n✔ 保存 xt_set 模块加载命令到路由器的 services-start 启动脚本中：/jffs/scripts/services-start\n✔ 载入 ipset 规则：ipset restore \u003c /jffs/configs/ipset.rules\n✔ 保存 ipset 载入命令到路由器的 nat-start 启动脚本中：/jffs/scripts/nat-start\n✔ 添加 iptables 规则：iptables -t nat -A PREROUTING -p tcp -m set --match-set chn dst -j REDIRECT --to-port 1080\n✔ 保存 iptables 规则添加命令到路由器的 nat-start 启动脚本中：/jffs/scripts/nat-start\n✔ 重启 dnsmasq：service restart_dnsmasq\n✔ 定时每日 3 点更新规则：cru a unblockchn_renew \"0 3 * * * /opt/bin/python3 /tmp/mnt/sda1/unblockchn/unblockchn.py router renew\"\n✔ 保存定时更新规则命令到路由器的 services-start 启动脚本中：/jffs/scripts/services-start\n配置成功\n```\n\n如果想要跳过配置 ss-redir，那么就加上 `--no-ss` 参数：\n\n```console\n$ python3 unblockchn.py router setup --no-ss\n```\n\n至此，回国代理和自动分流就配置并开启好了。\n\n可以访问下列地址以验证回国代理是否成功，如果显示 `true`，就说明回国代理已生效：\n\nhttp://uku.im/check\n\n#### 查看代理状态\n\n```console\n$ python3 unblockchn.py router status\n已开启 (Shanghai)\n```\n\n#### 关闭代理\n\n```console\n$ python3 unblockchn.py router off\n关闭成功\n```\n\n#### 开启代理\n\n```console\n$ python3 unblockchn.py router on\n开启成功 (Shanghai)\n```\n\n#### 列出代理服务器\n\n```console\n$ python3 unblockchn.py router servers\n共有 2 个代理服务器：\n1) Beijing\nShadowsocks 服务器地址：beijing.asuscomm.com\nShadowsocks 服务器端口：2222\nShadowsocks 密码：xxxxxxxxxx\nShadowsocks 加密方法：aes-128-cfb\n2) Shanghai\nShadowsocks 服务器地址：180.160.0.1\nShadowsocks 服务器端口：1111\nShadowsocks 密码：xxxxxxxxxx\nShadowsocks 加密方法：rc4-md5\n-\n目前使用的代理服务器为：Shanghai\n```\n\n如果加上 `--json` 参数，则会输出 json 格式：\n\n```console\n$ python3 unblockchn.py router servers --json\n{\n    \"Beijing\": {\n        \"server\": \"beijing.asuscomm.com\",\n        \"server_port\": 2222,\n        \"local_address\": \"0.0.0.0\",\n        \"local_port\": 1080,\n        \"password\": \"xxxxxxxxxx\",\n        \"timeout\": 300,\n        \"method\": \"aes-128-cfb\",\n        \"fast_open\": false,\n        \"mode\": \"tcp_and_udp\"\n    },\n    \"Shanghai\": {\n        \"server\": \"180.160.0.1\",\n        \"server_port\": 1111,\n        \"local_address\": \"0.0.0.0\",\n        \"local_port\": 1080,\n        \"password\": \"xxxxxxxxxx\",\n        \"timeout\": 300,\n        \"method\": \"rc4-md5\",\n        \"fast_open\": false,\n        \"mode\": \"tcp_and_udp\",\n        \"selected\": true\n    }\n}\n```\n\nss-redir 代理服务器配置文件保存在 `shadowsocks` 子目录下，你可以手动删改。\n\n#### 切换代理服务器\n\n```console\n$ python3 unblockchn.py router switch\n0) 自动选择延迟最低的代理服务器\n1) Beijing [beijing.asuscomm.com:2222]\n2) Shanghai [180.160.0.1:1111]\n请选择要使用的代理服务器 [0-2]：1\n✔ 停止 ss-redir：kill 21810\n✔ 启动 ss-redir（Beijing 代理服务器）：/opt/bin/ss-redir -c /tmp/mnt/sda1/unblockchn/shadowsocks/Beijing.json -f /tmp/mnt/sda1/unblockchn/shadowsocks/ss-redir.pid\n切换到了 Beijing 代理服务器\n```\n\n或者直接输入代理服务器名来切换：\n\n```console\n$ python3 unblockchn.py router switch Shanghai\n✔ 停止 ss-redir：kill 21987\n✔ 启动 ss-redir（Shanghai 代理服务器）：/opt/bin/ss-redir -c /tmp/mnt/sda1/unblockchn/shadowsocks/Shanghai.json -f /tmp/mnt/sda1/unblockchn/shadowsocks/ss-redir.pid\n切换到了 Shanghai 代理服务器\n```\n\n你可以使用 `--auto` 参数，让 Unblock CHN 自动选择延迟最低的代理服务器：\n\n```console\n$ python3 unblockchn.py router switch --auto\n1) Beijing [beijing.asuscomm.com:2222]: 237 ms\n2) Shanghai [180.160.0.1:1111]: 523 ms\n✔ 停止 ss-redir：kill 22584\n✔ 启动 ss-redir（Beijing 代理服务器）：/opt/bin/ss-redir -c /tmp/mnt/sda1/unblockchn/shadowsocks/Beijing.json -f /tmp/mnt/sda1/unblockchn/shadowsocks/ss-redir.pid\n切换到了 Beijing 代理服务器（237 ms）\n```\n\n#### 检查 \u003cURL/IP/域名\u003e 是否走代理\n\n```console\n$ python3 unblockchn.py router check http://ipservice.163.com/isFromMainland\n59.111.19.7 走代理\n\n$ python3 unblockchn.py router check https://google.com\n216.58.193.78 不走代理\n\n$ python3 unblockchn.py router check www.bilibili.com\n148.153.45.166 走代理\n\n$ python3 unblockchn.py router check 192.168.2.1\n192.168.2.1 不走代理\n```\n\n#### 更新规则\n\n```console\n$ python3 unblockchn.py router renew\n✔ 生成 ipset 配置文件（configs 目录）：ipset.rules \u0026 ipset.headless.rules\n✔ 生成 dnsmasq 配置文件（configs 目录）：dnsmasq.conf.add\n✔ 复制：/tmp/mnt/sda1/unblockchn/configs/ipset.rules -\u003e /jffs/configs/ipset.rules\n✔ 复制：/tmp/mnt/sda1/unblockchn/configs/dnsmasq.conf.add -\u003e /jffs/configs/dnsmasq.conf.add\n✔ 清空 ipset 的 chn 表：ipset flush chn\n✔ 载入 ipset 规则：ipset restore \u003c /tmp/mnt/sda1/unblockchn/configs/ipset.headless.rules\n✔ 重启 dnsmasq：service restart_dnsmasq\n更新成功\n```\n\nUnblock CHN 在路由器上默认定时每日 03:00 自动更新分流规则，及时跟进 Unblock Youku 规则的变化。\n\n#### 还原路由器为未配置状态\n\n```console\n$ python3 unblockchn.py router restore\n✔ 停止 ss-redir：kill 24427\n✔ 从启动脚本里移除 ss-redir 启动命令：/jffs/scripts/services-start\n✔ 删除：/jffs/configs/ipset.rules\n✔ 从启动脚本里移除 ipset 载入命令：/jffs/scripts/nat-start\n✔ 删除：/jffs/configs/dnsmasq.conf.add\n✔ 删除 iptables 规则：iptables -t nat -D PREROUTING -p tcp -m set --match-set chn dst -j REDIRECT --to-port 1080\n✔ 从启动脚本里移除 iptables 规则添加命令：/jffs/scripts/nat-start\n✔ 删除 ipset 的 chn 表：ipset destroy chn\n✔ 删除每日更新规则的 cron 定时任务：cru d unblockchn_renew\n✔ 从启动脚本里移除定时命令：/jffs/scripts/services-start\n✔ 从启动脚本里移除 xt_set 模块加载命令：/jffs/scripts/services-start\n✔ 重启 dnsmasq：service restart_dnsmasq\n还原成功\n```\n\n如果想要跳过还原 ss-redir，那么就加上 `--no-ss` 参数：\n\n```console\n$ python3 unblockchn.py router restore --no-ss\n```\n\n#### 仅生成 ipset 和 dnsmasq 规则配置文件\n\n```console\n$ python3 unblockchn.py router create\n✔ 生成 ipset 默认配置模板文件（configs 目录）：ipset.rules.tpl\n✔ 生成 ipset 配置文件（configs 目录）：ipset.rules \u0026 ipset.headless.rules\n✔ 生成 dnsmasq 默认配置模板文件（configs 目录）：dnsmasq.conf.add.tpl\n✔ 生成 dnsmasq 配置文件（configs 目录）：dnsmasq.conf.add\n生成配置文件成功\n```\n\n此命令让 Unblock CHN 跳过配置路由器，仅提取 Unblock Youku 的规则，在 `configs` 子目录下生成相应的 ipset 和 dnsmasq 规则配置文件。\n\n#### 修改规则模板\n\n除了 Unblock CHN 自动生成的规则以外，如果你需要自定义一些 dnsmasq 或 ipset 规则，可以通过修改 `configs` 子目录下的规则模板文件 `dnsmasq.conf.add.tpl` 和 `ipset.rules.tpl` 来实现。\n\n保留模板文件中的 `{rules}` 一行，其在生成规则时会被 Unblock CHN 规则替换，然后在模板文件中添加你需要的规则，例如：\n\ndnsmasq.conf.add.tpl\n```\n# NAS\naddress=/nas.xxxx.com/192.168.1.100\n# CCTV\naddress=/cctv.xxxx.com/192.168.1.200\n{rules}\n```\n\nipset.rules.tpl\n```\n{rules}\ncreate blacklist hash:ip family inet hashsize 1024 maxelem 65536\nadd blacklist 103.31.6.5\nadd blacklist 208.73.51.100\n```\n\n运行更新规则命令来使自定义的规则生效：\n\n```console\n$ python3 unblockchn.py router renew\n```\n\n### 远程控制工具\n\n#### iOS 捷径（Shortcut）\n\n在 iOS 的通知中心里远程（局域网内）控制路由器上的 Unblock CHN 代理。\n\n![unblockchn_shortcut](https://user-images.githubusercontent.com/43481676/56479612-28c83b00-646b-11e9-8a06-476e8eaa017f.jpg)\n\n[点此安装（版本：2020.04.04）](https://www.icloud.com/shortcuts/2214d1c7c80f4918922d1e801c5b0210)\n\n#### Alfred Workflow\n\n在 macOS 上用 Alfred 来远程（局域网内）控制路由器上的 Unblock CHN 代理。\n\n![unblockchn_alfred](https://user-images.githubusercontent.com/43481676/56479251-4b595480-6469-11e9-8c60-3b3d80ed4350.png)\n\n[点此下载（版本：2020.04.04）](https://github.com/gxfxyz/unblockchn/raw/master/unblockchn.alfredworkflow)\n\n**关键词：**\n\nunblockchn\n\n**设置：**\n\n使用前需要先设置路由器 SSH 免密码登录：\n\n1. 生成密钥：\n\n```console\n$ ssh-keygen -t rsa\n```\n\n2. 查看密钥：\n\n```console\n$ cat ~/.ssh/id_rsa.pub\n```\n\n3. 复制密钥到路由器管理页面：\n\n```\n系统管理 - 系统设置 - 服务 - 授权密钥\n```\n\n除此之外，在 Workflow 设置里点 [x] 按钮可以设置一些参数：\n\n- router_ip:  路由器 IP 地址（默认 `192.168.1.1`）\n- router_user:  路由器用户名（默认 `admin`）\n- unblockchn_py_path:  unblockchn.py 在路由器上的路径（默认 `/tmp/mnt/sda1/unblockchn/unblockchn.py`）\n\n---\n\n## Surge\n\nUnblock CHN 从 Unblock Youku 提取分流规则，生成相应的 Surge 规则配置文件，供 [Surge](https://itunes.apple.com/us/app/id1329879957) 或 [Shadowrocket](https://itunes.apple.com/us/app/shadowrocket/id932747118) 使用，实现在 iOS 或 macOS 上解除国内网站的海外访问限制。\n\n### 现成配置\n\n懒得亲自运行 Unblock CHN？你可以直接使用下列 Unblock CHN 已经生成好的 Surge 配置文件或 ruleset 文件。\n\n如果 Unblock CHN 的规则有变化，下列文件会定时自动更新。\n\n#### 配置文件\n\n[unblockchn.surge.conf](https://gist.github.com/gxfxyz/0d0d91c526a6b07f59a700039f9fa334#file-unblockchn-surge-conf)\n\n[下载](https://gist.githubusercontent.com/gxfxyz/0d0d91c526a6b07f59a700039f9fa334/raw/22b3b052b145ebd3c0e6b6b3726c3bdac4c7bc0b/unblockchn.surge.conf)后修改代理服务器信息就可使用。\n\n若使用的是 Shadowrocket，可以分开设置代理节点，然后通过 [URL](https://gist.githubusercontent.com/gxfxyz/0d0d91c526a6b07f59a700039f9fa334/raw/22b3b052b145ebd3c0e6b6b3726c3bdac4c7bc0b/unblockchn.surge.conf) 单独更新配置内的规则。\n\n#### RULESET 文件\n\n[unblockchn.surge.ruleset](https://gist.github.com/gxfxyz/0d0d91c526a6b07f59a700039f9fa334#file-unblockchn-surge-ruleset)\n\n[RULESET](https://nssurge-english.zendesk.com/hc/en-us/articles/360010493933-Surge-Mac-3-Release-Note) 是 Surge 3 新加入的功能（Shadowrocket 不支持）。\n\nRULESET 让 Surge 配置文件可以通过路径或 URL 来引用外部规则文件，例如你可以这样引用 Unblock CHN 的 ruleset：\n\n```\nRULE-SET,https://git.io/fxjWu,PROXY\n```\n\n而完整配置文件就可以简化成这样：\n\n[unblockchn.surge.ruleset.conf](https://gist.github.com/gxfxyz/0d0d91c526a6b07f59a700039f9fa334#file-unblockchn-surge-ruleset-conf)\n\n这样若 Unblock CHN 的规则有更新，你可以通过重载配置文件来更新 RULESET 引用的规则，而无需更新整个配置文件。\n\n### 安装\n\n1. 安装 Unblock CHN：\n\n```console\n$ git clone https://github.com/gxfxyz/unblockchn.git\n```\n\n2. 进入 Unblock CHN 目录：\n\n```console\n$ cd unblockchn\n```\n\n3. 安装 Unblock CHN 依赖：\n\n- 通过 [pipenv](https://github.com/pypa/pipenv)（推荐）\n\n```console\n# 安装依赖并创建虚拟环境\n$ pipenv install\n\n# 激活 pipenv 虚拟环境\n$ pipenv shell\n```\n\n- 通过 pip\n\n```console\n$ pip3 install -r requirements.txt\n```\n\n### 使用\n\n```console\n$ python3 unblockchn.py surge --help\nusage: python3 unblockchn.py surge [-h] [-d DST]\n\nUnblock CHN\n\n生成 Surge 配置文件\n\noptional arguments:\n  -h, --help         show this help message and exit\n  -u, --url          生成基于 URL 正则表达式的规则（默认基于域名）\n  -r, --ruleset      生成 Surge ruleset 文件\n  -d DST, --dst DST  保存生成的文件到此目录\n```\n\n#### 准备模板\n\nUnblock CHN 生成配置时，会在 `surge` 子目录下查找 `.conf.tpl` 后缀的 Surge 配置模板文件，因此你需要先准备至少一个模板文件。\n\n复制样例模板文件 `sample_surge.conf.tpl`，重命名为例如 `surge.conf.tpl`。\n\n用文件编辑器打开 `surge.conf.tpl` ，修改 Shadowrocket 服务器的地址、端口、加密方式、密码等信息。\n\n你也可以加入其它 Surge 规则，但要保留 `{rules}` 一行，其在生成配置时会被 Unblock CHN 规则替换。\n\n#### 生成配置文件\n\n```console\n$ python3 unblockchn.py surge\n✔ 生成 Surge 配置文件（surge 目录）：surge.conf\n```\n\nUnblock CHN 就会在 `surge` 子目录下生成模板文件 `surge.conf.tpl` 对应的 Surge 配置文件 `surge.conf`。\n\nSurge 或 Shadowrocket 载入配置文件后，可以访问下列地址以验证回国代理是否成功，如果显示 `true`，就说明回国代理已生效：\n\nhttp://uku.im/check\n\n#### 指定目录\n\n你可以添加 `-d` 参数，让 Unblock CHN 在生成配置文件后，将其复制到另外一个文件夹，例如 iCloud Drive 或其它网盘文件夹，这样可以方便在手机上更新配置文件：\n\n```console\n$ python3 unblockchn.py surge -d ~/Library/Mobile\\ Documents/iCloud~com~nssurge~inc/Documents\n✔ 生成 Surge 配置文件（surge 目录）：surge.conf\n✔ 保存 Surge 配置文件到：/Users/User/Library/Mobile Documents/iCloud~com~nssurge~inc/Documents/surge.conf\n```\n\n#### 生成基于 URL 正则表达式的规则\n\n你可以使用 `--url` 参数：\n\n```console\n$ python3 unblockchn.py surge --url\n```\n\n让 Unblock CHN 生成基于 URL 正则表达式的 Surge 规则，例如：\n\n```\nURL-REGEX,^http://bangumi\\.bilibili\\.com/api/.*,PROXY\n```\n\n而 Unblock CHN 默认生成的是基于域名的 Surge 规则，例如：\n\n```\nDOMAIN,bangumi.bilibili.com,PROXY\n```\n\n域名规则会让整个域名下的所有 URL 都走回国代理，而 URL 规则只会让匹配正则表达式的 URL 走回国代理。\n\nURL 规则的好处是匹配更准确，站点一般只通过几个特定 URL 来检测用户地区，因此只需要代理这些 URL 就好了，其它网站页面和资源可以直连。但是站点用来检测的 URL 很可能经常会变化，而 Unblock Youku 的 URL 规则往往是滞后的、不完善的，因此容易导致解锁失败。而且 URL 规则只对 HTTP 请求有效，HTTPS 请求因为加密了 URL，无法用 URL 规则来匹配，所以仍需要通过域名规则来代理，现在大部分网站也默认采用 HTTPS，因此 URL 规则实用性不是很好。\n\n相比之下，域名规则虽然会让整个域名下的页面和资源都走回国代理，但是覆盖面广，规则不容易失效。而且站点的音视频流一般通过 CDN 来分发，会使用不同的域名，因此即使使用域名规则，大部分情况下音视频流还是会直连不走回国代理。\n\n所以 Unblock CHN 默认且推荐使用基于域名的规则。\n\n#### 生成 RULESET\n\n你可以使用 `--ruleset` 参数，让 Unblock CHN 生成回国规则的 ruleset 文件 `unblockchn.surge.ruleset`：\n\n```console\n$ python3 unblockchn.py surge --ruleset\n✔ 生成 Surge ruleset 文件（surge 目录）：unblockchn.surge.ruleset\n```\n\n[RULESET 文件的使用](#ruleset-%E6%96%87%E4%BB%B6)\n\n---\n\n## 一些说明\n\n1. Unblock Youku 的规则可能会失效，如果你遇到某个站点无法解锁，可以[向 Unblock Youku 反馈](https://bbs.uku.im)。\n\n2. 因为 Unblock Youku 的规则主要是针对浏览器 Web 端，而一些手机端 App 有可能会采用不同的检测，所以可能需要手动添加一些额外规则才能解锁这些手机端 App。\n\n3. 国内一些网站在海外可能会使用同一个 CDN 服务商，导致不同网站的域名会被解析为相同的 IP，由于 Unblock CHN 在路由器上实质是基于 IP 来分流的，因此可能会造成不需要代理回国的域名被误代理。目前，我发现微博图片的 `ws1.sinaimg.cn` 等域名和 B 站的 `data.bilibili.com` 域名有时会出现这种被解析到相同 IP 的情况，导致微博图片被误代理回国。临时解决办法是用 `renew` 命令来重置下 ipset，微博图片应该就能恢复直连，直到下一次访问 B 站。\n\n4. 更新 Unblock CHN：\n\n   1. 更新前还原路由器为未配置状态：`python3 unblockchn.py router restore`\n\n   2. 更新：`git pull`\n\n   3. 更新后重新配置路由器：`python3 unblockchn.py router setup`\n\n5. `default_config.py` 里有一些配置项，你可以将其复制为 `config.py` 后进行修改，`config.py` 内的配置会覆盖 `default_config.py` 内的配置。\n\n6. 目录下的 `unblockchn.log` 为日志文件，记录了运行过的命令和结果。\n\n7. 本项目衍生自 Unblock Youku，如果你觉得本项目或 Unblock Youku 有用，请考虑[向 Unblock Youku  捐款](https://www.uku.im/) ❤️。\n\n---\n\n## 许可协议\n\n[AGPL v3](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgxfxyz%2Funblockchn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgxfxyz%2Funblockchn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgxfxyz%2Funblockchn/lists"}