feat: bootstrap tauri motrix-style UI and aria2 torrent flow

This commit is contained in:
2026-02-24 00:51:25 +09:00
commit bba6b8de5c
44 changed files with 9173 additions and 0 deletions

4
src-tauri/.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
# Generated by Cargo
# will have compiled files and executables
/target/
/gen/schemas

5392
src-tauri/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

27
src-tauri/Cargo.toml Normal file
View File

@@ -0,0 +1,27 @@
[package]
name = "app"
version = "0.1.0"
description = "A Tauri App"
authors = ["you"]
license = ""
repository = ""
edition = "2021"
rust-version = "1.77.2"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "app_lib"
crate-type = ["staticlib", "cdylib", "rlib"]
[build-dependencies]
tauri-build = { version = "2.5.4", features = [] }
[dependencies]
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
log = "0.4"
tauri = { version = "2.10.0", features = [] }
tauri-plugin-log = "2"
reqwest = { version = "0.12.24", default-features = false, features = ["json", "rustls-tls"] }
base64 = "0.22"

3
src-tauri/build.rs Normal file
View File

@@ -0,0 +1,3 @@
fn main() {
tauri_build::build()
}

View File

@@ -0,0 +1,11 @@
{
"$schema": "../gen/schemas/desktop-schema.json",
"identifier": "default",
"description": "enables the default permissions",
"windows": [
"main"
],
"permissions": [
"core:default"
]
}

BIN
src-tauri/icons/128x128.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

BIN
src-tauri/icons/32x32.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

BIN
src-tauri/icons/icon.icns Normal file

Binary file not shown.

BIN
src-tauri/icons/icon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

BIN
src-tauri/icons/icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

447
src-tauri/src/engine.rs Normal file
View File

