@xiaolikt/ai-vault 0.9.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.lock +122 -4
- package/README.md +364 -222
- package/README.zh.md +388 -0
- package/bin/ai-vault.cjs +6 -0
- package/crates/ai-vault/Cargo.toml +2 -3
- package/crates/ai-vault/src/audit.rs +4 -6
- package/crates/ai-vault/src/cli.rs +56 -41
- package/crates/ai-vault/src/crypto.rs +4 -11
- package/crates/ai-vault/src/dialog.rs +294 -9
- package/crates/ai-vault/src/i18n.rs +56 -0
- package/crates/ai-vault/src/main.rs +2 -0
- package/crates/ai-vault/src/paths.rs +30 -7
- package/crates/ai-vault/src/perms.rs +54 -0
- package/crates/ai-vault/src/sealed.rs +3 -4
- package/crates/ai-vault/src/session.rs +9 -21
- package/package.json +5 -3
- package/packages/pi-ai-vault/README.md +40 -45
- package/packages/pi-ai-vault/README.zh.md +84 -0
- package/packages/pi-ai-vault/package.json +5 -4
- package/packages/pi-ai-vault/src/bitwarden.ts +2 -1
- package/packages/pi-ai-vault/src/cli.ts +17 -7
- package/packages/pi-ai-vault/src/ente.ts +4 -3
- package/packages/pi-ai-vault/src/i18n.ts +31 -0
- package/packages/pi-ai-vault/src/index.ts +77 -76
- package/packages/pi-ai-vault/src/prompt.ts +3 -2
- package/test/cli.sh +6 -4
- package/test/npm.sh +14 -1
- package/tests/mocks/bin/zenity +7 -2
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
//! these commands and never links against the encryption itself.
|
|
4
4
|
|
|
5
5
|
use std::io::{self, BufRead, Read};
|
|
6
|
-
use std::os::unix::fs::PermissionsExt;
|
|
7
6
|
use std::path::PathBuf;
|
|
8
7
|
|
|
9
8
|
use clap::{Parser, Subcommand};
|
|
@@ -13,6 +12,7 @@ use crate::sealed::{self, Kind};
|
|
|
13
12
|
use crate::config::{self, ImportPolicy, UnlockMode, UnlockPolicy};
|
|
14
13
|
use crate::crypto::{self, Kdf};
|
|
15
14
|
use crate::dialog::{self, Answer};
|
|
15
|
+
use crate::i18n::{tr, tr_owned};
|
|
16
16
|
use crate::import;
|
|
17
17
|
use crate::paths::{Paths, GRANT_MS_DEFAULT, GRANT_MS_MAX, IDLE_MS_DEFAULT};
|
|
18
18
|
use crate::session::{self, Session, StoreLock};
|
|
@@ -296,7 +296,7 @@ fn migrate_legacy(paths: &Paths) {
|
|
|
296
296
|
if std::fs::copy(&legacy_store, paths.store()).is_err() {
|
|
297
297
|
return;
|
|
298
298
|
}
|
|
299
|
-
|
|
299
|
+
crate::perms::restrict_file(&paths.store());
|
|
300
300
|
if legacy.join("audit.log").exists() && !paths.audit().exists() {
|
|
301
301
|
let _ = std::fs::copy(legacy.join("audit.log"), paths.audit());
|
|
302
302
|
}
|
|
@@ -591,7 +591,7 @@ fn rekey(paths: &Paths, out: &Out, password_stdin: bool) -> Outcome {
|
|
|
591
591
|
let password = if password_stdin {
|
|
592
592
|
read_password()?
|
|
593
593
|
} else {
|
|
594
|
-
ask_password("ai-vault · 升级密钥参数", "主密码")?
|
|
594
|
+
ask_password(tr("ai-vault · upgrade key parameters", "ai-vault · 升级密钥参数"), tr("master password", "主密码"))?
|
|
595
595
|
};
|
|
596
596
|
let (items, _old_key) = crypto::open_with_passphrase(&store, &password).map_err(|e| match e {
|
|
597
597
|
crypto::OpenError::WrongPassword => Fault::Auth("wrong master password".into()),
|
|
@@ -660,11 +660,11 @@ fn init(paths: &Paths, out: &Out, password_stdin: bool, requester: &str, idle_mi
|
|
|
660
660
|
let password = if password_stdin {
|
|
661
661
|
read_password()?
|
|
662
662
|
} else {
|
|
663
|
-
let first = ask_password("ai-vault · 创建主密码", "至少 8 位")?;
|
|
663
|
+
let first = ask_password(tr("ai-vault · set master password", "ai-vault · 创建主密码"), tr("at least 8 characters", "至少 8 位"))?;
|
|
664
664
|
if first.len() < 8 {
|
|
665
665
|
return Err(Fault::Usage("the master password must be at least 8 characters".into()));
|
|
666
666
|
}
|
|
667
|
-
let again = ask_password("ai-vault · 确认主密码", "再输入一次")?;
|
|
667
|
+
let again = ask_password(tr("ai-vault · confirm master password", "ai-vault · 确认主密码"), tr("type it again", "再输入一次"))?;
|
|
668
668
|
if again != first {
|
|
669
669
|
return Err(Fault::Denied("the two passwords do not match".into()));
|
|
670
670
|
}
|
|
@@ -715,7 +715,7 @@ fn unlock(paths: &Paths, out: &Out, password_stdin: bool, requester: &str, idle_
|
|
|
715
715
|
let password = if password_stdin {
|
|
716
716
|
read_password()?
|
|
717
717
|
} else {
|
|
718
|
-
ask_password("ai-vault · 解锁", "本地库主密码")?
|
|
718
|
+
ask_password(tr("ai-vault · unlock", "ai-vault · 解锁"), tr("local store master password", "本地库主密码"))?
|
|
719
719
|
};
|
|
720
720
|
let (items, key) = crypto::open_with_passphrase(&store, &password).map_err(|e| match e {
|
|
721
721
|
crypto::OpenError::WrongPassword => {
|
|
@@ -795,29 +795,30 @@ fn settings_value(config: &config::Config, paths: &Paths) -> serde_json::Value {
|
|
|
795
795
|
}
|
|
796
796
|
|
|
797
797
|
fn settings_human(config: &config::Config, paths: &Paths) -> String {
|
|
798
|
+
let yes_no = |yes: bool| tr(if yes { "yes" } else { "no" }, if yes { "有" } else { "无" });
|
|
798
799
|
[
|
|
799
800
|
format!("vault.unlock: {} (never | time | session)", config.unlock.mode.as_str()),
|
|
800
|
-
format!("vault.hours: {}
|
|
801
|
-
format!(
|
|
802
|
-
"vault 机器级会话: {}",
|
|
803
|
-
if paths.auto_session().exists() { "有(免密)" } else { "无" }
|
|
804
|
-
),
|
|
801
|
+
format!("vault.hours: {}{}", config.unlock.hours, tr(" (time mode)", " (time 模式)")),
|
|
802
|
+
format!("{} {}", tr("vault machine-wide session:", "vault 机器级会话:"), if paths.auto_session().exists() { tr("yes (no password)", "有(免密)") } else { tr("no", "无") }),
|
|
805
803
|
format!("bitwarden.unlock: {} (never | time | session)", config.bitwarden.store.unlock.as_str()),
|
|
806
804
|
format!("bitwarden.hours: {}", config.bitwarden.store.hours),
|
|
807
805
|
format!("bitwarden.import: {} (ask | always | never)", config.bitwarden.import.as_str()),
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
format!("ente 已落盘的密钥: {}", if sealed::load(paths, Kind::Ente).ok().flatten().is_some() { "有" } else { "无" }),
|
|
811
|
-
format!(
|
|
812
|
-
"bitwarden 已落盘的会话: {}",
|
|
813
|
-
match sealed::load(paths, Kind::Bitwarden).ok().flatten() {
|
|
806
|
+
{
|
|
807
|
+
let bw = match sealed::load(paths, Kind::Bitwarden).ok().flatten() {
|
|
814
808
|
Some(saved) => match saved.expires_at {
|
|
815
|
-
Some(at) =>
|
|
816
|
-
|
|
809
|
+
Some(at) => tr_owned(
|
|
810
|
+
format!("yes (expires in {} min)", ((at - session::now_ms()) / 60_000).max(0)),
|
|
811
|
+
format!("有({} 分钟后过期)", ((at - session::now_ms()) / 60_000).max(0)),
|
|
812
|
+
),
|
|
813
|
+
None => tr("yes (no expiry)", "有(不过期)").to_string(),
|
|
817
814
|
},
|
|
818
|
-
None => "无".to_string(),
|
|
819
|
-
}
|
|
820
|
-
|
|
815
|
+
None => tr("no", "无").to_string(),
|
|
816
|
+
};
|
|
817
|
+
format!("{} {}", tr("bitwarden persisted session:", "bitwarden 已落盘的会话:"), bw)
|
|
818
|
+
},
|
|
819
|
+
format!("ente.unlock: {} (never | time | session)", config.ente.unlock.as_str()),
|
|
820
|
+
format!("ente.hours: {}", config.ente.hours),
|
|
821
|
+
format!("{} {}", tr("ente persisted key:", "ente 已落盘的密钥:"), yes_no(sealed::load(paths, Kind::Ente).ok().flatten().is_some())),
|
|
821
822
|
]
|
|
822
823
|
.join("\n")
|
|
823
824
|
}
|
|
@@ -866,7 +867,7 @@ fn settings(
|
|
|
866
867
|
let password = if password_stdin {
|
|
867
868
|
read_password()?
|
|
868
869
|
} else {
|
|
869
|
-
ask_password("ai-vault · 主密码", "切换解锁模式")?
|
|
870
|
+
ask_password(tr("ai-vault · master password", "ai-vault · 主密码"), tr("switching the unlock policy", "切换解锁模式"))?
|
|
870
871
|
};
|
|
871
872
|
let (_, vault_key) = crypto::open_with_passphrase(&store, &password).map_err(|e| match e {
|
|
872
873
|
crypto::OpenError::WrongPassword => Fault::Auth("wrong master password".into()),
|
|
@@ -1125,10 +1126,17 @@ fn get(
|
|
|
1125
1126
|
// The window is what the user approves: no caller-supplied text may
|
|
1126
1127
|
// break out of its own line here.
|
|
1127
1128
|
let message = format!(
|
|
1128
|
-
"
|
|
1129
|
+
"{} {}\n{} {}\n{} {}\n\n{}",
|
|
1130
|
+
tr("Entry:", "条目:"),
|
|
1129
1131
|
dialog::for_dialog(&item.name, 80),
|
|
1132
|
+
tr("Requester:", "请求方:"),
|
|
1130
1133
|
dialog::for_dialog(&session.requester, 40),
|
|
1131
|
-
|
|
1134
|
+
tr("Reason:", "原因:"),
|
|
1135
|
+
dialog::for_dialog(reason.unwrap_or(&tr("(none given)", "(未说明)").to_string()), 120),
|
|
1136
|
+
tr(
|
|
1137
|
+
"Allowing lets the same requester read it again for 5 minutes. The password enters the conversation.",
|
|
1138
|
+
"允许后 5 分钟内同一请求方可再次读取。密码会进入对话。"
|
|
1139
|
+
)
|
|
1132
1140
|
);
|
|
1133
1141
|
let approved = if answer_stdin {
|
|
1134
1142
|
// Relaying an answer is a host privilege: it must be signed with the
|
|
@@ -1149,7 +1157,7 @@ fn get(
|
|
|
1149
1157
|
}
|
|
1150
1158
|
answer
|
|
1151
1159
|
} else {
|
|
1152
|
-
match dialog::confirm("ai-vault · 读取本地密码", &message) {
|
|
1160
|
+
match dialog::confirm(tr("ai-vault · read local password", "ai-vault · 读取本地密码"), &message) {
|
|
1153
1161
|
Answer::Value(ok) => ok,
|
|
1154
1162
|
Answer::Cancelled => false,
|
|
1155
1163
|
Answer::NoUi => {
|
|
@@ -1208,25 +1216,29 @@ fn add(paths: &Paths, out: &Out, token: Option<String>, req: AddRequest) -> Outc
|
|
|
1208
1216
|
if !req.yes {
|
|
1209
1217
|
let mut meta = Vec::new();
|
|
1210
1218
|
if let Some(user) = req.username.as_deref().map(str::trim).filter(|u| !u.is_empty()) {
|
|
1211
|
-
meta.push(format!("
|
|
1219
|
+
meta.push(format!("{} {}", tr("Username:", "用户名:"), dialog::for_dialog(user, 60)));
|
|
1212
1220
|
}
|
|
1213
1221
|
if let Some(uri) = req.uri.as_deref().map(str::trim).filter(|u| !u.is_empty()) {
|
|
1214
1222
|
meta.push(format!("URI: {}", dialog::for_dialog(uri, 120)));
|
|
1215
1223
|
}
|
|
1216
1224
|
if let Some(notes) = req.notes.as_deref().map(str::trim).filter(|n| !n.is_empty()) {
|
|
1217
|
-
meta.push(format!("
|
|
1225
|
+
meta.push(format!("{} {}", tr("Notes:", "备注:"), dialog::for_dialog(notes, 120)));
|
|
1218
1226
|
}
|
|
1219
1227
|
let shown = dialog::for_dialog(&name, 80);
|
|
1220
1228
|
let action = if existing.is_some() {
|
|
1221
|
-
format!("更新已有条目「{shown}」。")
|
|
1229
|
+
tr_owned(format!("Update the existing entry \"{shown}\"."), format!("更新已有条目「{shown}」。"))
|
|
1222
1230
|
} else {
|
|
1223
|
-
format!("新建条目「{shown}」。")
|
|
1231
|
+
tr_owned(format!("Create the entry \"{shown}\"."), format!("新建条目「{shown}」。"))
|
|
1224
1232
|
};
|
|
1225
1233
|
let message = format!(
|
|
1226
|
-
"{action}{}\n
|
|
1227
|
-
if meta.is_empty() { String::new() } else { format!("\n{}", meta.join("\n")) }
|
|
1234
|
+
"{action}{}\n{}",
|
|
1235
|
+
if meta.is_empty() { String::new() } else { format!("\n{}", meta.join("\n")) },
|
|
1236
|
+
tr(
|
|
1237
|
+
"A window opens next for you to type the password. It never enters the conversation.",
|
|
1238
|
+
"下一步会弹出窗口,由你输入密码。密码不会进入对话。"
|
|
1239
|
+
)
|
|
1228
1240
|
);
|
|
1229
|
-
match dialog::confirm("ai-vault · 保存到本地库", &message) {
|
|
1241
|
+
match dialog::confirm(tr("ai-vault · save to local store", "ai-vault · 保存到本地库"), &message) {
|
|
1230
1242
|
Answer::Value(true) => {}
|
|
1231
1243
|
Answer::Value(false) | Answer::Cancelled => return Err(Fault::Denied(format!("user denied saving \"{name}\""))),
|
|
1232
1244
|
Answer::NoUi => return Err(Fault::NeedsUi("confirm")),
|
|
@@ -1237,9 +1249,9 @@ fn add(paths: &Paths, out: &Out, token: Option<String>, req: AddRequest) -> Outc
|
|
|
1237
1249
|
Some(user) => user,
|
|
1238
1250
|
None => {
|
|
1239
1251
|
let hint = if existing.as_ref().and_then(|i| i.username.as_ref()).is_some() {
|
|
1240
|
-
"用户名(留空保持不变)"
|
|
1252
|
+
tr("username (leave blank to keep)", "用户名(留空保持不变)")
|
|
1241
1253
|
} else {
|
|
1242
|
-
"用户名(可留空)"
|
|
1254
|
+
tr("username (optional)", "用户名(可留空)")
|
|
1243
1255
|
};
|
|
1244
1256
|
ask_text(&format!("ai-vault · {name}"), hint)?
|
|
1245
1257
|
}
|
|
@@ -1247,7 +1259,7 @@ fn add(paths: &Paths, out: &Out, token: Option<String>, req: AddRequest) -> Outc
|
|
|
1247
1259
|
let password = if req.password_stdin {
|
|
1248
1260
|
read_password()?
|
|
1249
1261
|
} else {
|
|
1250
|
-
ask_password("ai-vault · 密码", &name)?
|
|
1262
|
+
ask_password(tr("ai-vault · password", "ai-vault · 密码"), &name)?
|
|
1251
1263
|
};
|
|
1252
1264
|
if password.is_empty() {
|
|
1253
1265
|
return Err(Fault::Denied("cancelled: no password entered".into()));
|
|
@@ -1318,7 +1330,7 @@ fn remove(paths: &Paths, out: &Out, token: Option<String>, name: &str, yes: bool
|
|
|
1318
1330
|
return Err(Fault::Error(format!("no such entry: \"{name}\"")));
|
|
1319
1331
|
}
|
|
1320
1332
|
if !yes {
|
|
1321
|
-
match dialog::confirm("ai-vault · 删除条目", &format!("删除「{}」?删除后无法恢复。", dialog::for_dialog(name, 80))) {
|
|
1333
|
+
match dialog::confirm(tr("ai-vault · delete entry", "ai-vault · 删除条目"), &tr_owned(format!("Delete \"{}\"? This cannot be undone.", dialog::for_dialog(name, 80)), format!("删除「{}」?删除后无法恢复。", dialog::for_dialog(name, 80)))) {
|
|
1322
1334
|
Answer::Value(true) => {}
|
|
1323
1335
|
Answer::Value(false) | Answer::Cancelled => return Err(Fault::Denied("cancelled".into())),
|
|
1324
1336
|
Answer::NoUi => return Err(Fault::NeedsUi("confirm")),
|
|
@@ -1350,7 +1362,7 @@ fn grant(paths: &Paths, out: &Out, token: Option<String>, name: &str, minutes: O
|
|
|
1350
1362
|
let password = if is_host() {
|
|
1351
1363
|
read_password()?
|
|
1352
1364
|
} else {
|
|
1353
|
-
ask_password("ai-vault · 授权读取", "主密码(给一条条目免确认读取)")?
|
|
1365
|
+
ask_password(tr("ai-vault · grant read", "ai-vault · 授权读取"), tr("master password (lets one entry be read without confirmation)", "主密码(给一条条目免确认读取)"))?
|
|
1354
1366
|
};
|
|
1355
1367
|
if let Err(e) = crypto::open_with_passphrase(&store, &password) {
|
|
1356
1368
|
return Err(match e {
|
|
@@ -1432,12 +1444,15 @@ fn import_file(paths: &Paths, out: &Out, token: Option<String>, file: &PathBuf,
|
|
|
1432
1444
|
})
|
|
1433
1445
|
.collect::<Vec<_>>()
|
|
1434
1446
|
.join("\n");
|
|
1435
|
-
let more = if drafts.len() > 15 { format!("\n… 还有 {} 条", drafts.len() - 15) } else { String::new() };
|
|
1447
|
+
let more = if drafts.len() > 15 { tr_owned(format!("\n… {} more", drafts.len() - 15), format!("\n… 还有 {} 条", drafts.len() - 15)) } else { String::new() };
|
|
1436
1448
|
let message = format!(
|
|
1437
|
-
"
|
|
1449
|
+
"{} {} {}\n\n{shown}{more}\n\n{}",
|
|
1450
|
+
tr("Matched", "匹配"),
|
|
1451
|
+
drafts.len(),
|
|
1452
|
+
tr("items. Login items with a password are written to the local encrypted store; same-name entries are updated. Import?", "条。其中带密码的登录项会写入本地加密库,同名条目会被更新。允许导入?"),
|
|
1438
1453
|
drafts.len()
|
|
1439
1454
|
);
|
|
1440
|
-
match dialog::confirm("ai-vault · 从 Bitwarden 导入", &message) {
|
|
1455
|
+
match dialog::confirm(tr("ai-vault · import from Bitwarden", "ai-vault · 从 Bitwarden 导入"), &message) {
|
|
1441
1456
|
Answer::Value(true) => {}
|
|
1442
1457
|
Answer::Value(false) | Answer::Cancelled => return Err(Fault::Denied("user denied the Bitwarden import".into())),
|
|
1443
1458
|
Answer::NoUi => return Err(Fault::NeedsUi("confirm")),
|
|
@@ -8,9 +8,8 @@
|
|
|
8
8
|
//! Identical to the format the TypeScript release used, so an existing store
|
|
9
9
|
//! opens without a migration step.
|
|
10
10
|
|
|
11
|
-
use std::fs
|
|
11
|
+
use std::fs;
|
|
12
12
|
use std::io::Write;
|
|
13
|
-
use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
|
|
14
13
|
use std::path::Path;
|
|
15
14
|
|
|
16
15
|
use base64::engine::general_purpose::STANDARD as B64;
|
|
@@ -184,7 +183,7 @@ pub fn read_store(path: &Path) -> Result<Option<StoreFile>, String> {
|
|
|
184
183
|
pub fn write_atomic(path: &Path, bytes: &[u8]) -> Result<(), String> {
|
|
185
184
|
let dir = path.parent().unwrap_or(Path::new("."));
|
|
186
185
|
fs::create_dir_all(dir).map_err(|e| format!("cannot create {}: {e}", dir.display()))?;
|
|
187
|
-
|
|
186
|
+
crate::perms::restrict_dir(dir);
|
|
188
187
|
|
|
189
188
|
let tmp = dir.join(format!(
|
|
190
189
|
".{}.{}.{}.tmp",
|
|
@@ -192,11 +191,7 @@ pub fn write_atomic(path: &Path, bytes: &[u8]) -> Result<(), String> {
|
|
|
192
191
|
std::process::id(),
|
|
193
192
|
B64.encode(random_bytes::<6>()).replace(['/', '+', '='], "")
|
|
194
193
|
));
|
|
195
|
-
let mut file =
|
|
196
|
-
.write(true)
|
|
197
|
-
.create_new(true)
|
|
198
|
-
.mode(0o600)
|
|
199
|
-
.open(&tmp)
|
|
194
|
+
let mut file = crate::perms::open_private(&tmp)
|
|
200
195
|
.map_err(|e| format!("cannot create a temp file next to {}: {e}", path.display()))?;
|
|
201
196
|
let outcome = (|| -> std::io::Result<()> {
|
|
202
197
|
file.write_all(bytes)?;
|
|
@@ -208,9 +203,7 @@ pub fn write_atomic(path: &Path, bytes: &[u8]) -> Result<(), String> {
|
|
|
208
203
|
let _ = fs::remove_file(&tmp);
|
|
209
204
|
return Err(format!("cannot write {}: {e}", path.display()));
|
|
210
205
|
}
|
|
211
|
-
|
|
212
|
-
let _ = dir_file.sync_all();
|
|
213
|
-
}
|
|
206
|
+
crate::perms::sync_dir(dir);
|
|
214
207
|
Ok(())
|
|
215
208
|
}
|
|
216
209
|
|
|
@@ -1,11 +1,18 @@
|
|
|
1
|
-
//! Desktop dialogs
|
|
1
|
+
//! Desktop dialogs.
|
|
2
2
|
//!
|
|
3
|
-
//!
|
|
4
|
-
//!
|
|
5
|
-
//!
|
|
3
|
+
//! Linux zenity (X11 or Wayland)
|
|
4
|
+
//! macOS osascript (a native dialog, hidden answer for secrets)
|
|
5
|
+
//! Windows PowerShell + WinForms (a native dialog, PasswordChar for secrets)
|
|
6
|
+
//!
|
|
7
|
+
//! When no window can open at all, the caller gets `NoUi` and the host UI
|
|
8
|
+
//! (pi/omp prompt) takes over. Cancel and our own timeout end the prompt here:
|
|
9
|
+
//! they never turn into a second prompt.
|
|
6
10
|
|
|
11
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
7
12
|
use std::env;
|
|
13
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
8
14
|
use std::path::Path;
|
|
15
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
9
16
|
use std::process::{Command, Stdio};
|
|
10
17
|
|
|
11
18
|
const DIALOG_SECONDS: u32 = 600;
|
|
@@ -20,11 +27,240 @@ pub enum Answer<T> {
|
|
|
20
27
|
}
|
|
21
28
|
|
|
22
29
|
pub fn has_display() -> bool {
|
|
23
|
-
[
|
|
24
|
-
|
|
25
|
-
|
|
30
|
+
#[cfg(target_os = "linux")]
|
|
31
|
+
{
|
|
32
|
+
return ["DISPLAY", "WAYLAND_DISPLAY"]
|
|
33
|
+
.iter()
|
|
34
|
+
.any(|name| env::var_os(name).is_some_and(|v| !v.is_empty()));
|
|
35
|
+
}
|
|
36
|
+
// macOS and Windows always have a window server for an interactive user.
|
|
37
|
+
#[cfg(not(target_os = "linux"))]
|
|
38
|
+
{
|
|
39
|
+
true
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// ---------------------------------------------------------------- macOS
|
|
44
|
+
|
|
45
|
+
#[cfg(target_os = "macos")]
|
|
46
|
+
mod mac {
|
|
47
|
+
use super::{Answer, DIALOG_SECONDS};
|
|
48
|
+
use std::process::{Command, Stdio};
|
|
49
|
+
|
|
50
|
+
/// Escape a value for AppleScript's double-quoted string literals.
|
|
51
|
+
fn quote(text: &str) -> String {
|
|
52
|
+
text.replace('\\', "\\\\").replace('"', "\\\"")
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
struct Out {
|
|
56
|
+
spawned: bool,
|
|
57
|
+
code: i32,
|
|
58
|
+
stdout: String,
|
|
59
|
+
stderr: String,
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
fn osascript(script: &str) -> Out {
|
|
63
|
+
match Command::new("osascript")
|
|
64
|
+
.args(["-e", script])
|
|
65
|
+
.stdin(Stdio::null())
|
|
66
|
+
.stdout(Stdio::piped())
|
|
67
|
+
.stderr(Stdio::piped())
|
|
68
|
+
.output()
|
|
69
|
+
{
|
|
70
|
+
Ok(out) => Out {
|
|
71
|
+
spawned: true,
|
|
72
|
+
code: out.status.code().unwrap_or(-1),
|
|
73
|
+
stdout: String::from_utf8_lossy(&out.stdout).into_owned(),
|
|
74
|
+
stderr: String::from_utf8_lossy(&out.stderr).into_owned(),
|
|
75
|
+
},
|
|
76
|
+
Err(_) => Out { spawned: false, code: 127, stdout: String::new(), stderr: String::new() },
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
fn failed(out: &Out) -> bool {
|
|
81
|
+
!out.spawned
|
|
82
|
+
|| {
|
|
83
|
+
let e = out.stderr.to_lowercase();
|
|
84
|
+
e.contains("not allowed") || e.contains("application isn")
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
pub fn prompt_secret(title: &str, label: &str) -> Answer<String> {
|
|
89
|
+
let script = format!(
|
|
90
|
+
"tell application \"System Events\" to display dialog \"{}\" with title \"{}\" default answer \"\" with hidden answer buttons {{\"{}\", \"OK\"}} default button \"OK\" giving up after {}",
|
|
91
|
+
quote(label),
|
|
92
|
+
quote(title),
|
|
93
|
+
crate::i18n::tr("Cancel", "取消"),
|
|
94
|
+
DIALOG_SECONDS
|
|
95
|
+
);
|
|
96
|
+
let out = osascript(&script);
|
|
97
|
+
if failed(&out) {
|
|
98
|
+
return Answer::NoUi;
|
|
99
|
+
}
|
|
100
|
+
if !out.stdout.contains("text returned:") {
|
|
101
|
+
return Answer::Cancelled;
|
|
102
|
+
}
|
|
103
|
+
let value = out
|
|
104
|
+
.stdout
|
|
105
|
+
.split("text returned:")
|
|
106
|
+
.nth(1)
|
|
107
|
+
.unwrap_or("")
|
|
108
|
+
.trim()
|
|
109
|
+
.to_string();
|
|
110
|
+
if value.is_empty() {
|
|
111
|
+
Answer::Cancelled
|
|
112
|
+
} else {
|
|
113
|
+
Answer::Value(value)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
pub fn prompt_text(title: &str, label: &str) -> Answer<String> {
|
|
118
|
+
let script = format!(
|
|
119
|
+
"tell application \"System Events\" to display dialog \"{}\" with title \"{}\" default answer \"\" buttons {{\"{}\", \"OK\"}} default button \"OK\" giving up after {}",
|
|
120
|
+
quote(label),
|
|
121
|
+
quote(title),
|
|
122
|
+
crate::i18n::tr("Cancel", "取消"),
|
|
123
|
+
DIALOG_SECONDS
|
|
124
|
+
);
|
|
125
|
+
let out = osascript(&script);
|
|
126
|
+
if failed(&out) {
|
|
127
|
+
return Answer::NoUi;
|
|
128
|
+
}
|
|
129
|
+
if !out.stdout.contains("text returned:") {
|
|
130
|
+
return Answer::Cancelled;
|
|
131
|
+
}
|
|
132
|
+
Answer::Value(
|
|
133
|
+
out.stdout
|
|
134
|
+
.split("text returned:")
|
|
135
|
+
.nth(1)
|
|
136
|
+
.unwrap_or("")
|
|
137
|
+
.trim()
|
|
138
|
+
.to_string(),
|
|
139
|
+
)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
pub fn confirm(title: &str, message: &str) -> Answer<bool> {
|
|
143
|
+
let script = format!(
|
|
144
|
+
"tell application \"System Events\" to display dialog \"{}\" with title \"{}\" buttons {{\"{}\", \"{}\"}} default button \"{}\" giving up after {}",
|
|
145
|
+
quote(message),
|
|
146
|
+
quote(title),
|
|
147
|
+
crate::i18n::tr("Deny", "拒绝"),
|
|
148
|
+
crate::i18n::tr("Allow", "允许"),
|
|
149
|
+
crate::i18n::tr("Deny", "拒绝"),
|
|
150
|
+
DIALOG_SECONDS
|
|
151
|
+
);
|
|
152
|
+
let out = osascript(&script);
|
|
153
|
+
if failed(&out) {
|
|
154
|
+
return Answer::NoUi;
|
|
155
|
+
}
|
|
156
|
+
let allow = crate::i18n::tr("Allow", "允许");
|
|
157
|
+
Answer::Value(out.code == 0 && out.stdout.contains(&format!("button returned:{allow}")))
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// -------------------------------------------------------------- Windows
|
|
162
|
+
|
|
163
|
+
#[cfg(target_os = "windows")]
|
|
164
|
+
mod win {
|
|
165
|
+
use super::{Answer, DIALOG_SECONDS};
|
|
166
|
+
use std::process::{Command, Stdio};
|
|
167
|
+
|
|
168
|
+
// WinForms from PowerShell: no extra dependency, and PasswordChar can hide
|
|
169
|
+
// the input. Exit 0 = OK (value on stdout), 1 = cancel, 2 = no window.
|
|
170
|
+
const PREAMBLE: &str = r#"
|
|
171
|
+
Add-Type -AssemblyName System.Windows.Forms, System.Drawing
|
|
172
|
+
$f = New-Object System.Windows.Forms.Form
|
|
173
|
+
$f.Text = $env:AI_VAULT_DLG_TITLE
|
|
174
|
+
$f.Width = 480; $f.Height = 200; $f.StartPosition = 'CenterScreen'
|
|
175
|
+
$f.TopMost = $true; $f.FormBorderStyle = 'FixedDialog'; $f.MaximizeBox = $false
|
|
176
|
+
$lbl = New-Object System.Windows.Forms.Label
|
|
177
|
+
$lbl.Text = $env:AI_VAULT_DLG_TEXT; $lbl.Left = 12; $lbl.Top = 12; $lbl.Width = 440; $lbl.Height = 60
|
|
178
|
+
$f.Controls.Add($lbl)
|
|
179
|
+
$box = New-Object System.Windows.Forms.TextBox
|
|
180
|
+
$box.Left = 12; $box.Top = 80; $box.Width = 440
|
|
181
|
+
if ($env:AI_VAULT_DLG_SECRET -eq '1') { $box.UseSystemPasswordChar = $true }
|
|
182
|
+
$f.Controls.Add($box)
|
|
183
|
+
$ok = New-Object System.Windows.Forms.Button
|
|
184
|
+
$ok.Text = 'OK'; $ok.DialogResult = 'OK'; $ok.Left = 280; $ok.Top = 115; $f.AcceptButton = $ok
|
|
185
|
+
$cancel = New-Object System.Windows.Forms.Button
|
|
186
|
+
$cancel.Text = $env:AI_VAULT_DLG_CANCEL; $cancel.DialogResult = 'Cancel'; $cancel.Left = 370; $cancel.Top = 115; $f.CancelButton = $cancel
|
|
187
|
+
$f.Controls.Add($ok); $f.Controls.Add($cancel)
|
|
188
|
+
$f.Add_Shown({ $box.Focus() })
|
|
189
|
+
if ($f.ShowDialog() -eq 'OK') { [Console]::Out.Write($box.Text); exit 0 } else { exit 1 }
|
|
190
|
+
"#;
|
|
191
|
+
|
|
192
|
+
const CONFIRM: &str = r#"
|
|
193
|
+
Add-Type -AssemblyName System.Windows.Forms, System.Drawing
|
|
194
|
+
$msg = $env:AI_VAULT_DLG_TEXT
|
|
195
|
+
$r = [System.Windows.Forms.MessageBox]::Show($msg, $env:AI_VAULT_DLG_TITLE, 'YesNo', 'Warning', 'Button2')
|
|
196
|
+
if ($r -eq 'Yes') { exit 0 } else { exit 1 }
|
|
197
|
+
"#;
|
|
198
|
+
|
|
199
|
+
struct Out {
|
|
200
|
+
spawned: bool,
|
|
201
|
+
code: i32,
|
|
202
|
+
stdout: String,
|
|
203
|
+
stderr: String,
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
fn powershell(script: &str, title: &str, text: &str, secret: bool) -> Out {
|
|
207
|
+
let mut cmd = Command::new("powershell");
|
|
208
|
+
cmd.args(["-NoProfile", "-NonInteractive", "-STA", "-Command", script])
|
|
209
|
+
.env("AI_VAULT_DLG_TITLE", title)
|
|
210
|
+
.env("AI_VAULT_DLG_TEXT", text)
|
|
211
|
+
.env("AI_VAULT_DLG_SECRET", if secret { "1" } else { "0" })
|
|
212
|
+
.env("AI_VAULT_DLG_CANCEL", crate::i18n::tr("Cancel", "取消"))
|
|
213
|
+
.stdin(Stdio::null())
|
|
214
|
+
.stdout(Stdio::piped())
|
|
215
|
+
.stderr(Stdio::piped());
|
|
216
|
+
let _ = DIALOG_SECONDS; // the dialog is modal for the user, not us
|
|
217
|
+
match cmd.output() {
|
|
218
|
+
Ok(out) => Out {
|
|
219
|
+
spawned: true,
|
|
220
|
+
code: out.status.code().unwrap_or(-1),
|
|
221
|
+
stdout: String::from_utf8_lossy(&out.stdout).into_owned(),
|
|
222
|
+
stderr: String::from_utf8_lossy(&out.stderr).into_owned(),
|
|
223
|
+
},
|
|
224
|
+
Err(_) => Out { spawned: false, code: 127, stdout: String::new(), stderr: String::new() },
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
fn failed(out: &Out) -> bool {
|
|
229
|
+
!out.spawned || (out.code != 0 && out.code != 1)
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
pub fn prompt_secret(title: &str, label: &str) -> Answer<String> {
|
|
233
|
+
let out = powershell(PREAMBLE, title, label, true);
|
|
234
|
+
if failed(&out) {
|
|
235
|
+
return Answer::NoUi;
|
|
236
|
+
}
|
|
237
|
+
if out.code != 0 || out.stdout.is_empty() {
|
|
238
|
+
return Answer::Cancelled;
|
|
239
|
+
}
|
|
240
|
+
Answer::Value(out.stdout)
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
pub fn prompt_text(title: &str, label: &str) -> Answer<String> {
|
|
244
|
+
let out = powershell(PREAMBLE, title, label, false);
|
|
245
|
+
if failed(&out) {
|
|
246
|
+
return Answer::NoUi;
|
|
247
|
+
}
|
|
248
|
+
if out.code != 0 {
|
|
249
|
+
return Answer::Cancelled;
|
|
250
|
+
}
|
|
251
|
+
Answer::Value(out.stdout)
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
pub fn confirm(title: &str, message: &str) -> Answer<bool> {
|
|
255
|
+
let out = powershell(CONFIRM, title, message, false);
|
|
256
|
+
if failed(&out) {
|
|
257
|
+
return Answer::NoUi;
|
|
258
|
+
}
|
|
259
|
+
Answer::Value(out.code == 0)
|
|
260
|
+
}
|
|
26
261
|
}
|
|
27
262
|
|
|
263
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
28
264
|
struct Zenity {
|
|
29
265
|
/// False when the process could not be started at all (zenity missing).
|
|
30
266
|
spawned: bool,
|
|
@@ -33,6 +269,7 @@ struct Zenity {
|
|
|
33
269
|
stderr: String,
|
|
34
270
|
}
|
|
35
271
|
|
|
272
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
36
273
|
fn zenity(args: &[String]) -> Zenity {
|
|
37
274
|
match Command::new("zenity")
|
|
38
275
|
.args(args)
|
|
@@ -51,14 +288,17 @@ fn zenity(args: &[String]) -> Zenity {
|
|
|
51
288
|
}
|
|
52
289
|
}
|
|
53
290
|
|
|
291
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
54
292
|
fn arg(text: impl Into<String>) -> String {
|
|
55
293
|
text.into()
|
|
56
294
|
}
|
|
57
295
|
|
|
296
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
58
297
|
fn timeout_arg() -> String {
|
|
59
298
|
format!("--timeout={DIALOG_SECONDS}")
|
|
60
299
|
}
|
|
61
300
|
|
|
301
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
62
302
|
fn trim_newline(text: &str) -> String {
|
|
63
303
|
text.trim_end_matches(['\r', '\n']).to_string()
|
|
64
304
|
}
|
|
@@ -66,6 +306,7 @@ fn trim_newline(text: &str) -> String {
|
|
|
66
306
|
/// True when the configured display looks usable. Only consulted when zenity
|
|
67
307
|
/// exits 1 with no output at all: a cancel on a live display, or a silent
|
|
68
308
|
/// start failure on a dead one.
|
|
309
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
69
310
|
fn display_reachable() -> bool {
|
|
70
311
|
if let Some(wl) = env::var_os("WAYLAND_DISPLAY").filter(|v| !v.is_empty()) {
|
|
71
312
|
let runtime = match env::var_os("XDG_RUNTIME_DIR") {
|
|
@@ -94,6 +335,7 @@ fn display_reachable() -> bool {
|
|
|
94
335
|
/// and a failed start both use it), so stderr decides: GTK's own diagnostics
|
|
95
336
|
/// carry a `(zenity:PID): Gtk-WARNING` prefix; any other line, or a display
|
|
96
337
|
/// failure, means the window never opened.
|
|
338
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
97
339
|
fn failed_to_open(z: &Zenity) -> bool {
|
|
98
340
|
if !z.spawned {
|
|
99
341
|
return true;
|
|
@@ -118,6 +360,7 @@ fn failed_to_open(z: &Zenity) -> bool {
|
|
|
118
360
|
.any(|line| !is_gtk_diagnostic(line))
|
|
119
361
|
}
|
|
120
362
|
|
|
363
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
121
364
|
fn is_gtk_diagnostic(line: &str) -> bool {
|
|
122
365
|
// (zenity:1234): Gtk-WARNING **: ...
|
|
123
366
|
let line = line.trim_start_matches('*');
|
|
@@ -163,6 +406,20 @@ pub fn for_dialog(value: &str, max: usize) -> String {
|
|
|
163
406
|
|
|
164
407
|
/// Secret entry. Empty and cancel are both "no value".
|
|
165
408
|
pub fn prompt_secret(title: &str, label: &str) -> Answer<String> {
|
|
409
|
+
#[cfg(target_os = "macos")]
|
|
410
|
+
{
|
|
411
|
+
return mac::prompt_secret(title, label);
|
|
412
|
+
}
|
|
413
|
+
#[cfg(target_os = "windows")]
|
|
414
|
+
{
|
|
415
|
+
return win::prompt_secret(title, label);
|
|
416
|
+
}
|
|
417
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
418
|
+
prompt_secret_zenity(title, label)
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
422
|
+
fn prompt_secret_zenity(title: &str, label: &str) -> Answer<String> {
|
|
166
423
|
if !has_display() {
|
|
167
424
|
return Answer::NoUi;
|
|
168
425
|
}
|
|
@@ -185,6 +442,20 @@ pub fn prompt_secret(title: &str, label: &str) -> Answer<String> {
|
|
|
185
442
|
|
|
186
443
|
/// Plain text entry. Cancel is `Cancelled`; confirming an empty field is `""`.
|
|
187
444
|
pub fn prompt_text(title: &str, label: &str) -> Answer<String> {
|
|
445
|
+
#[cfg(target_os = "macos")]
|
|
446
|
+
{
|
|
447
|
+
return mac::prompt_text(title, label);
|
|
448
|
+
}
|
|
449
|
+
#[cfg(target_os = "windows")]
|
|
450
|
+
{
|
|
451
|
+
return win::prompt_text(title, label);
|
|
452
|
+
}
|
|
453
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
454
|
+
prompt_text_zenity(title, label)
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
458
|
+
fn prompt_text_zenity(title: &str, label: &str) -> Answer<String> {
|
|
188
459
|
if !has_display() {
|
|
189
460
|
return Answer::NoUi;
|
|
190
461
|
}
|
|
@@ -208,6 +479,20 @@ pub fn prompt_text(title: &str, label: &str) -> Answer<String> {
|
|
|
208
479
|
|
|
209
480
|
/// Yes/no window. Deny and timeout are false and do not fall through.
|
|
210
481
|
pub fn confirm(title: &str, message: &str) -> Answer<bool> {
|
|
482
|
+
#[cfg(target_os = "macos")]
|
|
483
|
+
{
|
|
484
|
+
return mac::confirm(title, message);
|
|
485
|
+
}
|
|
486
|
+
#[cfg(target_os = "windows")]
|
|
487
|
+
{
|
|
488
|
+
return win::confirm(title, message);
|
|
489
|
+
}
|
|
490
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
491
|
+
confirm_zenity(title, message)
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
495
|
+
fn confirm_zenity(title: &str, message: &str) -> Answer<bool> {
|
|
211
496
|
if !has_display() {
|
|
212
497
|
return Answer::NoUi;
|
|
213
498
|
}
|
|
@@ -219,8 +504,8 @@ pub fn confirm(title: &str, message: &str) -> Answer<bool> {
|
|
|
219
504
|
arg("--no-markup"),
|
|
220
505
|
arg("--width=520"),
|
|
221
506
|
arg("--default-cancel"),
|
|
222
|
-
arg("--ok-label
|
|
223
|
-
arg("--cancel-label
|
|
507
|
+
arg(format!("--ok-label={}", crate::i18n::tr("Allow", "允许"))),
|
|
508
|
+
arg(format!("--cancel-label={}", crate::i18n::tr("Deny", "拒绝"))),
|
|
224
509
|
timeout_arg(),
|
|
225
510
|
]);
|
|
226
511
|
if failed_to_open(&z) {
|