fix: localhost 只連它的 loopback 位址;測試不再搶 65535、不再共用帳號長連線
第五輪(新的獨立審查,沒有 P0/P1): - localhost:只留它查到的 loopback 位址來連(舊 Mac 的 /etc/hosts 還有 fe80::1%lo0 localhost, 上一輪會因此整個拒絕、還說成「不經過 Tailscale」);一個 loopback 都沒有才拒絕,並照實說是 /etc/hosts。 - 測試 bind_problems_are_said_as_they_are 原本真的去綁 65535(放掉再綁一次),可是 65535 在 macOS 對外 連線的臨時連接埠範圍裡,平行的測試可能剛好拿走。改成檢查連接埠範圍(新的 lan_ports,bind_lan 也用它), 不碰真的 65535。the_phone_page_binds_where_told_and_keeps_trying 放掉占位的連接埠後,接受範圍裡任何一個 (放掉的那個可能被別的測試的連線拿走)。 - 早就有的測試互相干擾:tunnel 的測試把整個程式共用的帳號長連線開著,a_brain_too_old… 這時呼叫 to_brain 會走那條長連線而失敗。兩個測試用同一把只在測試裡的鎖(tunnel::LINK_TESTS)輪流跑,tunnel 測試結束前把 它開的長連線清掉。產品程式碼沒變。 - phone.md:已經連上的長連線不會中途重查,照 SECURITY.md、FEATURES.md 的寫法改。 已驗證: 完整 cargo test(金鑰清空、DOCKER_HOST 指到不存在)連跑兩次都全綠(654 過、47 ignored 沒跑); cargo test -- tunnel:: a_brain_too_old 連跑三次全綠(修之前三次都失敗);cargo clippy --bin anybot --all-targets -- -D warnings 沒有警告;sh deploy/ci/lint-sh.sh 過。回退各做一次都被測試抓到:localhost 改回要全部是 loopback、連接埠範圍改回不含最後一個(65535 變空的)、tunnel 測試不拿鎖。 未驗證: 沒有接真的帳號伺服器(同前幾輪);手機頁面行為沒變,沒有重跑 verify-anybot。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
cc16b7df0f
commit
8bb3ffc9b0
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
## 子功能
|
## 子功能
|
||||||
|
|
||||||
- AnyBot 帳號:帳號伺服器(自己架,在 `relay/`)保管帳號、裝置清單、加密備份,也替手機轉送連線;電腦主動連過去,不用開連接埠。可以順便建立新帳號(伺服器要求時填邀請碼);開了兩步驗證要填六位碼或恢復碼。帳號伺服器的網址要 https://;http:// 只給這台電腦、一個字的名字(同一個 Docker 網路),或經 Tailscale 通道連到的 Tailscale 位址(Wi-Fi 上的 100.x 不算)。登入、還原、配對和之後的每個請求都照這個查。不符合時,登入、還原、配對回到表單說原因;長連線和備用連線在「AnyBot 帳號」的狀態寫原因、之後自己再試(`--serve` 的 `ANYBOT_ACCOUNT` 寫進紀錄、每 30 秒再試)。
|
- 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)」,看得出是哪一段斷、線有多慢。長連線只要還有資料進來就不算斷(很慢的線上傳大份資料不會被切掉);斷了重新連上的那一刻,大腦的資料就重新要,不用等。
|
||||||
|
|
|
||||||
|
|
@ -1567,13 +1567,18 @@ fn lan_start(gen: u64, bind: std::net::IpAddr, start: u16, every: Duration) {
|
||||||
std::thread::spawn(move || lan_up(gen, bind, start, every));
|
std::thread::spawn(move || lan_up(gen, bind, start, every));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The ports tried from `start`: it and the nine after it, as far as there are ports (from 65535,
|
||||||
|
/// just 65535 — not an empty range).
|
||||||
|
fn lan_ports(start: u16) -> std::ops::RangeInclusive<u16> {
|
||||||
|
start..=start.saturating_add(9)
|
||||||
|
}
|
||||||
|
|
||||||
/// The first port of start..start+10 that `bind` gives, or why none did (in words for 設定):
|
/// The first port of start..start+10 that `bind` gives, or why none did (in words for 設定):
|
||||||
/// the address isn't on this computer, the ports aren't allowed (below 1024 without the
|
/// the address isn't on this computer, the ports aren't allowed (below 1024 without the
|
||||||
/// administrator; a range Windows keeps for itself), or another program has them.
|
/// administrator; a range Windows keeps for itself), or another program has them.
|
||||||
fn bind_lan(bind: std::net::IpAddr, start: u16) -> Result<std::net::TcpListener, String> {
|
fn bind_lan(bind: std::net::IpAddr, start: u16) -> Result<std::net::TcpListener, String> {
|
||||||
let last = start.saturating_add(9);
|
|
||||||
let mut errors = vec![];
|
let mut errors = vec![];
|
||||||
for p in start..=last {
|
for p in lan_ports(start) {
|
||||||
match std::net::TcpListener::bind((bind, p)) {
|
match std::net::TcpListener::bind((bind, p)) {
|
||||||
Ok(l) => return Ok(l),
|
Ok(l) => return Ok(l),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
|
@ -1591,7 +1596,7 @@ fn bind_lan(bind: std::net::IpAddr, start: u16) -> Result<std::net::TcpListener,
|
||||||
/// Why no port of start…start+9 could be had, from what each try said.
|
/// Why no port of start…start+9 could be had, from what each try said.
|
||||||
fn bind_problem(bind: std::net::IpAddr, start: u16, errors: &[std::io::Error]) -> String {
|
fn bind_problem(bind: std::net::IpAddr, start: u16, errors: &[std::io::Error]) -> String {
|
||||||
use std::io::ErrorKind as K;
|
use std::io::ErrorKind as K;
|
||||||
let last = start.saturating_add(9);
|
let last = *lan_ports(start).end();
|
||||||
let ports = if last == start { format!("連接埠 {start}") } else { format!("連接埠 {start}–{last}") };
|
let ports = if last == start { format!("連接埠 {start}") } else { format!("連接埠 {start}–{last}") };
|
||||||
if errors.iter().any(|e| e.kind() == K::AddrNotAvailable) {
|
if errors.iter().any(|e| e.kind() == K::AddrNotAvailable) {
|
||||||
format!("這台電腦現在沒有 {bind} 這個位址(Tailscale 還沒連上、網路還沒好?):一出現就會自動打開")
|
format!("這台電腦現在沒有 {bind} 這個位址(Tailscale 還沒連上、網路還沒好?):一出現就會自動打開")
|
||||||
|
|
@ -1921,8 +1926,16 @@ 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 連上"));
|
||||||
// This machine: every address is a loopback one (`localhost` too counts only when it
|
// `localhost`: only its loopback addresses (an older Mac's hosts file adds fe80::1%lo0, and
|
||||||
// resolves there — a hosts file can send it elsewhere).
|
// one could send it anywhere); none at all, and it isn't this machine.
|
||||||
|
if host.eq_ignore_ascii_case("localhost") {
|
||||||
|
let here: Vec<std::net::SocketAddr> = addrs.iter().filter(|a| a.ip().to_canonical().is_loopback()).copied().collect();
|
||||||
|
if here.is_empty() && !addrs.is_empty() {
|
||||||
|
return Err("帳號伺服器的網址是 localhost,可是這台電腦把 localhost 查到別的位址(看 /etc/hosts):帳號密碼不會送過去".to_string());
|
||||||
|
}
|
||||||
|
return Ok(here);
|
||||||
|
}
|
||||||
|
// This machine: every address is a loopback one.
|
||||||
if !addrs.is_empty() && addrs.iter().all(|a| a.ip().to_canonical().is_loopback()) {
|
if !addrs.is_empty() && addrs.iter().all(|a| a.ip().to_canonical().is_loopback()) {
|
||||||
return Ok(addrs.to_vec());
|
return Ok(addrs.to_vec());
|
||||||
}
|
}
|
||||||
|
|
@ -2902,6 +2915,8 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn a_brain_too_old_for_a_request_says_to_update_it() {
|
fn a_brain_too_old_for_a_request_says_to_update_it() {
|
||||||
|
// (Not while a tunnel test has the account link up: to_brain would go through it.)
|
||||||
|
let _link = crate::tunnel::LINK_TESTS.lock().unwrap_or_else(|e| e.into_inner());
|
||||||
// A stand-in account server passing /api/sync/… to a brain from before 記憶 and 傳送:
|
// A stand-in account server passing /api/sync/… to a brain from before 記憶 and 傳送:
|
||||||
// its own 「沒有這個網址」 for /memory; for /sync/x a real 404 of a newer brain's.
|
// its own 「沒有這個網址」 for /memory; for /sync/x a real 404 of a newer brain's.
|
||||||
let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
|
let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
|
||||||
|
|
@ -3141,7 +3156,8 @@ mod tests {
|
||||||
lan_start(lan_stop(), ip("127.0.0.1"), start, every);
|
lan_start(lan_stop(), ip("127.0.0.1"), start, every);
|
||||||
wait("the taken ports said", &|| lan_problem().is_some_and(|p| p.contains("占用")));
|
wait("the taken ports said", &|| lan_problem().is_some_and(|p| p.contains("占用")));
|
||||||
drop(blockers);
|
drop(blockers);
|
||||||
wait("up once the ports are free", &|| lan_port() == Some(start));
|
// (Any port of the range: a freed one can be taken by another test's outgoing connection.)
|
||||||
|
wait("up once the ports are free", &|| lan_port().is_some_and(|p| lan_ports(start).contains(&p)));
|
||||||
assert_eq!(lan_problem(), None);
|
assert_eq!(lan_problem(), None);
|
||||||
set_lan(false).unwrap();
|
set_lan(false).unwrap();
|
||||||
assert_eq!(lan_port(), None);
|
assert_eq!(lan_port(), None);
|
||||||
|
|
@ -3160,11 +3176,11 @@ mod tests {
|
||||||
assert!(said(8793, &[K::AddrNotAvailable]).contains("沒有 127.0.0.1 這個位址"));
|
assert!(said(8793, &[K::AddrNotAvailable]).contains("沒有 127.0.0.1 這個位址"));
|
||||||
let top = said(65535, &[K::AddrInUse]);
|
let top = said(65535, &[K::AddrInUse]);
|
||||||
assert!(top.contains("連接埠 65535 都被") && !top.contains("65534"), "{top}");
|
assert!(top.contains("連接埠 65535 都被") && !top.contains("65534"), "{top}");
|
||||||
// The top port is tried, not an empty range.
|
// The top port is tried, not an empty range. (Checked on the range, not by binding 65535:
|
||||||
if let Ok(free) = std::net::TcpListener::bind(("127.0.0.1", 65535)) {
|
// it is in the system's range for outgoing connections, so another test may hold it.)
|
||||||
drop(free);
|
assert_eq!(lan_ports(65535).collect::<Vec<_>>(), vec![65535]);
|
||||||
assert_eq!(bind_lan(lo, 65535).unwrap().local_addr().unwrap().port(), 65535);
|
assert_eq!(lan_ports(65530).count(), 6);
|
||||||
}
|
assert_eq!(lan_ports(8793), 8793..=8802);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The account password goes to an http:// account server only off the air: this computer,
|
/// The account password goes to an http:// account server only off the air: this computer,
|
||||||
|
|
@ -3206,6 +3222,14 @@ mod tests {
|
||||||
assert!(plain_hop("localhost", &at("192.168.1.5")).is_err(), "localhost sent elsewhere by a hosts file");
|
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());
|
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());
|
||||||
|
// An older Mac's hosts file also gives localhost fe80::1%lo0 (not loopback): only the
|
||||||
|
// loopback addresses are kept and connected to.
|
||||||
|
let link_local = std::net::SocketAddr::V6(std::net::SocketAddrV6::new("fe80::1".parse().unwrap(), 8780, 0, 1));
|
||||||
|
let both = vec![link_local, std::net::SocketAddr::new("127.0.0.1".parse().unwrap(), 8780), std::net::SocketAddr::new("::1".parse().unwrap(), 8780)];
|
||||||
|
let kept = crate::lantls::with_tunnels(&[], || plain_hop("localhost", &both)).unwrap();
|
||||||
|
assert_eq!(kept, both[1..].to_vec());
|
||||||
|
let e = crate::lantls::with_tunnels(&[], || plain_hop("localhost", &[link_local])).unwrap_err();
|
||||||
|
assert!(e.contains("localhost") && e.contains("/etc/hosts"), "{e}");
|
||||||
// (A number that does land on this machine is still this machine.)
|
// (A number that does land on this machine is still this machine.)
|
||||||
assert!(plain_hop("0x7f000001", &at("127.0.0.1")).is_ok());
|
assert!(plain_hop("0x7f000001", &at("127.0.0.1")).is_ok());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,6 +38,11 @@ struct Link {
|
||||||
serial: u64,
|
serial: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Tests that bring the account link up, or need it down (`call`, and so remote::to_brain, use it
|
||||||
|
/// whenever it is up): one at a time.
|
||||||
|
#[cfg(test)]
|
||||||
|
pub static LINK_TESTS: Mutex<()> = Mutex::new(());
|
||||||
|
|
||||||
fn link() -> &'static Mutex<Option<Link>> {
|
fn link() -> &'static Mutex<Option<Link>> {
|
||||||
static L: OnceLock<Mutex<Option<Link>>> = OnceLock::new();
|
static L: OnceLock<Mutex<Option<Link>>> = OnceLock::new();
|
||||||
L.get_or_init(|| Mutex::new(None))
|
L.get_or_init(|| Mutex::new(None))
|
||||||
|
|
@ -604,6 +609,8 @@ mod tests {
|
||||||
fn a_big_answer_arriving_slowly_keeps_the_link() {
|
fn a_big_answer_arriving_slowly_keeps_the_link() {
|
||||||
use std::io::Write as _;
|
use std::io::Write as _;
|
||||||
use std::sync::atomic::{AtomicBool, Ordering};
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
|
// The account link is one for the whole program: no other test may see this one up.
|
||||||
|
let _link = super::LINK_TESTS.lock().unwrap_or_else(|e| e.into_inner());
|
||||||
super::DEAD_MS.store(1_000, Ordering::Relaxed);
|
super::DEAD_MS.store(1_000, Ordering::Relaxed);
|
||||||
let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
|
let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
|
||||||
let port = l.local_addr().unwrap().port();
|
let port = l.local_addr().unwrap().port();
|
||||||
|
|
@ -640,8 +647,10 @@ mod tests {
|
||||||
assert!(said.lock().unwrap().iter().any(|(_, m)| m.contains("中斷")), "{:?}", said.lock().unwrap());
|
assert!(said.lock().unwrap().iter().any(|(_, m)| m.contains("中斷")), "{:?}", said.lock().unwrap());
|
||||||
stop.store(true, Ordering::Relaxed);
|
stop.store(true, Ordering::Relaxed);
|
||||||
super::DEAD_MS.store(super::DEAD_SECS * 1000, Ordering::Relaxed);
|
super::DEAD_MS.store(super::DEAD_SECS * 1000, Ordering::Relaxed);
|
||||||
// (Not joined: it may be waiting on a reconnect the fake server no longer answers.)
|
// (Not joined: it may be waiting on a reconnect the fake server no longer answers. Told to
|
||||||
|
// stop, it opens no new link; the one this test made goes before the next test may look.)
|
||||||
drop(h);
|
drop(h);
|
||||||
|
*super::link().lock().unwrap_or_else(|e| e.into_inner()) = None;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue