mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-12-04 05:46:23 +01:00
203 lines
8.0 KiB
Rust
203 lines
8.0 KiB
Rust
use log::{error, info, warn};
|
|
use tauri::Runtime;
|
|
use tauri::path::BaseDirectory;
|
|
use tauri::{Emitter, Manager, RunEvent, State, WebviewWindow};
|
|
use yaak_proxy_lib::{MasterKeySource, ProxyCtx};
|
|
use yaak_rpc::{RpcEventEmitter, RpcRouter};
|
|
use yaak_window::window::CreateWindowConfig;
|
|
|
|
mod window_menu;
|
|
|
|
fn setup_window_menu<R: Runtime>(win: &WebviewWindow<R>) {
|
|
#[allow(unused_variables)]
|
|
let menu = match window_menu::app_menu(win.app_handle()) {
|
|
Ok(m) => m,
|
|
Err(e) => {
|
|
warn!("Failed to create menu: {e:?}");
|
|
return;
|
|
}
|
|
};
|
|
|
|
// This causes the window to not be clickable (in AppImage), so disable on Linux
|
|
#[cfg(not(target_os = "linux"))]
|
|
win.app_handle().set_menu(menu).expect("Failed to set app menu");
|
|
|
|
let webview_window = win.clone();
|
|
win.on_menu_event(move |w, event| {
|
|
if !w.is_focused().unwrap() {
|
|
return;
|
|
}
|
|
|
|
let event_id = event.id().0.as_str();
|
|
match event_id {
|
|
"hacked_quit" => {
|
|
w.webview_windows().iter().for_each(|(_, w)| {
|
|
info!("Closing window {}", w.label());
|
|
let _ = w.close();
|
|
});
|
|
}
|
|
window_menu::EXPORT_HAR_ID => {
|
|
if let Err(e) = webview_window.emit(window_menu::EXPORT_HAR_EVENT, ()) {
|
|
error!("Failed to emit the HAR export: {e}");
|
|
}
|
|
}
|
|
"dev.refresh" => webview_window.eval("location.reload()").unwrap(),
|
|
"dev.toggle_devtools" => {
|
|
if webview_window.is_devtools_open() {
|
|
webview_window.close_devtools();
|
|
} else {
|
|
webview_window.open_devtools();
|
|
}
|
|
}
|
|
_ => {}
|
|
}
|
|
});
|
|
}
|
|
|
|
/// Where `tauri.bundle.conf.json` puts the `yaakprotoc` that Yaak's vendor script fetches, in
|
|
/// the app's resources.
|
|
const PROTOC_DIR: &str = "vendored/protoc";
|
|
/// Where it puts protoc's include files.
|
|
const PROTOC_INCLUDE_DIR: &str = "vendored/protoc/include";
|
|
|
|
/// The `yaakprotoc` and its includes in the app's resources, if the bundle has them, as a
|
|
/// packaged app's does; a development build has none.
|
|
fn bundled_protoc<R: Runtime>(app: &tauri::AppHandle<R>) -> Option<yaak_proxy_lib::GrpcConfig> {
|
|
let bin_name = if cfg!(windows) { "yaakprotoc.exe" } else { "yaakprotoc" };
|
|
let resource = |path: &str| app.path().resolve(path, BaseDirectory::Resource).ok();
|
|
let protoc_bin_path = resource(&format!("{PROTOC_DIR}/{bin_name}"))?;
|
|
let protoc_include_dir = resource(PROTOC_INCLUDE_DIR)?;
|
|
protoc_bin_path
|
|
.is_file()
|
|
.then_some(yaak_proxy_lib::GrpcConfig { protoc_include_dir, protoc_bin_path })
|
|
}
|
|
|
|
#[tauri::command]
|
|
async fn rpc(
|
|
router: State<'_, RpcRouter<ProxyCtx>>,
|
|
ctx: State<'_, ProxyCtx>,
|
|
cmd: String,
|
|
payload: serde_json::Value,
|
|
) -> Result<serde_json::Value, String> {
|
|
router.dispatch(&cmd, payload, &ctx).await.map_err(|e| e.message)
|
|
}
|
|
|
|
pub fn run() {
|
|
tauri::Builder::default()
|
|
.plugin(tauri_plugin_os::init())
|
|
.plugin(tauri_plugin_dialog::init())
|
|
.plugin(tauri_plugin_opener::init())
|
|
.plugin(yaak_mac_window::init())
|
|
.setup(|app| {
|
|
let data_dir = app.path().app_data_dir().expect("no app data dir");
|
|
std::fs::create_dir_all(&data_dir).expect("failed to create app data dir");
|
|
|
|
let (emitter, event_rx) = RpcEventEmitter::new();
|
|
// Captured traffic is encrypted with a key wrapped by a master key in the keychain,
|
|
// under this app's identifier, so a development build keeps its own. Linux without
|
|
// a Secret Service keeps it in a file only the user can read, with a warning.
|
|
let keys = MasterKeySource::keychain_or_file(
|
|
&app.config().identifier,
|
|
data_dir.join("encryption.key"),
|
|
);
|
|
let mut ctx = ProxyCtx::new(&data_dir, emitter, keys)?;
|
|
// Compiles protos attached for gRPC decoding when the bundle has protoc, as Yaak's
|
|
// does; otherwise one on PATH is used.
|
|
if let Some(config) = bundled_protoc(app.handle()) {
|
|
ctx = ctx.with_protoc(config);
|
|
}
|
|
// Not while the CLI daemon owns the engine: the window offers to take it over.
|
|
match ctx.serve_control_api() {
|
|
// The engine runs for as long as the app does. A failure, such as the port
|
|
// being taken, is kept for the window to show.
|
|
Ok(()) => {
|
|
let ctx = ctx.clone();
|
|
std::thread::spawn(move || {
|
|
if let Err(e) = ctx.start() {
|
|
warn!("Failed to start the proxy: {e}");
|
|
}
|
|
});
|
|
}
|
|
Err(e) => warn!("Not serving the proxy control API: {e}"),
|
|
}
|
|
app.manage(ctx);
|
|
app.manage(yaak_proxy_lib::build_router());
|
|
|
|
// Drain RPC events and forward as Tauri events
|
|
let app_handle = app.handle().clone();
|
|
std::thread::spawn(move || {
|
|
for event in event_rx {
|
|
if let Err(e) = app_handle.emit(event.event, event.payload) {
|
|
error!("Failed to emit RPC event: {e}");
|
|
}
|
|
}
|
|
});
|
|
|
|
Ok(())
|
|
})
|
|
.invoke_handler(tauri::generate_handler![rpc])
|
|
.build(tauri::generate_context!())
|
|
.expect("error while building yaak proxy tauri application")
|
|
.run(|app_handle, event| {
|
|
if let RunEvent::Exit = event {
|
|
// Never leave the Mac pointed at a proxy that is gone.
|
|
let ctx = app_handle.state::<ProxyCtx>();
|
|
ctx.shutdown();
|
|
ctx.stop_control_api();
|
|
return;
|
|
}
|
|
if let RunEvent::Ready = event {
|
|
let config = CreateWindowConfig {
|
|
url: "/",
|
|
label: "main_0",
|
|
title: "Yaak Proxy",
|
|
inner_size: Some((1000.0, 700.0)),
|
|
hidden: true,
|
|
hide_titlebar: true,
|
|
..Default::default()
|
|
};
|
|
match yaak_window::window::create_window(app_handle, config) {
|
|
Ok(win) => setup_window_menu(&win),
|
|
Err(e) => error!("Failed to create proxy window: {e:?}"),
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
/// The bundle puts protoc, and its includes, where the app looks for them.
|
|
#[test]
|
|
fn the_bundle_ships_protoc_where_the_app_looks() {
|
|
let config: serde_json::Value =
|
|
serde_json::from_str(include_str!("../tauri.bundle.conf.json")).unwrap();
|
|
let resources = config["bundle"]["resources"].as_object().unwrap();
|
|
let target = |source: &str| resources.get(source).and_then(|t| t.as_str());
|
|
assert_eq!(
|
|
target("../yaak-app-client/vendored/protoc/yaakprotoc*")
|
|
.map(|t| t.trim_end_matches('/')),
|
|
Some(PROTOC_DIR)
|
|
);
|
|
assert_eq!(target("../yaak-app-client/vendored/protoc/include"), Some(PROTOC_INCLUDE_DIR));
|
|
}
|
|
|
|
/// The window may call each opener command the app's settings use: without its permission,
|
|
/// Tauri refuses the call and the button does nothing.
|
|
#[test]
|
|
fn the_window_may_open_links_and_reveal_folders() {
|
|
let capability: serde_json::Value =
|
|
serde_json::from_str(include_str!("../capabilities/default.json")).unwrap();
|
|
let permissions: Vec<&str> = capability["permissions"]
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.filter_map(|p| p.as_str())
|
|
.collect();
|
|
for needed in ["opener:allow-open-url", "opener:allow-reveal-item-in-dir"] {
|
|
assert!(permissions.contains(&needed), "{needed} missing from {permissions:?}");
|
|
}
|
|
}
|
|
}
|