fix: http 帳號伺服器每個請求都開新連線才查得到;一個字的名字不能是數字;文件照實寫重試
第四輪(新的獨立審查,沒有 P0/P1): - http 的帳號伺服器 client 不留連線(max_idle_connections 0):每個請求都開新的連線,所以每個請求都在 開連線那一刻查。上一個 commit 說「workers 每一輪也一樣」其實不對——keep-alive 會重用同一條連線;現在才對。 https 照舊。長連線(WebSocket)是每次連上、重連時查,連著的時候不中途重查,文件照這樣寫。 - 一個字的名字要以英文字母開頭、只有字母數字 - _:系統的解析器會把 134744072、0x8080808、01002004010 讀成 8.8.8.8,這些不再當成 Docker 的名字。localhost 只有查到的位址都是 loopback 才算這台。 - phone.md、SECURITY.md、FEATURES.md:登入、還原、配對被擋時回到表單說原因;只有長連線、備用連線和 --serve 的 ANYBOT_ACCOUNT 會自己再試。 已驗證: 完整 cargo test(金鑰清空、DOCKER_HOST 指到不存在)全綠(654 過、47 ignored 沒跑);cargo clippy --bin anybot --all-targets -- -D warnings 沒有警告;sh deploy/ci/lint-sh.sh 過。新測試各回退一次都抓到: 連線池打開(三個請求只開一條連線)、一個字的規則改回「沒有點、不是 IpAddr」、localhost 改回照名字相信。 未驗證: 沒有接真的帳號伺服器(同上一輪的原因);手機頁面行為沒變,這輪沒有重跑 verify-anybot。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
0c250d5a41
commit
cc16b7df0f
|
|
@ -502,7 +502,7 @@ AnyBot 是一個桌面聊天 App,介面和運作方式照 Grok Bot 重做。
|
||||||
- **伺服器上的大腦**收到停止訊號(docker stop、systemd、Ctrl-C)會先停掉 Bot 啟動的東西、關掉連接器、存檔再結束。
|
- **伺服器上的大腦**收到停止訊號(docker stop、systemd、Ctrl-C)會先停掉 Bot 啟動的東西、關掉連接器、存檔再結束。
|
||||||
- **每晚整理**(凌晨三點後):聊天已經不用的附件檔(超過一天、沒有任何訊息、舊對話或頁面提到)、超過保存期限的稽核紀錄、從 Bot 電腦抓檔留下的暫存資料夾。
|
- **每晚整理**(凌晨三點後):聊天已經不用的附件檔(超過一天、沒有任何訊息、舊對話或頁面提到)、超過保存期限的稽核紀錄、從 Bot 電腦抓檔留下的暫存資料夾。
|
||||||
- **區網直連改成 HTTPS**:「同一個網路直接連」的那個埠現在用 https(這台電腦自己做的憑證,存在資料夾裡,800 天後或電腦換了位址會自動重做)。手機第一次打開瀏覽器會問要不要信任,選「繼續」;設定裡顯示憑證的 SHA-256 指紋可以比對。從區網打 http:// 會自動轉到 https://;這台電腦自己,和從 Tailscale 連到這台 Tailscale 通道位址的(本來就加密)仍可用 http。憑證做不出來時退回 http,設定會用紅字提醒沒有加密。
|
- **區網直連改成 HTTPS**:「同一個網路直接連」的那個埠現在用 https(這台電腦自己做的憑證,存在資料夾裡,800 天後或電腦換了位址會自動重做)。手機第一次打開瀏覽器會問要不要信任,選「繼續」;設定裡顯示憑證的 SHA-256 指紋可以比對。從區網打 http:// 會自動轉到 https://;這台電腦自己,和從 Tailscale 連到這台 Tailscale 通道位址的(本來就加密)仍可用 http。憑證做不出來時退回 http,設定會用紅字提醒沒有加密。
|
||||||
- **「Tailscale」看通道,不看位址像不像 100.x**:100.64.0.0/10 是電信業者 NAT 的共用位址,有些 Wi-Fi、PPPoE、Cloudflare WARP 也用這一段。Tailscale 通道是:macOS 上帶著 Tailscale IPv6 位址的 utun、Linux 的 tailscale… 介面、Windows 名字有 Tailscale 的介面卡。區網直連只有「來源在 Tailscale 範圍、而且連到這台 Tailscale 通道上的位址」才收 http,列出的網址也只有通道位址寫 http。帳號伺服器用 http:// 時,帳號密碼、配對碼和這台電腦的金鑰只送到這台電腦、一個字的名字(同一個 Docker 網路),或位址都是 Tailscale 的、而且路由從 Tailscale 通道出去的伺服器——登入、從帳號備份還原、長連線每次重連、備份和同步都在開連線那一刻對真正要連的位址查,不符合就不連、狀態寫原因、之後再試;http 的帳號伺服器不走代理。網址照 HTTP 程式庫自己的讀法讀(`?`、`#` 後面不是主機)。`ANYBOT_PHONE_BIND` 讓直接連線只聽一個位址;位址還不在、連接埠被占用或不准用時,紀錄和設定照實說,之後每 5 秒自己再試,不用關掉再開。
|
- **「Tailscale」看通道,不看位址像不像 100.x**:100.64.0.0/10 是電信業者 NAT 的共用位址,有些 Wi-Fi、PPPoE、Cloudflare WARP 也用這一段。Tailscale 通道是:macOS 上帶著 Tailscale IPv6 位址的 utun、Linux 的 tailscale… 介面、Windows 名字有 Tailscale 的介面卡。區網直連只有「來源在 Tailscale 範圍、而且連到這台 Tailscale 通道上的位址」才收 http,列出的網址也只有通道位址寫 http。帳號伺服器用 http:// 時,帳號密碼、配對碼和這台電腦的金鑰只送到這台電腦、一個字的名字(同一個 Docker 網路),或位址都是 Tailscale 的、而且路由從 Tailscale 通道出去的伺服器——在開連線那一刻對真正要連的位址查:http 的帳號伺服器每個請求都開新連線、每次都查,長連線在每次連上和重連時查(連著時不中途重查)。不符合就不連:登入、還原、配對回到表單說原因;長連線、備用連線在狀態寫原因、之後自己再試。http 的帳號伺服器不走代理;一個字的名字要以英文字母開頭(純數字、0x 的寫法不算),`localhost` 要查到這台才算。網址照 HTTP 程式庫自己的讀法讀(`?`、`#` 後面不是主機)。`ANYBOT_PHONE_BIND` 讓直接連線只聽一個位址;位址還不在、連接埠被占用或不准用時,紀錄和設定照實說,之後每 5 秒自己再試,不用關掉再開。
|
||||||
- **手腳只聽大腦簽過的指令**:在大腦這台按「產生配對碼」,貼上的那一行裡,6 位配對碼後面多一段密鑰(`135790-abc…`)。密鑰只在兩台電腦上,不會傳給帳號伺服器;之後大腦給這台手腳的每個指令都帶簽章(綁定是哪台電腦、時間、只能用一次),手腳驗不過就拒絕。所以帳號伺服器(或攻進去的人)不能自己造指令、也不能重放。用手機產生的配對碼、或是當「另一個大腦的螢幕」時產生的,沒有密鑰(跟以前一樣,沒有簽章)。舊版 AnyBot 讀到新格式只取 6 位數字,照舊配對。
|
- **手腳只聽大腦簽過的指令**:在大腦這台按「產生配對碼」,貼上的那一行裡,6 位配對碼後面多一段密鑰(`135790-abc…`)。密鑰只在兩台電腦上,不會傳給帳號伺服器;之後大腦給這台手腳的每個指令都帶簽章(綁定是哪台電腦、時間、只能用一次),手腳驗不過就拒絕。所以帳號伺服器(或攻進去的人)不能自己造指令、也不能重放。用手機產生的配對碼、或是當「另一個大腦的螢幕」時產生的,沒有密鑰(跟以前一樣,沒有簽章)。舊版 AnyBot 讀到新格式只取 6 位數字,照舊配對。
|
||||||
- **更新要驗簽章**:`make sign-setup`(作者做一次)讓之後每個 commit 用 SSH 金鑰簽章,公鑰記在 `deploy/allowed_signers`。安裝時把它記到原始碼資料夾外面(`~/.config/anybot/allowed_signers`);之後 `make update`/重跑安裝腳本,新版不是那把金鑰簽的就不裝(Mac、Linux、Windows 都一樣)。換金鑰:用舊金鑰簽一個把新公鑰加進名單的 commit。確定要跳過一次:`ANYBOT_UNSIGNED=1`。還沒有 `deploy/allowed_signers` 時跟以前一樣不檢查。
|
- **更新要驗簽章**:`make sign-setup`(作者做一次)讓之後每個 commit 用 SSH 金鑰簽章,公鑰記在 `deploy/allowed_signers`。安裝時把它記到原始碼資料夾外面(`~/.config/anybot/allowed_signers`);之後 `make update`/重跑安裝腳本,新版不是那把金鑰簽的就不裝(Mac、Linux、Windows 都一樣)。換金鑰:用舊金鑰簽一個把新公鑰加進名單的 commit。確定要跳過一次:`ANYBOT_UNSIGNED=1`。還沒有 `deploy/allowed_signers` 時跟以前一樣不檢查。
|
||||||
- **聊天各存各的檔**:state.json 只剩設定;每個 Bot 的聊天存在 `chats/bot-<編號>.json`,有變才寫。Bot 工作時每兩秒存一次的不再是整份幾十 MB;刪掉 Bot 時它的聊天檔一起刪。舊版存的(聊天在 state.json 裡)照樣讀得進來,下次存檔就分開。
|
- **聊天各存各的檔**:state.json 只剩設定;每個 Bot 的聊天存在 `chats/bot-<編號>.json`,有變才寫。Bot 工作時每兩秒存一次的不再是整份幾十 MB;刪掉 Bot 時它的聊天檔一起刪。舊版存的(聊天在 state.json 裡)照樣讀得進來,下次存檔就分開。
|
||||||
|
|
|
||||||
|
|
@ -44,8 +44,8 @@ AnyBot 的 privacy 模型一句话:**Bot 跑在你自己的機器上,你的
|
||||||
- 預設只綁 **loopback**;egress 綁環回+自動偵測的 Docker 網橋閘道(docker0 / docker_gwbridge / br-*),**從不綁 0.0.0.0**。
|
- 預設只綁 **loopback**;egress 綁環回+自動偵測的 Docker 網橋閘道(docker0 / docker_gwbridge / br-*),**從不綁 0.0.0.0**。
|
||||||
- 手機連 relay 走你自架的 relay+Caddy HTTPS;`/relay/*` 有登入鎖、錯誤鎖與 session 登出。
|
- 手機連 relay 走你自架的 relay+Caddy HTTPS;`/relay/*` 有登入鎖、錯誤鎖與 session 登出。
|
||||||
- LAN 模式(設定開)才會對區域網路開放,並要求 App 密碼(雜湊比對)。連線走這台自己做的憑證的 HTTPS;不加密的 http 只收這台自己(loopback),和「來源在 Tailscale 的範圍、而且連到的那個位址是這台 Tailscale 通道介面上的位址」的連線——不是只看來源像不像 100.64.0.0/10(Wi-Fi 自己發那一段位址時,那是區網)。看的是連線落在這台的哪個位址,不是封包從哪張網卡進來:同一個 Wi-Fi 上的裝置故意把封包送到這台的 Tailscale 位址,系統可能收下,但那樣明文走過空中的只有它自己的流量;用 Tailscale 連的手機一定走 WireGuard。「Tailscale 通道介面」是:macOS 上同時帶著 Tailscale IPv6 位址(fd7a:115c:a1e0::/48)的 utun 這類點對點介面、Linux 上叫 tailscale… 的介面(或帶著那個 IPv6 位址的)、Windows 上名字有 Tailscale 的介面卡(從 ifconfig、ip -o addr、ipconfig 讀,30 秒更新一次)。只有 100.64.0.0/10 位址的點對點介面(PPPoE、手機的電信 NAT、Cloudflare WARP)不算;Windows 上介面卡改過名字就認不出來,http 一律轉 https。`ANYBOT_PHONE_BIND` 可以讓它只聽一個位址(例如 `127.0.0.1`,前面放代理)。
|
- LAN 模式(設定開)才會對區域網路開放,並要求 App 密碼(雜湊比對)。連線走這台自己做的憑證的 HTTPS;不加密的 http 只收這台自己(loopback),和「來源在 Tailscale 的範圍、而且連到的那個位址是這台 Tailscale 通道介面上的位址」的連線——不是只看來源像不像 100.64.0.0/10(Wi-Fi 自己發那一段位址時,那是區網)。看的是連線落在這台的哪個位址,不是封包從哪張網卡進來:同一個 Wi-Fi 上的裝置故意把封包送到這台的 Tailscale 位址,系統可能收下,但那樣明文走過空中的只有它自己的流量;用 Tailscale 連的手機一定走 WireGuard。「Tailscale 通道介面」是:macOS 上同時帶著 Tailscale IPv6 位址(fd7a:115c:a1e0::/48)的 utun 這類點對點介面、Linux 上叫 tailscale… 的介面(或帶著那個 IPv6 位址的)、Windows 上名字有 Tailscale 的介面卡(從 ifconfig、ip -o addr、ipconfig 讀,30 秒更新一次)。只有 100.64.0.0/10 位址的點對點介面(PPPoE、手機的電信 NAT、Cloudflare WARP)不算;Windows 上介面卡改過名字就認不出來,http 一律轉 https。`ANYBOT_PHONE_BIND` 可以讓它只聽一個位址(例如 `127.0.0.1`,前面放代理)。
|
||||||
- 帳號伺服器用 `http://` 時,帳號密碼(登入、建立帳號、兩步驗證、從帳號備份還原)、配對碼和這台電腦的金鑰(長連線、它的備用連線、備份、和大腦同步、手腳的指令)只送到:這台電腦、一個字的名字(當成同一個 Docker 網路裡的機器,例如 `http://relay:8080`),或「位址都是 Tailscale 的、而且這台到它的路由從 Tailscale 通道出去」的伺服器;其他(包括 Wi-Fi 上的 100.x)要 `https://`。這個檢查在每次開連線時,對真正要連的那幾個位址做(名字第二次查可能答不一樣),長連線每次重連都重查;不符合就不連,狀態寫原因,之後再試(例如 Tailscale 連上後)。http 的帳號伺服器不走代理(代理會照它自己的路把明文轉出去)。沒有路由的位址(沒有 IPv6 的電腦上的 IPv6 位址)略過。網址照 HTTP 程式庫自己的讀法讀(`?`、`#` 後面不算主機)。
|
- 帳號伺服器用 `http://` 時,帳號密碼(登入、建立帳號、兩步驗證、從帳號備份還原)、配對碼和這台電腦的金鑰(長連線、它的備用連線、備份、和大腦同步、手腳的指令)只送到:這台電腦、一個字的名字(當成同一個 Docker 網路裡的機器,例如 `http://relay:8080`),或「位址都是 Tailscale 的、而且這台到它的路由從 Tailscale 通道出去」的伺服器;其他(包括 Wi-Fi 上的 100.x)要 `https://`。這個檢查在開連線的那一刻,對真正要連的那幾個位址做(名字第二次查可能答不一樣):http 的帳號伺服器每個請求都開新的連線(不留著給下一個請求用),所以每個請求都查;長連線(WebSocket)在每次連上、重連時查,連著的時候不會中途再查。不符合就不連:登入、建立帳號、還原、配對直接回到表單說原因;長連線、備用連線和 `--serve` 的 `ANYBOT_ACCOUNT` 寫進狀態或紀錄,之後自己再試(例如 Tailscale 連上後)。http 的帳號伺服器不走代理(代理會照它自己的路把明文轉出去)。`localhost` 只有查到的位址都是這台(loopback)才算這台。沒有路由的位址(沒有 IPv6 的電腦上的 IPv6 位址)略過。網址照 HTTP 程式庫自己的讀法讀(`?`、`#` 後面不算主機)。
|
||||||
- 一個字的名字是照「同一個 Docker 網路」相信的:在一般的網路上,路由器的 DNS 或 LLMNR 也可能把這種名字查到區網位址,那時密碼會明文走過區網——Docker 以外,不要用一個字的名字當 http 帳號伺服器的網址。
|
- 一個字的名字(以英文字母開頭,只有字母、數字、`-`、`_`,例如 `relay`)是照「同一個 Docker 網路」相信的:在一般的網路上,路由器的 DNS、系統的搜尋網域或 LLMNR 也可能把這種名字查到區網位址,那時密碼會明文走過區網——Docker 以外,不要用一個字的名字當 http 帳號伺服器的網址。純數字或 0x 開頭的寫法(系統會把 `134744072`、`0x8080808` 讀成 8.8.8.8)不算名字。
|
||||||
|
|
||||||
## 已知限制(誠實清單)
|
## 已知限制(誠實清單)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
## 子功能
|
## 子功能
|
||||||
|
|
||||||
- AnyBot 帳號:帳號伺服器(自己架,在 `relay/`)保管帳號、裝置清單、加密備份,也替手機轉送連線;電腦主動連過去,不用開連接埠。可以順便建立新帳號(伺服器要求時填邀請碼);開了兩步驗證要填六位碼或恢復碼。帳號伺服器的網址要 https://;http:// 只給這台電腦、一個字的名字(同一個 Docker 網路),或經 Tailscale 通道連到的 Tailscale 位址(Wi-Fi 上的 100.x 不算)。登入、還原、配對和之後的每一次連線都照這個查;不符合就先不連,「AnyBot 帳號」的狀態寫原因,之後自己再試。
|
- AnyBot 帳號:帳號伺服器(自己架,在 `relay/`)保管帳號、裝置清單、加密備份,也替手機轉送連線;電腦主動連過去,不用開連接埠。可以順便建立新帳號(伺服器要求時填邀請碼);開了兩步驗證要填六位碼或恢復碼。帳號伺服器的網址要 https://;http:// 只給這台電腦、一個字的名字(同一個 Docker 網路),或經 Tailscale 通道連到的 Tailscale 位址(Wi-Fi 上的 100.x 不算)。登入、還原、配對和之後的每個請求都照這個查。不符合時,登入、還原、配對回到表單說原因;長連線和備用連線在「AnyBot 帳號」的狀態寫原因、之後自己再試(`--serve` 的 `ANYBOT_ACCOUNT` 寫進紀錄、每 30 秒再試)。
|
||||||
- 備份:「每天自動備份到帳號」和「立刻備份」,在電腦上加密後才上傳,伺服器打不開;「從帳號備份還原」把這台的 Bot 換成帳號裡最新的備份。
|
- 備份:「每天自動備份到帳號」和「立刻備份」,在電腦上加密後才上傳,伺服器打不開;「從帳號備份還原」把這台的 Bot 換成帳號裡最新的備份。
|
||||||
- 大腦:登入後可以選帳號裡一台 AnyBot 當大腦,其他電腦當螢幕或手腳;「加一台電腦」產生配對碼(10 分鐘有效)。細節見 [computers](computers.md)。
|
- 大腦:登入後可以選帳號裡一台 AnyBot 當大腦,其他電腦當螢幕或手腳;「加一台電腦」產生配對碼(10 分鐘有效)。細節見 [computers](computers.md)。
|
||||||
- 大腦那一欄(螢幕電腦上):狀態下面分段寫「這台 ↔ 帳號伺服器:…」「帳號伺服器 ↔ 大腦「…」:在線上/不在線上」和「上次同步 10:31(1.2 MB,11 秒,約 110 KB/s)」,看得出是哪一段斷、線有多慢。長連線只要還有資料進來就不算斷(很慢的線上傳大份資料不會被切掉);斷了重新連上的那一刻,大腦的資料就重新要,不用等。
|
- 大腦那一欄(螢幕電腦上):狀態下面分段寫「這台 ↔ 帳號伺服器:…」「帳號伺服器 ↔ 大腦「…」:在線上/不在線上」和「上次同步 10:31(1.2 MB,11 秒,約 110 KB/s)」,看得出是哪一段斷、線有多慢。長連線只要還有資料進來就不算斷(很慢的線上傳大份資料不會被切掉);斷了重新連上的那一刻,大腦的資料就重新要,不用等。
|
||||||
|
|
|
||||||
|
|
@ -1911,7 +1911,8 @@ fn base(url: &str) -> String {
|
||||||
/// computer's key may go to it unencrypted. `host` is the name in the URL and `addrs` what it
|
/// computer's key may go to it unencrypted. `host` is the name in the URL and `addrs` what it
|
||||||
/// resolved to — the very addresses about to be connected to (a name can answer differently the
|
/// resolved to — the very addresses about to be connected to (a name can answer differently the
|
||||||
/// second time, so the check is made at the moment of connecting, on what it answered then).
|
/// second time, so the check is made at the moment of connecting, on what it answered then).
|
||||||
/// Off the air: this machine (localhost, loopback); a one-word name, taken to be another
|
/// Off the air: this machine (every address a loopback one — `localhost` only when it resolves
|
||||||
|
/// there); a one-word name (`one_word_name`), taken to be another
|
||||||
/// container of the same Docker network (http://relay:8080 — see SECURITY.md about such names on
|
/// container of the same Docker network (http://relay:8080 — see SECURITY.md about such names on
|
||||||
/// an ordinary network); or a server reached through this machine's Tailscale tunnel
|
/// an ordinary network); or a server reached through this machine's Tailscale tunnel
|
||||||
/// (lantls::tunnel_route: Tailscale addresses whose route leaves from the tunnel; one with no
|
/// (lantls::tunnel_route: Tailscale addresses whose route leaves from the tunnel; one with no
|
||||||
|
|
@ -1920,10 +1921,12 @@ fn base(url: &str) -> String {
|
||||||
pub(crate) fn plain_hop(host: &str, addrs: &[std::net::SocketAddr]) -> Result<Vec<std::net::SocketAddr>, String> {
|
pub(crate) fn plain_hop(host: &str, addrs: &[std::net::SocketAddr]) -> Result<Vec<std::net::SocketAddr>, String> {
|
||||||
let host = host.trim_start_matches('[').trim_end_matches(']');
|
let host = host.trim_start_matches('[').trim_end_matches(']');
|
||||||
let refuse = || Err(format!("帳號伺服器 {host} 用的是 http://,這台到它的路不經過 Tailscale:帳號密碼和這台電腦的金鑰會在網路上明文傳送,所以先不連(之後會再試)。請改用 https://,或等 Tailscale 連上"));
|
let refuse = || Err(format!("帳號伺服器 {host} 用的是 http://,這台到它的路不經過 Tailscale:帳號密碼和這台電腦的金鑰會在網路上明文傳送,所以先不連(之後會再試)。請改用 https://,或等 Tailscale 連上"));
|
||||||
if host.eq_ignore_ascii_case("localhost") || (!addrs.is_empty() && addrs.iter().all(|a| a.ip().to_canonical().is_loopback())) {
|
// This machine: every address is a loopback one (`localhost` too counts only when it
|
||||||
|
// resolves there — a hosts file can send it elsewhere).
|
||||||
|
if !addrs.is_empty() && addrs.iter().all(|a| a.ip().to_canonical().is_loopback()) {
|
||||||
return Ok(addrs.to_vec());
|
return Ok(addrs.to_vec());
|
||||||
}
|
}
|
||||||
if !host.is_empty() && !host.contains('.') && host.parse::<std::net::IpAddr>().is_err() {
|
if one_word_name(host) {
|
||||||
return Ok(addrs.to_vec());
|
return Ok(addrs.to_vec());
|
||||||
}
|
}
|
||||||
let ips: Vec<std::net::IpAddr> = addrs.iter().map(|a| a.ip()).collect();
|
let ips: Vec<std::net::IpAddr> = addrs.iter().map(|a| a.ip()).collect();
|
||||||
|
|
@ -1933,6 +1936,14 @@ pub(crate) fn plain_hop(host: &str, addrs: &[std::net::SocketAddr]) -> Result<Ve
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A one-word name like a Docker service's (`relay`, `anybot_relay_1`): it starts with a letter
|
||||||
|
/// and has only letters, digits, '-' and '_'. Not `localhost` (that one is judged by where it
|
||||||
|
/// resolves), and never a number: the system's resolver reads `134744072`, `0x8080808` or
|
||||||
|
/// `01002004010` as the address 8.8.8.8, which Rust's own parser doesn't.
|
||||||
|
fn one_word_name(host: &str) -> bool {
|
||||||
|
host.chars().next().is_some_and(|c| c.is_ascii_alphabetic()) && host.chars().all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_') && !host.eq_ignore_ascii_case("localhost")
|
||||||
|
}
|
||||||
|
|
||||||
/// Why the account server at `url` (its address or anything under it) won't be sent anything
|
/// Why the account server at `url` (its address or anything under it) won't be sent anything
|
||||||
/// right now, or None (https, or an http:// one off the air — `plain_hop`). For saying so: what
|
/// right now, or None (https, or an http:// one off the air — `plain_hop`). For saying so: what
|
||||||
/// actually holds is the same check made where the connection is opened (`relay_agent`'s
|
/// actually holds is the same check made where the connection is opened (`relay_agent`'s
|
||||||
|
|
@ -1953,7 +1964,9 @@ pub(crate) fn relay_plain_problem(url: &str) -> Option<String> {
|
||||||
/// The client for a request to the account server at `url`. An https:// one is the common
|
/// The client for a request to the account server at `url`. An https:// one is the common
|
||||||
/// client. An http:// one gets a client that makes the `plain_hop` check on the addresses it is
|
/// client. An http:// one gets a client that makes the `plain_hop` check on the addresses it is
|
||||||
/// about to connect to, and goes there directly — not through a proxy, which would carry the
|
/// about to connect to, and goes there directly — not through a proxy, which would carry the
|
||||||
/// password on in the clear wherever it routes.
|
/// password on in the clear wherever it routes — and keeps no connection for the next request:
|
||||||
|
/// each request opens its own, so each one is checked (a kept one would have been checked once,
|
||||||
|
/// on a network that may since have changed).
|
||||||
pub(crate) fn relay_agent(secs: u64, url: &str) -> ureq::Agent {
|
pub(crate) fn relay_agent(secs: u64, url: &str) -> ureq::Agent {
|
||||||
if url.trim().get(..8).is_some_and(|p| p.eq_ignore_ascii_case("https://")) {
|
if url.trim().get(..8).is_some_and(|p| p.eq_ignore_ascii_case("https://")) {
|
||||||
return agent(secs);
|
return agent(secs);
|
||||||
|
|
@ -1965,7 +1978,7 @@ pub(crate) fn relay_agent(secs: u64, url: &str) -> ureq::Agent {
|
||||||
.unwrap_or_else(|e| e.into_inner())
|
.unwrap_or_else(|e| e.into_inner())
|
||||||
.entry(secs)
|
.entry(secs)
|
||||||
.or_insert_with(|| {
|
.or_insert_with(|| {
|
||||||
let config = crate::net::config().timeout_global(Some(Duration::from_secs(secs))).http_status_as_error(false).proxy(None).build();
|
let config = crate::net::config().timeout_global(Some(Duration::from_secs(secs))).http_status_as_error(false).proxy(None).max_idle_connections(0).max_idle_connections_per_host(0).build();
|
||||||
ureq::Agent::with_parts(config, ureq::unversioned::transport::DefaultConnector::new(), OffTheAir(Default::default()))
|
ureq::Agent::with_parts(config, ureq::unversioned::transport::DefaultConnector::new(), OffTheAir(Default::default()))
|
||||||
})
|
})
|
||||||
.clone()
|
.clone()
|
||||||
|
|
@ -3180,6 +3193,60 @@ mod tests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A one-word name is a Docker network's machine — never a number the system's resolver reads
|
||||||
|
/// as an address (8.8.8.8 as 134744072, 0x8080808, 01002004010), and `localhost` only when it
|
||||||
|
/// resolves to this machine.
|
||||||
|
#[test]
|
||||||
|
fn a_one_word_name_is_never_a_number() {
|
||||||
|
let at = |s: &str| vec![std::net::SocketAddr::new(s.parse().unwrap(), 8780)];
|
||||||
|
crate::lantls::with_tunnels(&[], || {
|
||||||
|
for number in ["134744072", "0x8080808", "0X8080808", "01002004010", "8"] {
|
||||||
|
assert!(plain_hop(number, &at("8.8.8.8")).is_err(), "{number}");
|
||||||
|
}
|
||||||
|
assert!(plain_hop("localhost", &at("192.168.1.5")).is_err(), "localhost sent elsewhere by a hosts file");
|
||||||
|
});
|
||||||
|
assert!(plain_hop("localhost", &at("127.0.0.1")).is_ok() && plain_hop("relay", &at("172.18.0.3")).is_ok() && plain_hop("anybot_relay_1", &at("172.18.0.3")).is_ok());
|
||||||
|
// (A number that does land on this machine is still this machine.)
|
||||||
|
assert!(plain_hop("0x7f000001", &at("127.0.0.1")).is_ok());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The http:// account-server client keeps no connection: every request opens — and so
|
||||||
|
/// checks — its own (a kept one would ride on a check made earlier, maybe on another network).
|
||||||
|
#[test]
|
||||||
|
fn the_plain_http_client_opens_a_connection_per_request() {
|
||||||
|
let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
|
||||||
|
let port = l.local_addr().unwrap().port();
|
||||||
|
let opened = Arc::new(std::sync::atomic::AtomicUsize::new(0));
|
||||||
|
let seen = opened.clone();
|
||||||
|
std::thread::spawn(move || {
|
||||||
|
for c in l.incoming().flatten() {
|
||||||
|
seen.fetch_add(1, Ordering::SeqCst);
|
||||||
|
std::thread::spawn(move || {
|
||||||
|
let mut c = c;
|
||||||
|
let mut buf = [0u8; 4096];
|
||||||
|
let mut got = vec![];
|
||||||
|
// Answers as many requests as the connection brings (it would keep it open).
|
||||||
|
while let Ok(n) = std::io::Read::read(&mut c, &mut buf) {
|
||||||
|
if n == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
got.extend_from_slice(&buf[..n]);
|
||||||
|
while let Some(end) = got.windows(4).position(|w| w == b"\r\n\r\n") {
|
||||||
|
got.drain(..end + 4);
|
||||||
|
let _ = std::io::Write::write_all(&mut c, b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let url = format!("http://127.0.0.1:{port}/relay/hello");
|
||||||
|
for _ in 0..3 {
|
||||||
|
let mut r = relay_agent(5, &url).get(&url).call().unwrap();
|
||||||
|
assert_eq!(r.body_mut().read_to_string().unwrap(), "ok");
|
||||||
|
}
|
||||||
|
assert_eq!(opened.load(Ordering::SeqCst), 3, "a connection kept and reused");
|
||||||
|
}
|
||||||
|
|
||||||
/// The URL is read the way the HTTP client reads it: the host stops at '?' and '#', so what is
|
/// The URL is read the way the HTTP client reads it: the host stops at '?' and '#', so what is
|
||||||
/// checked is what is connected to (the private-address check of model requests too).
|
/// checked is what is connected to (the private-address check of model requests too).
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue