mirror of
https://github.com/speatzle/nfsense.git
synced 2025-05-10 10:38:20 +00:00
Initial Rust test
This commit is contained in:
commit
f6929c2575
3 changed files with 255 additions and 0 deletions
99
Cargo.lock
generated
Normal file
99
Cargo.lock
generated
Normal file
|
@ -0,0 +1,99 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ipnet"
|
||||||
|
version = "2.8.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "28b29a3cd74f0f4598934efe3aeba42bae0eb4680554128851ebbecb02af14e6"
|
||||||
|
dependencies = [
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "itoa"
|
||||||
|
version = "1.0.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "nfsense"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"ipnet",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-macro2"
|
||||||
|
version = "1.0.69"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da"
|
||||||
|
dependencies = [
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quote"
|
||||||
|
version = "1.0.33"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ryu"
|
||||||
|
version = "1.0.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde"
|
||||||
|
version = "1.0.189"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8e422a44e74ad4001bdc8eede9a4570ab52f71190e9c076d14369f38b9200537"
|
||||||
|
dependencies = [
|
||||||
|
"serde_derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_derive"
|
||||||
|
version = "1.0.189"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1e48d1f918009ce3145511378cf68d613e3b3d9137d67272562080d68a2b32d5"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_json"
|
||||||
|
version = "1.0.107"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6b420ce6e3d8bd882e9b243c6eed35dbc9a6110c9769e74b584e0d68d1f20c65"
|
||||||
|
dependencies = [
|
||||||
|
"itoa",
|
||||||
|
"ryu",
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "syn"
|
||||||
|
version = "2.0.38"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e96b79aaa137db8f61e26363a0c9b47d8b4ec75da28b7d1d614c2303e232408b"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-ident"
|
||||||
|
version = "1.0.12"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
|
11
Cargo.toml
Normal file
11
Cargo.toml
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
[package]
|
||||||
|
name = "nfsense"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
ipnet = { version = "2.8.0", features = ["serde"] }
|
||||||
|
serde = { version = "1.0.189", features = ["derive"] }
|
||||||
|
serde_json = "1.0.107"
|
145
src/main.rs
Normal file
145
src/main.rs
Normal file
|
@ -0,0 +1,145 @@
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
|
use ipnet::IpNet;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::{collections::HashMap, net::IpAddr};
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Default, Debug)]
|
||||||
|
struct Config {
|
||||||
|
config_version: u64,
|
||||||
|
network: Network,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Default, Debug)]
|
||||||
|
struct Network {
|
||||||
|
interfaces: HashMap<String, NetworkInterface>,
|
||||||
|
static_routes: Vec<StaticRoute>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
struct NetworkInterface {
|
||||||
|
alias: String,
|
||||||
|
comment: String,
|
||||||
|
interface_type: NetworkInterfaceType,
|
||||||
|
addressing_mode: AddressingMode,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
#[serde(rename_all = "snake_case")]
|
||||||
|
enum NetworkInterfaceType {
|
||||||
|
Hardware { device: String },
|
||||||
|
Vlan { id: i32, parent: String },
|
||||||
|
Bond { members: Vec<String> },
|
||||||
|
Bridge { members: Vec<String> },
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
#[serde(rename_all = "snake_case")]
|
||||||
|
enum AddressingMode {
|
||||||
|
None,
|
||||||
|
Static { address: String },
|
||||||
|
DHCP,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
struct StaticRoute {
|
||||||
|
name: String,
|
||||||
|
interface: String,
|
||||||
|
gateway: IpAddr,
|
||||||
|
destination: IpNet,
|
||||||
|
metric: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Default, Debug)]
|
||||||
|
struct Object {
|
||||||
|
addresses: HashMap<String, Address>,
|
||||||
|
services: HashMap<String, Service>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
struct Address {
|
||||||
|
address_type: AddressType,
|
||||||
|
comment: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
#[serde(rename_all = "snake_case")]
|
||||||
|
enum AddressType {
|
||||||
|
Host { host: String },
|
||||||
|
Range { range: IpAddr },
|
||||||
|
Network { network: IpNet },
|
||||||
|
Group { children: Vec<String> },
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
struct Service {
|
||||||
|
service_type: ServiceType,
|
||||||
|
comment: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
#[serde(rename_all = "snake_case")]
|
||||||
|
enum ServiceType {
|
||||||
|
TCP {
|
||||||
|
source_port_start: u64,
|
||||||
|
source_port_end: u64,
|
||||||
|
destination_port_start: u64,
|
||||||
|
destination_port_end: u64,
|
||||||
|
},
|
||||||
|
UDP {
|
||||||
|
range: IpAddr,
|
||||||
|
},
|
||||||
|
ICMP {
|
||||||
|
code: u8,
|
||||||
|
},
|
||||||
|
Group {
|
||||||
|
children: Vec<String>,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("Hello, world!");
|
||||||
|
|
||||||
|
let mut config = Config::default();
|
||||||
|
config.config_version = 1;
|
||||||
|
|
||||||
|
config.network.interfaces.insert(
|
||||||
|
"inter1".to_string(),
|
||||||
|
NetworkInterface {
|
||||||
|
alias: "test".to_owned(),
|
||||||
|
comment: "test comment".to_owned(),
|
||||||
|
interface_type: NetworkInterfaceType::Hardware {
|
||||||
|
device: "eth0".to_owned(),
|
||||||
|
},
|
||||||
|
addressing_mode: AddressingMode::None,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
config.network.interfaces.insert(
|
||||||
|
"inter2".to_string(),
|
||||||
|
NetworkInterface {
|
||||||
|
alias: "test2".to_owned(),
|
||||||
|
comment: "test comment".to_owned(),
|
||||||
|
interface_type: NetworkInterfaceType::Hardware {
|
||||||
|
device: "eth0".to_owned(),
|
||||||
|
},
|
||||||
|
addressing_mode: AddressingMode::Static {
|
||||||
|
address: "192.168.1.1".to_owned(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
config.network.static_routes.push(StaticRoute {
|
||||||
|
name: "test1".to_string(),
|
||||||
|
interface: "eth0".to_string(),
|
||||||
|
gateway: "192.168.1.1".parse().unwrap(),
|
||||||
|
destination: "10.42.42.0/24".parse().unwrap(),
|
||||||
|
metric: 0,
|
||||||
|
});
|
||||||
|
|
||||||
|
let serialized = serde_json::to_string_pretty(&config).unwrap();
|
||||||
|
println!("serialized = {}", serialized);
|
||||||
|
|
||||||
|
let deserialized: Config = serde_json::from_str(&serialized).unwrap();
|
||||||
|
println!("deserialized = {:?}", deserialized);
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue