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
|
use std::fs::{self, File};
use std::io::{self, BufRead, BufReader};
use std::net::{IpAddr, Ipv4Addr};
use std::thread;
use rsdsl_netlinklib::blocking::Connection;
use thiserror::Error;
use wireguard_control::{AllowedIp, Backend, DeviceUpdate, Key, KeyPair, PeerConfigBuilder};
const IFNAME: &str = "wg0";
const INNER_ADDRESS: IpAddr = IpAddr::V4(Ipv4Addr::new(10, 128, 50, 254));
const PORT: u16 = 51820;
const CONFIGFILE_PRIVATEKEY: &str = "/data/wgd.key";
const CONFIGFILE_PEERS: &str = "/data/wgd.peers";
#[derive(Debug, Error)]
enum Error {
#[error(
"too few peer config columns (want <name> <pubkey> <psk> <allowed-ip> [allowed-ip...])"
)]
TooFewPeerColumns,
#[error("invalid allowed ip {0}")]
InvalidAllowedIp(String),
#[error("io: {0}")]
Io(#[from] io::Error),
#[error("invalid base64 key: {0}")]
InvalidKey(String),
#[error("rsdsl_netlinklib: {0}")]
RsdslNetlinklib(#[from] rsdsl_netlinklib::Error),
}
type Result<T> = std::result::Result<T, Error>;
fn main() -> Result<()> {
println!("[info] init");
let connection = Connection::new()?;
if !connection.link_exists(String::from(IFNAME))? {
connection.link_add_wireguard(String::from(IFNAME))?;
println!("[info] create link {}", IFNAME);
} else {
println!("[info] link {} exists", IFNAME);
}
let keypair = read_or_generate_keypair()?;
println!("[info] pubkey {}", keypair.public.to_base64());
configure_link(&connection, keypair)?;
println!("[info] config {}", IFNAME);
loop {
thread::park();
}
}
fn configure_link(connection: &Connection, keypair: KeyPair) -> Result<()> {
unconfigure_link(connection)?;
println!("[info] unconfig {}", IFNAME);
DeviceUpdate::new()
.add_peers(&read_peers()?)
.set_listen_port(PORT)
.set_keypair(keypair)
.apply(&IFNAME.parse().expect("valid link name"), Backend::Kernel)?;
connection.address_add(String::from(IFNAME), INNER_ADDRESS, 24)?;
connection.link_set(String::from(IFNAME), true)?;
Ok(())
}
fn unconfigure_link(connection: &Connection) -> Result<()> {
connection.address_flush(String::from(IFNAME))?;
DeviceUpdate::new()
.replace_peers()
.set_listen_port(PORT)
.unset_fwmark()
.unset_private_key()
.unset_public_key()
.apply(&IFNAME.parse().expect("valid link name"), Backend::Kernel)?;
Ok(())
}
fn read_or_generate_keypair() -> Result<KeyPair> {
match read_keypair() {
Ok(keypair) => Ok(keypair),
Err(e) => {
println!("[warn] unable to read keypair: {}", e);
generate_and_save_keypair()
}
}
}
fn read_keypair() -> Result<KeyPair> {
let private_base64 = fs::read_to_string(CONFIGFILE_PRIVATEKEY)?;
let private_key =
Key::from_base64(&private_base64).map_err(|_| Error::InvalidKey(private_base64))?;
Ok(KeyPair::from_private(private_key))
}
fn generate_and_save_keypair() -> Result<KeyPair> {
let keypair = KeyPair::generate();
fs::write(CONFIGFILE_PRIVATEKEY, keypair.private.to_base64())?;
Ok(keypair)
}
fn read_peers() -> Result<Vec<PeerConfigBuilder>> {
let file = File::open(CONFIGFILE_PEERS)?;
let br = BufReader::new(file);
let mut peers = Vec::new();
for line in br.lines() {
let line = line?;
let mut columns = line.split_whitespace();
let name = columns.next().ok_or(Error::TooFewPeerColumns)?;
let public_key_base64 = columns.next().ok_or(Error::TooFewPeerColumns)?;
let preshared_key_base64 = columns.next().ok_or(Error::TooFewPeerColumns)?;
let public_key = Key::from_base64(public_key_base64)
.map_err(|_| Error::InvalidKey(public_key_base64.to_string()))?;
let preshared_key = Key::from_base64(preshared_key_base64)
.map_err(|_| Error::InvalidKey(preshared_key_base64.to_string()))?;
let mut builder = PeerConfigBuilder::new(&public_key)
.replace_allowed_ips()
.set_preshared_key(preshared_key);
println!(
"[info] peer {} pubkey {} psk (hidden)",
name, public_key_base64
);
for column in columns {
let allowed_ip: AllowedIp = column
.parse()
.map_err(|_| Error::InvalidAllowedIp(column.to_string()))?;
builder = builder.add_allowed_ip(allowed_ip.address, allowed_ip.cidr);
println!(
"[info] peer {} allowedip {}/{}",
name, allowed_ip.address, allowed_ip.cidr
);
}
peers.push(builder);
}
Ok(peers)
}
|