Skip to content

Commit 8619413

Browse files
Merge pull request #2 from modrinth/update-analytics
Update analytics
2 parents 51844bc + 1520d7e commit 8619413

File tree

4 files changed

+23
-19
lines changed

4 files changed

+23
-19
lines changed

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ pub async fn main(
4949
.options("/*route", |_req, _ctx| {
5050
Response::ok("")?.with_cors(&CORS_POLICY)
5151
})
52-
.get("/teapot", routes::teapot::teapot)
5352
.head_async("/*file", |_req, ctx| async move {
5453
let cdn = ctx.env.var(CDN_BACKEND_URL)?.to_string();
5554
let url = make_cdn_url(&cdn, get_param(&ctx, "file"))?.to_string();

src/routes/download.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::utils::*;
22
use chrono::Duration;
33
use serde::Serialize;
4+
use std::collections::HashMap;
45
use std::{net::IpAddr, path::Path};
56
use worker::wasm_bindgen::JsValue;
67
use worker::*;
@@ -52,21 +53,18 @@ pub fn handle_download(
5253
}
5354

5455
/// Tries to count a download, provided the IP address is discernable and the limit hasn't already been reachedy
55-
async fn count_download(
56-
req: &Request,
57-
ctx: &RouteContext<()>,
58-
) -> Result<()> {
59-
if let Some(ip) = req.headers().get(CF_IP_HEADER)? {
56+
async fn count_download(req: &Request, ctx: &RouteContext<()>) -> Result<()> {
57+
if let Some(raw_ip) = req.headers().get(CF_IP_HEADER)? {
6058
let (project, file) = (get_param(ctx, "hash"), get_param(ctx, "file"));
6159

6260
if !is_counted(file) {
6361
console_debug!("[DEBUG]: Not counting {file} due to extension");
6462
return Ok(());
6563
}
66-
console_debug!("[DEBUG]: Attempting to count download from IP {ip} in project {project}");
64+
console_debug!("[DEBUG]: Attempting to count download from IP {raw_ip} in project {project}");
6765

6866
let ip = u64::from_le_bytes(
69-
match ip.parse::<IpAddr>().map_err(|err| err.to_string())? {
67+
match raw_ip.parse::<IpAddr>().map_err(|err| err.to_string())? {
7068
IpAddr::V4(it) => {
7169
[it.octets(), [0u8; 4]].concat().try_into().unwrap()
7270
}
@@ -135,10 +133,16 @@ async fn count_download(
135133
if (downloader_downloads as i64) < max_downloads {
136134
let labrinth_url = ctx.var(LABRINTH_URL)?.to_string();
137135
let labrinth_secret = ctx.secret(LABRINTH_SECRET)?.to_string();
138-
let rate_limit_key_secret = ctx.secret(RATE_LIMIT_IGNORE_KEY)?.to_string();
136+
let rate_limit_key_secret =
137+
ctx.secret(RATE_LIMIT_IGNORE_KEY)?.to_string();
139138
let hash = get_param(ctx, "hash").to_owned();
140139
let version_name = get_param(ctx, "version").to_owned();
141140
let og_url = req.url()?.to_string();
141+
let ip = raw_ip.clone();
142+
let headers = req
143+
.headers()
144+
.into_iter()
145+
.collect::<HashMap<String, String>>();
142146

143147
wasm_bindgen_futures::spawn_local(async move {
144148
match request_download_count(
@@ -148,6 +152,8 @@ async fn count_download(
148152
&hash,
149153
&version_name,
150154
og_url,
155+
ip,
156+
headers,
151157
)
152158
.await
153159
{
@@ -180,8 +186,11 @@ async fn count_download(
180186
#[derive(Serialize)]
181187
struct DownloadRequest {
182188
pub url: String,
183-
pub hash: String,
189+
pub project_id: String,
184190
pub version_name: String,
191+
192+
pub ip: String,
193+
pub headers: HashMap<String, String>,
185194
}
186195

187196
async fn request_download_count(
@@ -191,6 +200,8 @@ async fn request_download_count(
191200
hash: &str,
192201
version_name: &str,
193202
req_url: String,
203+
ip: String,
204+
req_headers: HashMap<String, String>,
194205
) -> Result<Response> {
195206
let url = format!(
196207
"{url}/v2/admin/_count-download",
@@ -214,8 +225,10 @@ async fn request_download_count(
214225
body: Some(JsValue::from_str(&serde_json::to_string(
215226
&DownloadRequest {
216227
url: req_url.to_string(),
217-
hash: hash.to_string(),
228+
project_id: hash.to_string(),
218229
version_name: version_name.to_string(),
230+
ip,
231+
headers: req_headers,
219232
},
220233
)?)),
221234
..Default::default()

src/routes/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
pub mod download;
2-
pub mod teapot;

src/routes/teapot.rs

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)