1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
| // SPDX-FileCopyrightText: 2026 Nikolay Govorov <me@govorov.online>
// SPDX-License-Identifier: AGPL-3.0-or-later
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use axum::body::Body;
use axum::http::{HeaderValue, Method, Request, Response, StatusCode, header};
use axum::{extract, routing};
use chrono::{DateTime, Utc};
use maud::{Markup, html};
use rust_embed::Embed;
use sqlx::types::chrono;
use tower::{Layer, Service};
use tracing::error;
use crate::backends::{GoBackend, ZigBackend};
#[derive(Embed)]
#[folder = "src/assets/"]
struct Assets;
const CSP: &str = "default-src 'self'; base-uri 'none'; img-src 'self'; font-src 'self'; style-src 'self'; script-src 'self'; object-src 'none'; frame-ancestors 'none'";
/// Handles html pages rendering and static files
pub struct WebController {
zig: Option<Arc<ZigBackend>>,
go: Option<Arc<GoBackend>>,
}
impl WebController {
pub fn new(zig: Option<Arc<ZigBackend>>, go: Option<Arc<GoBackend>>) -> Self {
Self { zig, go }
}
}
impl WebController {
pub fn router(self: Arc<Self>) -> axum::Router {
let pages = axum::Router::new()
.route("/", axum::routing::get(Self::index))
.layer(tower_http::set_header::SetResponseHeaderLayer::overriding(
header::CACHE_CONTROL,
HeaderValue::from_static("no-cache"),
))
.with_state(self.clone());
let assets = axum::Router::new()
.route("/assets/{*path}", routing::get(Self::assets))
.layer(LastModifiedLayer {});
axum::Router::new().merge(pages).merge(assets).layer(
tower_http::set_header::SetResponseHeaderLayer::overriding(
header::CONTENT_SECURITY_POLICY,
HeaderValue::from_static(CSP),
),
)
}
async fn assets(extract::Path(path): extract::Path<String>) -> Response<Body> {
match Assets::get(&path) {
Some(file) => {
let mime = mime_guess::from_path(&path).first_or_octet_stream();
let is_font = matches!(
path.rsplit('.').next(),
Some("ttf" | "otf" | "woff" | "woff2" | "eot")
);
let mut builder = Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, mime.as_ref());
if is_font {
builder = builder
.header(header::CACHE_CONTROL, "public, max-age=31536000, immutable");
}
builder.body(Body::from(file.data.into_owned())).unwrap()
}
None => Response::builder()
.status(StatusCode::NOT_FOUND)
.body(Body::empty())
.unwrap(),
}
}
async fn index(extract::State(ctrl): extract::State<Arc<Self>>) -> Markup {
let zig_versions = if let Some(ref backend) = ctrl.zig {
match backend.get_releases().await {
Ok(v) => v,
Err(e) => {
error!("failed to get zig versions: {e}");
Vec::new()
}
}
} else {
Vec::new()
};
let go_versions = if let Some(ref backend) = ctrl.go {
match backend.get_releases().await {
Ok(v) => v,
Err(e) => {
error!("failed to get go versions: {e}");
Vec::new()
}
}
} else {
Vec::new()
};
html! {
(maud::DOCTYPE)
html lang="en" {
meta charset="UTF-8";
meta name="viewport" content="width=device-width,initial-scale=1";
meta http-equiv="X-UA-Compatible" content="ie=edge";
link rel="stylesheet" href="assets/base.css";
title { "Earth PKG — tiny & opinionated packages mirror" }
}
body {
h1 { "Zorian — tiny & opinionated packages mirror." }
main {
p {
r#"This site provides a caching proxy for downloading Zig and Go installation files.
It reduces load on upstream servers and makes your infrastructure more reliable by adding redundancy."#
}
p {
"Zorian is open source software licensed under " a href="https://www.gnu.org/licenses/agpl-3.0.html" { "AGPL-3.0" } ". "
"Source code is available on " a href="https://github.com/mrdimidium/zorian" { "GitHub" }
}
h2 { "Usage" }
p {
"Replace official download URLs with " code { "https://pkg.earth/{tool}/{filename}" } ". "
"Files are cached automatically after the first download."
}
h3 id="zig" {
a href="#zig" { "Zig" }
}
@if !zig_versions.is_empty() {
details {
summary { "Available versions (" (zig_versions.len()) ")" }
p { "You can take actual minisig public key at " a href="https://ziglang.org/download/" { "ziglang.org/download" } "." }
table {
thead {
tr {
th { "Version" }
th { "Date" }
th { "Docs" }
th { "Targets" }
}
}
tbody {
@for v in zig_versions.iter().rev() {
tr {
td { (v.version) }
td { (v.meta.date.as_deref().unwrap_or("-")) }
td {
@if let Some(ref url) = v.meta.docs {
a href=(url) { "docs" }
}
" "
@if let Some(ref url) = v.meta.std_docs {
a href=(url) { "std" }
}
" "
@if let Some(ref url) = v.meta.notes {
a href=(url) { "notes" }
}
}
td {
@for file in &v.files {
a href=(format!("/zig/{}", file.filename)) { (&file.meta.target) }
" "
}
}
}
}
}
}
}
}
p {
"Read more about community mirrors in the " a href="https://ziglang.org/download/community-mirrors/" { "blog post" } ". "
"Information on how to deploy your own mirror is available " a href="https://github.com/ziglang/www.ziglang.org/blob/main/MIRRORS.md" { "in the documentation" } "."
}
p {
"For simplicity, you can use tools like " a href="https://github.com/prantlf/zigup" { "prantlf/zigup" }
" and " a href="https://github.com/mlugg/setup-zig" { "mlugg/setup-zig" } "."
}
p {
"To install manually:"
ol {
li { "download zig dist file:" br; code { "wget https://pkg.earth/zig/zig-x86_64-linux-0.15.1.tar.xz" } ";" }
li { "download zig minisig file:" br; code { "wget https://pkg.earth/zig/zig-x86_64-linux-0.15.1.tar.xz.minisig" } ";" }
li { "check archive integrity:" br; code { "minisign -Vm zig-x86_64-linux-0.15.1.tar.xz -P RWSGOq2NVecA2UPNdBUZykf1CCb147pkmdtYxgb3Ti+JO/wCYvhbAb/U" } ";" }
li { "unpack archive:" br; code { "tar -xf 'zig-x86_64-linux-0.15.1.tar.xz'" } ";" }
li { "check installed zig:" br; code { "./zig-x86_64-linux-0.15.1/zig --version" } ";" }
}
}
h3 id="go" { a href="#go" { "Go" } }
@if !go_versions.is_empty() {
details {
summary { "Available versions (" (go_versions.len()) ")" }
p { "You can find available versions at " a href="https://go.dev/dl/" { "go.dev/dl" } "." }
table {
thead {
tr {
th { "Version" }
th { "Stable" }
th { "Files" }
}
}
tbody {
@for v in go_versions.iter().rev() {
tr {
td { (v.version) }
td { @if v.meta.stable { "✓" } @else { "" } }
td {
@for file in &v.files {
a href=(format!("/go/{}", file.filename)) { (file.filename) }
" "
}
}
}
}
}
}
}
}
p { "To install manually:" }
ol {
li { "download go dist file:" br; code { "wget https://pkg.earth/go/go1.23.0.linux-amd64.tar.gz" } ";" }
li { "download go sha256 file:" br; code { "wget https://pkg.earth/go/go1.23.0.linux-amd64.tar.gz.sha256" } ";" }
li { "check archive integrity:" br; code { "sha256sum -c go1.23.0.linux-amd64.tar.gz.sha256" } ";" }
li { "unpack archive:" br; code { "tar -xzf go1.23.0.linux-amd64.tar.gz" } ";" }
li { "check installed go:" br; code { "./go/bin/go version" } ";" }
}
h2 { "Privacy policy" }
p { "This mirror is a non-profit project available on a voluntary basis. The author has no plans to fund it." }
p { r#"Since the mirror is hosted on hardware, we collect access logs to combat bots and brute-force attacks.
The logs are used for security purposes and load planning, are not shared with third parties,
and are deleted after 30 days."# }
p { "Third-party analytics systems are not used, same as client-side trackers." }
}
}
}
}
}
#[derive(Clone)]
pub struct LastModifiedLayer {}
impl<S> Layer<S> for LastModifiedLayer {
type Service = LastModifiedService<S>;
fn layer(&self, inner: S) -> Self::Service {
LastModifiedService {
inner,
// Since the static file is packaged in a binary, we cache it across restarts.
// In the future, we can use the build time.
last_moidified: Utc::now(),
}
}
}
#[derive(Clone)]
pub struct LastModifiedService<S> {
inner: S,
last_moidified: DateTime<Utc>,
}
impl<S> Service<Request<Body>> for LastModifiedService<S>
where
S: Service<Request<Body>, Response = Response<Body>> + Clone + Send + 'static,
S::Future: Send + 'static,
S::Error: Send + 'static,
{
type Response = Response<Body>;
type Error = S::Error;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
}
fn call(&mut self, req: Request<Body>) -> Self::Future {
let method = req.method().clone();
let headers = req.headers().clone();
let last_modified = self.last_moidified;
let mut inner = self.inner.clone();
Box::pin(async move {
let raw = last_modified
.format("%a, %d %b %Y %H:%M:%S GMT")
.to_string();
let lmh = HeaderValue::from_str(raw.as_str()).unwrap();
if (method == Method::GET || method == Method::HEAD)
&& headers
.get(header::IF_MODIFIED_SINCE)
.and_then(|v| v.to_str().ok())
.and_then(parse_http_date)
.is_some_and(|ims| last_modified <= ims)
{
match Response::builder()
.status(StatusCode::NOT_MODIFIED)
.header(header::LAST_MODIFIED, lmh.clone())
.body(Body::empty())
{
Ok(resp) => {
return Ok(resp);
}
Err(err) => {
error!("build response: {err}")
}
}
}
let mut resp = inner.call(req).await?;
resp.headers_mut().insert(header::LAST_MODIFIED, lmh);
Ok(resp)
})
}
}
/// Parse HTTP-date (IMF-fixdate per RFC 7231) or RFC 2822
fn parse_http_date(value: &str) -> Option<DateTime<Utc>> {
// IMF-fixdate: "Tue, 14 Jan 2026 12:34:56 GMT"
DateTime::parse_from_rfc2822(value.trim())
.map(|d| d.with_timezone(&Utc))
.ok()
}
|