@@ -0,0 +1,447 @@
use base64::{engine::general_purpose::STANDARD, Engine as _};
use reqwest::Client;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::path::Path;
use std::process::{Child, Command, Stdio};
use std::sync::Mutex;
use std::time::{SystemTime, UNIX_EPOCH};
use tauri::State;
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EngineStartRequest {
pub binary_path: Option<String>,
pub rpc_listen_port: Option<u16>,
pub rpc_secret: Option<String>,
pub download_dir: Option<String>,
pub max_concurrent_downloads: Option<u16>,
pub split: Option<u16>,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Aria2RpcConfig {
pub rpc_listen_port: Option<u16>,
pub rpc_secret: Option<String>,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Aria2AddUriRequest {
pub rpc: Aria2RpcConfig,
pub uri: String,
pub out: Option<String>,
pub dir: Option<String>,
pub split: Option<u16>,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Aria2AddTorrentRequest {
pub rpc: Aria2RpcConfig,
pub torrent_base64: String,
pub out: Option<String>,
pub dir: Option<String>,
pub split: Option<u16>,
}
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct EngineStatusResponse {
pub running: bool,
pub pid: Option<u32>,
pub binary_path: Option<String>,
pub args: Vec<String>,
pub started_at: Option<u64>,
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Aria2TaskSummary {
pub gid: String,
pub status: String,
pub total_length: String,
pub completed_length: String,
pub download_speed: String,
pub dir: String,
pub file_name: String,
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Aria2TaskSnapshot {
pub active: Vec<Aria2TaskSummary>,
pub waiting: Vec<Aria2TaskSummary>,
pub stopped: Vec<Aria2TaskSummary>,
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TorrentFilePayload {
pub name: String,
pub base64: String,
pub size: u64,
}
#[derive(Debug)]
struct EngineRuntime {
child: Option<Child>,
binary_path: Option<String>,
args: Vec<String>,
started_at: Option<u64>,
}
impl Default for EngineRuntime {
fn default() -> Self {
Self {
child: None,
binary_path: None,
args: vec![],
started_at: None,
}
}
}
#[derive(Default)]
pub struct EngineState {
runtime: Mutex<EngineRuntime>,
}
impl EngineState {
fn status(runtime: &EngineRuntime) -> EngineStatusResponse {
EngineStatusResponse {
running: runtime.child.is_some(),
pid: runtime.child.as_ref().map(std::process::Child::id),
binary_path: runtime.binary_path.clone(),
args: runtime.args.clone(),
started_at: runtime.started_at,
}
}
}
fn build_engine_args(request: &EngineStartRequest) -> Vec<String> {
let mut args = vec![
"--enable-rpc=true".to_string(),
"--rpc-listen-all=true".to_string(),
"--rpc-allow-origin-all=true".to_string(),
"--rpc-listen-port=".to_string() + &request.rpc_listen_port.unwrap_or(6800).to_string(),
"--max-concurrent-downloads=".to_string() + &request.max_concurrent_downloads.unwrap_or(5).to_string(),
"--split=".to_string() + &request.split.unwrap_or(8).to_string(),
"--continue=true".to_string(),
];
if let Some(secret) = &request.rpc_secret {
if !secret.trim().is_empty() {
args.push(format!("--rpc-secret={secret}"));
}
}
if let Some(download_dir) = &request.download_dir {
if !download_dir.trim().is_empty() {
args.push(format!("--dir={download_dir}"));
}
}
args
}
fn default_aria2_binary() -> String {
if cfg!(target_os = "windows") {
"aria2c.exe".to_string()
} else {
"aria2c".to_string()
}
}
fn rpc_endpoint(config: &Aria2RpcConfig) -> String {
format!(
"http://127.0.0.1:{}/jsonrpc",
config.rpc_listen_port.unwrap_or(6800)
)
}
fn rpc_token(config: &Aria2RpcConfig) -> Option<String> {
let secret = config.rpc_secret.as_ref()?.trim();
if secret.is_empty() {
None
} else {
Some(format!("token:{secret}"))
}
}
async fn call_aria2_rpc(
client: &Client,
config: &Aria2RpcConfig,
method: &str,
mut params: Vec<Value>,
) -> Result<Value, String> {
if let Some(token) = rpc_token(config) {
params.insert(0, json!(token));
}
let endpoint = rpc_endpoint(config);
let payload = json!({
"jsonrpc": "2.0",
"id": "gdown",
"method": method,
"params": params,
});
let response = client
.post(&endpoint)
.json(&payload)
.send()
.await
.map_err(|err| format!("aria2 RPC request failed ({method}): {err}"))?;
let status = response.status();
let body: Value = response
.json()
.await
.map_err(|err| format!("aria2 RPC invalid response ({method}): {err}"))?;
if !status.is_success() {
return Err(format!("aria2 RPC HTTP error ({method}): {status} / {body}"));
}
if let Some(error) = body.get("error") {
return Err(format!("aria2 RPC error ({method}): {error}"));
}
body
.get("result")
.cloned()
.ok_or_else(|| format!("aria2 RPC missing result ({method}): {body}"))
}
fn value_to_string(value: Option<&Value>) -> String {
value
.and_then(Value::as_str)
.unwrap_or_default()
.to_string()
}
fn pick_file_name(path: &str) -> String {
if path.is_empty() {
return "-".to_string();
}
Path::new(path)
.file_name()
.map(|name| name.to_string_lossy().to_string())
.filter(|name| !name.is_empty())
.unwrap_or_else(|| path.to_string())
}
fn map_task(task: &Value) -> Aria2TaskSummary {
let file_path = task
.get("files")
.and_then(Value::as_array)
.and_then(|files| files.first())
.and_then(|file| file.get("path"))
.and_then(Value::as_str)
.unwrap_or("");
Aria2TaskSummary {
gid: value_to_string(task.get("gid")),
status: value_to_string(task.get("status")),
total_length: value_to_string(task.get("totalLength")),
completed_length: value_to_string(task.get("completedLength")),
download_speed: value_to_string(task.get("downloadSpeed")),
dir: value_to_string(task.get("dir")),
file_name: pick_file_name(file_path),
}
}
fn map_tasks(result: Value) -> Vec<Aria2TaskSummary> {
result
.as_array()
.map(|items| items.iter().map(map_task).collect())
.unwrap_or_default()
}
fn build_rpc_options(out: Option<&String>, dir: Option<&String>, split: Option<u16>) -> Value {
let mut options = serde_json::Map::new();
if let Some(value) = out {
if !value.trim().is_empty() {
options.insert("out".to_string(), json!(value));
}
}
if let Some(value) = dir {
if !value.trim().is_empty() {
options.insert("dir".to_string(), json!(value));
}
}
if let Some(value) = split {
options.insert("split".to_string(), json!(value.to_string()));
}
Value::Object(options)
}
#[tauri::command]
pub fn engine_start(
state: State<'_, EngineState>,
request: EngineStartRequest,
) -> Result<EngineStatusResponse, String> {
let mut runtime = state
.runtime
.lock()
.map_err(|err| format!("failed to lock engine state: {err}"))?;
if let Some(child) = runtime.child.as_mut() {
match child.try_wait() {
Ok(Some(_)) => {
runtime.child = None;
}
Ok(None) => {
return Ok(EngineState::status(&runtime));
}
Err(err) => {
return Err(format!("failed to inspect current engine process: {err}"));
}
}
}
let args = build_engine_args(&request);
let binary = request.binary_path.unwrap_or_else(default_aria2_binary);
let child = Command::new(&binary)
.args(&args)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
.map_err(|err| format!("failed to start aria2 engine with '{binary}': {err}"))?;
runtime.child = Some(child);
runtime.binary_path = Some(binary);
runtime.args = args;
runtime.started_at = Some(
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_err(|err| format!("failed to get system time: {err}"))?
.as_secs(),
);
Ok(EngineState::status(&runtime))
}
#[tauri::command]
pub fn engine_stop(state: State<'_, EngineState>) -> Result<EngineStatusResponse, String> {
let mut runtime = state
.runtime
.lock()
.map_err(|err| format!("failed to lock engine state: {err}"))?;
if let Some(mut child) = runtime.child.take() {
child
.kill()
.map_err(|err| format!("failed to stop aria2 engine: {err}"))?;
let _ = child.wait();
}
runtime.started_at = None;
Ok(EngineState::status(&runtime))
}
#[tauri::command]
pub fn engine_status(state: State<'_, EngineState>) -> Result<EngineStatusResponse, String> {
let mut runtime = state
.runtime
.lock()
.map_err(|err| format!("failed to lock engine state: {err}"))?;
if let Some(child) = runtime.child.as_mut() {
match child.try_wait() {
Ok(Some(_)) => {
runtime.child = None;
runtime.started_at = None;
}
Ok(None) => {}
Err(err) => {
return Err(format!("failed to inspect engine process status: {err}"));
}
}
}
Ok(EngineState::status(&runtime))
}
#[tauri::command]
pub async fn aria2_add_uri(request: Aria2AddUriRequest) -> Result<String, String> {
if request.uri.trim().is_empty() {
return Err("uri is required".to_string());
}
let client = Client::new();
let mut params = vec![json!([request.uri.trim()])];
let options = build_rpc_options(request.out.as_ref(), request.dir.as_ref(), request.split);
if options.as_object().map(|obj| !obj.is_empty()).unwrap_or(false) {
params.push(options);
}
let result = call_aria2_rpc(&client, &request.rpc, "aria2.addUri", params).await?;
result
.as_str()
.map(|gid| gid.to_string())
.ok_or_else(|| format!("aria2.addUri returned unexpected result: {result}"))
}
#[tauri::command]
pub async fn aria2_add_torrent(request: Aria2AddTorrentRequest) -> Result<String, String> {
if request.torrent_base64.trim().is_empty() {
return Err("torrentBase64 is required".to_string());
}
let client = Client::new();
let options = build_rpc_options(request.out.as_ref(), request.dir.as_ref(), request.split);
let mut params = vec![json!(request.torrent_base64.trim())];
if options.as_object().map(|obj| !obj.is_empty()).unwrap_or(false) {
params.push(json!([]));
params.push(options);
}
let result = call_aria2_rpc(&client, &request.rpc, "aria2.addTorrent", params).await?;
result
.as_str()
.map(|gid| gid.to_string())
.ok_or_else(|| format!("aria2.addTorrent returned unexpected result: {result}"))
}
#[tauri::command]
pub async fn aria2_list_tasks(config: Aria2RpcConfig) -> Result<Aria2TaskSnapshot, String> {
let client = Client::new();
let active = call_aria2_rpc(&client, &config, "aria2.tellActive", vec![]).await?;
let waiting =
call_aria2_rpc(&client, &config, "aria2.tellWaiting", vec![json!(0), json!(30)]).await?;
let stopped =
call_aria2_rpc(&client, &config, "aria2.tellStopped", vec![json!(0), json!(30)]).await?;
Ok(Aria2TaskSnapshot {
active: map_tasks(active),
waiting: map_tasks(waiting),
stopped: map_tasks(stopped),
})
}
#[tauri::command]
pub fn load_torrent_file(path: String) -> Result<TorrentFilePayload, String> {
if !path.to_ascii_lowercase().ends_with(".torrent") {
return Err("torrent 파일만 허용됩니다.".to_string());
}
let bytes = std::fs::read(&path).map_err(|err| format!("파일 읽기 실패: {err}"))?;
let name = Path::new(&path)
.file_name()
.map(|v| v.to_string_lossy().to_string())
.filter(|v| !v.is_empty())
.ok_or_else(|| "파일명을 확인할 수 없습니다.".to_string())?;
Ok(TorrentFilePayload {
name,
base64: STANDARD.encode(bytes.as_slice()),
size: bytes.len() as u64,
})
}

33
src-tauri/src/lib.rs Normal file
View File

@@ -0,0 +1,33 @@
mod engine;
use engine::{
aria2_add_torrent, aria2_add_uri, aria2_list_tasks, engine_start, engine_status, engine_stop,
load_torrent_file, EngineState,
};
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.manage(EngineState::default())
.setup(|app| {
if cfg!(debug_assertions) {
app.handle().plugin(
tauri_plugin_log::Builder::default()
.level(log::LevelFilter::Info)
.build(),
)?;
}
Ok(())
})
.invoke_handler(tauri::generate_handler![
engine_start,
engine_stop,
engine_status,
aria2_add_torrent,
aria2_add_uri,
aria2_list_tasks,
load_torrent_file
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}

6
src-tauri/src/main.rs Normal file
View File

@@ -0,0 +1,6 @@
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
fn main() {
app_lib::run();
}

37
src-tauri/tauri.conf.json Normal file
View File

@@ -0,0 +1,37 @@
{
"$schema": "../node_modules/@tauri-apps/cli/config.schema.json",
"productName": "gdown",
"version": "0.1.0",
"identifier": "com.tauri.dev",
"build": {
"frontendDist": "../dist",
"devUrl": "http://localhost:5173",
"beforeDevCommand": "npm run dev",
"beforeBuildCommand": "npm run build"
},
"app": {
"windows": [
{
"title": "gdown",
"width": 800,
"height": 600,
"resizable": true,
"fullscreen": false
}
],
"security": {
"csp": null
}
},
"bundle": {
"active": true,
"targets": "all",
"icon": [
"icons/32x32.png",
"icons/128x128.png",
"icons/128x128@2x.png",
"icons/icon.icns",
"icons/icon.ico"
]
}
}