帳號伺服器換了網址不用再登出重登:tailnet 從 Tailscale 換到自架 headscale 後,舊的 *.ts.net 網址一次全死(nobledesktop 的帳號伺服器改成 http://100.64.0.5),電腦版只能「登出再登入」——帳號裡多一台新的、舊的那台留著,大腦要重選(MacBook Pro 就這樣變成「(2)」,Mac mini 還卡在死網址)。現在 設定 › 手機與帳號 的帳號卡片多一列「帳號伺服器換了網址」:按「換網址」填新網址(連不上時自己展開),先確認那是 AnyBot 帳號伺服器、再用這台自己的金鑰問它認不認得這台(帳號裡的電腦有這台)才換;只換網址,不送密碼、不註冊,登記、大腦、副本、排著的訊息全部照舊;另一台伺服器、這台已被移除就不換,說要登出再登入。沒有視窗的電腦(手腳、伺服器)用 anybot setup --move-to 網址。功能地圖 account.md 加 account-move、FEATURES.md 同步。
已驗證: cargo test 全綠(817,含 featuremap::、layout_audit:: 的「已登入、連不上」畫面);新測試 a_moved_account_server_is_followed_only_when_it_knows_this_computer(同一台換成功且金鑰/大腦不變、另一台、不在帳號、不是帳號伺服器、同網址、沒登入都拒絕);verify-anybot 實跑:scratch relay A 配對加入 → A 關掉、同資料在 B 開 → --move-to 空的 C 被拒、--move-to 假模型埠被拒、--move-to B 成功,重開後 serve.log「透過帳號 verify 連線(…28782)」、/relay/me 同一台在線、沒有多一台;桌面截圖 desktop/account-unreachable.png 卡片自動展開新網址欄位。 未驗證: 桌面上真的點「換過去」那一下(沒有點畫面的工具;同一個 relay_move 由 CLI 和單元測試走過);手機頁面沒有這個功能(手機直接用新網址重新登入)。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Uirha2uBHmbnuWBhNzjbJm
This commit is contained in:
parent
44db2bab74
commit
178200306f
|
|
@ -2239,6 +2239,7 @@ computer_use_screenshot、click、mouse_move、mouse_button(按住/放開)
|
|||
- 登入後,這台電腦會拿到自己的金鑰,之後保持連線。帳號下面一行顯示「已連線」或錯誤原因(長的錯誤自動換行,不會把卡片撐寬、把「登出」擠出視窗)。
|
||||
- 電腦不會保存你的密碼,只保存由密碼算出的備份金鑰。
|
||||
- 可以設定這台電腦在手機上顯示的名字(欄位跟卡片一樣寬,長名字不會被切掉)。
|
||||
- 「帳號伺服器換了網址」那一列:帳號伺服器搬到別的位址(換機器、換連接埠、換 VPN,例如從 Tailscale 換到自架 headscale,舊的 *.ts.net 名字一次全部失效)時,按「換網址」填新的網址、按「換過去」。AnyBot 先確認新網址是 AnyBot 帳號伺服器,再用這台電腦自己的金鑰問它認不認得這台(帳號裡的電腦有這台)才換:只換網址,不用密碼、不重新登入,這台的登記、選的大腦、大腦的副本和還沒送出的訊息全部照舊。新網址是另一台伺服器(不認得這台)就不換,說要先登出再登入。連不上帳號伺服器時這一列自己展開。沒有視窗的電腦(手腳、伺服器)用 `anybot setup --move-to 網址`(先關掉這台的 AnyBot)。以前只能登出再登入:帳號裡多一台新的電腦、舊的那台留著,大腦也要重選。
|
||||
- 備份下面隔一條線是「登出帳號」那一列,右邊「登出」按鈕(紅字):把這台從帳號移除;?說明會斷掉哪些。
|
||||
- **備份**:
|
||||
- 「每天自動備份到帳號」(預設開),也可以按「立刻備份」。
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
- `account-login` AnyBot 帳號:帳號伺服器(自己架,在 `relay/`)保管帳號、裝置清單、加密備份,也替手機轉送連線;電腦主動連過去,不用開連接埠。可以順便建立新帳號(伺服器要求時填邀請碼);開了兩步驗證要填六位碼或恢復碼。帳號伺服器的網址要 https://;http:// 只給這台電腦、一個字的名字(同一個 Docker 網路),或經 Tailscale 通道連到的 Tailscale 位址(Wi-Fi 上的 100.x 不算)。登入、還原、配對和之後的每個請求都照這個查(長連線在連上、重連時查,連著時不中途重查)。帳號伺服器轉給手機的回答,電腦那段壓縮著上傳(家裡網路的上傳最慢),到帳號伺服器才打開。不符合時,登入、還原、配對回到表單說原因;長連線和備用連線在「AnyBot 帳號」的狀態寫原因、之後自己再試(`--serve` 的 `ANYBOT_ACCOUNT` 寫進紀錄、每 30 秒再試)。
|
||||
- `account-backup` 備份:「每天自動備份到帳號」和「立刻備份」,在電腦上加密後才上傳,伺服器打不開;「從帳號備份還原」把這台的 Bot 換成帳號裡最新的備份。
|
||||
- `account-move` 帳號伺服器換了網址(換機器、連接埠、VPN,例如 Tailscale 換成 headscale):只換網址。先確認新網址是帳號伺服器,再用這台的金鑰問它認不認得這台才換;不用密碼,登記、大腦、副本、排著的訊息都照舊。不認得(另一台伺服器)就不換,說要登出再登入。沒有視窗的電腦用 `anybot setup --move-to 網址`(先關掉這台的 AnyBot)。
|
||||
- `account-brain` 大腦:登入後可以選帳號裡一台 AnyBot 當大腦,其他電腦當螢幕或手腳;「加一台電腦」產生配對碼(10 分鐘有效,[computers](computers.md))。螢幕電腦上的大腦那一欄分段寫「這台 ↔ 帳號伺服器:…」「帳號伺服器 ↔ 大腦「…」:在線上/不在線上」和「上次同步 10:31(1.2 MB,11 秒,約 110 KB/s)」,看得出是哪一段斷、線有多慢。長連線只要還有資料進來就不算斷(很慢的線上傳大份資料不會被切掉);斷了重新連上的那一刻,大腦的資料就重新要,不用等,而且只要斷線之後變了的。螢幕電腦存著這個大腦上次同步的副本時,打開 App(或從這台自己的 Bot 換回同一個大腦)就直接顯示副本,不等網路,之後大腦的資料來了只補變了的;第一次(還沒有副本,或副本是另一個大腦的)才整個視窗停在「正在連到「…」…」,等了 10 秒以上多一顆「改回用這台自己的 Bot」;大腦的設定讀不出來(版本不同)時照樣進去,說原因。重開 App 只向大腦要上次之後變了的。連不到大腦超過幾秒,聊天頂端「分享」旁邊只多一個小小的「離線」(有訊息在等就是「離線 · N 則等送出」;剛打開還沒連上是「連線中…」),不佔聊天的高度;滑鼠移上去說連不到的原因、顯示的是幾點同步的內容、做的事連上後才送。離線照樣能傳訊息(連附件)和改設定:訊息馬上出現在聊天最下面,標「等連上大腦後送出」(送的那一刻是「傳送中…」),還沒送出去過的可以按「取消」放回輸入框(送過一次、可能已經到了的就不能取消);排著的訊息和改動存在這台(每個大腦各一份),關掉 App 再開還在,連上後依序送出,斷線多久都等;附件傳上去的不會再傳一次。改的設定疊在大腦那時的設定上,大腦那邊同時改的別的項目不會被蓋掉(同一個項目以這台改的為準)。傳出的訊息和動作各帶編號,大腦同一個只做一次。大腦自己拒收的,或大腦一直連得到、這則卻試了 20 次都送不過去(例如檔案太大、線太慢),才放棄、放回輸入框並說原因(試過的會提醒「它也可能已經到了,先看聊天裡有沒有再傳」),後面排著的照送;放棄的設定改動退回大腦的設定並說原因。離線時新增的 Bot 和例行任務,編號從大腦上次的編號之後 1000 起跳(每台不同),大腦那邊同時新增的不會跟它撞號,之後對它的改動和傳給它的訊息都到這台新增的那個。停止、重新產生、還原、執行例行任務、新對話、清除、回答卡片、按鈕送的指令(清除目標、清除計畫…)要連上才能做,不排隊:斷線不到約 100 秒的照樣送、連回來就到(提示「網路不穩,正在重新傳送…」),更久的不做,輸入框下說「這個動作要連上才能做」。按過的允許卡片不會再跳一次。
|
||||
- `account-computers` 帳號裡的電腦:同一個帳號的每台 AnyBot,在線或離線;那台打開「裝置連結」後,會出現在 Bot 可以用的電腦裡。
|
||||
- `account-share` 分享給團隊(登入後):把這台的 Bot 分享給帳號伺服器上的其他帳號,選成員或管理員([team](team.md))。
|
||||
|
|
@ -17,7 +18,7 @@
|
|||
|
||||
## 使用者怎麼到
|
||||
|
||||
- 電腦:設定 › 一般 › 帳戶 ›「登入」(登入後是「管理帳戶」)打開「手機與帳號」頁,左上「‹ 一般」回去。由上到下:「手機與帳號」「AnyBot 帳號」、大腦(登入後)、「同一個網路直接連」「手機可以做什麼」「直接連線的裝置」「分享給團隊」「帳號裡的電腦」(最後兩個登入後才有)。登入後「AnyBot 帳號」裡備份下面隔一條線是「登出帳號」,按右邊的「登出」。
|
||||
- 電腦:設定 › 一般 › 帳戶 ›「登入」(登入後是「管理帳戶」)打開「手機與帳號」頁,左上「‹ 一般」回去。由上到下:「手機與帳號」「AnyBot 帳號」、大腦(登入後)、「同一個網路直接連」「手機可以做什麼」「直接連線的裝置」「分享給團隊」「帳號裡的電腦」(最後兩個登入後才有)。登入後「AnyBot 帳號」裡備份下面隔一條線是「帳號伺服器換了網址」(右邊「換網址」;連不上帳號伺服器時自己展開「新的網址」和「換過去」),再隔一條線是「登出帳號」,按右邊的「登出」。
|
||||
- 手機:帳戶選單 ›「通知」(開關、要通知哪幾種、測試通知);設定 › 手機與帳號:直接連線的手機;用帳號登入時另有通知、兩步驗證、登入的裝置、「加一台電腦」(擁有者)、「切換到別台 AnyBot」;最下面「登出這支手機」(帳戶選單最下面也有,兩邊問的一樣:還有沒送出的訊息就說會清掉幾則)。
|
||||
|
||||
## 怎麼操作(verify-anybot)
|
||||
|
|
@ -35,8 +36,9 @@
|
|||
- **登出這支手機**(covers `account-devices`):指令:`$CTL phone click '[data-s="out"]' && $CTL phone click '#sheet-in .opt' --text 登出 && $CTL phone wait '#f-pass' && $CTL api GET /api/settings --jq '[.devices[].name]'`。看到:回到密碼頁;裝置清單沒有 iPhone 了。
|
||||
- **錯 5 次要等**(covers `account-direct`):同一個位址連打 5 次錯的密碼。指令:`(for i in 1 2 3 4 5; do $CTL api --no-auth --expect 401 POST /api/login '{"password":"wrong","device":"lockout"}' || exit 1; done) && $CTL api --no-auth --expect 429 POST /api/login '{"password":"wrong","device":"lockout"}'`。看到:前 5 次「密碼不對」,第 6 次「密碼錯太多次,請一分鐘後再試」。
|
||||
- **桌面的手機與帳號頁**(covers `account-direct`、`account-devices`、`account-can`):指令:要開視窗——`$CTL stop && ABV_ALLOW_WINDOW=1 $CTL desktop-shot account ANYBOT_SHOT_REAL=1 ANYBOT_SHOT_VIEW=remote`。看到:「同一個網路直接連」的開關和網址、「手機可以做什麼」兩個關著的開關、「直接連線的裝置」。
|
||||
- **帳號伺服器換了網址**(covers `account-move`):scratch 開 `relay/` 帳號伺服器(127.0.0.1),配對碼讓 scratch 加入(`anybot setup 網址 配對碼`,經 `scripts/scratch-env`);關掉它,同一個資料夾換連接埠再開,另開一台空的。指令:`scripts/scratch-env "$SCR" "$PWD/target/debug/anybot" setup --move-to <空的那台>`,再 `--move-to <搬家後的>`,然後 `$CTL serve`;桌面:關掉帳號伺服器、`$CTL stop && ABV_ALLOW_WINDOW=1 $CTL desktop-shot account-unreachable ANYBOT_SHOT_REAL=1 ANYBOT_SHOT_VIEW=remote ANYBOT_SHOT_FRAMES=400`。看到:空的那台「…是另一台帳號伺服器,不認得這台電腦…」,state 沒變;搬家後「✓ 帳號伺服器改用 …」,relay_url 換了、host_id 一樣,`serve.log`「透過帳號 … 連線(新網址)」,`/relay/me` 還是同一台、在線;桌面卡片自己展開「帳號伺服器換了網址」「新的網址」。
|
||||
- **帳號、備份、大腦、分享、推播**(covers `account-login`、`account-backup`、`account-brain`、`account-computers`、`account-share`、`account-push`):指令:走不到——要帳號伺服器(`relay/`)和真的手機;scratch 不准帶 `ANYBOT_ACCOUNT`。看到:最接近的真路是 `relay/` 的 `cargo test`(帳號、通知種類)和下面的自動測試(帳號網址的規則、同步、大腦那一欄)。
|
||||
- 自動測試:`cargo test remote::`、`cargo test remote_ui::`、`cargo test mirror_ui::`(`a_lost_answer_costs_only_what_changed`、`an_edit_sent_twice_is_done_once`、`a_screen_with_a_copy_goes_in_at_once`、`a_message_sent_offline_reaches_the_brain_once`、`a_message_tried_long_ago_is_looked_for_first`、`an_edit_made_offline_lands_on_the_brains_newer_settings`、`a_job_that_never_goes_while_the_brain_answers_is_given_up`、`a_screen_away_doesnt_take_the_brains_numbers`、`the_brains_settings_read_back_the_same`)、`cargo test an_offline_screen_queues_what_is_sent`、`cargo test httpd::`、`cargo test lantls::`(哪些連線可以不加密)、`cargo test drops::`;帳號伺服器在 `relay/` 的 `cargo test`。
|
||||
- 自動測試:`cargo test remote::`(`a_moved_account_server_is_followed_only_when_it_knows_this_computer`)、`cargo test remote_ui::`、`cargo test mirror_ui::`(`a_lost_answer_costs_only_what_changed`、`an_edit_sent_twice_is_done_once`、`a_screen_with_a_copy_goes_in_at_once`、`a_message_sent_offline_reaches_the_brain_once`、`a_message_tried_long_ago_is_looked_for_first`、`an_edit_made_offline_lands_on_the_brains_newer_settings`、`a_job_that_never_goes_while_the_brain_answers_is_given_up`、`a_screen_away_doesnt_take_the_brains_numbers`、`the_brains_settings_read_back_the_same`)、`cargo test an_offline_screen_queues_what_is_sent`、`cargo test httpd::`、`cargo test lantls::`(哪些連線可以不加密)、`cargo test drops::`;帳號伺服器在 `relay/` 的 `cargo test`。
|
||||
|
||||
## 陷阱
|
||||
|
||||
|
|
@ -48,5 +50,6 @@
|
|||
- 第一次用 https 打開,手機會警告憑證:選「繼續」;要確認沒連錯台,比對設定頁上的 SHA-256 指紋。
|
||||
- 螢幕電腦離線時看到的是上次同步的副本:大腦那邊後來的對話、Bot 正在做的事,連上才出現。排著的訊息只送到原本那個大腦:換到別的大腦時先留著,換回來才送。副本只存一份:從大腦 A 換到大腦 B 再換回 A,A 要等第一次回答才進得去。
|
||||
- 一則訊息其實送到了、只是大腦的回答在路上掉了:大腦記得編號一天,這之內重送只算一次;離第一次送出超過 23 小時才再送的,先看大腦的聊天裡有沒有這句(從第一次送出起),有就不再送。靠的是聊天裡的字:指令(/compact 這類不留在聊天的)、Bot 正在做事時插進去還沒讀到的,看不到就會再送一次;別台剛好打了一模一樣的字,會被當成已經到了。大腦在這之間重新啟動過就認不得編號,訊息可能多收一次,設定改動裡的「新增 Bot」可能變成兩個。
|
||||
- 「登出帳號」會把這台從帳號移除:手機從外面連、裝置連結、分享都會斷;這台的 Bot 和聊天還在。
|
||||
- 「登出帳號」會把這台從帳號移除:手機從外面連、裝置連結、分享都會斷;這台的 Bot 和聊天還在。伺服器只是換了網址時用「換網址」,不要登出:登出會在帳號裡留下舊的這台、多一台新的,大腦要重選。
|
||||
- 換網址只認同一份帳號伺服器資料:重建過、或這台已被移除,就只能登出再登入。手機頁面沒有換網址:用新網址重新登入(「加入主畫面」要重加)。
|
||||
- (驗證)錯 5 次之後,127.0.0.1 一分鐘內誰都登不進來(`ctl login-cli`、手機都是):放在最後一步。
|
||||
|
|
|
|||
|
|
@ -31,6 +31,11 @@ pub struct RemoteUi {
|
|||
pub(super) note: Arc<Mutex<String>>,
|
||||
join_rx: Option<Receiver<Result<(Settings, remote::AccountFind), String>>>,
|
||||
restore_rx: Option<Receiver<Result<Vec<u8>, String>>>,
|
||||
/// 換網址: the form is open (it opens by itself while the account server can't be reached),
|
||||
/// the address typed, and the check under way (remote::relay_move).
|
||||
move_open: bool,
|
||||
move_url: String,
|
||||
move_rx: Option<Receiver<Result<Settings, String>>>,
|
||||
busy: Arc<AtomicBool>,
|
||||
pub(super) peers: Arc<Mutex<Vec<(String, String, bool)>>>,
|
||||
last_seen_mark: Option<Instant>,
|
||||
|
|
@ -176,6 +181,24 @@ impl App {
|
|||
}
|
||||
}
|
||||
}
|
||||
if let Some(rx) = &self.rm.move_rx {
|
||||
if let Ok(r) = rx.try_recv() {
|
||||
self.rm.move_rx = None;
|
||||
match r {
|
||||
// Only the address: whatever else changed meanwhile (a name, backups) stays.
|
||||
// The connection restarts on it (remote_apply_settings sees the new address).
|
||||
Ok(s) if s.host_token == self.state.remote.host_token => {
|
||||
*self.rm.note.lock().unwrap() = format!("已改用 {}:帳號、大腦和還沒送出的訊息都照舊", s.relay_url);
|
||||
self.state.remote.relay_url = s.relay_url;
|
||||
self.rm.move_open = false;
|
||||
self.rm.move_url.clear();
|
||||
self.mark();
|
||||
}
|
||||
Ok(_) => *self.rm.note.lock().unwrap() = "這台在檢查時登出或換了帳號,網址沒有換".into(),
|
||||
Err(e) => *self.rm.note.lock().unwrap() = e,
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(rx) = &self.rm.restore_rx {
|
||||
if let Ok(r) = rx.try_recv() {
|
||||
self.rm.restore_rx = None;
|
||||
|
|
@ -861,7 +884,7 @@ impl App {
|
|||
// line, an error (「連不上帳號伺服器:找不到伺服器:…」) pushed the card wider than
|
||||
// the window, and 登出 off its right edge.
|
||||
if !status.is_empty() {
|
||||
ui.add(egui::Label::new(RichText::new(status).size(FONT_XS).color(p.text3)).wrap());
|
||||
ui.add(egui::Label::new(RichText::new(status.as_str()).size(FONT_XS).color(p.text3)).wrap());
|
||||
}
|
||||
hint(ui, format!("手機打開 {} 用同一個帳號登入就能用。可以「加入主畫面」當成 App。", s.relay_url), p);
|
||||
// (A field as wide as the card: a long name like 「…的MacBook Pro (2)」 was cut off.)
|
||||
|
|
@ -884,6 +907,49 @@ impl App {
|
|||
ui.label(RichText::new(format!("上次備份 {t}")).size(FONT_XS).color(p.text3));
|
||||
}
|
||||
});
|
||||
// 換網址: the account server moved (another machine or port, another VPN — going
|
||||
// from Tailscale to headscale killed every *.ts.net name at once). Without this the
|
||||
// only way was to sign out and in again: a new computer in the account (the old
|
||||
// one left behind, its goodbye sent to the dead address) and the brain picked
|
||||
// again. Open by itself while the server can't be reached.
|
||||
let unreachable = status.starts_with("連不上帳號伺服器");
|
||||
ui.add_space(6.0);
|
||||
ui.separator();
|
||||
ui.horizontal(|ui| {
|
||||
ui.set_min_height(30.0);
|
||||
ui.label(RichText::new(t("帳號伺服器換了網址")).size(FONT_SM).strong().family(crate::bold()));
|
||||
help_mark(ui, "帳號伺服器搬到別的位址(換了機器、連接埠或 VPN)時,在這裡填新的網址:AnyBot 先用這台電腦的金鑰確認那邊是同一台帳號伺服器才換,不用登出、不用密碼,大腦、聊天和還沒送出的訊息都照舊。要改用另一台帳號伺服器,請先登出再登入。", p);
|
||||
if !unreachable && !self.rm.move_open {
|
||||
ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
|
||||
if ui.add(secondary_button("換網址")).clicked() {
|
||||
self.rm.move_open = true;
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
if unreachable || self.rm.move_open {
|
||||
if unreachable {
|
||||
hint(ui, "連不上現在的網址。帳號伺服器換了位址的話,填新的網址,不用登出:", p);
|
||||
}
|
||||
form_row(ui, "新的網址", p, |ui| {
|
||||
ui.add(egui::TextEdit::singleline(&mut self.rm.move_url).hint_text(t("https://anybot.你的網域,或 http://100.x.x.x")).desired_width(f32::INFINITY));
|
||||
});
|
||||
ui.horizontal(|ui| {
|
||||
let busy = self.rm.move_rx.is_some();
|
||||
if ui.add_enabled(!busy && !self.rm.move_url.trim().is_empty(), primary_button(if busy { "確認中…" } else { "換過去" }, p)).clicked() {
|
||||
let (tx, rx) = channel();
|
||||
self.rm.move_rx = Some(rx);
|
||||
let (base, url) = (self.state.remote.clone(), self.rm.move_url.trim().to_string());
|
||||
std::thread::spawn(move || {
|
||||
let _ = tx.send(remote::relay_move(&base, &url));
|
||||
});
|
||||
}
|
||||
if !unreachable && ui.add(secondary_button("取消")).clicked() {
|
||||
self.rm.move_open = false;
|
||||
self.rm.move_url.clear();
|
||||
}
|
||||
});
|
||||
}
|
||||
// 登出帳號: a row of its own under a line, like 加一台電腦 (it was red words at the
|
||||
// end of the backup row, not a button and not about backups).
|
||||
ui.add_space(6.0);
|
||||
|
|
|
|||
102
src/remote.rs
102
src/remote.rs
|
|
@ -3271,6 +3271,50 @@ pub fn check_server(url: &str) -> Result<String, String> {
|
|||
Ok(u)
|
||||
}
|
||||
|
||||
/// 換網址: the same account server, reached at a new address (moved to another machine's port,
|
||||
/// the VPN changed — e.g. Tailscale to headscale, where every *.ts.net name died at once). Only
|
||||
/// the address changes: this computer's key, the account, the brain it uses, the copy of the
|
||||
/// brain and what waits to be sent all stay. Taken only when the server at the new address knows
|
||||
/// this computer — asked with this computer's own key, and this computer is among the account's
|
||||
/// computers it lists. No password is sent and nothing is registered: another server (or this
|
||||
/// computer removed from the account) is a sign-out and a sign-in, as before.
|
||||
pub fn relay_move(s: &Settings, new_url: &str) -> Result<Settings, String> {
|
||||
if s.host_token.is_empty() || s.host_id.is_empty() {
|
||||
return Err("這台還沒登入帳號:直接用新的網址登入".into());
|
||||
}
|
||||
let same = |u: &str| base(u).eq_ignore_ascii_case(&base(&s.relay_url));
|
||||
if same(new_url) {
|
||||
return Err("跟現在的網址一樣".into());
|
||||
}
|
||||
let url = base(&check_server(new_url)?);
|
||||
if same(&url) {
|
||||
return Err("跟現在的網址一樣".into());
|
||||
}
|
||||
// This computer's key goes there: only over https, or plain http off the air (plain_hop).
|
||||
if let Some(why) = relay_plain_problem(&url) {
|
||||
return Err(why);
|
||||
}
|
||||
let (st, v) = match relay_agent(15, &url).get(&format!("{url}/relay/host/peers")).header("Authorization", &format!("Bearer {}", s.host_token)).call() {
|
||||
Ok(mut r) => (r.status().as_u16(), serde_json::from_str::<Value>(&r.body_mut().read_to_string().unwrap_or_default()).unwrap_or(Value::Null)),
|
||||
Err(ureq::Error::StatusCode(c)) => (c, Value::Null),
|
||||
Err(e) => return Err(format!("連不到 {url}:{}", why_unreachable(&e))),
|
||||
};
|
||||
let other = || format!("{url} 是另一台帳號伺服器,不認得這台電腦(或這台已經從帳號移除)。要改用那台,請先登出,再用新的網址登入");
|
||||
if st == 401 || st == 403 {
|
||||
return Err(other());
|
||||
}
|
||||
if !(200..300).contains(&st) {
|
||||
return Err(why_refused(st, &v, "帳號伺服器"));
|
||||
}
|
||||
let known = v["hosts"].as_array().is_some_and(|a| a.iter().any(|h| h["id"].as_str() == Some(s.host_id.as_str())));
|
||||
if !known {
|
||||
return Err(other());
|
||||
}
|
||||
let mut moved = s.clone();
|
||||
moved.relay_url = url;
|
||||
Ok(moved)
|
||||
}
|
||||
|
||||
/// AnyBots of the account that lend their hands (not offered as a brain).
|
||||
pub fn is_hands(id: &str) -> bool {
|
||||
hands_ids().lock().unwrap().contains(id)
|
||||
|
|
@ -3732,6 +3776,64 @@ mod tests {
|
|||
assert_eq!(to_brain(&s, "GET", "/x", &[], &[], 10).unwrap_err(), "找不到這個 Bot");
|
||||
}
|
||||
|
||||
/// A stand-in account server for `n` requests: each (path, bearer) → the answer.
|
||||
fn stand_in_relay(n: usize, answer: impl Fn(&str, &str) -> Resp + Send + 'static) -> String {
|
||||
let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let port = l.local_addr().unwrap().port();
|
||||
std::thread::spawn(move || {
|
||||
for c in l.incoming().take(n) {
|
||||
let mut c = c.unwrap();
|
||||
let mut got = vec![];
|
||||
let mut buf = [0u8; 4096];
|
||||
while !got.windows(4).any(|w| w == b"\r\n\r\n") {
|
||||
match std::io::Read::read(&mut c, &mut buf) {
|
||||
Ok(0) | Err(_) => break,
|
||||
Ok(n) => got.extend_from_slice(&buf[..n]),
|
||||
}
|
||||
}
|
||||
let head = String::from_utf8_lossy(&got).to_string();
|
||||
let path = head.split_whitespace().nth(1).unwrap_or("").to_string();
|
||||
let bearer = head.lines().find_map(|l| l.strip_prefix("authorization: Bearer ").or_else(|| l.strip_prefix("Authorization: Bearer "))).unwrap_or("").trim().to_string();
|
||||
httpd::write_response(&mut c, &answer(&path, &bearer));
|
||||
}
|
||||
});
|
||||
format!("http://127.0.0.1:{port}")
|
||||
}
|
||||
|
||||
/// 換網址 (the server moved, e.g. Tailscale → headscale killed every *.ts.net name): taken only
|
||||
/// when the server at the new address knows this computer's key and lists this computer —
|
||||
/// then only the address changes. Another server, a stranger, the same address: refused.
|
||||
#[test]
|
||||
fn a_moved_account_server_is_followed_only_when_it_knows_this_computer() {
|
||||
let s = Settings { relay_url: "https://old.tailbcc3a6.ts.net:8443".into(), relay_user: "wang".into(), host_id: "mac-1".into(), host_token: "key-1".into(), brain_id: "brain-9".into(), brain_name: "雲端大腦".into(), backup_key: "bk".into(), ..Default::default() };
|
||||
let hello = |p: &str| p == "/api/hello";
|
||||
// The same server, new address: it knows the key and lists this computer.
|
||||
let same = stand_in_relay(2, move |p, b| match p {
|
||||
_ if hello(p) => Resp::json(&json!({"app": "anybot-relay"})),
|
||||
"/relay/host/peers" if b == "key-1" => Resp::json(&json!({"hosts": [{"id": "brain-9", "name": "雲端大腦"}, {"id": "mac-1", "name": "Mac mini"}]})),
|
||||
_ => Resp::error(401, "這台電腦的金鑰無效"),
|
||||
});
|
||||
let moved = relay_move(&s, &format!("{same}/")).unwrap();
|
||||
assert_eq!(moved.relay_url, same);
|
||||
assert_eq!((moved.host_id.as_str(), moved.host_token.as_str(), moved.brain_id.as_str(), moved.backup_key.as_str(), moved.relay_user.as_str()), ("mac-1", "key-1", "brain-9", "bk", "wang"));
|
||||
// Another account server: the key means nothing there.
|
||||
let other = stand_in_relay(2, move |p, _| if hello(p) { Resp::json(&json!({"app": "anybot-relay"})) } else { Resp::error(401, "這台電腦的金鑰無效") });
|
||||
let e = relay_move(&s, &other).unwrap_err();
|
||||
assert!(e.contains("另一台帳號伺服器") && e.contains("先登出"), "{e}");
|
||||
// Answers for the key, but this computer isn't among the account's computers.
|
||||
let gone = stand_in_relay(2, move |p, _| if hello(p) { Resp::json(&json!({"app": "anybot-relay"})) } else { Resp::json(&json!({"hosts": [{"id": "brain-9"}]})) });
|
||||
assert!(relay_move(&s, &gone).unwrap_err().contains("另一台帳號伺服器"));
|
||||
// Not an account server at all: nothing with the key is sent.
|
||||
let web = stand_in_relay(1, |_, b| {
|
||||
assert!(b.is_empty(), "the key went to a stranger");
|
||||
Resp::json(&json!({"app": "something-else"}))
|
||||
});
|
||||
assert!(relay_move(&s, &web).unwrap_err().contains("不是 AnyBot 帳號伺服器"));
|
||||
// The address it already uses; a computer not signed in.
|
||||
assert_eq!(relay_move(&moved, &same).unwrap_err(), "跟現在的網址一樣");
|
||||
assert!(relay_move(&Settings::default(), &same).unwrap_err().contains("還沒登入"));
|
||||
}
|
||||
|
||||
/// Every setting the phone can send, which way it moves the guards over a direct connection:
|
||||
/// each loosening is refused, each tightening (and what changes no guard) goes through, and a
|
||||
/// name that isn't exactly one AnyBot knows is refused, not guessed.
|
||||
|
|
|
|||
52
src/setup.rs
52
src/setup.rs
|
|
@ -1199,12 +1199,12 @@ fn seed_first() -> i32 {
|
|||
// ---------- entry ----------
|
||||
|
||||
/// Flags that take a value (so the value isn't read as the address or the code).
|
||||
const VALUED: [&str; 4] = ["--server", "--user", "--name", "--brain-name"];
|
||||
const VALUED: [&str; 5] = ["--server", "--user", "--name", "--brain-name", "--move-to"];
|
||||
|
||||
pub fn run(args: &[String]) -> i32 {
|
||||
let flag = |k: &str| args.iter().position(|a| a == k).and_then(|i| args.get(i + 1)).cloned();
|
||||
if args.iter().any(|a| a == "-h" || a == "--help") {
|
||||
println!("anybot setup:把這台電腦接到你的 AnyBot。\n\n anybot setup 網址 配對碼 (手機或電腦的 AnyBot 按「加一台電腦」會給你這一行)\n anybot setup 一個畫面:填網址和配對碼(或帳號密碼)\n 加 --brain 這台當大腦,不當手腳\n 加 --no-service 不設開機自動執行\n 加 --real-screen 讓 Bot 用這台登入中的桌面(預設用自己的虛擬螢幕)\n anybot setup --seed 寫入第一次的模型(ANYBOT_PROVIDER、ANYBOT_API_KEY)\n\n給程式用:anybot setup --server 網址 --user 帳號 [--brain-name 大腦名字],密碼放在 ANYBOT_ACCOUNT_PASSWORD。");
|
||||
println!("anybot setup:把這台電腦接到你的 AnyBot。\n\n anybot setup 網址 配對碼 (手機或電腦的 AnyBot 按「加一台電腦」會給你這一行)\n anybot setup 一個畫面:填網址和配對碼(或帳號密碼)\n 加 --brain 這台當大腦,不當手腳\n 加 --no-service 不設開機自動執行\n 加 --real-screen 讓 Bot 用這台登入中的桌面(預設用自己的虛擬螢幕)\n anybot setup --seed 寫入第一次的模型(ANYBOT_PROVIDER、ANYBOT_API_KEY)\n anybot setup --move-to 網址 帳號伺服器換了網址(換機器、換 VPN):只換網址,不用密碼、不重新登入\n\n給程式用:anybot setup --server 網址 --user 帳號 [--brain-name 大腦名字],密碼放在 ANYBOT_ACCOUNT_PASSWORD。");
|
||||
return 0;
|
||||
}
|
||||
if args.iter().any(|a| a == "--refresh-service") {
|
||||
|
|
@ -1213,7 +1213,10 @@ pub fn run(args: &[String]) -> i32 {
|
|||
if args.iter().any(|a| a == "--seed") {
|
||||
return seed_first();
|
||||
}
|
||||
let role = if args.iter().any(|a| a == "--brain") { Role::Brain } else { Role::Hands };
|
||||
if args.iter().any(|a| a == "--move-to") {
|
||||
return move_to(&flag("--move-to").unwrap_or_default());
|
||||
}
|
||||
let role =if args.iter().any(|a| a == "--brain") { Role::Brain } else { Role::Hands };
|
||||
let mut pos = vec![];
|
||||
let mut skip = false;
|
||||
for a in args {
|
||||
|
|
@ -1352,6 +1355,49 @@ fn base_settings(name: Option<String>) -> Settings {
|
|||
base
|
||||
}
|
||||
|
||||
/// `anybot setup --move-to 網址`: the account server moved (another machine, another VPN —
|
||||
/// Tailscale to headscale killed every *.ts.net name). Only the address changes, once the server
|
||||
/// there knows this computer's key (remote::relay_move): its registration, the brain it shows or
|
||||
/// lends its hands to, all stay — signing in again would add a second computer to the account and
|
||||
/// leave this one behind. For computers without a window (hands, servers); the desktop does it in
|
||||
/// 設定 › 手機與帳號 ›「帳號伺服器換了網址」.
|
||||
fn move_to(url: &str) -> i32 {
|
||||
header();
|
||||
if url.trim().is_empty() {
|
||||
say(vec![plain(" "), colored("✗ 請給新的網址:anybot setup --move-to 網址", BAD)]);
|
||||
return 1;
|
||||
}
|
||||
// Nothing else may be using the folder while the address is written (a running AnyBot would
|
||||
// put its own copy back).
|
||||
let folder = match crate::lock::take(Duration::from_secs(10)) {
|
||||
Ok(l) => l,
|
||||
Err(e) => {
|
||||
say(vec![plain(" "), colored(format!("✗ {e}"), BAD)]);
|
||||
say(vec![plain(" 先關掉這台的 AnyBot(背景服務:Linux 是 systemctl --user stop anybot),再執行一次;開著視窗的話,也可以在 設定 › 手機與帳號 按「換網址」。")]);
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
let mut st = State::load();
|
||||
let (old, s, url) = (st.remote.relay_url.clone(), st.remote.clone(), url.to_string());
|
||||
match waiting("正在確認新網址是同一台帳號伺服器…", move || remote::relay_move(&s, &url)) {
|
||||
Ok(moved) => {
|
||||
st.remote.relay_url = moved.relay_url.clone();
|
||||
if !st.save() {
|
||||
say(vec![plain(" "), colored("✗ 設定存不進去(看上面的原因),網址沒有換", BAD)]);
|
||||
return 1;
|
||||
}
|
||||
drop(folder);
|
||||
say(vec![plain(" "), colored("✓ ", GOOD), plain(format!("帳號伺服器改用 {}(原本 {old}):這台的登記、大腦和手腳的設定都照舊", moved.relay_url))]);
|
||||
say(vec![plain(" 再打開 AnyBot(背景服務:Linux 是 systemctl --user start anybot)就會用新網址連線。")]);
|
||||
0
|
||||
}
|
||||
Err(e) => {
|
||||
say(vec![plain(" "), colored("✗ ", BAD), colored(e, BAD)]);
|
||||
1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `anybot setup 網址 配對碼`: nothing to answer.
|
||||
fn run_code(role: Role, url: &str, code: &str, name: Option<String>, service: bool) -> i32 {
|
||||
header();
|
||||
|
|
|
|||
Loading…
Reference in New Issue