Skip to content

Commit 4525395

Browse files
authored
Merge pull request #108 from zhuozhiyongde/master
2 parents 1b1ebbb + f9309fa commit 4525395

3 files changed

Lines changed: 25 additions & 4 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ prost = "0.14"
2828
hudsucker = "0.23"
2929
const_format = "0.2"
3030
rand = "0.9"
31+
url = { version = "2", features = ["serde"] }
3132

3233
[build-dependencies]
3334
prost-build = "0.14"

src/settings.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ pub struct Settings {
2525
auto_update: bool,
2626
liqi_version: String,
2727
github_token: String,
28+
#[serde(default)]
29+
req_proxy: Option<url::Url>,
2830
#[serde(skip)]
2931
methods_set: HashSet<String>,
3032
#[serde(skip)]
@@ -46,6 +48,18 @@ static REQUEST_CLIENT: LazyLock<reqwest::Client> = LazyLock::new(|| {
4648
});
4749

4850
impl Settings {
51+
fn create_github_client(&self) -> Result<reqwest::Client> {
52+
let mut builder = reqwest::Client::builder().user_agent(APP_USER_AGENT);
53+
54+
if let Some(req_proxy) = &self.req_proxy {
55+
let proxy = reqwest::Proxy::all(req_proxy.clone())
56+
.context("Failed to create proxy from req_proxy")?;
57+
builder = builder.proxy(proxy);
58+
}
59+
60+
builder.build().context("Failed to build HTTP client")
61+
}
62+
4963
pub fn new(arg_dir: &Path) -> Result<Self> {
5064
let exe = std::env::current_exe().context("无法获取当前可执行文件路径")?;
5165
let dir = if arg_dir.is_dir() {
@@ -113,14 +127,16 @@ impl Settings {
113127
);
114128

115129
let resp = if self.github_token.is_empty() {
116-
REQUEST_CLIENT
130+
let client = self.create_github_client()?;
131+
client
117132
.get("https://api.github.com/repos/Xerxes-2/AutoLiqi/releases/latest")
118133
.timeout(std::time::Duration::from_secs(10))
119134
.send()
120135
.await
121136
.context("Failed to get latest release")?
122137
} else {
123-
REQUEST_CLIENT
138+
let client = self.create_github_client()?;
139+
client
124140
.get("https://api.github.com/repos/Xerxes-2/AutoLiqi/releases/latest")
125141
.header("Authorization", format!("Bearer {}", self.github_token))
126142
.header("X-GitHub-Api-Version", "2022-11-28")
@@ -167,14 +183,16 @@ impl Settings {
167183
.as_str()
168184
.context("No download url found in asset")?;
169185
let resp = if self.github_token.is_empty() {
170-
REQUEST_CLIENT
186+
let client = self.create_github_client()?;
187+
client
171188
.get(url)
172189
.timeout(std::time::Duration::from_secs(10))
173190
.send()
174191
.await
175192
.context("Failed to download asset")?
176193
} else {
177-
REQUEST_CLIENT
194+
let client = self.create_github_client()?;
195+
client
178196
.get(url)
179197
.header("Authorization", format!("Bearer {}", self.github_token))
180198
.header("X-GitHub-Api-Version", "2022-11-28")

0 commit comments

Comments
 (0)