From e7d1081060cd3d0433cafba786399d5a1ecb1012 Mon Sep 17 00:00:00 2001 From: wujian0327 <353981613@qq.com> Date: Tue, 20 Aug 2024 18:26:53 +0800 Subject: [PATCH] repo_fork support alias --- gateway/src/api/ztm_router.rs | 15 +------ gemini/src/http/handler.rs | 78 ++++++++++++++++++++++++++++++++++- 2 files changed, 77 insertions(+), 16 deletions(-) diff --git a/gateway/src/api/ztm_router.rs b/gateway/src/api/ztm_router.rs index fe021861d..62e6d3435 100644 --- a/gateway/src/api/ztm_router.rs +++ b/gateway/src/api/ztm_router.rs @@ -72,23 +72,10 @@ async fn repo_folk( )); } }; - let local_port = match query.get("port") { - Some(i) => i, - None => { - return Err((StatusCode::BAD_REQUEST, String::from("Port not provide\n"))); - } - }; - let local_port = match local_port.parse::() { - Ok(i) => i, - Err(_) => { - return Err((StatusCode::BAD_REQUEST, String::from("Port not valid\n"))); - } - }; - let res = gemini::http::handler::repo_folk( + let res = gemini::http::handler::repo_folk_alias( state.ztm.ztm_agent_port, identifier.clone().to_string(), - local_port, ) .await; let res = match res { diff --git a/gemini/src/http/handler.rs b/gemini/src/http/handler.rs index 4c3c6af03..42d0de474 100644 --- a/gemini/src/http/handler.rs +++ b/gemini/src/http/handler.rs @@ -1,8 +1,9 @@ +use common::model::CommonResult; use jupiter::context::Context; use crate::{ - util::{get_short_peer_id, repo_alias_to_identifier}, - ztm::{agent::share_repo, create_tunnel}, + util::{get_available_port, get_short_peer_id, repo_alias_to_identifier}, + ztm::{agent::share_repo, create_tunnel, send_get_request_to_peer_by_tunnel}, RepoInfo, }; @@ -94,6 +95,49 @@ pub async fn repo_folk( Ok(msg) } +pub async fn repo_folk_alias(ztm_agent_port: u16, identifier: String) -> Result { + let remote_peer_id = match get_peer_id_from_identifier(identifier.clone()) { + Ok(p) => p, + Err(e) => return Err(e), + }; + let remote_port = 8000; + let alias = match get_alias_from_identifier(identifier) { + Ok(p) => p, + Err(e) => return Err(e), + }; + + let path = match get_git_path_by_alias(alias, remote_peer_id.clone(), ztm_agent_port).await { + Ok(path) => path, + Err(e) => return Err(e), + }; + + let peer_id = vault::get_peerid(); + let bound_name = format!( + "{}_{}", + get_short_peer_id(peer_id), + get_short_peer_id(remote_peer_id.clone()) + ); + let local_port = match get_available_port() { + Ok(p) => p, + Err(e) => return Err(e), + }; + match create_tunnel( + ztm_agent_port, + remote_peer_id, + local_port, + remote_port, + bound_name, + ) + .await + { + Ok(_) => (), + Err(e) => return Err(e), + } + + let msg = format!("git clone http://localhost:{local_port}{path}.git"); + Ok(msg) +} + pub fn get_peer_id_from_identifier(identifier: String) -> Result { // p2p://mrJ46F8gd2sa2Dx3iCYf6DauJ2WpAaepus7PwyZVebgD/8000/third-part/mega_143.git let words: Vec<&str> = identifier.split('/').collect(); @@ -125,5 +169,35 @@ pub fn get_git_path_from_identifier(identifier: String) -> Result Result { + // p2p://wGg2inNE22LY1eHttDB63znw2MnsK8CPXeG2nfhpXs5a/serde_python + let words: Vec<&str> = identifier.split('/').collect(); + if words.len() <= 3 { + return Err("invalid identifier".to_string()); + } + return Ok(words.get(3).unwrap().to_string()); +} + +pub async fn get_git_path_by_alias( + alias: String, + peer_id: String, + ztm_agent_port: u16, +) -> Result { + let path = format!("api/v1/mega/ztm/alias_to_path?alias={}", alias); + let result = match send_get_request_to_peer_by_tunnel(ztm_agent_port, peer_id, path).await { + Ok(r) => r, + Err(e) => return Err(e.to_string()), + }; + + let common_result: CommonResult = match serde_json::from_str(result.as_str()) { + Ok(s) => s, + Err(e) => return Err(e.to_string()), + }; + match common_result.data { + Some(path) => Ok(path), + None => Err("Path not found".to_string()), + } +} + #[cfg(test)] mod tests {}