openvpn-client-connect/src/main.rs

45 lines
1.6 KiB
Rust

use sha2::{Sha256, Digest};
use std::fs::OpenOptions;
use std::io::Write;
use std::env;
use clap::Parser;
#[derive(Parser,Default,Debug)]
struct Argumets {
#[clap(long, short = 'c')]
command: String,
#[clap(long, short = '6')]
pref_v6: Vec<String>,
#[clap(long, short = '4')]
pref_v4: Vec<String>,
#[clap(long, short = 'o')]
out_file: String,
}
fn main() {
let args = Argumets::parse();
match args.command.as_str() {
"connect" => {
let common_name = env::var("common_name").expect("common_name variable is not set");
let hash_binary = Sha256::digest(common_name.as_bytes());
let hash_string: String = String::from(format!("{:x}", hash_binary));
let mut file = OpenOptions::new().write(true).open(args.out_file.as_str()).expect("Can not open file");
file.set_len(0).expect("failed to set len");
let mut conf = String::new();
for pref in args.pref_v4 {
let conf_v4 = String::from(format!("ifconfig-push {0}.{1}.{2} 255.255.0.0\n",
&pref, &hash_binary[0], &hash_binary[1]));
conf.push_str(&conf_v4)
}
for pref in args.pref_v6 {
let conf_v6 = String::from(format!("ifconfig-ipv6-push {0}:{1}:{2}:{3}:{4}/64\n",
&pref, &hash_string[0..4], &hash_string[4..8], &hash_string[8..12], &hash_string[12..16]));
conf.push_str(&conf_v6)
}
file.write(conf.as_bytes()).expect("Can not write file");
}
_ => {}
}
}