用订阅文件搭建桌面客户端
本示例把 etemenanki-app 变成你自己电脑上的代理客户端。服务商给你一份订阅文件,其中列出节点、DNS 设置和路由组,例如“Streaming”或“Ads”。你自己的文件加上两个本地代理(一个 SOCKS5 端口和一个 HTTP 端口),并指定每个路由组使用哪个节点。回环地址上的一个小型 HTTP API 让脚本或托盘程序可以在代理运行期间切换这些选择。
如果服务商提供的是 etemenanki 订阅文件,并且你希望在服务商更新文件时保留自己的选择,就可以使用本示例。文件格式本身见订阅文件;API 见 REST API。
flowchart LR APP["浏览器及其他应用"] -->|"SOCKS5 :1080、HTTP :8080"| C["etemenanki-app"] C -->|"路由组:Streaming、Proxy……"| N["服务商的节点"] C -->|"Local、Ads"| D["direct / blackhole"] S["curl、托盘程序"] -->|"API :9090"| C
- 电脑上装好了 etemenanki-app,见安装。本页使用 Linux 路径;只要修改文件中的路径,放在任何目录都可以。
- 服务商的订阅文件,保存为
/etc/etemenanki/subscribe.toml。 - 服务商的路由组用到
geoip或geosite匹配条件时,需要 v2fly 的geoip.dat和geosite.dat。获取方式见分流路由。 - 用于 API 步骤的
curl。
# A desktop client: SOCKS5 and HTTP proxies on this machine, nodes and route# groups from a provider's subscribe file, and a local API to switch which# node a route group uses.## Put the provider's file at the path in [subscribe]. Saving either file# reloads; so does every route switch through the API.
[[inbound]]tag = "socks-in"protocol = "socks"listen = "127.0.0.1"port = 1080
[[inbound]]tag = "http-in"protocol = "http"listen = "127.0.0.1"port = 8080
# Routing comes from the subscribe file's route groups. [route] names the# geodata files those groups use.[route]geoip = "/etc/etemenanki/geoip.dat"geosite = "/etc/etemenanki/geosite.dat"
[subscribe]path = "/etc/etemenanki/subscribe.toml"
# The API rewrites these picks. A group without a pick uses what it# inherits in the subscribe file.[subscribe.routes]default = "vless-ws"
# Loopback only and no secret: every program on this machine may switch# routes, nothing else can reach it.[api]listen = "127.0.0.1:9090"# A subscribe file, as a provider hands it out. It lists the nodes# ([[proxy_server]]), a DNS setup ([dns]) and route groups ([[route_group]]).# Every host, address, key and password here is a placeholder.
# ---------------------------------------------------------------------------# Nodes. Each becomes an outbound tagged with its name.# ---------------------------------------------------------------------------[[proxy_server]]name = "vless-ws"server = "proxy1.example.com"port = 443protocol = "vless"id = "11111111-2222-3333-4444-555555555555"# WebSocket with TLS; the TLS name defaults to the server.stream = { network = "ws", path = "/ws", tls = {} }
[[proxy_server]]name = "trojan-tls"server = "proxy2.example.com"port = 443protocol = "trojan"password = "replace-with-a-long-random-password"# TLS over plain TCP.stream = { network = "tcp", tls = {} }
[[proxy_server]]name = "ss-2022"server = "192.0.2.10"port = 8388protocol = "shadowsocks"method = "2022-blake3-aes-128-gcm"password = "AAAAAAAAAAAAAAAAAAAAAA=="
[[proxy_server]]name = "hy2"server = "proxy3.example.com"port = 443protocol = "hysteria2"password = "replace-with-a-long-random-password"obfs = { type = "salamander", password = "replace-with-a-long-random-obfs-key" }# Port hopping across the ports the server forwards, and the client's rates.ports = "443,20000-30000"hop_interval = 30up = "50 mbps"down = "200 mbps"
[[proxy_server]]name = "wg"server = "2001:db8::10"port = 51820protocol = "wireguard"private_key = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="peer_public_key = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="address = ["10.0.0.2", "2001:db8:a::2"]
# ---------------------------------------------------------------------------# DNS. When present, every key is required.# ---------------------------------------------------------------------------[dns]# Asked directly: the nodes' host names, and the name of dns.example.com below.pre_proxy = ["udp://192.0.2.53:53"]# Asked through the route table: everything else.through_proxy = ["https://dns.example.com/dns-query"]use_hosts = true# Reach a node over IPv4 when its name has both A and AAAA records.connect_ipv6 = "tolerated"resolve_ipv6 = true
# ---------------------------------------------------------------------------# Route groups, matched from top to bottom. A group sends what it matches to# the node the user picks for it, or else to what it inherits.# ---------------------------------------------------------------------------[[route_group]]name = "Ads"inherit = "blackhole"matches = [{ geosite = ["category-ads-all"] }]
[[route_group]]name = "Streaming"inherit = { group = "Proxy" }matches = [{ geosite = ["netflix"] }]
[[route_group]]name = "Local"inherit = "direct"matches = [{ geoip = ["private"] }, { domain_suffix = ["lan"] }]
[[route_group]]name = "Proxy"matches = [{ domain_suffix = ["example.org"] }, { network = ["udp"] }]这样拆分是有意为之。服务商的文件包含随服务商变化的一切:节点、DNS 设置和路由组。你的文件包含属于你的部分:本地端口、geodata 路径、你的选择和 API。服务商发来新文件时,你替换他们的文件,保留自己的文件。
两个文件如何组合:
- 每个节点都成为一个出站,tag 就是节点的名称:这里是
vless-ws、trojan-tls、ss-2022、hy2和wg。 - 路由组就是路由表。 流会发往第一个
matches匹配它的路由组。设置了[subscribe]时,你自己写的[[route.rule]]条目(如果有)会被忽略。 - 路由组使用你的选择:如果
[subscribe.routes]中为它写了一项,就用它;否则使用该组继承的目标:另一个路由组({ group = "Proxy" })、direct、blackhole或default。 default是所有路由组都不匹配的流量所用的选择。 这里是vless-ws,Proxy组继承它。
-
把两个文件放到位并检查。
--test也会读取订阅文件:终端窗口 etemenanki-app --test -c /etc/etemenanki/config.tomlConfiguration OK. -
启动代理。 可以在前台运行,也可以使用生产环境运行中的单元:
终端窗口 etemenanki-app -c /etc/etemenanki/config.tomlINFO etemenanki_supervisor::system::listener: inbound socks-in listening on 127.0.0.1:1080INFO etemenanki_supervisor::system::listener: inbound http-in listening on 127.0.0.1:8080INFO etemenanki_app::instance: config loaded: built [dns, outbound vless-ws@v1, outbound trojan-tls@v1, outbound ss-2022@v1, outbound hy2@v1, outbound wg@v1, outbound direct@v1, outbound blackhole@v1, route, inbound socks-in, inbound http-in]INFO etemenanki_app: API listening on 127.0.0.1:9090direct和blackhole之所以作为出站出现,是因为路由组引用了它们;当你的出站中没有这两个 tag 时,etemenanki-app 会自行创建。 -
让应用程序使用它。 应用程序接受 SOCKS5 代理时,使用
socks5h://127.0.0.1:1080,这样它发送的是域名而不是地址,域名匹配条件才能看到域名;接受 HTTP 代理时,使用http://127.0.0.1:8080。在 shell 中:终端窗口 curl --socks5-hostname 127.0.0.1:1080 https://example.com
查看每个路由组的去向
Section titled “查看每个路由组的去向”GET /v1/routes 显示每个路由组、它的选择、它继承的目标,以及它的流量现在的去向:
curl -s http://127.0.0.1:9090/v1/routes{ "default": { "pick": "vless-ws", "target": "vless-ws" }, "groups": [ { "name": "Ads", "pick": null, "inherit": "blackhole", "target": "blackhole" }, { "name": "Streaming", "pick": null, "inherit": { "group": "Proxy" }, "target": "vless-ws" }, { "name": "Local", "pick": null, "inherit": "direct", "target": "direct" }, { "name": "Proxy", "pick": null, "inherit": "default", "target": "vless-ws" } ], "targets": [ { "name": "vless-ws", "kind": "node" }, { "name": "trojan-tls", "kind": "node" }, { "name": "ss-2022", "kind": "node" }, { "name": "hy2", "kind": "node" }, { "name": "wg", "kind": "node" }, { "name": "direct", "kind": "direct" }, { "name": "blackhole", "kind": "blackhole" } ]}Streaming 没有选择,所以跟随 Proxy,而 Proxy 跟随 default:它的流量发往 vless-ws。targets 列出选择可以使用的所有名称。
改为通过 Hysteria 2 节点发送流媒体流量:
curl -s -X PUT -H 'Content-Type: application/json' \ -d '{"target": "hy2"}' http://127.0.0.1:9090/v1/routes/Streaming{"status":"applied","built":["route"],"swapped":[],"drained":[],"rebound":[],"restarted":[],"removed":[],"reused":["dns","outbound vless-ws@v1","outbound trojan-tls@v1","outbound ss-2022@v1","outbound hy2@v1","outbound wg@v1","outbound direct@v1","outbound blackhole@v1","inbound socks-in","inbound http-in"]}API 把这个选择写进了你的配置(在 [subscribe.routes] 下新增一行),然后重新加载:
[subscribe.routes]default = "vless-ws"Streaming = "hy2"响应就是重载报告。只有路由表被重新构建;所有节点和两个监听器都被复用,因此没有断开任何连接。已打开的连接留在它开始时所用的节点上,新的流媒体连接发往 hy2。若要關閉這次切換移走的開啟中連線,讓它們的應用程式透過 hy2 重新連線,請在請求主體加上 "close": true;見切換路由。由于选择保存在文件中,它在重启后依然有效,你也可以直接编辑文件来修改它。
要让路由组恢复使用继承的目标,从 [subscribe.routes] 中删除它那一行并保存文件。路径中带空格的路由组名需要做百分号编码,例如 /v1/routes/Video%20Streaming。
GET /v1/traffic 是一个事件流,每秒一个事件:自启动以来发送和接收的字节数、最近一秒的速率,以及活动流的数量,既有总计,也按入站和出站 tag 分列:
curl -sN http://127.0.0.1:9090/v1/trafficevent: trafficdata: {"tick":2,"interval_ms":1001,"up":0,"down":0,"up_rate":0,"down_rate":0,"flows":0,"inbounds":{},"outbounds":{}}GET /v1/connections 列出活动连接,DELETE /v1/connections/{flow} 关闭其中一个;见端点。
服务商更新文件时
Section titled “服务商更新文件时”一次性把新的订阅文件覆盖旧文件:先写到旧文件旁边,再重命名覆盖,这样代理永远不会读到写了一半的文件。etemenanki-app 会监视订阅文件所在的目录并重新加载。DNS 设置没有变化时,设置未变的节点继续运行;有变化的节点会重新构建,新连接使用新的设置。
选择必须始终指向存在的东西。如果新文件重命名或删除了你选择的节点,或者你为之写了选择的路由组,整次重载都会被拒绝,代理保持原样继续运行:
ERROR etemenanki_app::instance: reload: subscribe: [subscribe.routes] "Streaming" = "hy2": no node, outbound or balancer has that name; keeping the running config在你的文件中修正或删除这个选择,下一次保存就会加载两个文件。
下列每种错误在 --test 下出现在 configuration invalid: 之后,启动时出现在 failed to start: 之后,重载时出现在 reload: …; keeping the running config 这一行中。API 无法完成切换时,也会在 {"error": …} 中给出同样的消息。
| 消息 | 原因 | 解决方法 |
|---|---|---|
subscribe file /etc/etemenanki/subscribe.toml: No such file or directory (os error 2) |
订阅文件不在 [subscribe] path 所写的位置 |
把它保存到那里,或修正路径 |
subscribe: [subscribe.routes] names "Video", which is not a route group in … |
为订阅文件中不存在的路由组写了选择,通常是服务商给它改了名 | 重命名或删除这个选择 |
subscribe: [subscribe.routes] "Streaming" = "hy3": no node, outbound or balancer has that name |
选择指向了不存在的节点 | 从 targets 中选一个名称 |
[api] listen 0.0.0.0:9090: an API reachable from other hosts needs a secret; listen on a loopback address or a unix socket, or set one |
API 在回环地址以外监听,却没有设置 secret | 让它留在 127.0.0.1,或设置 secret |
failed to start: API on 127.0.0.1:9090: Address already in use (os error 98) |
另一个程序占用了 API 端口 | 在 [api] listen 中换一个端口 |
building route failed: No such file or directory (os error 2) |
路由组需要的某个 geodata 文件不存在 | 把它下载到 [route] 中的路径 |
WARN … [subscribe] is set, so the config's 1 [[route.rule]] entries are ignored; … |
你的文件中仍有 [[route.rule]] 条目 |
删除它们;路由由路由组负责 |
API 自身的响应:
| 响应 | 原因 |
|---|---|
404 {"error":"no route group named \"Video\""} |
路径中的路由组不存在 |
422 {"error":"no node, outbound or balancer is named \"hy3\"; the route view lists them"} |
目标不在 targets 中 |
409 |
文件在你通过 If-Match 发送的 ETag 之后发生了变化 |
文件格式本身的错误见订阅文件。