Skip to content

Commit

Permalink
Windows pac
Browse files Browse the repository at this point in the history
  • Loading branch information
nullchinchilla committed Apr 25, 2024
1 parent 57e8ad1 commit 4d2893f
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 12 deletions.
20 changes: 16 additions & 4 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions binaries/geph5-client-gui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ tracing-subscriber = "0.3.18"
serde_json = "1.0.115"
oneshot = "0.1.6"
chrono = "0.4.38"
winreg = "0.52.0"
winapi = { version = "0.3.9", features = ["wininet"] }

2 changes: 1 addition & 1 deletion binaries/geph5-client-gui/src/l10n.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
label,en,zh,ru,fa
about,About,关于,О программе,Darbāre
account_info,Account Info,帐户信息,Информация об аккаунте,Eṭṭelā'āt-e ḥesāb
auto_configure_proxy,Auto-configure proxy,自动配置代理,Автоматическая настройка прокси,Peykarbandī-ye xodkār-e proxy
proxy_autoconf,Auto-configure proxy,自动配置代理,Автоматическая настройка прокси,Peykarbandī-ye xodkār-e proxy
cancel,Cancel,取消,Отмена,Lagv
connect,Connect,连接,Подключить,Etesāl
connected,Connected,已连接,Подключено,Mottasel
Expand Down
1 change: 1 addition & 0 deletions binaries/geph5-client-gui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
mod daemon;
mod l10n;
mod logs;
mod pac;
mod prefs;
mod refresh_cell;
mod settings;
Expand Down
4 changes: 4 additions & 0 deletions binaries/geph5-client-gui/src/pac/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod windows;

#[cfg(windows)]
pub use windows::*;
63 changes: 63 additions & 0 deletions binaries/geph5-client-gui/src/pac/windows.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

use anyhow::Context;
use std::net::SocketAddr;
use winapi::um::wininet::{
InternetSetOptionA, INTERNET_OPTION_REFRESH, INTERNET_OPTION_SETTINGS_CHANGED,
};
use winreg::enums::*;
use winreg::types::ToRegValue;
use winreg::RegKey;

const HTTP_PROXY_REG_PATH: &str = r"Software\Microsoft\Windows\CurrentVersion\Internet Settings";

fn set_registry_value(key: &RegKey, name: &str, value: &impl ToRegValue) -> anyhow::Result<()> {
key.set_value(name, value)
.context(format!("Failed to set registry value '{}'", name))
}

fn refresh_internet_options() {
unsafe {
InternetSetOptionA(
std::ptr::null_mut(),
INTERNET_OPTION_SETTINGS_CHANGED,
std::ptr::null_mut(),
0,
);
InternetSetOptionA(
std::ptr::null_mut(),
INTERNET_OPTION_REFRESH,
std::ptr::null_mut(),
0,
);
}
}

pub fn set_http_proxy(proxy: SocketAddr) -> anyhow::Result<()> {
let key = RegKey::predef(HKEY_CURRENT_USER)
.open_subkey_with_flags(HTTP_PROXY_REG_PATH, KEY_WRITE)
.context("Failed to open registry key with write access")?;

set_registry_value(
&key,
"ProxyServer",
&format!("{}:{}", proxy.ip(), proxy.port()),
)?;
set_registry_value(&key, "ProxyEnable", &1u32)?;

refresh_internet_options();

Ok(())
}

pub fn unset_http_proxy() -> anyhow::Result<()> {
let key = RegKey::predef(HKEY_CURRENT_USER)
.open_subkey_with_flags(HTTP_PROXY_REG_PATH, KEY_WRITE)
.context("Failed to open registry key with write access")?;

set_registry_value(&key, "ProxyServer", &"")?;
set_registry_value(&key, "ProxyEnable", &0u32)?;

refresh_internet_options();

Ok(())
}
21 changes: 15 additions & 6 deletions binaries/geph5-client-gui/src/settings.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

