-
Notifications
You must be signed in to change notification settings - Fork 0
feat(ipc): add Unix domain socket for inter-process communication #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| use serde::{Deserialize, Serialize}; | ||
| use std::path::{Path, PathBuf}; | ||
| use std::sync::Mutex; | ||
| use tauri::{Emitter, Manager}; | ||
| use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader}; | ||
| use tokio::net::{UnixListener, UnixStream}; | ||
|
|
||
| #[derive(Deserialize)] | ||
| struct IpcCommand { | ||
| command: String, | ||
| #[serde(default)] | ||
| path: Option<String>, | ||
| } | ||
|
|
||
| #[derive(Serialize)] | ||
| struct IpcResponse { | ||
| success: bool, | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| error: Option<String>, | ||
| } | ||
|
|
||
| pub struct SocketState(pub Mutex<Option<PathBuf>>); | ||
|
|
||
| pub fn setup(app: &tauri::App) -> Result<(), String> { | ||
| let sock_path = socket_path()?; | ||
| cleanup_stale_socket(&sock_path)?; | ||
|
|
||
| let app_handle = app.handle().clone(); | ||
| tauri::async_runtime::spawn(async move { | ||
| match UnixListener::bind(&sock_path) { | ||
| Ok(listener) => { | ||
| let state = app_handle.state::<SocketState>(); | ||
| if let Ok(mut guard) = state.0.lock() { | ||
| *guard = Some(sock_path.clone()); | ||
| } | ||
|
|
||
| #[cfg(unix)] | ||
| { | ||
| use std::os::unix::fs::PermissionsExt; | ||
| let perms = std::fs::Permissions::from_mode(0o600); | ||
| let _ = std::fs::set_permissions(&sock_path, perms); | ||
| } | ||
|
|
||
| socket_listener_loop(listener, app_handle).await; | ||
| } | ||
| Err(e) => { | ||
| eprintln!("Failed to bind socket: {}", e); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| pub fn cleanup(state: tauri::State<SocketState>) { | ||
| if let Ok(guard) = state.0.lock() { | ||
| if let Some(path) = guard.as_ref() { | ||
| let _ = std::fs::remove_file(path); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn socket_path() -> Result<PathBuf, String> { | ||
| let home = std::env::var("HOME") | ||
| .map_err(|_| "HOME environment variable not set".to_string())?; | ||
| let arandu_dir = PathBuf::from(home).join(".arandu"); | ||
|
|
||
| std::fs::create_dir_all(&arandu_dir) | ||
| .map_err(|e| format!("Failed to create ~/.arandu: {}", e))?; | ||
| #[cfg(unix)] | ||
| { | ||
| use std::os::unix::fs::PermissionsExt; | ||
| std::fs::set_permissions(&arandu_dir, std::fs::Permissions::from_mode(0o700)) | ||
| .map_err(|e| format!("Failed to set ~/.arandu permissions: {}", e))?; | ||
| } | ||
|
|
||
| Ok(arandu_dir.join("arandu.sock")) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| fn cleanup_stale_socket(path: &Path) -> Result<(), String> { | ||
| if !path.exists() { | ||
| return Ok(()); | ||
| } | ||
|
|
||
| match std::os::unix::net::UnixStream::connect(path) { | ||
| Ok(_) => Err("Socket already in use by another instance".to_string()), | ||
| Err(_) => { | ||
| std::fs::remove_file(path) | ||
| .map_err(|e| format!("Failed to remove stale socket: {}", e)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async fn socket_listener_loop(listener: UnixListener, app: tauri::AppHandle) { | ||
| loop { | ||
| match listener.accept().await { | ||
| Ok((stream, _addr)) => { | ||
| let app_clone = app.clone(); | ||
| tauri::async_runtime::spawn(async move { | ||
| if let Err(e) = handle_client(stream, app_clone).await { | ||
| eprintln!("Client error: {}", e); | ||
| } | ||
| }); | ||
| } | ||
| Err(e) => { | ||
| eprintln!("Accept error: {}", e); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async fn handle_client(stream: UnixStream, app: tauri::AppHandle) -> Result<(), String> { | ||
| let (reader, mut writer) = stream.into_split(); | ||
| let reader = BufReader::new(reader); | ||
| let mut lines = reader.lines(); | ||
|
|
||
| while let Some(line) = lines.next_line().await.map_err(|e| e.to_string())? { | ||
| let response = match serde_json::from_str::<IpcCommand>(&line) { | ||
| Ok(cmd) => process_command(cmd, &app), | ||
| Err(e) => IpcResponse { | ||
| success: false, | ||
| error: Some(format!("Invalid JSON: {}", e)), | ||
| }, | ||
| }; | ||
|
|
||
| let json = serde_json::to_string(&response).unwrap_or_default(); | ||
| writer | ||
| .write_all(format!("{}\n", json).as_bytes()) | ||
| .await | ||
| .map_err(|e| e.to_string())?; | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| fn process_command(cmd: IpcCommand, app: &tauri::AppHandle) -> IpcResponse { | ||
| match cmd.command.as_str() { | ||
| "open" => { | ||
| if let Some(path) = cmd.path { | ||
| match std::fs::canonicalize(&path) { | ||
| Ok(abs_path) => { | ||
| let path_str = abs_path.to_string_lossy().to_string(); | ||
|
|
||
| if let Some(window) = app.get_webview_window("main") { | ||
| let _ = window.unminimize(); | ||
| let _ = window.show(); | ||
| let _ = window.set_focus(); | ||
| } | ||
|
|
||
| match app.emit("open-file", &path_str) { | ||
| Ok(_) => IpcResponse { | ||
| success: true, | ||
| error: None, | ||
| }, | ||
| Err(e) => IpcResponse { | ||
| success: false, | ||
| error: Some(format!("Failed to emit event: {}", e)), | ||
| }, | ||
| } | ||
| } | ||
| Err(e) => IpcResponse { | ||
| success: false, | ||
| error: Some(format!("Invalid path: {}", e)), | ||
| }, | ||
| } | ||
| } else { | ||
| IpcResponse { | ||
| success: false, | ||
| error: Some("Missing 'path' field".to_string()), | ||
| } | ||
| } | ||
| } | ||
| "ping" => IpcResponse { | ||
| success: true, | ||
| error: None, | ||
| }, | ||
| "show" => { | ||
| if let Some(window) = app.get_webview_window("main") { | ||
| let _ = window.unminimize(); | ||
| let _ = window.show(); | ||
| let _ = window.set_focus(); | ||
| } | ||
| IpcResponse { | ||
| success: true, | ||
| error: None, | ||
| } | ||
| } | ||
| _ => IpcResponse { | ||
| success: false, | ||
| error: Some(format!("Unknown command: {}", cmd.command)), | ||
| }, | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.