Modpack downloads honour NO_PROXY too
The jars an import fetches went through the proxy whatever the host, so a host that only answers directly failed there while the launcher's own requests to it went direct. net::agent now takes the URL and applies the same NO_PROXY and loopback rules the launcher's own traffic gets. Co-Authored-By: WorkBuddy AI (DeepSeek-V4.1-Flash) <noreply@workbuddy.cn>
This commit is contained in:
@@ -143,8 +143,9 @@ pub fn fetch(
|
||||
if let Some((dest, url)) = crate::worldgen::loader_jar(work, &pack.loader, &pack.loader_version)
|
||||
.filter(|(dest, _)| !dest.exists())
|
||||
{
|
||||
let sha1 = crate::net::agent()
|
||||
.get(&format!("{url}.sha1"))
|
||||
let checksum = format!("{url}.sha1");
|
||||
let sha1 = crate::net::agent(&checksum)
|
||||
.get(&checksum)
|
||||
.set("User-Agent", modrinth::AGENT)
|
||||
.call()
|
||||
.ok()
|
||||
@@ -202,7 +203,7 @@ pub fn url_of(file: &PackFile, cache: &Cache) -> Option<String> {
|
||||
|
||||
/// Downloads `url` to `dest`, kept only if its SHA-1 is `sha1`.
|
||||
fn download(url: &str, sha1: Option<&str>, dest: &Path) -> Result<(), String> {
|
||||
let response = crate::net::agent()
|
||||
let response = crate::net::agent(url)
|
||||
.get(url)
|
||||
.set("User-Agent", modrinth::AGENT)
|
||||
.call()
|
||||
|
||||
@@ -79,7 +79,7 @@ impl Cache {
|
||||
}
|
||||
|
||||
fn post(url: &str, body: &Value) -> Result<Value, String> {
|
||||
let response = crate::net::agent()
|
||||
let response = crate::net::agent(url)
|
||||
.post(url)
|
||||
.set("User-Agent", AGENT)
|
||||
.set("Content-Type", "application/json")
|
||||
@@ -89,7 +89,7 @@ fn post(url: &str, body: &Value) -> Result<Value, String> {
|
||||
}
|
||||
|
||||
fn get(url: &str) -> Result<Value, String> {
|
||||
let response = crate::net::agent()
|
||||
let response = crate::net::agent(url)
|
||||
.get(url)
|
||||
.set("User-Agent", AGENT)
|
||||
.call()
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
//! The agents port-pack's downloads go through, since the jars of an imported modpack come from
|
||||
//! the same places as everything else and may need the same proxy.
|
||||
//! The agents port-pack's downloads go through: the jars of an imported modpack come from the same
|
||||
//! places as everything else, so they take the same proxy — and leave the same hosts alone.
|
||||
//!
|
||||
//! The launcher hands its own setting over (`set_proxy`, from `proxy.rs`); a run of the
|
||||
//! `horizon-port-pack` command reads `HTTPS_PROXY`, `HTTP_PROXY` and `ALL_PROXY` instead. A
|
||||
//! `NO_PROXY` list is the launcher's business: it decides per address, and nothing here is local.
|
||||
//! `horizon-port-pack` command reads `HTTPS_PROXY`, `HTTP_PROXY` and `ALL_PROXY` instead. Either
|
||||
//! way a host in `NO_PROXY`, or on this computer, is reached directly: a host that only answers
|
||||
//! without the proxy would otherwise fail here while the launcher's own downloads worked.
|
||||
//!
|
||||
//! The rules are `launcher/src/proxy.rs`'s, which the launcher applies to its own traffic. The two
|
||||
//! crates share no code (the launcher depends on this one, not the other way round), so they are
|
||||
//! kept in step by hand.
|
||||
use std::sync::Mutex;
|
||||
|
||||
/// What the launcher set, when it did.
|
||||
@@ -39,13 +44,79 @@ fn spec() -> Option<String> {
|
||||
})
|
||||
}
|
||||
|
||||
/// An agent for a download, through the proxy when one is set.
|
||||
pub fn agent() -> ureq::Agent {
|
||||
let mut builder = ureq::AgentBuilder::new();
|
||||
if let Some(proxy) = spec().and_then(|s| ureq::Proxy::new(s).ok()) {
|
||||
builder = builder.proxy(proxy);
|
||||
/// An agent for downloading `url`: through the proxy when one is set, unless `url` is a host that
|
||||
/// must be reached directly.
|
||||
pub fn agent(url: &str) -> ureq::Agent {
|
||||
let builder = ureq::AgentBuilder::new();
|
||||
if direct(url, &no_proxy()) {
|
||||
return builder.build();
|
||||
}
|
||||
builder.build()
|
||||
match spec().and_then(|s| ureq::Proxy::new(s).ok()) {
|
||||
Some(proxy) => builder.proxy(proxy).build(),
|
||||
None => builder.build(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether `url` is reached without the proxy, given a `NO_PROXY` list: a local address, or a host
|
||||
/// the list names.
|
||||
fn direct(url: &str, no_proxy: &str) -> bool {
|
||||
host_of(url).is_some_and(|host| local(host) || excluded(host, no_proxy))
|
||||
}
|
||||
|
||||
/// `NO_PROXY`'s list: the hosts reached directly whatever the proxy is.
|
||||
fn no_proxy() -> String {
|
||||
["NO_PROXY", "no_proxy"]
|
||||
.iter()
|
||||
.find_map(|name| std::env::var(name).ok())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// The host part of a URL (`https://user@host:port/path` is `host`).
|
||||
fn host_of(url: &str) -> Option<&str> {
|
||||
let rest = url.split_once("://").map_or(url, |(_, rest)| rest);
|
||||
let rest = rest.split(['/', '?', '#']).next()?;
|
||||
let rest = rest.rsplit_once('@').map_or(rest, |(_, host)| host);
|
||||
Some(match rest.rfind(':') {
|
||||
// A port follows the last colon; an IPv6 literal keeps the colons inside its brackets.
|
||||
Some(at) if !rest[at..].contains(']') => &rest[..at],
|
||||
_ => rest,
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether `host` is this computer.
|
||||
fn local(host: &str) -> bool {
|
||||
let host = bare(host);
|
||||
host == "localhost"
|
||||
|| host.ends_with(".localhost")
|
||||
|| host == "::1"
|
||||
|| host == "0.0.0.0"
|
||||
|| host.starts_with("127.")
|
||||
}
|
||||
|
||||
/// Whether `host` is in `NO_PROXY`: `*`, `host`, `.domain`, `*.domain` and `host:port` entries.
|
||||
fn excluded(host: &str, no_proxy: &str) -> bool {
|
||||
let host = bare(host);
|
||||
no_proxy
|
||||
.split(',')
|
||||
.map(str::trim)
|
||||
.filter(|e| !e.is_empty())
|
||||
.any(|entry| {
|
||||
if entry == "*" {
|
||||
return true;
|
||||
}
|
||||
let entry = entry.trim_start_matches('*').trim_start_matches('.');
|
||||
let entry = match entry.rfind(':') {
|
||||
Some(at) if !entry[at..].contains(']') => &entry[..at],
|
||||
_ => entry,
|
||||
};
|
||||
let entry = bare(entry);
|
||||
!entry.is_empty() && (host == entry || host.ends_with(&format!(".{entry}")))
|
||||
})
|
||||
}
|
||||
|
||||
/// A host without the brackets an IPv6 literal wears, lower case.
|
||||
fn bare(host: &str) -> String {
|
||||
host.trim().trim_matches(['[', ']']).to_ascii_lowercase()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -62,4 +133,52 @@ mod tests {
|
||||
set_proxy(Some(" ".into()));
|
||||
assert!(PROXY.lock().unwrap().is_none());
|
||||
}
|
||||
|
||||
/// A host that must be reached directly takes no proxy, however one is set: what an import
|
||||
/// with `NO_PROXY=modrinth.com` needs, since the launcher's own requests to it go direct.
|
||||
#[test]
|
||||
fn a_listed_host_is_reached_directly() {
|
||||
let list = "modrinth.com";
|
||||
assert!(direct("https://cdn.modrinth.com/data/a.jar", list));
|
||||
assert!(direct("https://api.modrinth.com/v2/version", list));
|
||||
assert!(!direct("https://edge.forgecdn.net/files/1.jar", list));
|
||||
assert!(!direct("https://example.com/a.jar", list));
|
||||
assert!(direct("http://127.0.0.1:8130/api/v1/status", ""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_host_is_read_out_of_the_address() {
|
||||
assert_eq!(
|
||||
host_of("https://cdn.modrinth.com/v2/x.jar"),
|
||||
Some("cdn.modrinth.com")
|
||||
);
|
||||
assert_eq!(
|
||||
host_of("https://user:pass@host.example:8443/a"),
|
||||
Some("host.example")
|
||||
);
|
||||
assert_eq!(host_of("https://[::1]:25565/"), Some("[::1]"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn this_computer_is_never_proxied() {
|
||||
for host in ["localhost", "[::1]", "127.0.0.1", "127.1.2.3"] {
|
||||
assert!(local(host), "{host}");
|
||||
}
|
||||
for host in ["cdn.modrinth.com", "10.0.0.1"] {
|
||||
assert!(!local(host), "{host}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_proxy_lists_hosts_and_their_subdomains() {
|
||||
assert!(excluded("anything.at.all", "*"));
|
||||
assert!(excluded("cdn.modrinth.com", "modrinth.com"));
|
||||
assert!(excluded(
|
||||
"wiki.corp.example",
|
||||
" .corp.example , other.host "
|
||||
));
|
||||
assert!(excluded("github.com", "github.com:443"));
|
||||
assert!(!excluded("notmodrinth.com", "modrinth.com"));
|
||||
assert!(!excluded("modrinth.com", ""));
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -98,7 +98,8 @@ page `servers.json` (the list), `servers/<name>/` (the servers created there) an
|
||||
sign-in, and the jars a modpack import fetches. It is kept as `proxy` in `settings.json`, and
|
||||
`horizon-launcher settings proxy http://127.0.0.1:7890` sets it too ("default" removes it).
|
||||
Without one, `HTTPS_PROXY` and `HTTP_PROXY` are used when the launcher is started with them, and
|
||||
`NO_PROXY` names the hosts to reach directly. A server on this computer never goes through it.
|
||||
`NO_PROXY` names the hosts to reach directly, a modpack import's jars included. A server on this
|
||||
computer never goes through it.
|
||||
- **Links**: the Discord invite, the Sponsors page and the website come from `launcher/links.json` on main
|
||||
(`{"discord": "...", "sponsor": "...", "website": "..."}`), so they change without a launcher release. Offline,
|
||||
the launcher uses the same values built in; an empty address hides its button.
|
||||
|
||||
@@ -8,6 +8,10 @@
|
||||
//! Tailscale's API, and the jars a modpack import downloads. A local address never does, and the
|
||||
//! Servers page's calls to a game server's API (`servers.rs`) build their agent without it: they
|
||||
//! are on loopback and asked every second.
|
||||
//!
|
||||
//! The jars an import downloads are fetched by `horizon-port-pack`, which builds its own agents and
|
||||
//! keeps these same `NO_PROXY` rules (`crates/horizon-port-pack/src/net.rs`). The two crates share
|
||||
//! no code, so a change to them here belongs there too.
|
||||
use crate::backend::Settings;
|
||||
|
||||
/// The proxy to reach `url` through: the one set for the launcher or in the environment, unless
|
||||
@@ -55,8 +59,9 @@ pub fn refuse(spec: &str) -> Option<String> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Tells the parts that build their own agents (`horizon-port-pack`) what to use. Called at start
|
||||
/// and whenever the player changes the setting.
|
||||
/// Tells the parts that build their own agents (`horizon-port-pack`) which proxy to use. Called at
|
||||
/// start and whenever the player changes the setting; the hosts `NO_PROXY` names stay direct there
|
||||
/// too, since it reads the same list.
|
||||
pub fn announce() {
|
||||
horizon_port_pack::net::set_proxy(written("https"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user