initial work on dns resolver + openwrt mgmt
This commit is contained in:
1 parent
b51257e5e1
commit
0614d7ff04
22 files changed
+505
-2
No files matched your search
Generated
+107
@@ -0,0 +1,107 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79"
|
||||
|
||||
[[package]]
|
||||
name = "openwrt-gen"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.106"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.45"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
|
||||
dependencies = [
|
||||
"serde_core",
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_core"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.150"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e8014e44b4736ed0538adeecded0fce2a272f22dc9578a7eb6b2d9993c74cfb9"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"memchr",
|
||||
"serde",
|
||||
"serde_core",
|
||||
"zmij",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.117"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
|
||||
|
||||
[[package]]
|
||||
name = "zmij"
|
||||
version = "1.0.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa"
|
||||
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "openwrt-gen"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[[bin]]
|
||||
name = "openwrt-gen"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
@@ -0,0 +1,155 @@
|
||||
use serde::Deserialize;
|
||||
use std::collections::BTreeMap;
|
||||
use std::io::{self, Read};
|
||||
|
||||
const ROOT_DOMAIN: &str = "internal";
|
||||
|
||||
static IP_PREFIXES: &[(&str, &str)] = &[
|
||||
("lab", "10.0.10."),
|
||||
("k3s", "10.0.11."),
|
||||
("hub", "10.0.12."),
|
||||
("cam", "10.0.13."),
|
||||
("iot", "10.0.14."),
|
||||
("apl", "10.0.15."),
|
||||
("router", "10.0.16."),
|
||||
("devices", "10.0.0."),
|
||||
];
|
||||
|
||||
// Networks where justMac defaults to true
|
||||
static JUST_MAC_NETWORKS: &[&str] = &["devices"];
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Config {
|
||||
#[serde(rename = "generalMappings")]
|
||||
general_mappings: Vec<GeneralMapping>,
|
||||
networks: BTreeMap<String, Vec<Device>>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct GeneralMapping {
|
||||
ip: String,
|
||||
domains: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Device {
|
||||
mac: String,
|
||||
name: String,
|
||||
#[serde(default)]
|
||||
id: Option<u32>,
|
||||
#[serde(default)]
|
||||
domains: Option<Vec<String>>,
|
||||
#[serde(rename = "justMac", default)]
|
||||
just_mac: bool,
|
||||
}
|
||||
|
||||
struct ProcessedDevice<'a> {
|
||||
device: &'a Device,
|
||||
ip: String,
|
||||
host: String,
|
||||
just_mac: bool,
|
||||
}
|
||||
|
||||
fn prefix_for(network: &str) -> &'static str {
|
||||
IP_PREFIXES
|
||||
.iter()
|
||||
.find(|(n, _)| *n == network)
|
||||
.map(|(_, p)| *p)
|
||||
.unwrap_or("10.0.0.")
|
||||
}
|
||||
|
||||
fn process_network<'a>(network: &str, devices: &'a [Device]) -> Vec<ProcessedDevice<'a>> {
|
||||
let prefix = prefix_for(network);
|
||||
let just_mac_default = JUST_MAC_NETWORKS.contains(&network);
|
||||
let mut suffix: u32 = 0;
|
||||
let mut seen: Vec<u32> = Vec::new();
|
||||
let mut result = Vec::new();
|
||||
|
||||
for device in devices {
|
||||
let base = device.id.unwrap_or(suffix + 1);
|
||||
// Skip any already-used suffixes
|
||||
let next = (base..).find(|s| !seen.contains(s)).unwrap();
|
||||
suffix = next;
|
||||
seen.push(next);
|
||||
|
||||
result.push(ProcessedDevice {
|
||||
device,
|
||||
ip: format!("{}{}", prefix, next),
|
||||
host: format!("{}.{}.{}", device.name, network, ROOT_DOMAIN),
|
||||
just_mac: device.just_mac || just_mac_default,
|
||||
});
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
fn gen_dnsmasq(config: &Config) -> String {
|
||||
let mut out = String::new();
|
||||
|
||||
for (network, devices) in &config.networks {
|
||||
for pd in process_network(network, devices) {
|
||||
if pd.just_mac {
|
||||
continue;
|
||||
}
|
||||
if let Some(domains) = &pd.device.domains {
|
||||
for domain in domains {
|
||||
out.push_str(&format!("address=/{}/{}\n", domain, pd.ip));
|
||||
out.push_str(&format!("address=/.{}/{}\n", domain, pd.ip));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for gm in &config.general_mappings {
|
||||
for domain in &gm.domains {
|
||||
out.push_str(&format!("address=/{}/{}\n", domain, gm.ip));
|
||||
out.push_str(&format!("address=/.{}/{}\n", domain, gm.ip));
|
||||
}
|
||||
}
|
||||
|
||||
out
|
||||
}
|
||||
|
||||
fn gen_ethers(config: &Config) -> String {
|
||||
let mut out = String::new();
|
||||
|
||||
for (network, devices) in &config.networks {
|
||||
let cap = |s: &str| {
|
||||
let mut c = s.chars();
|
||||
match c.next() {
|
||||
None => String::new(),
|
||||
Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
|
||||
}
|
||||
};
|
||||
out.push_str(&format!("#{}\n", cap(network)));
|
||||
|
||||
for pd in process_network(network, devices) {
|
||||
if !pd.just_mac {
|
||||
out.push_str(&format!("{} {}\n", pd.device.mac, pd.ip));
|
||||
}
|
||||
out.push_str(&format!("{} {}\n", pd.device.mac, pd.host));
|
||||
}
|
||||
}
|
||||
|
||||
out
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let cmd = std::env::args().nth(1).unwrap_or_default();
|
||||
|
||||
let mut input = String::new();
|
||||
io::stdin().read_to_string(&mut input).expect("failed to read stdin");
|
||||
|
||||
let config: Config = serde_json::from_str(&input).expect("invalid config JSON");
|
||||
|
||||
let output = match cmd.as_str() {
|
||||
"dnsmasq" => gen_dnsmasq(&config),
|
||||
"ethers" => gen_ethers(&config),
|
||||
_ => {
|
||||
eprintln!("usage: openwrt-gen <dnsmasq|ethers>");
|
||||
std::process::exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
print!("{}", output);
|
||||
}
|
||||
Reference in new issue
Block a user