Skip to content

Commit

Permalink
ref: 整理代码
Browse files Browse the repository at this point in the history
  • Loading branch information
HKMV committed May 26, 2024
1 parent c307334 commit 4c86c20
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 66 deletions.
18 changes: 2 additions & 16 deletions src/core/nal.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
use std::collections::HashMap;
use std::fs::File;
use std::time::Duration;
use async_trait::async_trait;
use log::{LevelFilter, warn};
use reqwest::{Client, Error};
use serde::{Deserialize, Serialize};
use serde::__private::de::Content::I16;
use serde_json::Value;
use serde_yaml::Serializer;
use tokio::time::timeout;
use tracing_subscriber::fmt::MakeWriter;

/// 网络自动登录trait
#[async_trait]
Expand All @@ -32,15 +27,6 @@ pub struct LoginConfig {
pub password: String,
}

impl LoginConfig {
pub fn new(username: &str, password: &str) -> Self {
Self {
username: String::from(username),
password: String::from(password),
}
}
}

/// 网络状态检测相关参数
#[derive(Debug, Serialize, Deserialize)]
pub struct NetStatusCheck {
Expand Down Expand Up @@ -89,7 +75,7 @@ impl NalConfig {
/// 初始化配置
pub fn init_config() -> NalConfig {
let conf_file_path = "./config.yml";
let result = File::open(conf_file_path.clone());
let result = File::open(conf_file_path);
if result.is_err() {
//初始化配置
let config = NalConfig::default();
Expand Down Expand Up @@ -126,7 +112,7 @@ pub fn get_no_proxy_client() -> Client {
/// 检测网络是否正常
pub async fn check_net() -> bool {
let mut fail_count = 0;
for i in 0..3 {
for _ in 0..3 {
if !get_no_proxy_client()
.get("https://baidu.com")
.send()
Expand Down
6 changes: 2 additions & 4 deletions src/core/sangfor.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use std::collections::HashMap;
use std::future::Future;
use std::time::{SystemTime, UNIX_EPOCH};
use async_trait::async_trait;
use log::{debug, info};
use reqwest::{Error, Response};
use crate::core::nal;
use log::{debug};
use reqwest::{Error};
use crate::core::nal::{get_no_proxy_client, LoginConfig, Nal};
use crate::util::rc4::RC4;

Expand Down
18 changes: 4 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
use std::ffi::OsString;
use std::{env, fs};
use std::fs::{File, OpenOptions};
use std::str::FromStr;
use tokio::time::{Duration, sleep};
use log::{debug, error, info, LevelFilter, warn};
use serde_json::Value::Bool;
use crate::core::nal;
use crate::core::nal::{login, LoginConfig, NalConfig, NetStatusCheck, NetType};
use crate::core::nal::{NalConfig};
use crate::core::sangfor::Sangfor;
use std::io::{Error, Write};
use std::path::PathBuf;
use std::time::SystemTime;
use clap::{Command, Parser};
use serde::{Deserialize, Serialize};
use service_manager::{ServiceInstallCtx, ServiceLabel, ServiceManager, ServiceStartCtx, ServiceStopCtx, ServiceUninstallCtx};
use util::service::Service;
use crate::util::cmd::Cli;
use clap::{Parser};
use crate::util::service::Service;

mod core;
mod util;
Expand Down Expand Up @@ -66,7 +56,7 @@ async fn handler(config: NalConfig) {
//登录
let sangfor = Sangfor::new("http://1.1.1.4");
let login_ok = nal::login(&sangfor, &config.login).await;
if login_ok.unwrap_or_else(|e| { false }) {
if login_ok.unwrap_or_else(|_| { false }) {
info!("登录成功");
} else {
error!("登录失败");
Expand Down
12 changes: 3 additions & 9 deletions src/test/nal.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
use log::log;
use crate::core::nal;
use crate::core::nal::{LoginConfig, Nal};
use crate::core::sangfor::Sangfor;

// #[test]
#[tokio::test]
async fn sangfor() {
let nal: &dyn Nal = &Sangfor::new("http://1.1.1.4/ac_portal/login.php");
let config = LoginConfig {
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(),
};
Expand All @@ -17,6 +11,6 @@ async fn sangfor() {

#[tokio::test]
async fn check_net() {
let is_ok = nal::check_net().await;
let is_ok = crate::core::nal::check_net().await;
println!(" is_ok: {:?}", is_ok);
}
7 changes: 1 addition & 6 deletions src/test/rc4.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
use std::string::String;
use std::str::{from_utf8, from_utf8_mut};
// use rc4::{consts::*, KeyInit, StreamCipher};
// use rc4::{Key, Rc4};

#[test]
fn custom_encode(){
let key = b"secret_key";
let mut rc4 = crate::util::rc4::RC4::new(key);

let mut data = b"hello world".to_vec();
println!("Original: {:?}", from_utf8(&*data));
println!("Original: {:?}", str::from_utf8(&*data));
println!("Original: {:?}", data.iter().map(|b| format!("{:02x}", b)).collect::<String>());

// let mut data = data.as_slice();
Expand Down
28 changes: 12 additions & 16 deletions src/util/logs.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::{env, fs};
use std::fs::File;
use std::io::{Error};
use std::path::Path;
use std::str::FromStr;
use std::sync::Mutex;
use log::{error, LevelFilter};
use tracing::{info, Level};
use log::{LevelFilter};
use tracing::{Level};
use tracing_subscriber::fmt::writer::MakeWriterExt;
use tracing_subscriber::util::SubscriberInitExt;

Expand All @@ -25,8 +24,8 @@ pub fn init(log_file: &str, level: LevelFilter) -> Result<(), Error> {
Ok(())
}

fn init_env_logger() {
/* //env_logger
/* fn init_env_logger() {
//env_logger
/// 定义一个函数用于将DateTime对象转换为指定格式的字符串
fn format_timestamp(dt: Timestamp) -> String {
let dt: DateTime<Utc> = DateTime::from_str(&*dt.to_string()).unwrap();
Expand Down Expand Up @@ -55,11 +54,11 @@ fn init_env_logger() {
}) // 自定义格式化逻辑
// .filter(Some("crate"), log::LevelFilter::Info) // 只记录指定crate的日志
.target(log_file_path)
.init();*/
}
.init();
}*/

fn init_fern() {
/* //fern
/* fn init_fern() {
//fern
use chrono::Local;
fern::Dispatch::new()
.format(|out, message, record| {
Expand All @@ -76,21 +75,19 @@ fn init_fern() {
.chain(std::io::stdout())
.chain(fern::log_file(log_file_path)?)
.apply()?;*/
}
.apply()?;
}*/


/// 这是创建新文件的大小。32MB
/*/// 这是创建新文件的大小。32MB
const TRIGGER_FILE_SIZE: u64 = 1024 * 1024 * 32;
/// 日志存档将移动到的位置 有关模式信息,请参阅:
/// https://docs.rs/log4rs/latest/log4rs/append/rolling_file/policy/compound/roll/fixed_window/struct.FixedWindowRollerBuilder.html#method.build
const ARCHIVE_PATTERN: &str = "./logs/nal.{}.log";
/// 要保留的存档日志文件数
const LOG_FILE_COUNT: u32 = 3;

/// 初始化log4rs
fn init_log4rs(log_file_path: String, level: LevelFilter) {
/*
use log4rs::append::console::ConsoleAppender;
use log4rs::append::file::FileAppender;
use log4rs::{Config};
Expand Down Expand Up @@ -143,8 +140,7 @@ fn init_log4rs(log_file_path: String, level: LevelFilter) {
.unwrap()
};
log4rs::init_config(config).unwrap();
*/
}
}*/

fn init_tracing(logs_dir: String, log_file: String, level: LevelFilter) {
let format = time::format_description::parse(
Expand Down
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::ffi::OsString;
use std::path::PathBuf;
use log::{debug, info};
use log::{info};
use service_manager::{ServiceInstallCtx, ServiceLabel, ServiceManager, ServiceStartCtx, ServiceStopCtx, ServiceUninstallCtx};

/// 系统服务
Expand Down

0 comments on commit 4c86c20

Please sign in to comment.