Skip to content

Commit

Permalink
Merge pull request #872 from wujian0327/main
Browse files Browse the repository at this point in the history
Feature: add ca service for p2p
  • Loading branch information
genedna authored Feb 26, 2025
2 parents 01d5d62 + e51f746 commit b9342cf
Show file tree
Hide file tree
Showing 22 changed files with 1,165 additions and 844 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ buck-out
# Gtk4 resource files
**/*.gresource
monobean/resources/lib

65 changes: 2 additions & 63 deletions aries/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
use clap::Parser;
use common::config::{Config, LogConfig};
use gemini::ztm::{
agent::{run_ztm_client, LocalZTMAgent, ZTMAgent},
hub::LocalZTMHub,
};
use service::{
ca_server::run_ca_server,
relay_server::{run_relay_server, RelayOptions},
};
use std::{
env,
path::PathBuf,
thread::{self},
time::{self},
};
use service::relay_server::{run_relay_server, RelayOptions};
use std::{env, path::PathBuf};
use tracing_subscriber::fmt::writer::MakeWriterExt;

pub mod service;
Expand Down Expand Up @@ -44,55 +32,6 @@ async fn main() {

tracing::info!("{:?}", option);

if option.only_agent {
let (peer_id, _) = vault::init().await;
let ztm_agent: LocalZTMAgent = LocalZTMAgent {
agent_port: option.ztm_agent_port,
};
ztm_agent.clone().start_ztm_agent();
thread::sleep(time::Duration::from_secs(3));
run_ztm_client(
"http://gitmono.org/relay".to_string(),
config.clone(),
peer_id,
ztm_agent,
8001,
)
.await
}

//Start a sub thread to ca server
let config_clone = config.clone();
let ca_port = option.ca_port;
tokio::spawn(async move { run_ca_server(config_clone, ca_port).await });
thread::sleep(time::Duration::from_secs(3));

//Start a sub thread to run ztm-hub
let ca = format!("127.0.0.1:{ca_port}");
let ztm_hub: LocalZTMHub = LocalZTMHub {
hub_port: option.ztm_hub_port,
ca,
name: vec!["relay".to_string()],
};
ztm_hub.clone().start_ztm_hub();
thread::sleep(time::Duration::from_secs(3));

//Start a sub thread to run ztm-agent
let ztm_agent = LocalZTMAgent {
agent_port: option.ztm_agent_port,
};
thread::sleep(time::Duration::from_secs(3));

match ztm_agent.get_ztm_endpoints().await {
Ok(ztm_ep_list) => {
tracing::info!("ztm agent connect success");
tracing::info!("{} online endpoints", ztm_ep_list.len());
}
Err(_) => {
tracing::error!("ztm agent connect failed");
}
}

//Start relay server
run_relay_server(config, option).await;
}
Expand Down
67 changes: 67 additions & 0 deletions aries/src/service/api/ca_router.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use axum::{
body::{to_bytes, Body},
extract::{Query, State},
http::{Request, Response, StatusCode, Uri},
routing::get,
Router,
};
use gemini::RelayGetParams;
use regex::Regex;

use crate::service::relay_server::AppState;

pub fn routers() -> Router<AppState> {
Router::new().route("/{*path}", get(get_method_router).post(post_method_router))
}

async fn get_method_router(
_state: State<AppState>,
Query(_params): Query<RelayGetParams>,
uri: Uri,
) -> Result<Response<Body>, (StatusCode, String)> {
if Regex::new(r"/certificates/[a-zA-Z0-9]+$")
.unwrap()
.is_match(uri.path())
{
let name = match gemini::ca::server::get_cert_name_from_path(uri.path()) {
Some(n) => n,
None => {
return Err((StatusCode::BAD_REQUEST, "Bad request".to_string()));
}
};
return match gemini::ca::server::get_certificate(name).await {
Ok(cert) => Ok(Response::builder().body(Body::from(cert)).unwrap()),
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
};
}
Err((
StatusCode::NOT_FOUND,
String::from("Operation not supported\n"),
))
}

async fn post_method_router(
_state: State<AppState>,
uri: Uri,
req: Request<Body>,
) -> Result<Response<Body>, (StatusCode, String)> {
if Regex::new(r"/certificates/[a-zA-Z0-9]+$")
.unwrap()
.is_match(uri.path())
{
let name = match gemini::ca::server::get_cert_name_from_path(uri.path()) {
Some(n) => n,
None => return Err((StatusCode::BAD_REQUEST, "Bad request".to_string())),
};
let bytes = to_bytes(req.into_body(), usize::MAX).await.unwrap();
let csr = String::from_utf8(bytes.to_vec()).unwrap();
return match gemini::ca::server::issue_certificate(name, csr).await {
Ok(cert) => Ok(Response::builder().body(Body::from(cert)).unwrap()),
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
};
}
Err((
StatusCode::NOT_FOUND,
String::from("Operation not supported\n"),
))
}
1 change: 1 addition & 0 deletions aries/src/service/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod nostr_router;
pub mod ca_router;

#[cfg(test)]
mod tests {}
17 changes: 8 additions & 9 deletions aries/src/service/api/nostr_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use gemini::nostr::{
};
use jupiter::storage::ztm_storage::ZTMStorage;
use serde_json::Value;
use tokio::task;
use uuid::Uuid;

use crate::service::{relay_server::AppState, Req};
Expand Down Expand Up @@ -70,13 +69,13 @@ async fn recieve(
storage.insert_nostr_event(ztm_nostr_event).await.unwrap();

//Event is forwarded to subscribed nodes
let nostr_event_clone = nostr_event.clone();
let storage_clone = storage.clone();
let ztm_agent_port = state.relay_option.clone().ztm_agent_port;
task::spawn(async move {
transfer_event_to_subscribed_nodes(storage_clone, nostr_event_clone, ztm_agent_port)
.await
});
// let nostr_event_clone = nostr_event.clone();
// let storage_clone = storage.clone();
// let ztm_agent_port = state.relay_option.clone().ztm_agent_port;
// task::spawn(async move {
// transfer_event_to_subscribed_nodes(storage_clone, nostr_event_clone, ztm_agent_port)
// .await
// });

let res = RelayMessage::new_ok(nostr_event.id, true, "ok".to_string());
let value = serde_json::to_value(res).unwrap();
Expand Down Expand Up @@ -114,7 +113,7 @@ async fn recieve(
}
}

async fn transfer_event_to_subscribed_nodes(
async fn _transfer_event_to_subscribed_nodes(
storage: ZTMStorage,
nostr_event: NostrEvent,
ztm_agent_port: u16,
Expand Down
144 changes: 0 additions & 144 deletions aries/src/service/ca_server.rs

This file was deleted.

1 change: 0 additions & 1 deletion aries/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use gemini::nostr::client_message::Filter;
use serde::{Deserialize, Serialize};

pub mod api;
pub mod ca_server;
pub mod relay_server;

#[derive(Serialize, Deserialize, Debug, Clone)]
Expand Down
Loading

1 comment on commit b9342cf

@vercel
Copy link

@vercel vercel bot commented on b9342cf Feb 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

mega – ./

mega-gitmono.vercel.app
mega-git-main-gitmono.vercel.app
gitmega.dev
www.gitmega.dev

Please sign in to comment.