forked from nbdd0121/wsld
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0eb04dd
commit 7861a12
Showing
7 changed files
with
141 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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,40 @@ | ||
use super::config::GpgAgentConfig; | ||
use super::util::{connect_stream, either}; | ||
use super::vmsocket::VmSocket; | ||
use super::CONFIG; | ||
|
||
use std::fs::Permissions; | ||
use std::os::unix::fs::PermissionsExt; | ||
use std::path::Path; | ||
use tokio::io::AsyncWriteExt; | ||
use tokio::net::{UnixListener, UnixStream}; | ||
|
||
async fn handle_stream(mut stream: UnixStream) -> std::io::Result<()> { | ||
let mut server = VmSocket::connect(CONFIG.service_port).await?; | ||
server.write_all(b"gpga").await?; | ||
|
||
let (client_r, client_w) = stream.split(); | ||
let (server_r, server_w) = server.split(); | ||
let a = connect_stream(client_r, server_w); | ||
let b = connect_stream(server_r, client_w); | ||
either(a, b).await | ||
} | ||
|
||
pub async fn gpg_agent_forward(config: &'static GpgAgentConfig) -> std::io::Result<()> { | ||
// Remove existing socket | ||
let _ = std::fs::create_dir_all(Path::new(&config.gpg_agent_sock).parent().unwrap()); | ||
let _ = std::fs::remove_file(&config.gpg_agent_sock); | ||
|
||
let listener = UnixListener::bind(&config.gpg_agent_sock)?; | ||
let _ = std::fs::set_permissions(&config.gpg_agent_sock, Permissions::from_mode(0o600)); | ||
|
||
loop { | ||
let stream = listener.accept().await?.0; | ||
|
||
tokio::task::spawn(async move { | ||
if let Err(err) = handle_stream(stream).await { | ||
eprintln!("Failed to transfer: {}", err); | ||
} | ||
}); | ||
} | ||
} |
This file contains 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 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 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,50 @@ | ||
use std::process::Stdio; | ||
|
||
use super::util::{connect_stream, either}; | ||
|
||
use anyhow::{anyhow, Context as _, Result}; | ||
use tokio::fs::read; | ||
use tokio::io::AsyncWriteExt; | ||
use tokio::net::TcpStream; | ||
use tokio::process::Command; | ||
|
||
pub async fn handle_gpg_agent(mut stream: TcpStream) -> Result<()> { | ||
let gpg_conf = Command::new("gpgconf.exe") | ||
.arg("--list-dir") | ||
.arg("agent-socket") | ||
.stdout(Stdio::piped()) | ||
.spawn()?; | ||
let output = gpg_conf.wait_with_output().await?; | ||
if !output.status.success() { | ||
return Err(anyhow!("gpgconf failure")); | ||
} | ||
|
||
// Start gpg-agent | ||
let run_gpg_agent = Command::new("gpg-connect-agent.exe") | ||
.arg("/bye") | ||
.spawn()? | ||
.wait() | ||
.await?; | ||
if !run_gpg_agent.success() { | ||
return Err(anyhow!("gpg-connect-agent failure")); | ||
} | ||
|
||
let content = read(std::str::from_utf8(&output.stdout)?.trim()).await?; | ||
for i in 0..content.len() { | ||
if content[i] == b'\n' { | ||
let port: u16 = std::str::from_utf8(&content[0..i]) | ||
.context("non utf8 port number")? | ||
.parse() | ||
.context("invalid port number for gpg-agent")?; | ||
let (client_r, client_w) = stream.split(); | ||
let mut server = TcpStream::connect(("127.0.0.1", port)).await?; | ||
server.set_nodelay(true)?; | ||
let (server_r, mut server_w) = server.split(); | ||
server_w.write_all(&content[i + 1..]).await?; | ||
let a = connect_stream(client_r, server_w); | ||
let b = connect_stream(server_r, client_w); | ||
return Ok(either(a, b).await?); | ||
} | ||
} | ||
Err(anyhow!("invalid format of agent-socket")) | ||
} |
This file contains 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