Merge branch 'mkconfig' into canon

This commit is contained in:
Ezra Barrow 2023-08-15 11:12:26 -05:00
commit bd70f012a3
6 changed files with 220 additions and 8 deletions

90
Cargo.lock generated
View File

@ -149,6 +149,12 @@ version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7684a49fb1af197853ef7b2ee694bc1f5b4179556f1e5710e1760c5db6f5e929"
[[package]]
name = "equivalent"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "eyre"
version = "0.6.8"
@ -182,6 +188,12 @@ version = "0.27.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6c80984affa11d98d1b88b66ac8853f143217b399d3c74116778ff8fdb4ed2e"
[[package]]
name = "hashbrown"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c6201b9ff9fd90a5a3bac2e56a830d0caa509576f0e503818ee82c181b3437a"
[[package]]
name = "hermit-abi"
version = "0.3.1"
@ -194,6 +206,16 @@ version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683"
[[package]]
name = "indexmap"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d5477fe2230a79769d8dc68e0eabf5437907c0457a5614a9e8dddb67f65eb65d"
dependencies = [
"equivalent",
"hashbrown",
]
[[package]]
name = "instant"
version = "0.1.12"
@ -600,6 +622,29 @@ name = "serde"
version = "1.0.183"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32ac8da02677876d532745a130fc9d8e6edfa81a269b107c5b00829b91d8eb3c"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.183"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aafe972d60b0b9bee71a91b92fee2d4fb3c9d7e8f6b179aa99f27203d99a4816"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_spanned"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96426c9936fd7a0124915f9185ea1d20aa9445cc9821142f0a73bc9207a2e186"
dependencies = [
"serde",
]
[[package]]
name = "sharded-slab"
@ -639,8 +684,10 @@ dependencies = [
"quinn",
"rcgen",
"rustls",
"serde",
"tokio",
"tokio-tun",
"toml",
]
[[package]]
@ -786,6 +833,40 @@ dependencies = [
"tokio",
]
[[package]]
name = "toml"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c17e963a819c331dcacd7ab957d80bc2b9a9c1e71c804826d2f283dd65306542"
dependencies = [
"serde",
"serde_spanned",
"toml_datetime",
"toml_edit",
]
[[package]]
name = "toml_datetime"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7cda73e2f1397b1262d6dfdcef8aafae14d1de7748d66822d3bfeeb6d03e5e4b"
dependencies = [
"serde",
]
[[package]]
name = "toml_edit"
version = "0.19.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8123f27e969974a3dfba720fdb560be359f57b44302d280ba72e76a74480e8a"
dependencies = [
"indexmap",
"serde",
"serde_spanned",
"toml_datetime",
"winnow",
]
[[package]]
name = "tracing"
version = "0.1.37"
@ -1017,6 +1098,15 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a"
[[package]]
name = "winnow"
version = "0.5.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5504cc7644f4b593cbc05c4a55bf9bd4e94b867c3c0bd440934174d50482427d"
dependencies = [
"memchr",
]
[[package]]
name = "yasna"
version = "0.5.2"

View File

@ -13,5 +13,7 @@ color-eyre = "0.6.2"
quinn = { version = "0.10.2", features = [] }
rcgen = "0.11.1"
rustls = { version = "0.21.6", features = ["dangerous_configuration", "quic"] }
serde = { version = "1.0.183", features = ["derive"] }
tokio = { version = "1.31.0", features = ["full"] }
tokio-tun = "0.9.0"
toml = "0.7.6"

9
config.toml Normal file
View File

@ -0,0 +1,9 @@
[server]
endpoint = "127.0.0.1:9092"
[client]
endpoint = "alphamethyl.barr0w.net:9092"
[interface]
address = "192.168.255.1"
netmask = "255.255.255.255"

View File

@ -0,0 +1,22 @@
/*
* --------------------
* THIS FILE IS LICENSED UNDER THE FOLLOWING TERMS
*
* this code may not be used for any purpose. be gay, do crime
*
* THE FOLLOWING MESSAGE IS NOT A LICENSE
*
* <barrow@tilde.team> wrote this file.
* by reading this text, you are reading "TRANS RIGHTS".
* this file and the content within it is the gay agenda.
* if we meet some day, and you think this stuff is worth it,
* you can buy me a beer, tea, or something stronger.
* -Ezra Barrow
* --------------------
*/
#[tokio::main]
async fn main() -> anyhow::Result<()> {
sleepytunny::config::Configuration::generate_config("config.toml")
}

