Skip to content

Commit

Permalink
feat: 改为同步实现
Browse files Browse the repository at this point in the history
  • Loading branch information
SerenitySir committed Jul 24, 2024
1 parent 7215fc9 commit 514d0ea
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 36 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ tracing-appender = "0.2"
clap = { version = "4.5.4", features = ["derive"] }
clap_derive = "4.5.4"

tokio = { version = "1", features = ["full"] }
async-trait = "0.1.77"
#tokio = { version = "1", features = ["full"] }
#async-trait = "0.1.77"
time = { version = "0.3.36", features = ["parsing", "macros"] }
#rc4 = "0.1.0"

service-manager = "0.7"

reqwest = { version = "0.11", features = ["json"] }
reqwest = { version = "0.11", features = ["json", "blocking"] }

serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.114"
Expand Down
18 changes: 8 additions & 10 deletions src/core/nal.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use async_trait::async_trait;
use log::{warn, LevelFilter};
use reqwest::{Client, Error};
use reqwest::{Error};
use serde::{Deserialize, Serialize};
use serde_yaml::Serializer;
use std::fs::File;
use std::time::Duration;
use reqwest::blocking::Client;

/// 网络自动登录trait
#[async_trait]
pub trait Nal {
/// 登录网络
async fn login_net(&self, config: &LoginConfig) -> Result<bool, Error>;
fn login_net(&self, config: &LoginConfig) -> Result<bool, Error>;
}

/// 网络类型
Expand Down Expand Up @@ -104,21 +103,20 @@ pub fn init_config() -> NalConfig {
/// 获取没有代理的客户端
pub fn get_no_proxy_client() -> Client {
Client::builder()
.no_proxy() // 禁用代理
.no_proxy()
.connect_timeout(Duration::from_secs(10))
.timeout(Duration::from_secs(30))
.timeout(Duration::from_secs(10))
.build()
.unwrap()
}

/// 检测网络是否正常
pub async fn check_net() -> bool {
pub fn check_net() -> bool {
let mut fail_count = 0;
for _ in 0..3 {
if !get_no_proxy_client()
.get("https://baidu.com")
.send()
.await
.is_ok()
{
fail_count += 1;
Expand All @@ -128,6 +126,6 @@ pub async fn check_net() -> bool {
fail_count < 3
}

pub async fn login<T: Nal>(nal: &T, lc: &LoginConfig) -> Result<bool, Error> {
nal.login_net(lc).await
pub fn login<T: Nal>(nal: &T, lc: &LoginConfig) -> Result<bool, Error> {
nal.login_net(lc)
}
10 changes: 3 additions & 7 deletions src/core/sangfor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::core::nal::{get_no_proxy_client, LoginConfig, Nal};
use crate::util::rc4::RC4;
use async_trait::async_trait;
use log::debug;
use reqwest::Error;
use std::collections::HashMap;
Expand Down Expand Up @@ -39,9 +38,8 @@ impl Sangfor {
}
}

#[async_trait]
impl Nal for Sangfor {
async fn login_net(&self, config: &LoginConfig) -> Result<bool, Error> {
fn login_net(&self, config: &LoginConfig) -> Result<bool, Error> {
let client = get_no_proxy_client();

let mut params = HashMap::new();
Expand All @@ -61,10 +59,8 @@ impl Nal for Sangfor {
let rsp = client
.post(lu)
.form(&params)
.send()
.await?
.json::<serde_json::Value>()
.await?;
.send()?
.json::<serde_json::Value>()?;
debug!("login result: {rsp:#?}");
// rsp.is_ok()
Ok(rsp["success"] == true)
Expand Down
18 changes: 9 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use crate::util::service::Service;
use clap::Parser;
use log::{debug, error, info, warn, LevelFilter};
use std::str::FromStr;
use tokio::time::{sleep, Duration};
use std::thread::sleep;
use std::time::Duration;

mod core;
mod test;
mod util;

#[tokio::main]
async fn main() {
fn main() {
let config = nal::init_config();
debug!("config: {config:#?}");
let level_filter =
Expand Down Expand Up @@ -54,26 +54,26 @@ async fn main() {
return;
}
if cli.run {
handler(config).await;
handler(config);
return;
}
//默认直接运行
handler(config).await
handler(config)
}

async fn handler(config: NalConfig) {
fn handler(config: NalConfig) {
println!("开始检测。。。");
if !cfg!(debug_assertions) {
info!("开始检测。。。");
}
loop {
//检测网络是否正常
let is_ok = nal::check_net().await;
let is_ok = nal::check_net();
if !is_ok {
warn!("网络异常");
//登录
let sangfor = Sangfor::new("http://1.1.1.4");
let login_ok = nal::login(&sangfor, &config.login).await;
let login_ok = nal::login(&sangfor, &config.login);
if login_ok.unwrap_or_else(|_| false) {
info!("登录成功");
} else {
Expand All @@ -84,6 +84,6 @@ async fn handler(config: NalConfig) {
};

// 延迟指定秒后再次执行
sleep(Duration::from_secs(config.check.interval as u64)).await;
sleep(Duration::from_secs(config.check.interval as u64));
}
}
14 changes: 8 additions & 6 deletions src/test/nal.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#[tokio::test]
async fn sangfor() {
#[test]
fn sangfor() {
let nal: &dyn crate::core::nal::Nal =
&crate::core::sangfor::Sangfor::new("http://1.1.1.4/ac_portal/login.php");
let config = crate::core::nal::LoginConfig {
username: 2336.to_string(),
password: 13141.to_string(),
};
let is_ok = nal.login_net(&config).await;
let is_ok = nal.login_net(&config).unwrap_or_else(|_| false);
println!(" is_ok: {:?}", is_ok);
assert_eq!(is_ok, true);
}

#[tokio::test]
async fn check_net() {
let is_ok = crate::core::nal::check_net().await;
#[test]
fn check_net() {
let is_ok = crate::core::nal::check_net();
println!(" is_ok: {:?}", is_ok);
assert_eq!(is_ok, true);
}
2 changes: 1 addition & 1 deletion src/util/service.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::env;
use std::path::PathBuf;
use log::{error, info};
use service_manager::{ScConfig, ScInstallConfig, ScServiceManager, ServiceInstallCtx, ServiceLabel, ServiceManager, ServiceStartCtx, ServiceStopCtx, ServiceUninstallCtx, WindowsErrorSeverity};
use service_manager::{ServiceInstallCtx, ServiceLabel, ServiceManager, ServiceStartCtx, ServiceStopCtx, ServiceUninstallCtx};

/// 系统服务
pub struct Service {
Expand Down

0 comments on commit 514d0ea

Please sign in to comment.