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
//! `ewg mesh`: design a mesh in a manifest, then generate each node's config.
use anyhow::{Context, Result};
use std::path::PathBuf;
use crate::manifest;
use crate::manifest::Manifest;
use clap::{Args, Subcommand};
use std::io::IsTerminal;
#[derive(Args)]
pub struct MeshArgs {
#[command(subcommand)]
action: Option<MeshAction>,
/// Verbose: show address and endpoint, not just names
#[arg(short, long, global = true)]
verbose: bool,
/// Machine-readable JSON
#[arg(long, global = true)]
json: bool,
/// Manifest file to read/edit
#[arg(short = 'm', long, global = true, default_value = "mesh.toml")]
manifest: PathBuf,
}
#[derive(Subcommand)]
#[allow(clippy::large_enum_variant)] // clap arg enums: boxing fights the derive
pub enum MeshAction {
/// Add a node to the manifest <NAME>
#[command(verbatim_doc_comment)]
Add {
name: String,
#[arg(long)]
address: String,
#[arg(long)]
pubkey: String,
#[arg(long)]
endpoint: Option<String>,
/// What peers route TO this node: `0.0.0.0/0` (full-tunnel exit) or a LAN
/// subnet (site-to-site). Defaults to this node's own `/32`.
#[arg(long = "allowed-ips")]
allowed_ips: Option<String>,
/// DNS for this node's own interface (e.g. a Pi-hole behind the tunnel)
#[arg(long)]
dns: Option<String>,
/// Seconds between keepalives peers send to this node (e.g. 25)
#[arg(long)]
keepalive: Option<u16>,
/// Hub(s) this spoke dials, by name (repeatable). Omit = all hubs. Ignored
/// for a hub (one with an endpoint), which meshes with everyone.
#[arg(long = "hub")]
hub: Vec<String>,
#[arg(long)]
private: Option<String>,
#[arg(long)]
postup: Option<String>,
#[arg(long)]
postdown: Option<String>,
},
/// Remove a node <NAME>
#[command(verbatim_doc_comment)]
Rm { name: String },
/// List nodes in the manifest (same as bare `mesh`)
#[command(visible_alias = "ls")]
List,
/// Generate each node's wg config from the manifest
/// -o DIR output directory (default: current directory)
#[command(verbatim_doc_comment)]
Gen {
#[arg(short, long, default_value = ".")]
out: PathBuf,
},
}
pub fn run(args: MeshArgs) -> Result<()> {
match args.action {
Some(MeshAction::Add {
name,
address,
pubkey,
endpoint,
allowed_ips,
dns,
keepalive,
hub,
private,
postup,
postdown,
}) => {
let mut m = Manifest::load_or_empty(&args.manifest)?;
m.add(manifest::Node {
name: name.clone(),
address,
public_key: pubkey,
endpoint,
allowed_ips,
dns,
keepalive,
hubs: hub,
private_key: private,
post_up: postup,
post_down: postdown,
})?;
m.save(&args.manifest)?;
println!("added node `{name}`");
}
Some(MeshAction::Rm { name }) => {
let mut m = Manifest::load_or_empty(&args.manifest)?;
m.remove(&name)?;
m.save(&args.manifest)?;
println!("removed node `{name}`");
}
Some(MeshAction::Gen { out }) => {
let manifest = Manifest::load(&args.manifest)?;
std::fs::create_dir_all(&out)
.with_context(|| format!("creating output dir `{}`", out.display()))?;
for node in &manifest.nodes {
let path = out.join(format!("{}.conf", node.name));
std::fs::write(&path, manifest.node_config(node))
.with_context(|| format!("writing `{}`", path.display()))?;
if std::io::stderr().is_terminal() {
eprintln!("wrote {}", path.display());
}
}
}
None | Some(MeshAction::List) => {
let m = Manifest::load_or_empty(&args.manifest)?;
if args.json {
let view: Vec<_> = m
.nodes
.iter()
.map(|n| {
serde_json::json!({
"name": n.name,
"address": n.address,
"mesh_ip": n.mesh_ip(),
"public_key": n.public_key,
"endpoint": n.endpoint,
})
})
.collect();
println!("{}", serde_json::to_string_pretty(&view)?);
} else if args.verbose {
for n in &m.nodes {
println!(
"{:<12} {:<16} {}",
n.name,
n.mesh_ip(),
n.endpoint.as_deref().unwrap_or("-")
);
}
} else {
for n in &m.nodes {
println!("{}", n.name);
}
}
}
}
Ok(())
}