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
|
// Reboot and rollback logic
// /data/update does not exist? => MONITOR
// CHECK:
// Connection to tcp!ipv4.google.com!80 unsuccessful? => FAIL
// Connection to tcp!ipv6.google.com!80 unsuccessful? => FAIL
// No connection from updater? => FAIL
// => MONITOR
// FAIL:
// If 5m passed since first CHECK? => TIMEOUT
// Wait 30s => CHECK
// TIMEOUT:
// Trigger rollback (can use /data/admind.passwd) => REBOOT
// MONITOR:
// Connection to tcp!ipv4.google.com!80 unsuccessful? => DROPOUT
// Connection to tcp!ipv6.google.com!80 unsuccessful? => DROPOUT
// Wait 5m => MONITOR
// DROPOUT:
// If DROPOUT for 1h? => REBOOT
// Wait 5m => MONITOR
use std::fs;
use std::io::{self, Read, Write};
use std::net::{Shutdown, SocketAddr, TcpListener, TcpStream};
use std::time::{Duration, Instant};
const UPDATE_FILE: &str = "/data/update";
const LISTEN_SOCKET: &str = "[::]:12808";
const MAGIC: [u8; 4] = [0x32, 0x7f, 0xfe, 0x4c];
const RESP_OK: [u8; 5] = [0x32, 0x7f, 0xfe, 0x4c, 0x00];
const RESP_NORMAL: [u8; 5] = [0x32, 0x7f, 0xfe, 0x4c, 0x01];
const CHECK_INTERVAL: Duration = Duration::from_secs(30);
const CHECK_TIMEOUT: Duration = Duration::from_secs(300);
const MONITOR_INTERVAL: Duration = Duration::from_secs(600);
const MONITOR_TIMEOUT: Duration = Duration::from_secs(3600);
const TCP_TIMEOUT: Duration = Duration::from_secs(8);
const POLL_INTERVAL: Duration = Duration::from_millis(500);
const PING_V4: &str = "1.1.1.1:80";
const PING_V6: &str = "[2606:4700:4700::1111]:80";
fn main() {
println!("[info] init");
match run() {
Ok(_) => eprintln!("[warn] logic terminated unexpectedly"),
Err(e) => eprintln!("[warn] {}", e),
}
}
fn run() -> io::Result<()> {
let ln = TcpListener::bind(LISTEN_SOCKET)?;
ln.set_nonblocking(true)?;
if fs::exists(UPDATE_FILE)? {
eprintln!("[info] update detected");
check_rollback(&ln)?;
eprintln!("[info] no rollback needed");
} else {
println!("[info] no update, skipping rollback check");
}
monitor(&ln)
}
fn check_rollback(ln: &TcpListener) -> io::Result<()> {
let mut outbound_healthy_v4 = false;
let mut outbound_healthy_v6 = false;
let mut inbound_healthy = false;
let t_start = Instant::now();
let mut t = t_start;
loop {
match ln.accept() {
Ok((conn, raddr)) => match handle_conn_check(conn, raddr, &mut inbound_healthy) {
Ok(_) => {}
Err(e) => eprintln!("[warn] handle (rollback) {}: {}", raddr, e),
},
Err(e) if e.kind() == io::ErrorKind::WouldBlock => {}
Err(e) => return Err(e),
}
let now = Instant::now();
if now.duration_since(t) >= CHECK_INTERVAL {
check_connectivity(&mut outbound_healthy_v4, &mut outbound_healthy_v6)?;
t = now;
}
if outbound_healthy_v4 && outbound_healthy_v6 && inbound_healthy {
break;
}
if now.duration_since(t_start) >= CHECK_TIMEOUT {
eprintln!(
"rollback, IPv4: {}, IPv6: {}, inbound: {}",
if outbound_healthy_v4 { "OK" } else { "ERR" },
if outbound_healthy_v6 { "OK" } else { "ERR" },
if inbound_healthy { "OK" } else { "ERR" }
);
return rollback();
}
std::thread::sleep(POLL_INTERVAL);
}
Ok(())
}
fn monitor(ln: &TcpListener) -> io::Result<()> {
let mut t_healthy = Instant::now();
let mut t = t_healthy;
loop {
match ln.accept() {
Ok((conn, raddr)) => match handle_conn_monitor(conn, raddr) {
Ok(_) => {}
Err(e) => eprintln!("[warn] handle (monitor) {}: {}", raddr, e),
},
Err(e) if e.kind() == io::ErrorKind::WouldBlock => {}
Err(e) => return Err(e),
}
let mut outbound_healthy_v4 = false;
let mut outbound_healthy_v6 = false;
let now = Instant::now();
if now.duration_since(t) >= MONITOR_INTERVAL {
check_connectivity(&mut outbound_healthy_v4, &mut outbound_healthy_v6)?;
t = now;
}
if outbound_healthy_v4 && outbound_healthy_v6 {
t_healthy = now;
continue;
}
if now.duration_since(t_healthy) >= MONITOR_TIMEOUT {
return reboot();
}
std::thread::sleep(POLL_INTERVAL);
}
}
fn handle_conn_check(
mut conn: TcpStream,
raddr: SocketAddr,
inbound_healthy: &mut bool,
) -> io::Result<()> {
conn.set_read_timeout(Some(TCP_TIMEOUT))?;
conn.set_write_timeout(Some(TCP_TIMEOUT))?;
let mut buf = [0; 4];
loop {
match conn.read_exact(&mut buf) {
Ok(_) => break,
Err(e) if e.kind() == io::ErrorKind::WouldBlock => {}
Err(e) => return Err(e),
}
}
if buf != MAGIC {
eprintln!("[warn] handle {raddr}: bad magic {buf:?}");
return Ok(());
}
*inbound_healthy = true;
conn.write_all(&RESP_OK)?;
conn.shutdown(Shutdown::Both)?;
println!("[info] inbound: {raddr}");
Ok(())
}
fn handle_conn_monitor(mut conn: TcpStream, raddr: SocketAddr) -> io::Result<()> {
conn.set_read_timeout(Some(TCP_TIMEOUT))?;
conn.set_write_timeout(Some(TCP_TIMEOUT))?;
let mut buf = [0; 4];
loop {
match conn.read_exact(&mut buf) {
Ok(_) => break,
Err(e) if e.kind() == io::ErrorKind::WouldBlock => {}
Err(e) => return Err(e),
}
}
if buf != MAGIC {
eprintln!("[warn] handle {raddr}: bad magic {buf:?}");
return Ok(());
}
conn.write_all(&RESP_NORMAL)?;
conn.shutdown(Shutdown::Both)?;
println!("[info] redundant inbound: {raddr}");
Ok(())
}
fn check_connectivity(
outbound_healthy_v4: &mut bool,
outbound_healthy_v6: &mut bool,
) -> io::Result<()> {
let mut buf = [0; 1024];
let conn4 =
match TcpStream::connect_timeout(&PING_V4.parse().expect("PING_V4 invalid"), TCP_TIMEOUT) {
Ok(conn) => Some(conn),
Err(e) => {
eprintln!("[warn] IPv4: connect: {}", e);
*outbound_healthy_v4 = false;
None
}
};
if let Some(mut conn4) = conn4 {
conn4.set_read_timeout(Some(TCP_TIMEOUT))?;
conn4.set_write_timeout(Some(TCP_TIMEOUT))?;
conn4.write_all(b"GET / HTTP/1.1\n\n").ok();
loop {
match conn4.read(&mut buf) {
Ok(0) => {
eprintln!("[warn] IPv4: connection closed");
*outbound_healthy_v4 = false;
}
Ok(_) => {
*outbound_healthy_v4 = true;
}
Err(e) if e.kind() == io::ErrorKind::WouldBlock => continue,
Err(e) => {
eprintln!("[warn] IPv4: read: {}", e);
*outbound_healthy_v4 = false;
}
}
break;
}
conn4.shutdown(Shutdown::Both).ok();
}
let conn6 =
match TcpStream::connect_timeout(&PING_V6.parse().expect("PING_V6 invalid"), TCP_TIMEOUT) {
Ok(conn) => Some(conn),
Err(e) => {
eprintln!("[warn] IPv6: connect: {}", e);
*outbound_healthy_v6 = false;
None
}
};
if let Some(mut conn6) = conn6 {
conn6.set_read_timeout(Some(TCP_TIMEOUT))?;
conn6.set_write_timeout(Some(TCP_TIMEOUT))?;
conn6.write_all(b"GET / HTTP/1.1\n\n").ok();
loop {
match conn6.read(&mut buf) {
Ok(0) => {
eprintln!("[warn] IPv6: connection closed");
*outbound_healthy_v6 = false;
}
Ok(_) => {
*outbound_healthy_v6 = true;
}
Err(e) if e.kind() == io::ErrorKind::WouldBlock => continue,
Err(e) => {
eprintln!("[warn] IPv6: read: {}", e);
*outbound_healthy_v6 = false;
}
}
break;
}
conn6.shutdown(Shutdown::Both).ok();
}
Ok(())
}
fn rollback() -> io::Result<()> {
todo!()
}
fn reboot() -> io::Result<()> {
todo!()
}
|