84
src/config.rs Normal file
View File

@ -0,0 +1,84 @@
use anyhow::Context;
use serde::{Serialize, Deserialize};
use std::fs;
use std::io::Write;
use std::net::Ipv4Addr;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Server {
pub endpoint: String,
}
impl Server {
pub fn new() -> Self {
Self {
endpoint: String::from("127.0.0.1:9092"),
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Client {
pub endpoint: String,
}
impl Client {
pub fn new() -> Self {
Self {
endpoint: String::from("alphamethyl.barr0w.net:9092"),
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Interface {
pub address: Ipv4Addr,
pub netmask: Ipv4Addr,
}
impl Interface {
pub fn new() -> Self {
Self {
address: Ipv4Addr::from([192, 168, 255, 1]),
netmask: Ipv4Addr::from([255, 255, 255, 255]),
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Configuration {
#[serde(skip_serializing_if = "Option::is_none")]
pub server: Option<Server>,
#[serde(skip_serializing_if = "Option::is_none")]
pub client: Option<Client>,
pub interface: Interface,
}
impl Configuration {
pub fn new() -> Self {
Self {
server: Some(Server::new()),
client: Some(Client::new()),
interface: Interface::new(),
}
}
/// Loads configuration file f and returns a struct of all parsed values
pub fn load_config(f: &str) -> anyhow::Result<Self> {
let config = fs::read_to_string(f);
match config {
Ok(c) => Ok(toml::from_str(&c)?),
Err(_) => Ok(Self::new()),
}
}
/// Generates new configuration file f with default values
pub fn generate_config(f: &str) -> anyhow::Result<()> {
let config = Self::new();
let mut config_file = fs::File::create(&f)?;
config_file.write(toml::to_string(&config)?.as_bytes())?;
Ok(())
}
/// Returns the server config or an appropriate error
pub fn server(&self) -> anyhow::Result<&Server> {
self.server.as_ref().context("this config is not a server config")
}
/// Returns the client config or an appropriate error
pub fn client(&self) -> anyhow::Result<&Client> {
self.client.as_ref().context("this config is not a client config")
}
}

View File

@ -25,6 +25,9 @@ use encoder::Encoder;
mod quic;
use quic::QuicModem;
pub mod config;
use config::Configuration;
fn is_timeout<T>(res: &anyhow::Result<T>) -> bool {
let e = match res {
Ok(_) => return false,
@ -63,16 +66,17 @@ macro_rules! handle_timeout {
}
pub async fn client_main() -> anyhow::Result<()> {
let c = Configuration::load_config("config.toml")?;
let tun = Tun::builder()
.name("")
.name("sleepy")
.tap(false)
.packet_info(true)
.address([10, 177, 233, 1].into())
.netmask([255, 255, 255, 0].into())
.address(c.interface.address)
.netmask(c.interface.netmask)
.up()
.try_build()?;
let mut tun = Encoder::new(tun);
let mut quic = QuicModem::new_client("10.177.1.7:9092")?;
let mut quic = QuicModem::new_client(&c.client()?.endpoint)?;
let mut backoff: u64 = 1;
loop {
@ -95,16 +99,17 @@ pub async fn client_main() -> anyhow::Result<()> {
}
pub async fn server_main() -> anyhow::Result<()> {
let c = Configuration::load_config("config.toml")?;
let tun = Tun::builder()
.name("")
.name("sleepy")
.tap(false)
.packet_info(true)
.address([10, 177, 233, 2].into())
.netmask([255, 255, 255, 0].into())
.address(c.interface.address)
.netmask(c.interface.netmask)
.up()
.try_build()?;
let mut tun = Encoder::new(tun);
let mut quic = QuicModem::new_server("10.177.1.7:9092")?;
let mut quic = QuicModem::new_server(&c.server()?.endpoint)?;
loop {
println!("[server] waiting for connection...");