Skip to content

Commit

Permalink
feat: Test before login
Browse files Browse the repository at this point in the history
  • Loading branch information
zu1k committed Nov 9, 2021
1 parent a3266c8 commit 0dfcfe1
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[package]
name = "sdusrun"
version = "0.4.4"
version = "0.4.5"
authors = ["zu1k <[email protected]>"]
edition = "2021"
description = "SRun3000 login"
description = "SRun authentication system login tools"
readme = "README.md"
homepage = "https://github.com/zu1k/sdusrun"
repository = "https://github.com/zu1k/sdusrun"
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ SRun authentication system login tools. [compatible versions](https://github.com

## Usage

[Pre-built binaries](https://github.com/zu1k/sdusrun/releases)

### CMD mode

```
Expand Down
36 changes: 16 additions & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,15 @@ fn main() {
let mut opts = Options::new();
opts.optopt("s", "server", "auth server", "");
opts.optopt("c", "config", "config file path", "");
opts.optflag("", "continue", "continuous login");
opts.optflag("", "continue", "[Unimplemented] continuous login");
// login
opts.optopt("u", "username", "username", "");
opts.optopt("p", "password", "password", "");
opts.optopt("i", "ip", "ip", "");
opts.optflag("d", "detect", "detect client ip");
opts.optflag("", "select-ip", "select client ip");
opts.optflag("", "strict-bind", "strict bind ip");
opts.optflag(
"",
"test",
"test before login, only avaiable for single user",
);
opts.optflag("", "test", "test network connection before login");

opts.optopt("", "acid", "acid", "");
opts.optopt("", "os", "os, e.g. Windows", "");
Expand Down Expand Up @@ -76,26 +72,26 @@ fn config_login(matches: Matches) {
});
for user in config_i {
println!("login user: {:#?}", user);
let mut mng = SrunClient::new_from_user(&server, user)
let mut client = SrunClient::new_from_user(&server, user)
.set_strict_bind(config.strict_bind)
.set_double_stack(config.double_stack);
if let Some(acid) = config.acid {
mng.set_acid(acid);
client.set_acid(acid);
}
if let Some(ref os) = config.os {
mng.set_os(os);
client.set_os(os);
}
if let Some(ref name) = config.name {
mng.set_name(name);
client.set_name(name);
}
if let Some(retry_delay) = config.retry_delay {
mng.set_retry_delay(retry_delay);
client.set_retry_delay(retry_delay);
}
if let Some(retry_times) = config.retry_times {
mng.set_retry_times(retry_times);
client.set_retry_times(retry_times);
}

if let Err(e) = mng.login() {
if let Err(e) = client.login() {
println!("login error: {}", e);
}
}
Expand Down Expand Up @@ -152,32 +148,32 @@ fn single_login(matches: Matches) {
if_name: None,
};
println!("login user: {:#?}", user);
let mut mng = SrunClient::new_from_user(&auth_server, user)
let mut client = SrunClient::new_from_user(&auth_server, user)
.set_detect_ip(detect_ip)
.set_test_before_login(test)
.set_strict_bind(strict_bind);

if let Some(acid) = matches.opt_str("acid") {
mng.set_acid(acid.parse().unwrap());
client.set_acid(acid.parse().unwrap());
}

if let Some(ref os) = matches.opt_str("os") {
mng.set_os(os);
client.set_os(os);
}

if let Some(ref name) = matches.opt_str("name") {
mng.set_name(name);
client.set_name(name);
}

if let Some(retry_delay) = matches.opt_str("retry-delay") {
mng.set_retry_delay(retry_delay.parse().unwrap_or(300));
client.set_retry_delay(retry_delay.parse().unwrap_or(300));
}

if let Some(retry_times) = matches.opt_str("retry-times") {
mng.set_retry_times(retry_times.parse().unwrap_or(10));
client.set_retry_times(retry_times.parse().unwrap_or(10));
}

if let Err(e) = mng.login() {
if let Err(e) = client.login() {
println!("login error: {}", e);
}
}
16 changes: 15 additions & 1 deletion src/srun.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::{param_i, utils::get_ip_by_if_name, Result, User};
use crate::{
param_i,
utils::{self, get_ip_by_if_name},
Result, User,
};
use hmac::{Hmac, Mac, NewMac};
use md5::Md5;
use reqwest::blocking::{Client, ClientBuilder};
Expand Down Expand Up @@ -156,6 +160,16 @@ impl SrunClient {
}

pub fn login(&mut self) -> Result<()> {
if self.test_before_login {
if let Ok(d) = utils::tcp_ping("baidu.com:80") {
println!(
"Network already connected: tcping baidu.com:80, delay: {}ms",
d
);
return Ok(());
}
}

self.get_token()?;

if self.ip.is_empty() {
Expand Down
3 changes: 1 addition & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ quick_error! {
}
}

#[allow(dead_code)]
fn tcp_ping(addr: &str) -> Result<u16> {
pub fn tcp_ping(addr: &str) -> Result<u16> {
let addr = addr.to_socket_addrs()?.next();
if addr.is_none() {
return Err(Box::new(UtilError::AddrResolveError));
Expand Down

0 comments on commit 0dfcfe1

Please sign in to comment.