Skip to content

Commit

Permalink
use config file to logout (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
1328411791 committed Apr 9, 2024
1 parent d47cd60 commit 7057325
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
3 changes: 2 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::{env, net::Ipv4Addr};

fn main() {
let auth_server_ip = env!(
let auth_server_ip =
env!(
"AUTH_SERVER_IP",
"Expect env AUTH_SERVER_IP, export AUTH_SERVER_IP=10.0.0.1"
);
Expand Down
17 changes: 7 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
mod user;
pub use file::read_config_from_file;
pub use srun::*;
pub use user::User;
pub use utils::get_ip_by_if_name;
pub use utils::select_ip;
pub use xencode::param_i;

mod user;
mod srun;
pub use srun::*;

mod xencode;
pub use xencode::param_i;

mod file;
pub use file::read_config_from_file;

mod utils;
pub use utils::select_ip;

#[cfg(feature = "ureq")]
mod http_client;

pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
45 changes: 43 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use getopts::{Matches, Options};
use srun::{read_config_from_file, select_ip, SrunClient, User};
use std::env;

use getopts::{Matches, Options};

use srun::{get_ip_by_if_name, read_config_from_file, select_ip, SrunClient, User};

fn print_usage(opts: Option<&Options>) {
let brief = "Usage: srun ACTION [options]\n\nActions: login | logout".to_string();
if let Some(opts) = opts {
Expand Down Expand Up @@ -77,6 +79,7 @@ fn logout_match(args: &[String]) {
opts.optopt("u", "username", "username", "");
opts.optopt("i", "ip", "ip", "");
opts.optflag("d", "detect", "detect client ip");
opts.optopt("c", "config", "logout by config file", "");
opts.optflag("", "select-ip", "select client ip");
opts.optflag("", "strict-bind", "strict bind ip");
opts.optopt("", "acid", "acid", "");
Expand All @@ -93,6 +96,8 @@ fn logout_match(args: &[String]) {

if matches.opt_present("h") {
print_usage(Some(&options));
} else if matches.opt_present("c") {
config_logout(matches);
} else {
logout(matches)
}
Expand Down Expand Up @@ -234,6 +239,42 @@ fn single_login(matches: Matches) {
}
}

fn config_logout(matches: Matches) {
let config_path = matches.opt_str("c").unwrap();
match read_config_from_file(config_path) {
Ok(config) => {
let config_i = config.clone();
let auth_server = config
.server
.clone()
.unwrap_or_else(|| match matches.opt_str("s") {
Some(u) => u,
None => format!("http://{}", env!("AUTH_SERVER_IP")),
});
for user in config_i {
println!("logout user: {:#?}", user);
let ip = user.ip.unwrap_or_else(|| {
get_ip_by_if_name(&user.if_name.unwrap_or_default()).unwrap_or_default()
});
let mut client = SrunClient::new_for_logout(&auth_server, &user.username, &ip)
.set_detect_ip(config.detect_ip)
.set_strict_bind(config.strict_bind);

if let Some(acid) = config.acid {
client.set_acid(acid);
}

if let Err(e) = client.logout() {
println!("logout error: {}", e);
}
}
}
Err(e) => {
println!("read config file error: {}", e);
}
}
}

fn logout(matches: Matches) {
let auth_server = match matches.opt_str("s") {
Some(u) => u,
Expand Down

0 comments on commit 7057325

Please sign in to comment.