use geph5_broker_protocol::Credential;
use geph5_client::Config;
use once_cell::sync::Lazy;
Expand All @@ -6,7 +7,7 @@ use smol_str::{SmolStr, ToSmolStr};
use crate::{l10n, store_cell::StoreCell};

pub fn get_config() -> anyhow::Result<Config> {
let yaml: serde_yaml::Value = serde_yaml::from_str(include_str!("settings_default.yaml"))?;
let yaml: serde_yaml::Value = serde_yaml::from_str(include_str!("settings_default.yaml"))?;
let json: serde_json::Value = serde_json::to_value(&yaml)?;
let mut cfg: Config = serde_json::from_value(json)?;
cfg.credentials = Credential::LegacyUsernamePassword {
Expand All @@ -28,25 +29,28 @@ static ZOOM_FACTOR: Lazy<StoreCell<f32>> =
pub static LANG_CODE: Lazy<StoreCell<SmolStr>> =
Lazy::new(|| StoreCell::new_persistent("lang_code", || "en".to_smolstr()));

pub static PROXY_AUTOCONF: Lazy<StoreCell<bool>> =
Lazy::new(|| StoreCell::new_persistent("proxy_autoconf", || false));

pub fn render_settings(ctx: &egui::Context, ui: &mut egui::Ui) -> anyhow::Result<()> {
ctx.set_zoom_factor(ZOOM_FACTOR.get());
USERNAME.modify(|username| {
ui.horizontal(|ui| {
ui.label(l10n("username"));
ui.text_edit_singleline(username);
});
})
});
PASSWORD.modify(|password| {
ui.horizontal(|ui| {
ui.label(l10n("password"));
ui.add(egui::TextEdit::singleline(password).password(true));
});
})
});
ZOOM_FACTOR.modify(|zoom_factor| {
ui.horizontal(|ui| {
ui.label(l10n("zoom_factor"));
ui.add(egui::Slider::new(zoom_factor, 1.0..=1.5));
});
})
});
ui.horizontal(|ui| {
ui.label(l10n("language"));
Expand All @@ -64,7 +68,13 @@ pub fn render_settings(ctx: &egui::Context, ui: &mut egui::Ui) -> anyhow::Result
ui.selectable_value(lang_code, "zh".into(), "中文");
ui.selectable_value(lang_code, "fa".into(), "Fārsī");
ui.selectable_value(lang_code, "ru".into(), "Русский");
});
})
});
});
PROXY_AUTOCONF.modify(|proxy_autoconf| {
ui.horizontal(|ui| {
ui.label(l10n("proxy_autoconf"));
ui.add(egui::Checkbox::new(proxy_autoconf, ""));
})
});
let config = get_config()?;
Expand All @@ -74,6 +84,5 @@ pub fn render_settings(ctx: &egui::Context, ui: &mut egui::Ui) -> anyhow::Result
ui.centered_and_justified(|ui| {
egui::ScrollArea::vertical().show(ui, |ui| ui.code_editor(&mut config_yaml.as_str()))
});

Ok(())
}
9 changes: 8 additions & 1 deletion binaries/geph5-client-gui/src/tabs/dashboard.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use std::time::Duration;

use crate::{daemon::DAEMON, l10n::l10n, settings::get_config};
use crate::{
daemon::DAEMON,
l10n::l10n,
pac::{set_http_proxy, unset_http_proxy},
settings::get_config,
};

pub struct Dashboard {}

Expand Down Expand Up @@ -34,10 +39,12 @@ impl Dashboard {
if daemon.is_none() {
if ui.button(l10n("connect")).clicked() {
tracing::warn!("connect clicked");
set_http_proxy("127.0.0.1:11111".parse()?)?;
*daemon = Some(geph5_client::Client::start(get_config()?));
}
} else if ui.button(l10n("disconnect")).clicked() {
tracing::warn!("disconnect clicked");
unset_http_proxy()?;
*daemon = None;
}
anyhow::Ok(())
Expand Down

0 comments on commit 4d2893f

Please sign in to comment.