Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 14 deletions gateway/src/api/ztm_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u16>() {
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 {
Expand Down
78 changes: 76 additions & 2 deletions gemini/src/http/handler.rs
Original file line number Diff line number Diff line change
@@ -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,
};

Expand Down Expand Up @@ -94,6 +95,49 @@ pub async fn repo_folk(
Ok(msg)
}

pub async fn repo_folk_alias(ztm_agent_port: u16, identifier: String) -> Result<String, String> {
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<String, String> {
// p2p://mrJ46F8gd2sa2Dx3iCYf6DauJ2WpAaepus7PwyZVebgD/8000/third-part/mega_143.git
let words: Vec<&str> = identifier.split('/').collect();
Expand Down Expand Up @@ -125,5 +169,35 @@ pub fn get_git_path_from_identifier(identifier: String) -> Result<String, String
Ok(path)
}

pub fn get_alias_from_identifier(identifier: String) -> Result<String, String> {
// 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<String, String> {
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<String> = 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 {}