Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

chore: refactor modelhub (#7468)

hiento095e138c67af1b
diff --git a/src-tauri/src/core/downloads/helpers.rs b/src-tauri/src/core/downloads/helpers.rs
index 799fd8a45..08f8f2bad 100644
--- a/src-tauri/src/core/downloads/helpers.rs
+++ b/src-tauri/src/core/downloads/helpers.rs
@@ -1,5 +1,7 @@
use super::models::{DownloadEvent, DownloadItem, ProgressTracker, ProxyConfig};
use crate::core::app::commands::get_jan_data_folder_path;
+use crate::core::updater::session::get_session_id;
+use crate::core::updater::hmac_client::SignedRequestHeaders;
use futures_util::StreamExt;
use jan_utils::normalize_path;
use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
@@ -12,12 +14,75 @@ use tokio::io::AsyncWriteExt;
use tokio_util::sync::CancellationToken;
use url::Url;
+// ===== CONSTANTS =====
+
+/// Jan mirror prefix for HuggingFace downloads
+/// - Stable builds: https://apps.jan.ai/
+/// - Nightly builds: https://apps-nightly.jan.ai/
+const JAN_MIRROR_PREFIX_STABLE: &str = "https://apps.jan.ai/";
+const JAN_MIRROR_PREFIX_NIGHTLY: &str = "https://apps-nightly.jan.ai/";
+
+/// Domains that should use mirror download with fallback
+const MIRROR_DOMAINS: &[&str] = &["huggingface.co"];
+
+/// Check if this is a nightly build based on package name
+fn is_nightly_build() -> bool {
+ let pkg_name = env!("CARGO_PKG_NAME");
+ pkg_name.to_lowercase().contains("nightly")
+}
+
+/// Get the appropriate mirror prefix based on build type
+fn get_mirror_prefix() -> &'static str {
+ if is_nightly_build() {
+ JAN_MIRROR_PREFIX_NIGHTLY
+ } else {
+ JAN_MIRROR_PREFIX_STABLE
+ }
+}
+
+/// Secret key for HMAC request authentication
+/// - In CI: Set JAN_SIGNING_KEY environment variable at build time
+/// - In local dev: Falls back to a test key
+const SECRET_KEY: &str = match option_env!("JAN_SIGNING_KEY") {
+ Some(key) => key,
+ None => "local-dev-test-key-not-for-production",
+};
+
// ===== UTILITY FUNCTIONS =====
pub fn err_to_string<E: std::fmt::Display>(e: E) -> String {
format!("Error: {e}")
}
+/// Converts a URL to Jan mirror URL if applicable
+/// e.g., https://huggingface.co/... -> https://apps.jan.ai/huggingface.co/...
+/// or for nightly: https://huggingface.co/... -> https://apps-nightly.jan.ai/huggingface.co/...
+pub fn convert_to_mirror_url(url: &str) -> Option<String> {
+ let parsed = Url::parse(url).ok()?;
+ let host = parsed.host_str()?;
+
+ // Check if the domain should use mirror
+ if MIRROR_DOMAINS.iter().any(|domain| host == *domain || host.ends_with(&format!(".{}", domain))) {
+ // Remove the scheme (https://) and prepend mirror prefix
+ let url_without_scheme = url
+ .strip_prefix("https://")
+ .or_else(|| url.strip_prefix("http://"))?;
+ Some(format!("{}{}", get_mirror_prefix(), url_without_scheme))
+ } else {
+ None
+ }
+}
+
+/// Get session identifier for request signing
+fn get_download_nonce_seed() -> String {
+ get_session_id()
+}
+
+/// Get current app version from Cargo.toml
+fn get_app_version() -> &'static str {
+ env!("CARGO_PKG_VERSION")
+}
+
// ===== VALIDATION FUNCTIONS =====
/// Validates a downloaded file against expected hash and size
@@ -519,7 +584,7 @@ async fn download_single_file(
let mut download_delta = 0u64;
let mut initial_progress = 0u64;
- let resp = if should_resume {
+ let (resp, actual_url) = if should_resume {
let downloaded_size = tmp_save_path.metadata().map_err(err_to_string)?.len();
match _get_maybe_resume(&client, &item.url, downloaded_size).await {
Ok(resp) => {
@@ -544,18 +609,25 @@ async fn download_single_file(
};
app.emit(&evt_name, evt).unwrap();
- resp
+ (resp, item.url.clone())
}
Err(e) => {
- // fallback to normal download
+ // fallback to normal download with proxy support
log::warn!("Failed to resume download: {e}");
should_resume = false;
- _get_maybe_resume(&client, &item.url, 0).await?
+ _get_maybe_resume_with_fallback(&client, &item.url, 0).await?
}
}
} else {
- _get_maybe_resume(&client, &item.url, 0).await?
+ // Use mirror fallback for new downloads
+ _get_maybe_resume_with_fallback(&client, &item.url, 0).await?
};
+
+ // Log which URL is being used for download
+ if actual_url != item.url {
+ log::info!("Downloading via Jan mirror: {}", actual_url);
+ }
+
let mut stream = resp.bytes_stream();
let file = if should_resume {
@@ -643,6 +715,112 @@ async fn download_single_file(
// ===== HTTP CLIENT HELPER FUNCTIONS =====
+/// Attempts to download from mirror URL first, falls back to original URL if mirror fails
+/// When using mirror URL, adds HMAC headers for request authentication
+pub async fn _get_maybe_resume_with_fallback(
+ client: &reqwest::Client,
+ url: &str,
+ start_bytes: u64,
+) -> Result<(reqwest::Response, String), String> {
+ // Try mirror URL first if applicable
+ if let Some(mirror_url) = convert_to_mirror_url(url) {
+ log::info!("Attempting download from Jan mirror: {}", mirror_url);
+ match _get_maybe_resume_with_hmac(client, &mirror_url, start_bytes).await {
+ Ok(resp) => {
+ log::info!("Successfully connected to Jan mirror");
+ return Ok((resp, mirror_url));
+ }
+ Err(e) => {
+ log::warn!("Jan mirror download failed: {}. Falling back to original URL...", e);
+ }
+ }
+ }
+
+ // Fallback to original URL (no HMAC headers needed)
+ log::info!("Downloading from original URL: {}", url);
+ let resp = _get_maybe_resume_internal(client, url, start_bytes).await?;
+ Ok((resp, url.to_string()))
+}
+
+/// Download from URL with HMAC headers for Jan mirror authentication
+async fn _get_maybe_resume_with_hmac(
+ client: &reqwest::Client,
+ url: &str,
+ start_bytes: u64,
+) -> Result<reqwest::Response, String> {
+ // Generate HMAC headers for request authentication
+ let nonce_seed = get_download_nonce_seed();
+ let app_version = get_app_version();
+ let signed_headers = SignedRequestHeaders::new(SECRET_KEY, &nonce_seed, app_version);
+
+ let mut request = if start_bytes > 0 {
+ client
+ .get(url)
+ .header("Range", format!("bytes={start_bytes}-"))
+ } else {
+ client.get(url)
+ };
+
+ // Add HMAC headers
+ for (key, value) in signed_headers.to_header_pairs() {
+ request = request.header(key, value);
+ }
+
+ let resp = request.send().await.map_err(err_to_string)?;
+
+ if start_bytes > 0 {
+ if resp.status() != reqwest::StatusCode::PARTIAL_CONTENT {
+ return Err(format!(
+ "Failed to resume download: HTTP status {}, {}",
+ resp.status(),
+ resp.text().await.unwrap_or_default()
+ ));
+ }
+ } else if !resp.status().is_success() {
+ return Err(format!(
+ "Failed to download: HTTP status {}, {}",
+ resp.status(),
+ resp.text().await.unwrap_or_default()
+ ));
+ }
+
+ Ok(resp)
+}
+
+/// Internal function to attempt download from a single URL (without HMAC)
+async fn _get_maybe_resume_internal(
+ client: &reqwest::Client,
+ url: &str,
+ start_bytes: u64,
+) -> Result<reqwest::Response, String> {
+ if start_bytes > 0 {
+ let resp = client
+ .get(url)
+ .header("Range", format!("bytes={start_bytes}-"))
+ .send()
+ .await
+ .map_err(err_to_string)?;
+ if resp.status() != reqwest::StatusCode::PARTIAL_CONTENT {
+ return Err(format!(
+ "Failed to resume download: HTTP status {}, {}",
+ resp.status(),
+ resp.text().await.unwrap_or_default()
+ ));
+ }
+ Ok(resp)
+ } else {
+ let resp = client.get(url).send().await.map_err(err_to_string)?;
+ if !resp.status().is_success() {
+ return Err(format!(
+ "Failed to download: HTTP status {}, {}",
+ resp.status(),
+ resp.text().await.unwrap_or_default()
+ ));
+ }
+ Ok(resp)
+ }
+}
+
pub async fn _get_maybe_resume(
client: &reqwest::Client,
url: &str,
diff --git a/src-tauri/src/core/updater/mod.rs b/src-tauri/src/core/updater/mod.rs
index 0d5453ddd..ca23b0d79 100644
--- a/src-tauri/src/core/updater/mod.rs
+++ b/src-tauri/src/core/updater/mod.rs
@@ -1,3 +1,4 @@
pub mod commands;
pub mod custom_updater;
pub mod hmac_client;
+pub mod session;
diff --git a/src-tauri/src/core/updater/session.rs b/src-tauri/src/core/updater/session.rs
new file mode 100644
index 000000000..83ff0ff59
--- /dev/null
+++ b/src-tauri/src/core/updater/session.rs
@@ -0,0 +1,96 @@
+/**
+ * Session Management Module
+ *
+ * Manages persistent session identifier for request signing and caching.
+ * Uses tauri-plugin-store for persistence (compatible with TypeScript).
+ *
+ * Storage location: {app_data_dir}/updater.json
+ */
+
+use std::sync::OnceLock;
+use tauri::{AppHandle, Runtime};
+use tauri_plugin_store::StoreExt;
+use uuid::Uuid;
+
+/// Store filename (same as TypeScript)
+const STORE_NAME: &str = "updater.json";
+/// Key for session in store (same as TypeScript)
+const SESSION_KEY: &str = "nonce_seed";
+
+/// Cached session ID to avoid repeated store reads
+static CACHED_SESSION_ID: OnceLock<String> = OnceLock::new();
+
+/// Get or generate session ID using tauri-plugin-store
+/// Compatible with TypeScript's store access
+pub fn get_session_id_with_app<R: Runtime>(app: &AppHandle<R>) -> String {
+ // Return cached value if available
+ if let Some(cached) = CACHED_SESSION_ID.get() {
+ return cached.clone();
+ }
+
+ let session_id = match app.store(STORE_NAME) {
+ Ok(store) => {
+ // Try to get existing session ID
+ if let Some(id) = store.get(SESSION_KEY) {
+ if let Some(id_str) = id.as_str() {
+ log::debug!("Using existing session from store");
+ id_str.to_string()
+ } else {
+ generate_and_save_session(&store)
+ }
+ } else {
+ generate_and_save_session(&store)
+ }
+ }
+ Err(e) => {
+ log::warn!("Failed to access store: {}. Using fallback.", e);
+ get_session_id_fallback()
+ }
+ };
+
+ // Cache the ID
+ let _ = CACHED_SESSION_ID.set(session_id.clone());
+
+ session_id
+}
+
+/// Generate new session ID and save to store
+fn generate_and_save_session(store: &tauri_plugin_store::Store<impl Runtime>) -> String {
+ let new_id = Uuid::new_v4().to_string();
+ log::debug!("Generated new session");
+
+ // Save to store (store.set returns () in this version)
+ store.set(SESSION_KEY, serde_json::json!(new_id));
+
+ new_id
+}
+
+/// Get session ID without app handle
+/// This is useful when app handle is not available (e.g., in download context)
+pub fn get_session_id() -> String {
+ if let Some(cached) = CACHED_SESSION_ID.get() {
+ return cached.clone();
+ }
+
+ get_session_id_fallback()
+}
+
+/// Fallback session ID using process ID
+fn get_session_id_fallback() -> String {
+ // Use a combination of hostname and process id as fallback
+ let hostname = hostname::get()
+ .map(|h| h.to_string_lossy().to_string())
+ .unwrap_or_else(|_| "unknown".to_string());
+ format!("jan-{}-{}", hostname, std::process::id())
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_fallback_session() {
+ let id = get_session_id_fallback();
+ assert!(!id.is_empty());
+ }
+}