From 03fbb0914e6b1347165b00aef873815a36a84758 Mon Sep 17 00:00:00 2001 From: ardocrat Date: Tue, 25 Nov 2025 23:49:05 +0300 Subject: [PATCH] wallet: optimize proxy url parse to use localhost or dns record --- Cargo.toml | 28 ++++++++++---------- src/wallet/wallet.rs | 62 +++++++++++++++++++++++++++++--------------- 2 files changed, 56 insertions(+), 34 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4f7fe33..b469a22 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,7 @@ panic = "abort" [dependencies] log = "0.4.27" -## grin +## grin node #grin_api = "5.3.3" #grin_chain = "5.3.3" #grin_config = "5.3.3" @@ -37,7 +37,7 @@ log = "0.4.27" #grin_keychain = "5.3.3" #grin_util = "5.3.3" - +# test grin_api = { git = "https://code.gri.mw/ardocrat/node", rev = "a3ee15a8b27216cd2ff4b3b99894cd98dd33fa7e" } grin_chain = { git = "https://code.gri.mw/ardocrat/node", rev = "a3ee15a8b27216cd2ff4b3b99894cd98dd33fa7e" } grin_config = { git = "https://code.gri.mw/ardocrat/node", rev = "a3ee15a8b27216cd2ff4b3b99894cd98dd33fa7e" } @@ -47,12 +47,6 @@ grin_servers = { git = "https://code.gri.mw/ardocrat/node", rev = "a3ee15a8b2721 grin_keychain = { git = "https://code.gri.mw/ardocrat/node", rev = "a3ee15a8b27216cd2ff4b3b99894cd98dd33fa7e" } grin_util = { git = "https://code.gri.mw/ardocrat/node", rev = "a3ee15a8b27216cd2ff4b3b99894cd98dd33fa7e" } -#grin_wallet_impls = "5.3.3" -#grin_wallet_api = "5.3.3" -#grin_wallet_libwallet = "5.3.3" -#grin_wallet_util = "5.3.3" -#grin_wallet_controller = "5.3.3" - # local #grin_api = { path = "../grin/api" } #grin_chain = { path = "../grin/chain" } @@ -63,11 +57,12 @@ grin_util = { git = "https://code.gri.mw/ardocrat/node", rev = "a3ee15a8b27216cd #grin_keychain = { path = "../grin/keychain" } #grin_util = { path = "../grin/util" } -#grin_wallet_impls = { path = "../grin-wallet/impls" } -#grin_wallet_api = { path = "../grin-wallet/api"} -#grin_wallet_libwallet = { path = "../grin-wallet/libwallet" } -#grin_wallet_util = { path = "../grin-wallet/util" } -#grin_wallet_controller = { path = "../grin-wallet/controller" } +## grin wallet +#grin_wallet_impls = "5.3.3" +#grin_wallet_api = "5.3.3" +#grin_wallet_libwallet = "5.3.3" +#grin_wallet_util = "5.3.3" +#grin_wallet_controller = "5.3.3" # test grin_wallet_impls = { git = "https://code.gri.mw/ardocrat/wallet", rev = "8c9171e640f4a95bec7e22d9acb1725ebb2e30a1" } @@ -76,6 +71,13 @@ grin_wallet_libwallet = { git = "https://code.gri.mw/ardocrat/wallet", rev = "8c grin_wallet_util = { git = "https://code.gri.mw/ardocrat/wallet", rev = "8c9171e640f4a95bec7e22d9acb1725ebb2e30a1" } grin_wallet_controller = { git = "https://code.gri.mw/ardocrat/wallet", rev = "8c9171e640f4a95bec7e22d9acb1725ebb2e30a1" } +# local +#grin_wallet_impls = { path = "../wallet/impls" } +#grin_wallet_api = { path = "../wallet/api"} +#grin_wallet_libwallet = { path = "../wallet/libwallet" } +#grin_wallet_util = { path = "../wallet/util" } +#grin_wallet_controller = { path = "../wallet/controller" } + ## ui egui = { version = "0.33.0", default-features = false } egui_extras = { version = "0.33.0", features = ["image", "svg"] } diff --git a/src/wallet/wallet.rs b/src/wallet/wallet.rs index e3e30e6..ded6749 100644 --- a/src/wallet/wallet.rs +++ b/src/wallet/wallet.rs @@ -12,6 +12,14 @@ // See the License for the specific language governing permissions and // limitations under the License. +use crate::node::{Node, NodeConfig}; +use crate::tor::Tor; +use crate::wallet::seed::WalletSeed; +use crate::wallet::store::TxHeightStore; +use crate::wallet::types::{ConnectionMethod, PhraseMode, WalletAccount, WalletData, WalletInstance, WalletTask, WalletTransaction, WalletTransactionAction}; +use crate::wallet::{ConnectionsConfig, Mnemonic, WalletConfig}; +use crate::AppConfig; + use futures::channel::oneshot; use grin_api::{ApiServer, Router}; use grin_chain::SyncStatus; @@ -32,8 +40,9 @@ use rand::Rng; use serde_json::{json, Value}; use std::fs::File; use std::io::Write; -use std::net::{SocketAddr, TcpListener}; +use std::net::{SocketAddr, TcpListener, ToSocketAddrs}; use std::path::PathBuf; +use std::str::FromStr; use std::sync::atomic::{AtomicBool, AtomicU8, Ordering}; use std::sync::mpsc::Sender; use std::sync::{mpsc, Arc}; @@ -41,14 +50,6 @@ use std::thread::Thread; use std::time::Duration; use std::{fs, thread}; -use crate::node::{Node, NodeConfig}; -use crate::tor::Tor; -use crate::wallet::seed::WalletSeed; -use crate::wallet::store::TxHeightStore; -use crate::wallet::types::{ConnectionMethod, PhraseMode, WalletAccount, WalletData, WalletInstance, WalletTask, WalletTransaction, WalletTransactionAction}; -use crate::wallet::{ConnectionsConfig, Mnemonic, WalletConfig}; -use crate::AppConfig; - /// Contains wallet instance, configuration and state, handles wallet commands. #[derive(Clone)] pub struct Wallet { @@ -210,20 +211,39 @@ impl Wallet { AppConfig::socks_proxy_url() } else { AppConfig::http_proxy_url() - }.unwrap_or("".to_string()); - let res = url.replace("http://", "").replace("socks5://", "").parse(); - if let Ok(addr) = res { - let scheme = if socks { - "socks5://" - } else { - "http://" - }; - HTTPNodeClient::new_proxy(&node_api_url, node_secret, Some((addr, scheme)))? - } else { - HTTPNodeClient::new_proxy(&node_api_url, node_secret, None)? + }.unwrap_or("".to_string()).replace("http://", "").replace("socks5://", ""); + + // Convert URL to SocketAddr. + let addr_res = match SocketAddr::from_str(url.as_str()) { + Ok(ip_addr) => Some(ip_addr), + Err(_) => { + if let Ok(mut socket_addr_list) = url.to_socket_addrs() { + if let Some(addr) = socket_addr_list.next() { + Some(addr) + } else { + None + } + } else { + None + } + } + }; + + match addr_res { + None => { + HTTPNodeClient::new(&node_api_url, node_secret)? + } + Some(addr) => { + let scheme = if socks { + "socks5://" + } else { + "http://" + }; + HTTPNodeClient::new_proxy(&node_api_url, node_secret, Some((addr, scheme)))? + } } } else { - HTTPNodeClient::new_proxy(&node_api_url, node_secret, None)? + HTTPNodeClient::new(&node_api_url, node_secret)? }; Ok(client) }