mirror of
https://github.com/kunkunsh/kunkun.git
synced 2025-04-03 22:26:43 +00:00
fix: file transfer ip bug
When sending to non-localhost host, will fail. grpc server somehow cannot get client ip. Modified grpc proto to send a src ip.
This commit is contained in:
parent
becfc184be
commit
2bb24799f3
38
Cargo.lock
generated
38
Cargo.lock
generated
@ -4000,6 +4000,18 @@ dependencies = [
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "local-ip-address"
|
||||
version = "0.6.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3669cf5561f8d27e8fc84cc15e58350e70f557d4d65f70e3154e54cd2f8e1782"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"neli",
|
||||
"thiserror 1.0.66",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "localauthentication-rs"
|
||||
version = "0.1.0"
|
||||
@ -4322,6 +4334,31 @@ dependencies = [
|
||||
"jni-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "neli"
|
||||
version = "0.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1100229e06604150b3becd61a4965d5c70f3be1759544ea7274166f4be41ef43"
|
||||
dependencies = [
|
||||
"byteorder",
|
||||
"libc",
|
||||
"log",
|
||||
"neli-proc-macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "neli-proc-macros"
|
||||
version = "0.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c168194d373b1e134786274020dae7fc5513d565ea2ebb9bc9ff17ffb69106d4"
|
||||
dependencies = [
|
||||
"either",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"serde",
|
||||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "network-interface"
|
||||
version = "1.1.4"
|
||||
@ -7476,6 +7513,7 @@ dependencies = [
|
||||
"futures-util",
|
||||
"grpc",
|
||||
"ico",
|
||||
"local-ip-address",
|
||||
"log",
|
||||
"mac-security-rs",
|
||||
"mdns-sd",
|
||||
|
@ -57,16 +57,21 @@
|
||||
unlistenReq = await listen<FileTransferPayload>("file-transfer-request", async (e) => {
|
||||
console.log(e)
|
||||
const confirmed = await confirm(
|
||||
`Download files (${e.payload.totalFiles} files, ${prettyBytes(e.payload.totalBytes)})?`
|
||||
`Download files (${e.payload.totalFiles} files, ${prettyBytes(e.payload.totalBytes)}) from ${e.payload.ip}?`
|
||||
)
|
||||
if (!confirmed) return
|
||||
downloadFiles(e.payload, await path.downloadDir(), (progress) => {
|
||||
progressMap[e.payload.code] = progress
|
||||
console.log(progress)
|
||||
}).finally(() => {
|
||||
console.log("finally clean", e.payload.code)
|
||||
delete progressMap[e.payload.code]
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error("Fail to download files", err)
|
||||
toast.error("Fail to download files", { description: err.message })
|
||||
})
|
||||
.finally(() => {
|
||||
console.log("finally clean", e.payload.code)
|
||||
delete progressMap[e.payload.code]
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@ -85,7 +90,7 @@
|
||||
}
|
||||
|
||||
function sendFile(peer: MdnsServiceInfo, files: string[]) {
|
||||
console.log(peer, files)
|
||||
// console.log(peer.addresses[0], peer.port, peer.sslCert, files)
|
||||
localNetSendFile(peer.addresses[0], peer.port, peer.sslCert, files)
|
||||
}
|
||||
</script>
|
||||
|
@ -20,9 +20,10 @@ message FileNode {
|
||||
|
||||
message StartTransferRequest {
|
||||
string port = 1;
|
||||
string code = 2;
|
||||
string ssl_cert = 3;
|
||||
FileNode root = 4;
|
||||
string ip = 2;
|
||||
string code = 3;
|
||||
string ssl_cert = 4;
|
||||
FileNode root = 5;
|
||||
}
|
||||
|
||||
message StartTransferResponse {
|
||||
|
@ -55,6 +55,7 @@ obfstr = { workspace = true }
|
||||
grpc = { workspace = true }
|
||||
futures-util = "0.3.31"
|
||||
rayon = { workspace = true }
|
||||
local-ip-address = "0.6.3"
|
||||
|
||||
|
||||
[target.'cfg(target_os = "macos")'.dependencies]
|
||||
|
@ -11,6 +11,7 @@ use grpc::file_transfer::{
|
||||
file_transfer_client::FileTransferClient, FileNode, FileType, StartTransferRequest,
|
||||
StartTransferResponse,
|
||||
};
|
||||
use local_ip_address::local_ip;
|
||||
use std::{
|
||||
collections::HashSet,
|
||||
path::{Path, PathBuf},
|
||||
@ -84,6 +85,7 @@ async fn download_file_node_recursively(
|
||||
stats: &mut TransferStats,
|
||||
progress_tx: mpsc::Sender<FileDownloadProgress>,
|
||||
) -> anyhow::Result<()> {
|
||||
println!("download_file_node_recursively node: {:#?}", node);
|
||||
let mut stack = vec![(node.clone(), dir.to_path_buf())];
|
||||
|
||||
while let Some((current_node, current_dir)) = stack.pop() {
|
||||
@ -96,7 +98,7 @@ async fn download_file_node_recursively(
|
||||
port = config.port,
|
||||
id = current_node.id
|
||||
);
|
||||
download_file(
|
||||
match download_file(
|
||||
&url,
|
||||
&config.code,
|
||||
&download_path,
|
||||
@ -104,7 +106,16 @@ async fn download_file_node_recursively(
|
||||
stats,
|
||||
progress_tx.clone(),
|
||||
)
|
||||
.await?;
|
||||
.await
|
||||
{
|
||||
Ok(_) => (),
|
||||
Err(e) => {
|
||||
println!("download_file error: {:?}", e);
|
||||
println!("download_file url: {:?}", url);
|
||||
println!("download_file code: {:?}", config.code);
|
||||
return Err(e);
|
||||
}
|
||||
};
|
||||
} else if current_node.r#type == FileType::Directory as i32 {
|
||||
tokio::fs::create_dir_all(&download_path).await?;
|
||||
for child in current_node.children.iter() {
|
||||
@ -134,12 +145,12 @@ pub async fn download_files(
|
||||
save_dir.display()
|
||||
));
|
||||
}
|
||||
|
||||
println!("download_files payload: {:?}", payload);
|
||||
let (progress_tx, mut progress_rx) = mpsc::channel::<FileDownloadProgress>(100);
|
||||
|
||||
let total_bytes = compute_total_size(&payload.root);
|
||||
let total_files = count_file_nodes(&payload.root);
|
||||
let client = build_ssl_reqwest_client(None, Some(payload.ssl_cert.clone()))
|
||||
let client = build_ssl_reqwest_client(Some(true), Some(payload.ssl_cert.clone()))
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
if payload.root.filename.is_empty() {
|
||||
@ -262,12 +273,14 @@ pub async fn local_net_send_file(
|
||||
.map_err(|err| err.to_string())?;
|
||||
let mut client = FileTransferClient::new(tls_channel);
|
||||
// Send the transfer request
|
||||
let my_local_ip = local_ip().unwrap();
|
||||
let response: tonic::Response<StartTransferResponse> = client
|
||||
.start_transfer(StartTransferRequest {
|
||||
port: port.to_string(),
|
||||
root: Some(root),
|
||||
code: uuid.clone(),
|
||||
ssl_cert: cert_pem,
|
||||
ip: my_local_ip.to_string(),
|
||||
})
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
@ -32,19 +32,19 @@ impl FileTransfer for MyFileTransfer {
|
||||
request: Request<StartTransferRequest>, // Accept request of type StartTransferRequest
|
||||
) -> Result<Response<StartTransferResponse>, Status> {
|
||||
let reply = StartTransferResponse {};
|
||||
// let ip = request.remote_addr().unwrap().ip();
|
||||
let ip = "localhost";
|
||||
println!("start_transfer remote addr: {:?}", request.remote_addr());
|
||||
let payload = request.into_inner();
|
||||
println!("start_transfer payload: {:?}", payload);
|
||||
let src_ip = payload.ip;
|
||||
println!("src_ip: {:?}", src_ip);
|
||||
let root = if let Some(root) = payload.root {
|
||||
root
|
||||
} else {
|
||||
return Err(Status::invalid_argument("root is required"));
|
||||
};
|
||||
|
||||
let total_bytes = compute_total_size(&root);
|
||||
let total_files = count_file_nodes(&root);
|
||||
// get ip from request
|
||||
|
||||
self.app_handle
|
||||
.emit(
|
||||
"file-transfer-request",
|
||||
@ -54,12 +54,13 @@ impl FileTransfer for MyFileTransfer {
|
||||
root,
|
||||
total_bytes,
|
||||
total_files,
|
||||
ip: ip.to_string(),
|
||||
ip: src_ip,
|
||||
ssl_cert: payload.ssl_cert,
|
||||
},
|
||||
)
|
||||
.map_err(|e| Status::internal(e.to_string()))?;
|
||||
Ok(Response::new(reply)) // Send back our formatted greeting
|
||||
|
||||
Ok(Response::new(reply))
|
||||
}
|
||||
}
|
||||
|
||||
@ -257,8 +258,9 @@ mod test {
|
||||
.arg("f")
|
||||
.output()
|
||||
.unwrap()
|
||||
.stdout
|
||||
).unwrap();
|
||||
.stdout,
|
||||
)
|
||||
.unwrap();
|
||||
let count2 = stdout.lines().count();
|
||||
assert_eq!(count, count2);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user