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
//! Router nets (MikroTik RouterOS): network-condition control for
//! connectivity testing — toggling interfaces, changing SSIDs/passwords,
//! blocking traffic.
//!
//! The common read/control actions get typed methods; the long tail of
//! RouterOS configuration (security profiles, DHCP tuning, bandwidth limits,
//! firewall rules, access lists, raw REST calls) is reachable through the
//! generic [`Router::command`] escape hatch with the same action names the
//! box handler dispatches on.
use serde_json::Value;
use super::net_handle;
pub use crate::wire::RouterSystemInfo;
pub(crate) mod ops {
use std::time::Duration;
use serde_json::{json, Value};
use crate::error::Result;
use crate::wire::{net_command, unit, value_as, CommandResponse, Op, RouterSystemInfo, Timeout};
// Router saved-net records may carry the role string "router" or the
// legacy alias "mikrotik" (both dispatch to the same box handler). The
// box verifies a supplied role hint against the saved string exactly, so
// sending "router" would 404 on a net saved as "mikrotik". Omit the hint
// and let the box resolve the role from saved_nets.json.
const ROLE: Option<&str> = None;
/// RouterOS REST calls are quick, but reboot/reset actions and busy
/// routers can lag; give every action a comfortable budget.
fn budget() -> Timeout {
Timeout::After(Duration::from_secs(60))
}
fn value_raw(resp: CommandResponse) -> Result<Value> {
Ok(resp.value.unwrap_or(Value::Null))
}
pub(crate) fn connect(name: &str) -> Op<Value> {
Op {
req: net_command(name, ROLE, "connect", json!({}), budget()),
parse: value_raw,
}
}
pub(crate) fn system_info(name: &str) -> Op<RouterSystemInfo> {
Op {
req: net_command(name, ROLE, "system_info", json!({}), budget()),
parse: value_as::<RouterSystemInfo>,
}
}
pub(crate) fn interfaces(name: &str) -> Op<Value> {
Op {
req: net_command(name, ROLE, "interfaces", json!({}), budget()),
parse: value_raw,
}
}
pub(crate) fn wireless_interfaces(name: &str) -> Op<Value> {
Op {
req: net_command(name, ROLE, "wireless_interfaces", json!({}), budget()),
parse: value_raw,
}
}
pub(crate) fn wireless_clients(name: &str) -> Op<Value> {
Op {
req: net_command(name, ROLE, "wireless_clients", json!({}), budget()),
parse: value_raw,
}
}
pub(crate) fn dhcp_leases(name: &str) -> Op<Value> {
Op {
req: net_command(name, ROLE, "dhcp_leases", json!({}), budget()),
parse: value_raw,
}
}
pub(crate) fn reboot(name: &str) -> Op<()> {
Op {
req: net_command(name, ROLE, "reboot", json!({}), budget()),
parse: unit,
}
}
pub(crate) fn enable_interface(name: &str, interface: &str) -> Op<()> {
Op {
req: net_command(
name,
ROLE,
"enable_interface",
json!({ "interface": interface }),
budget(),
),
parse: unit,
}
}
pub(crate) fn disable_interface(name: &str, interface: &str) -> Op<()> {
Op {
req: net_command(
name,
ROLE,
"disable_interface",
json!({ "interface": interface }),
budget(),
),
parse: unit,
}
}
pub(crate) fn set_wireless_ssid(name: &str, interface: &str, ssid: &str) -> Op<Value> {
Op {
req: net_command(
name,
ROLE,
"set_wireless_ssid",
json!({ "interface": interface, "ssid": ssid }),
budget(),
),
parse: value_raw,
}
}
pub(crate) fn block_internet(name: &str) -> Op<()> {
Op {
req: net_command(name, ROLE, "block_internet", json!({}), budget()),
parse: unit,
}
}
pub(crate) fn remove_firewall_rules(name: &str) -> Op<()> {
Op {
req: net_command(name, ROLE, "remove_firewall_rules", json!({}), budget()),
parse: unit,
}
}
pub(crate) fn command(name: &str, action: &str, params: Value) -> Op<Value> {
Op {
req: net_command(name, ROLE, action, params, budget()),
parse: value_raw,
}
}
}
net_handle! {
/// Handle for a router net.
sync: Router,
async: AsyncRouter,
methods: {
/// Verify connectivity; returns identity/version/board/uptime.
fn connect() -> Value = ops::connect;
/// System resource information.
fn system_info() -> RouterSystemInfo = ops::system_info;
/// All network interfaces (raw RouterOS records).
fn interfaces() -> Value = ops::interfaces;
/// Wireless interfaces (raw RouterOS records).
fn wireless_interfaces() -> Value = ops::wireless_interfaces;
/// Currently associated wireless clients.
fn wireless_clients() -> Value = ops::wireless_clients;
/// Active DHCP leases.
fn dhcp_leases() -> Value = ops::dhcp_leases;
/// Reboot the router.
fn reboot() -> () = ops::reboot;
/// Enable a network interface by name.
fn enable_interface(interface: &str) -> () = ops::enable_interface;
/// Disable a network interface by name.
fn disable_interface(interface: &str) -> () = ops::disable_interface;
/// Change the SSID broadcast by a wireless interface.
fn set_wireless_ssid(interface: &str, ssid: &str) -> Value = ops::set_wireless_ssid;
/// Drop all forwarded traffic (simulate internet outage).
fn block_internet() -> () = ops::block_internet;
/// Remove every Lager-added firewall rule (undo `block_*`).
fn remove_firewall_rules() -> () = ops::remove_firewall_rules;
/// Run any other router action by name with raw JSON params
/// (security profiles, DHCP config, bandwidth limits, firewall
/// rules, access lists, `run` for raw RouterOS REST paths, ...).
fn command(action: &str, params: Value) -> Value = ops::command;
}
}