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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
use std::fmt;
use std::fs::File;
use std::net::{Ipv4Addr, Ipv6Addr};
use std::str::FromStr;
use std::sync::{mpsc, Arc};
use std::thread;
use std::time::Duration;
use inwx::call::nameserver::{RecordInfo as RecordInfoCall, RecordUpdate};
use inwx::common::nameserver::RecordType;
use inwx::response::nameserver::RecordInfo as RecordInfoResponse;
use inwx::{Client, Endpoint};
use ipnet::{IpBitAnd, IpBitOr, Ipv6Net};
use serde::{Deserialize, Serialize};
#[derive(Debug)]
enum Error {
ChannelRecv(mpsc::RecvError),
ChannelSend4(mpsc::SendError<Ipv4Addr>),
ChannelSend6(mpsc::SendError<Ipv6Net>),
Inwx(inwx::Error),
PreferredIp(preferred_ip::Error),
ParseAddr(std::net::AddrParseError),
PrefixLen(ipnet::PrefixLenError),
Io(std::io::Error),
SerdeJson(serde_json::Error),
}
impl std::error::Error for Error {}
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ChannelRecv(e) => write!(fmt, "can't recv from mpsc channel: {}", e),
Self::ChannelSend4(e) => write!(fmt, "can't send to mpsc channel: {}", e),
Self::ChannelSend6(e) => write!(fmt, "can't send to mpsc channel: {}", e),
Self::Inwx(e) => write!(fmt, "inwx library error: {}", e),
Self::PreferredIp(e) => write!(fmt, "preferred_ip library error: {}", e),
Self::ParseAddr(e) => write!(fmt, "can't parse ip address: {}", e),
Self::PrefixLen(e) => write!(fmt, "prefix length error: {}", e),
Self::Io(e) => write!(fmt, "io error: {}", e),
Self::SerdeJson(e) => write!(fmt, "serde_json library error: {}", e),
}
}
}
impl From<mpsc::RecvError> for Error {
fn from(e: mpsc::RecvError) -> Self {
Self::ChannelRecv(e)
}
}
impl From<mpsc::SendError<Ipv4Addr>> for Error {
fn from(e: mpsc::SendError<Ipv4Addr>) -> Self {
Self::ChannelSend4(e)
}
}
impl From<mpsc::SendError<Ipv6Net>> for Error {
fn from(e: mpsc::SendError<Ipv6Net>) -> Self {
Self::ChannelSend6(e)
}
}
impl From<inwx::Error> for Error {
fn from(e: inwx::Error) -> Self {
Self::Inwx(e)
}
}
impl From<preferred_ip::Error> for Error {
fn from(e: preferred_ip::Error) -> Self {
Self::PreferredIp(e)
}
}
impl From<std::net::AddrParseError> for Error {
fn from(e: std::net::AddrParseError) -> Self {
Self::ParseAddr(e)
}
}
impl From<ipnet::PrefixLenError> for Error {
fn from(e: ipnet::PrefixLenError) -> Self {
Self::PrefixLen(e)
}
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Self::Io(e)
}
}
impl From<serde_json::Error> for Error {
fn from(e: serde_json::Error) -> Self {
Self::SerdeJson(e)
}
}
type Result<T> = std::result::Result<T, Error>;
#[derive(Clone, Debug, Serialize, Deserialize)]
struct Config {
user: String,
pass: String,
records4: Vec<i32>,
records6: Vec<i32>,
prefix_len: u8,
link4: String,
link6: String,
interval4: u64,
interval6: u64,
}
fn main() -> Result<()> {
let config_file = File::open("/etc/dyndns.conf")?;
let parsed_config: Config = serde_json::from_reader(config_file)?;
let config = Arc::new(parsed_config);
let config0 = config.clone();
let config1 = config.clone();
let config2 = config.clone();
let config3 = config;
let (tx4, rx4) = mpsc::channel();
let (tx6, rx6) = mpsc::channel();
let push4_thread = thread::spawn(move || {
loop {
match push4(config0.clone(), &rx4) {
Ok(_) => { /* unreachable */ }
Err(e) => println!("failed to push ipv4 address: {}", e),
}
thread::sleep(Duration::from_secs(config0.interval4));
}
});
let push6_thread = thread::spawn(move || {
loop {
match push6(config1.clone(), &rx6) {
Ok(_) => { /* unreachable */ }
Err(e) => println!("failed to push ipv6 prefix: {}", e),
}
thread::sleep(Duration::from_secs(config1.interval6));
}
});
let monitor4_thread = thread::spawn(move || {
loop {
match monitor4(config2.clone(), tx4.clone()) {
Ok(_) => { /* unreachable */ }
Err(e) => println!("failed to monitor ipv4 address: {}", e),
}
thread::sleep(Duration::from_secs(config2.interval4));
}
});
let monitor6_thread = thread::spawn(move || {
loop {
match monitor6(config3.clone(), tx6.clone()) {
Ok(_) => { /* unreachable */ }
Err(e) => println!("failed to monitor ipv6 prefix: {}", e),
}
thread::sleep(Duration::from_secs(config3.interval6));
}
});
push4_thread.join().unwrap();
push6_thread.join().unwrap();
monitor4_thread.join().unwrap();
monitor6_thread.join().unwrap();
Ok(())
}
fn monitor4(config: Arc<Config>, tx: mpsc::Sender<Ipv4Addr>) -> Result<()> {
let mut ipv4 = None;
loop {
let new_ipv4 = preferred_ip::ipv4_global(&config.link4)?;
if ipv4.is_none() || ipv4.unwrap() != new_ipv4 {
tx.send(new_ipv4)?;
ipv4 = Some(new_ipv4);
}
thread::sleep(Duration::from_secs(config.interval4));
}
}
fn monitor6(config: Arc<Config>, tx: mpsc::Sender<Ipv6Net>) -> Result<()> {
let mut ipv6 = None;
loop {
let new_ipv6 = preferred_ip::ipv6_unicast_global(&config.link6)?;
if ipv6.is_none() || ipv6.unwrap() != new_ipv6 {
tx.send(Ipv6Net::new(new_ipv6, config.prefix_len)?)?;
ipv6 = Some(new_ipv6);
}
thread::sleep(Duration::from_secs(config.interval6));
}
}
fn push4(config: Arc<Config>, rx: &mpsc::Receiver<Ipv4Addr>) -> Result<()> {
let mut last_address = None;
loop {
let address = rx.recv()?;
if last_address.is_none() || address != last_address.unwrap() {
let clt = Client::login(Endpoint::Sandbox, &config.user, &config.pass)?;
clt.call(RecordUpdate {
ids: config.records4.to_vec(),
name: None,
record_type: Some(RecordType::A),
content: Some(address.to_string()),
ttl: Some(300),
priority: None,
url_rdr_type: None,
url_rdr_title: None,
url_rdr_desc: None,
url_rdr_keywords: None,
url_rdr_favicon: None,
url_append: None,
testing_mode: false,
})?;
last_address = Some(address);
}
}
}
fn push6(config: Arc<Config>, rx: &mpsc::Receiver<Ipv6Net>) -> Result<()> {
let mut last_prefix = None;
loop {
let prefix = rx.recv()?;
if last_prefix.is_none() || prefix != last_prefix.unwrap() {
let clt = Client::login(Endpoint::Sandbox, &config.user, &config.pass)?;
let mut total_records = Vec::new();
for id in &config.records6 {
let info: RecordInfoResponse = clt
.call(RecordInfoCall {
domain_name: None,
domain_id: None,
record_id: Some(*id),
record_type: Some(RecordType::Aaaa),
name: None,
content: None,
ttl: None,
priority: None,
})?
.try_into()?;
let mut records = info
.records
.expect("no AAAA records (this should never happen");
total_records.append(&mut records);
}
for record in total_records {
let address = Ipv6Addr::from_str(&record.content)?;
// Get the interface identifier.
let if_id = address.bitand(prefix.hostmask());
let clean_prefix = prefix.addr().bitand(prefix.netmask());
let new = clean_prefix.bitor(if_id);
clt.call(RecordUpdate {
ids: vec![record.id],
name: None,
record_type: Some(RecordType::Aaaa),
content: Some(new.to_string()),
ttl: Some(300),
priority: None,
url_rdr_type: None,
url_rdr_title: None,
url_rdr_desc: None,
url_rdr_keywords: None,
url_rdr_favicon: None,
url_append: None,
testing_mode: false,
})?;
}
last_prefix = Some(prefix);
}
}
}
|