From 7a74819674510f26b97987cde9236b229e3160dd Mon Sep 17 00:00:00 2001 From: Felipe Lema <1232306+FelipeLema@users.noreply.github.com> Date: Tue, 4 Apr 2023 17:55:55 -0400 Subject: [PATCH 1/3] skeleton --- Cargo.toml | 1 + src/cmd.rs | 5 +++++ src/dns.rs | 21 +++++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 2e8859e..b2cb909 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ documentation = "https://docs.rs/openaws-vpn-client/" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +regex= "1" gtk = "=0.14.3" lazy_static = "=1.4.0" tokio = { version = "=1.14.0", features = ["full"] } diff --git a/src/cmd.rs b/src/cmd.rs index 23bd158..8be2fff 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -165,6 +165,11 @@ pub async fn connect_ovpn( if let Ok(ref line) = next { if let Some(line) = line { log.append_process(pid, line.as_str()); + tokio::spawn(async move { + // Process each socket concurrently. + parse_dns(line.to_string()) + .and_then(|dns_address| _WIP_("Do something with this IP address")) + }); } else { break; } diff --git a/src/dns.rs b/src/dns.rs index c7b2650..a321d67 100644 --- a/src/dns.rs +++ b/src/dns.rs @@ -4,6 +4,7 @@ use domain::base::iana::Class; use domain::base::{Dname, Rtype}; use domain::rdata::A; use rand::prelude::*; +use regex::Regex; use std::net::IpAddr; use std::ops::Deref; use std::rc::Rc; @@ -78,3 +79,23 @@ fn rng_domain() -> String { rng.fill_bytes(&mut bts); hex::encode(bts) } + +/// parse DNS from openvpn log written to stdout +pub fn parse_dns(line: String) -> Option { + let header = &line.as_bytes()[..32]; + // expect a header for this line + if std::str::from_utf8(header).unwrap() != "PUSH: Received control message: ".to_string() { + println!( + "no son iguales «{}» & »{}«", + std::str::from_utf8(header).unwrap(), + "PUSH: Received control message:" + ); + return None; + } + let dhcp_option_dns_re = Regex::new(r"dhcp-option DNS ([^,]+),").unwrap(); + for ip in dhcp_option_dns_re.captures_iter(&line) { + return Some((&ip[1]).to_string()); + } + None +} + From e6135ed9cf7fe650c9b622cd1087729d31f9aaed Mon Sep 17 00:00:00 2001 From: Felipe Lema <1232306+FelipeLema@users.noreply.github.com> Date: Wed, 5 Apr 2023 10:02:49 -0400 Subject: [PATCH 2/3] missing import --- src/cmd.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cmd.rs b/src/cmd.rs index 8be2fff..ba3f61b 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -1,3 +1,4 @@ +use crate::dns::parse_dns; use crate::saml_server::Saml; use crate::{LocalConfig, Log}; use lazy_static::lazy_static; From 4b3621a4990d36065a4d682393a3319e7c3a9018 Mon Sep 17 00:00:00 2001 From: Felipe Lema <1232306+FelipeLema@users.noreply.github.com> Date: Wed, 5 Apr 2023 11:50:13 -0400 Subject: [PATCH 3/3] remove debugging println --- src/dns.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/dns.rs b/src/dns.rs index a321d67..d7af8da 100644 --- a/src/dns.rs +++ b/src/dns.rs @@ -85,11 +85,6 @@ pub fn parse_dns(line: String) -> Option { let header = &line.as_bytes()[..32]; // expect a header for this line if std::str::from_utf8(header).unwrap() != "PUSH: Received control message: ".to_string() { - println!( - "no son iguales «{}» & »{}«", - std::str::from_utf8(header).unwrap(), - "PUSH: Received control message:" - ); return None; } let dhcp_option_dns_re = Regex::new(r"dhcp-option DNS ([^,]+),").unwrap();