Skip to content

Commit

Permalink
Remove the code only for Linux. And fix some bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanChane committed Mar 20, 2024
1 parent 77e139b commit 79233da
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 470 deletions.
31 changes: 15 additions & 16 deletions clashtui/Cargo.lock

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

5 changes: 1 addition & 4 deletions clashtui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ log = "0.4"
log4rs = {version = "1.3", default-features = false, features = ["pattern_encoder", "file_appender"]}
enumflags2 = "0.7.9"
nix = {version = "0.28.0", features = ["fs", "user"]}
libc = "0.2.153"
regex = "1.10.3"

[target.'cfg(target_os = "windows")'.dependencies]
encoding = "0.2.33"

[workspace]
Expand All @@ -55,6 +52,6 @@ section = "utility"
priority = "optional"
assets = [
['target/release/clashtui', 'usr/bin/clashtui', '755'],
['../README.md', 'usr/share/doc/hust-network-login/README.md', '644'],
['../README.md', 'usr/share/doc/clashtui/README.md', '644'],
]
maintainer-scripts = 'debian/'
4 changes: 3 additions & 1 deletion clashtui/api/src/clash.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const DEFAULT_PAYLOAD: &str = "'{\"path\": \"\", \"payload\": \"\"}'";
const TIMEOUT: u8 = 3;
//const TIMEOUT: u8 = 3;
const TIMEOUT: u8 = 10; // Adapting to poor network conditions.
// ToDo: Users can adjust settings based on their network quality.
#[cfg(target_feature = "deprecated")]
const GEO_URI: &str = "https://api.github.com/repos/MetaCubeX/meta-rules-dat/releases/latest";
#[cfg(target_feature = "deprecated")]
Expand Down
35 changes: 1 addition & 34 deletions clashtui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ impl App {

pub fn event(&mut self, ev: &event::Event) -> Result<EventState, std::io::Error> {
let mut event_state = self.msgpopup.event(ev)?;
// Popup Priority Event Handling. Because popups always appear above non-popups.
if event_state.is_notconsumed() {
event_state = self.popup_event(ev)?;
}
Expand Down Expand Up @@ -250,39 +251,6 @@ impl App {
}

fn do_some_job_after_initapp_before_setupui(&mut self) {
// ## Correct the perm of files in clash_cfg_dir.
if ! self.clashtui_util.check_perms_of_ccd_files() {
let ccd_str = self.clashtui_util.tui_cfg.clash_cfg_dir.as_str();
if ! utils::is_run_as_root() {
print!("The permissions of the '{}' files are incorrect. clashtui need to run as root to correct. Proceed with running as root? [Y/n] ", ccd_str);
std::io::stdout().flush().expect("Failed to flush stdout");

let mut input = String::new();
let stdin = std::io::stdin();
stdin.lock().read_line(&mut input).unwrap();

if input.trim().to_lowercase().as_str() == "y" {
utils::run_as_root();
}

} else {
if utils::is_clashtui_ep() {
println!("\nStart correct the permissions of files in '{}':\n", ccd_str);
let dir = std::path::Path::new(ccd_str);
if let Some(group_name) = utils::get_file_group_name(&dir.to_path_buf()) {
utils::restore_fileop_as_root();
utils::modify_file_perms_in_dir(&dir.to_path_buf(), group_name.as_str());
utils::mock_fileop_as_sudo_user();
}
print!("\nEnd correct the permissions of files in '{}'. \n\nPress any key to continue. ", ccd_str);
std::io::stdout().flush().expect("Failed to flush stdout");
let _ = std::io::stdin().read(&mut [0u8]);
} else { // user manually executing `sudo clashtui`
// Do nothing, as root is unaffected by permissions.
}
}
}

let cli_env: CliEnv = argh::from_env();

// ## CliMode
Expand All @@ -291,7 +259,6 @@ impl App {
if cli_env.update_all_profiles {
is_cli_mode = true;

log::info!("Cron Mode!");
self.clashtui_util.get_profile_names()
.unwrap()
.into_iter()
Expand Down
24 changes: 5 additions & 19 deletions clashtui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ mod app;
mod tui;
mod utils;

use core::time::Duration;
use nix::sys;

use crate::app::App;
use crate::utils::{Flag, Flags};

use core::time::Duration;

pub const VERSION: &str = concat!(env!("CLASHTUI_VERSION"));

fn main() {
Expand All @@ -23,21 +22,10 @@ fn main() {

let mut flags = Flags::empty();

// ## Is CliMode
if cli_env.update_all_profiles {
flags.insert(Flag::CliMode)
}

// ## Setup logging as early as possible. So We can log.
let config_dir = load_app_dir(&mut flags);
setup_logging(config_dir.join("clashtui.log").to_str().unwrap());

// To allow the mihomo process to read and write files created by clashtui in clash_cfg_dir, set the umask to 0o002and Users manually add SGID to clash_cfg_dir.
if utils::is_clashtui_ep() {
utils::mock_fileop_as_sudo_user();
}
sys::stat::umask(sys::stat::Mode::from_bits_truncate(0o002));

let tick_rate = 250; // time in ms between two ticks.
if let Err(e) = run(&mut flags, tick_rate, &config_dir, &mut warning_list_msg) {
eprintln!("{e}");
Expand Down Expand Up @@ -66,6 +54,7 @@ pub fn run(flags: &mut Flags<Flag>, tick_rate: u64, config_dir: &std::path::Path
Ok(())
}

use ui::event::KeyCode;
use utils::CfgError;
fn run_app(
app: &mut App,
Expand Down Expand Up @@ -98,6 +87,8 @@ fn run_app(
app.late_event();

if event::poll(tick_rate)? {
//println!("{:?}", &event::read()?); // debug

if let Err(e) = app.event(&event::read()?) {
app.popup_txt_msg(e.to_string())
};
Expand All @@ -117,11 +108,6 @@ fn load_app_dir(flags: &mut Flags<Flag>) -> std::path::PathBuf {
flags.insert(Flag::PortableMode);
data_dir
} else {
#[cfg(target_os = "linux")]
let clashtui_config_dir_str = env::var("XDG_CONFIG_HOME").map(|p| format!("{}/clashtui", p))
.or_else(|_| env::var("HOME").map(|home| format!("{}/.config/clashtui", home)))
.unwrap();
#[cfg(target_os = "windows")]
let clashtui_config_dir_str = env::var("APPDATA")
.map(|appdata| format!("{}/clashtui", appdata))
.unwrap();
Expand Down
27 changes: 14 additions & 13 deletions clashtui/src/tui/tabs/clashsrvctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,12 @@ impl ClashSrvCtlTab {
pub fn new(clashtui_util: SharedClashTuiUtil, clashtui_state: SharedClashTuiState) -> Self {
let mut operations = List::new(CLASHSRVCTL.to_string());
operations.set_items(vec![
#[cfg(target_os = "linux")]
ClashSrvOp::SetPermission.into(),
ClashSrvOp::StartClashService.into(),
ClashSrvOp::StopClashService.into(),
ClashSrvOp::SwitchMode.into(),
#[cfg(target_os = "windows")]
ClashSrvOp::SwitchSysProxy.into(),
#[cfg(target_os = "windows")]
ClashSrvOp::EnableLoopback.into(),
#[cfg(target_os = "windows")]
ClashSrvOp::InstallSrv.into(),
#[cfg(target_os = "windows")]
ClashSrvOp::UnInstallSrv.into(),
]);
let mut modes = List::new("Mode".to_string());
Expand All @@ -69,24 +63,33 @@ impl super::TabEvent for ClashSrvCtlTab {
if !self.is_visible {
return Ok(EventState::NotConsumed);
}
let event_state;

let mut event_state;

if self.mode_selector.is_visible() {
event_state = self.mode_selector.event(ev)?;
if event_state == EventState::WorkDone {
if event_state.is_consumed() {
return Ok(event_state);
}

if let ui::event::Event::Key(key) = ev {
// One-click key will trigger two KeyEventKind, Press and Release.
//log::info!("{:?}", ev);
if key.kind != ui::event::KeyEventKind::Press {
return Ok(EventState::NotConsumed);
}

if &Keys::Select == key {
if let Some(new) = self.mode_selector.selected() {
self.clashtui_state.borrow_mut().set_mode(new.clone());
}
self.mode_selector.hide();
}
if &Keys::Esc == key {
} else if &Keys::Esc == key {
self.mode_selector.hide();
}

return Ok(EventState::WorkDone);
}
return Ok(EventState::WorkDone);
}

event_state = self.msgpopup.event(ev)?;
Expand All @@ -97,7 +100,6 @@ impl super::TabEvent for ClashSrvCtlTab {
if !self.is_visible {
return Ok(EventState::NotConsumed);
}

let event_state;
if let ui::event::Event::Key(key) = ev {
if key.kind != ui::event::KeyEventKind::Press {
Expand Down Expand Up @@ -127,7 +129,6 @@ impl super::TabEvent for ClashSrvCtlTab {
self.hide_msgpopup();
match op {
ClashSrvOp::SwitchMode => unreachable!(),
#[cfg(target_os = "windows")]
ClashSrvOp::SwitchSysProxy => {
let cur = self
.clashtui_state
Expand Down
11 changes: 0 additions & 11 deletions clashtui/src/tui/tabs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,6 @@ macro_rules! define_enum {
};
}

#[cfg(target_os = "linux")]
define_enum!(
pub ClashSrvOp,
[
StartClashService,
StopClashService,
SetPermission,
SwitchMode
]
);
#[cfg(target_os = "windows")]
define_enum!(
pub ClashSrvOp,
[
Expand Down
Loading

0 comments on commit 79233da

Please sign in to comment.