Skip to content

src/pm: use the first reachable URL for mirrors #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/pm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ const STATE_DIR: &str = "/var/lib/atm/";
const DPKG_STATE: &str = "/var/lib/dpkg/status";
const APT_GEN_LIST_STATUS: &str = "/var/lib/apt/gen/status.json";
const DEFAULT_REPO_URL: &str = "https://repo.aosc.io";
static APP_USER_AGENT: &str = concat!(
env!("CARGO_PKG_NAME"),
"/",
env!("CARGO_PKG_VERSION"),
);

#[derive(Deserialize, Debug)]
struct AptGenListStatus {
Expand All @@ -35,13 +40,25 @@ struct Mirror {
pub fn get_mirror_url() -> Result<String> {
let status_data = fs::read(APT_GEN_LIST_STATUS)?;
let status_data: AptGenListStatus = serde_json::from_slice(&status_data)?;
if let Some((_, url)) = status_data.mirror.first() {

for (_, url) in status_data.mirror {
let url = if url.ends_with('/') {
url.clone()
} else {
format!("{}/", url)
};
return Ok(url);

if reqwest::blocking::Client::builder()
.timeout(std::time::Duration::from_secs(30))
.user_agent(APP_USER_AGENT)
.build()?
.get(&url)
.send()?
.error_for_status()
.is_ok()
{
return Ok(url);
}
}

Ok(DEFAULT_REPO_URL.to_string())
Expand Down