-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.rs
264 lines (237 loc) · 9.79 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
use apt_swarm::args::{Args, FileOrStdin, SubCommand};
use apt_swarm::config;
use apt_swarm::db::{AccessMode, Database, DatabaseClient};
use apt_swarm::errors::*;
use apt_swarm::fetch;
use apt_swarm::keyring::Keyring;
use apt_swarm::latest;
use apt_swarm::net;
use apt_swarm::p2p;
use apt_swarm::plumbing;
use apt_swarm::signed::Signed;
use apt_swarm::sync;
use chrono::{DateTime, Utc};
use clap::Parser;
use colored::Colorize;
use env_logger::Env;
use futures::StreamExt;
use num_format::{Locale, ToFormattedString};
use sequoia_openpgp::KeyHandle;
use std::borrow::Cow;
use std::sync::Arc;
use tokio::io::{self, AsyncBufReadExt, AsyncWriteExt};
#[tokio::main]
async fn main() -> Result<()> {
#[cfg(target_os = "openbsd")]
pledge::pledge("stdio dns inet rpath wpath cpath flock unix", "")
.context("Failed to setup pledge sandbox")?;
let args = Args::parse();
let log_level = match (args.quiet, args.verbose) {
(0, 0) => "warn,apt_swarm=info",
(1, 0) => "warn",
(_, 0) => "error",
(_, 1) => "info,apt_swarm=debug",
(_, 2) => "debug",
(_, 3) => "debug,apt_swarm=trace",
_ => "trace",
};
env_logger::init_from_env(Env::default().default_filter_or(log_level));
rustls::crypto::ring::default_provider()
.install_default()
.map_err(|_| anyhow!("Failed to install rustls ring CryptoProvider"))?;
let config = config::Config::load_with_args(&args).await;
if args.colors {
colored::control::set_override(true);
}
match args.subcommand {
SubCommand::Import(mut import) => {
let config = config?;
let keyring = Some(Keyring::load(&config)?);
let mut db = Database::open(&config, AccessMode::Exclusive).await?;
FileOrStdin::default_stdin(&mut import.paths);
for path in import.paths {
let reader = path.open().await?;
let mut reader = io::BufReader::new(reader);
while !reader.fill_buf().await?.is_empty() {
let signed = Signed::from_reader(&mut reader)
.await
.context("Failed to parse release file")?;
for (fp, variant) in signed.canonicalize(keyring.as_ref())? {
let fp = fp.context(
"Signature can't be imported because the signature is unverified",
)?;
db.add_release(&fp, &variant).await?;
}
}
}
}
SubCommand::Export(export) => {
let config = config?;
let db = Database::open_directly(&config, AccessMode::Relaxed).await?;
let mut stdout = io::stdout();
if export.release_hashes.is_empty() {
let stream = db.scan_values(&[]);
tokio::pin!(stream);
while let Some(item) = stream.next().await {
let (_hash, data) = item.context("Failed to read from database")?;
stdout.write_all(&data).await?;
}
} else {
for hash in &export.release_hashes {
if export.scan {
let stream = db.scan_values(hash.as_bytes());
tokio::pin!(stream);
while let Some(item) = stream.next().await {
let (_hash, data) = item.context("Failed to read from database")?;
stdout.write_all(&data).await?;
}
} else {
let data = db
.get(hash)
.await
.context("Failed to read database")?
.with_context(|| anyhow!("Failed to find key in database: {hash:?}"))?;
stdout.write_all(&data).await?;
}
}
}
// https://github.com/tokio-rs/tokio/issues/7174
stdout.flush().await?;
}
SubCommand::Fetch(fetch) => {
let config = config?;
let keyring = Keyring::load(&config)?;
let mut db = Database::open_directly(&config, AccessMode::Exclusive).await?;
let keyring = Arc::new(Some(keyring));
fetch::fetch_updates(
&mut db,
keyring,
fetch.concurrency,
config.data.repositories,
args.proxy,
)
.await?;
}
SubCommand::Latest(latest) => {
let config = config?;
let db = Database::open_directly(&config, AccessMode::Relaxed).await?;
let max_allowed_datetime = if latest.allow_future_dates {
DateTime::<Utc>::MAX_UTC
} else {
Utc::now()
};
if let Some((date, mut key, signed, content, idx)) =
latest::find(&db, latest.fingerprint, max_allowed_datetime).await?
{
let mut stdout = io::stdout();
let value: Cow<'_, [u8]> = if latest.key {
key.push(b'\n');
Cow::Owned(key)
} else if latest.date {
let mut date = date.to_rfc3339();
date.push('\n');
Cow::Owned(date.into_bytes())
} else if latest.body {
Cow::Borrowed(&content)
} else if latest.header {
Cow::Borrowed(&content[..idx])
} else if latest.attachment {
Cow::Borrowed(&content[idx..])
} else {
Cow::Borrowed(&signed)
};
stdout.write_all(&value).await?;
// https://github.com/tokio-rs/tokio/issues/7174
stdout.flush().await?;
}
}
SubCommand::Ls(ls) => {
let config = config?;
// TODO: this should call open(), but needs to be rewritten because
// .scan_keys is not available over unix domain socket
let db = Database::open_directly(&config, AccessMode::Relaxed).await?;
let prefix = if let Some(prefix) = &ls.prefix {
prefix.as_bytes()
} else {
&[]
};
let mut stdout = io::stdout();
let mut count = 0;
let stream = db.scan_keys(prefix);
tokio::pin!(stream);
while let Some(item) = stream.next().await {
if ls.count {
count += 1;
continue;
}
let hash = item.context("Failed to read from database (ls)")?;
stdout.write_all(&hash).await?;
stdout.write_all(b"\n").await?;
}
// https://github.com/tokio-rs/tokio/issues/7174
stdout.flush().await?;
if ls.count {
println!("{count}");
}
}
SubCommand::Keyring(args) => {
let config = config?;
let keyring = Keyring::load(&config)?;
let mut db = Database::open(&config, AccessMode::Relaxed).await?;
if args.json {
let keyring = keyring.generate_report()?;
let keyring = serde_json::to_string_pretty(&keyring)
.context("Failed to encode keyring as json")?;
println!("{keyring}");
} else {
for key in keyring.keys.values() {
let hex = key.hex_fingerprint();
for uid in &key.uids {
println!("{} {}", hex.green(), uid.yellow());
}
for (handle, _fp) in &key.key_handles {
if let KeyHandle::Fingerprint(fp) = handle {
let subkey = format!("Subkey {fp:X}");
let stats = if args.stats {
let prefix = format!("{fp:X}/");
let count = db.count(prefix.as_bytes()).await?;
if count > 0 {
let count = count.to_formatted_string(&Locale::en);
Some(format!(" ({count} known signatures)"))
} else {
None
}
} else {
None
};
let stats = stats.as_deref().unwrap_or("");
println!("{} {}{}", hex.green(), subkey.purple(), stats.cyan());
}
}
}
}
}
SubCommand::Pull(pull) => {
let config = config?;
let keyring = Keyring::load(&config)?;
let mut db = Database::open(&config, AccessMode::Exclusive).await?;
let mut sock = net::connect(&pull.addr, args.proxy).await?;
let (rx, mut tx) = sock.split();
let result =
sync::sync_pull(&mut db, &keyring, &pull.keys, pull.dry_run, &mut tx, rx).await;
tx.shutdown().await.ok();
result?;
}
SubCommand::P2p(p2p) => {
let config = config?;
let keyring = Keyring::load(&config)?;
// Explicitly open database, do not test for unix domain socket
let db = Database::open_directly(&config, AccessMode::Exclusive).await?;
p2p::spawn(db, keyring, config, p2p, args.proxy).await?;
}
SubCommand::Plumbing(plumbing) => {
plumbing::run(config, plumbing, args.quiet, args.proxy).await?
}
}
Ok(())
}