Skip to content

Commit

Permalink
refact: custom client proxy (#8093)
Browse files Browse the repository at this point in the history
Signed-off-by: fufesou <shuanglongchen@yeah.net>
  • Loading branch information
fufesou committed May 19, 2024
1 parent 986b9fb commit d8c9250
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
10 changes: 9 additions & 1 deletion flutter/lib/desktop/pages/desktop_setting_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2170,6 +2170,11 @@ void changeSocks5Proxy() async {
var pwdController = TextEditingController(text: password);
RxBool obscure = true.obs;

// proxy settings
// The following option is a not real key, it is just used for custom client advanced settings.
const String optionProxyUrl = "proxy-url";
final isOptFixed = isOptionFixed(optionProxyUrl);

var isInProgress = false;
gFFI.dialogManager.show((setState, close, context) {
submit() async {
Expand Down Expand Up @@ -2247,6 +2252,7 @@ void changeSocks5Proxy() async {
),
controller: proxyController,
autofocus: true,
enabled: !isOptFixed,
),
),
],
Expand All @@ -2262,6 +2268,7 @@ void changeSocks5Proxy() async {
Expanded(
child: TextField(
controller: userController,
enabled: isInProgress,
),
),
],
Expand All @@ -2284,6 +2291,7 @@ void changeSocks5Proxy() async {
? Icons.visibility_off
: Icons.visibility))),
controller: pwdController,
enabled: !isOptFixed,
)),
),
],
Expand All @@ -2296,7 +2304,7 @@ void changeSocks5Proxy() async {
),
actions: [
dialogButton('Cancel', onPressed: close, isOutline: true),
dialogButton('OK', onPressed: submit),
if (!isOptFixed) dialogButton('OK', onPressed: submit),
],
onSubmit: submit,
onCancel: close,
Expand Down
56 changes: 52 additions & 4 deletions libs/hbb_common/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1014,8 +1014,30 @@ impl Config {
config.store();
}

#[inline]
fn get_socks_from_custom_client_advanced_settings(
settings: &HashMap<String, String>,
) -> Option<Socks5Server> {
let url = settings.get(keys::OPTION_PROXY_URL)?;
Some(Socks5Server {
proxy: url.to_owned(),
username: settings
.get(keys::OPTION_PROXY_USERNAME)
.map(|x| x.to_string())
.unwrap_or_default(),
password: settings
.get(keys::OPTION_PROXY_PASSWORD)
.map(|x| x.to_string())
.unwrap_or_default(),
})
}

pub fn get_socks() -> Option<Socks5Server> {
CONFIG2.read().unwrap().socks.clone()
Self::get_socks_from_custom_client_advanced_settings(&OVERWRITE_SETTINGS.read().unwrap())
.or(CONFIG2.read().unwrap().socks.clone())
.or(Self::get_socks_from_custom_client_advanced_settings(
&DEFAULT_SETTINGS.read().unwrap(),
))
}

#[inline]
Expand All @@ -1024,10 +1046,26 @@ impl Config {
}

pub fn get_network_type() -> NetworkType {
match &CONFIG2.read().unwrap().socks {
None => NetworkType::Direct,
Some(_) => NetworkType::ProxySocks,
if OVERWRITE_SETTINGS
.read()
.unwrap()
.get(keys::OPTION_PROXY_URL)
.is_some()
{
return NetworkType::ProxySocks;
}
if CONFIG2.read().unwrap().socks.is_some() {
return NetworkType::ProxySocks;
}
if DEFAULT_SETTINGS
.read()
.unwrap()
.get(keys::OPTION_PROXY_URL)
.is_some()
{
return NetworkType::ProxySocks;
}
NetworkType::Direct
}

pub fn get() -> Config {
Expand Down Expand Up @@ -2056,6 +2094,13 @@ pub mod keys {
pub const OPTION_FLUTTER_PEER_CARD_UI_TYLE: &str = "peer-card-ui-type";
pub const OPTION_FLUTTER_CURRENT_AB_NAME: &str = "current-ab-name";

// proxy settings
// The following options are not real keys, they are just used for custom client advanced settings.
// The real keys are in Config2::socks.
pub const OPTION_PROXY_URL: &str = "proxy-url";
pub const OPTION_PROXY_USERNAME: &str = "proxy-username";
pub const OPTION_PROXY_PASSWORD: &str = "proxy-password";

// DEFAULT_DISPLAY_SETTINGS, OVERWRITE_DISPLAY_SETTINGS
pub const KEYS_DISPLAY_SETTINGS: &[&str] = &[
OPTION_VIEW_ONLY,
Expand Down Expand Up @@ -2131,6 +2176,9 @@ pub mod keys {
OPTION_ALLOW_LINUX_HEADLESS,
OPTION_ENABLE_HWCODEC,
OPTION_APPROVE_MODE,
OPTION_PROXY_URL,
OPTION_PROXY_USERNAME,
OPTION_PROXY_PASSWORD,
];
}

Expand Down

0 comments on commit d8c9250

Please sign in to comment.