diff options
author | HimbeerserverDE <himbeerserverde@gmail.com> | 2023-03-18 12:50:01 +0100 |
---|---|---|
committer | HimbeerserverDE <himbeerserverde@gmail.com> | 2023-03-18 12:50:01 +0100 |
commit | f4483b497ab616ab33e149f6603d0a33283d84c7 (patch) | |
tree | bd8eb8175002a665c08a2bc1a66d4bd9587087ad | |
parent | 0653235b277c8b9b0a6f54db52490bcbad7ea8d1 (diff) |
read interface and credentials from config file
-rw-r--r-- | src/client.rs | 14 | ||||
-rw-r--r-- | src/config.rs | 8 | ||||
-rw-r--r-- | src/error.rs | 2 | ||||
-rw-r--r-- | src/lib.rs | 1 | ||||
-rw-r--r-- | src/main.rs | 6 |
5 files changed, 22 insertions, 9 deletions
diff --git a/src/client.rs b/src/client.rs index a31d0fc..18c3b7d 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,3 +1,4 @@ +use crate::config::Config; use crate::error::{Error, Result}; use std::net::Ipv4Addr; @@ -67,10 +68,13 @@ pub struct Client { } impl Client { - pub fn new(interface: &str) -> Result<Self> { + pub fn new(config: Config) -> Result<Self> { + let link = config.link.clone(); + Ok(Self { inner: Arc::new(RwLock::new(ClientRef { - socket: Socket::on_interface(interface)?, + config, + socket: Socket::on_interface(&link)?, started: false, host_uniq: rand::random(), state: State::default(), @@ -519,8 +523,9 @@ impl Client { match chap_code { chap::CHALLENGE => { - let username = b"alice"; - let password = b"1234"; + let config = &self.inner.read().unwrap().config; + let username = config.username.as_bytes(); + let password = config.password.as_bytes(); let limit = 1 + chap.payload()[0]; let challenge = &chap.payload()[1..limit as usize]; @@ -831,6 +836,7 @@ impl Client { #[derive(Debug)] struct ClientRef { + config: Config, socket: Socket, started: bool, host_uniq: [u8; 16], diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..6866e8e --- /dev/null +++ b/src/config.rs @@ -0,0 +1,8 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct Config { + pub link: String, + pub username: String, + pub password: String, +} diff --git a/src/error.rs b/src/error.rs index 72c38d2..ed02626 100644 --- a/src/error.rs +++ b/src/error.rs @@ -8,8 +8,6 @@ use thiserror::Error; #[derive(Debug, Error)] pub enum Error { - #[error("usage: missing interface argument")] - MissingInterface, #[error("client has already been started")] AlreadyActive, #[error("no active PPPoE session")] @@ -1,2 +1,3 @@ pub mod client; +pub mod config; pub mod error; diff --git a/src/main.rs b/src/main.rs index ae127bc..e2b4b8d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,6 @@ use rsdsl_pppoe::client::{Client, IpConfig}; use rsdsl_pppoe::error::{Error, Result}; -use std::env; use std::fs::File; use std::sync::mpsc; use std::sync::Arc; @@ -64,11 +63,12 @@ fn write_config(rx: mpsc::Receiver<IpConfig>) -> Result<()> { } fn main() -> Result<()> { - let link = env::args().nth(1).ok_or(Error::MissingInterface)?; + let mut file = File::open("/data/pppoe.conf")?; + let config = serde_json::from_reader(&mut file)?; let (tx, rx) = mpsc::channel(); let tun = Arc::new(Iface::new("rsppp0", Mode::Tun)?); - let clt = Client::new(&link)?; + let clt = Client::new(config)?; let tun2 = tun.clone(); let clt2 = clt.clone(); |