121 lines
3.6 KiB
Rust
121 lines
3.6 KiB
Rust
use std::error::Error;
|
|
|
|
use futures::prelude::*;
|
|
use libp2p::{
|
|
mdns,
|
|
request_response::{self, ProtocolSupport},
|
|
swarm::SwarmEvent,
|
|
};
|
|
use tracing_subscriber::EnvFilter;
|
|
|
|
use cll2v0::{
|
|
api, config, keys,
|
|
protocol::{mk_swarm, MyBehaviourEvent},
|
|
};
|
|
|
|
use clap::{Parser, Subcommand};
|
|
|
|
/// cll2v0 is a playground for rust libraries.
|
|
/// This is signing service.
|
|
#[derive(Parser)]
|
|
#[command(arg_required_else_help(true), version, about)]
|
|
struct Args {
|
|
#[command(subcommand)]
|
|
cmd: Option<Command>,
|
|
}
|
|
|
|
#[derive(Subcommand)]
|
|
enum Command {
|
|
/// Start server
|
|
Start(ServerParams),
|
|
}
|
|
|
|
#[derive(clap::Args, Debug)]
|
|
pub struct ServerParams {
|
|
/// Config file
|
|
#[arg(long, default_value = "./config.toml")]
|
|
pub config: String,
|
|
/// Server listening address
|
|
#[arg(long, default_value = "/ip4/0.0.0.0/tcp/52321")]
|
|
pub listen_on: String,
|
|
/// Signing key of server libp2p part
|
|
#[arg(
|
|
long,
|
|
default_value = "deadbeef00000000000000000000000000000000000000000000000000000000"
|
|
)]
|
|
pub skey: String,
|
|
}
|
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
|
match Args::parse().cmd {
|
|
Some(Command::Start(params)) => tokio::runtime::Builder::new_multi_thread()
|
|
.enable_all()
|
|
.build()
|
|
.unwrap()
|
|
.block_on(start(params)),
|
|
_ => {
|
|
panic!("oops")
|
|
}
|
|
}
|
|
}
|
|
|
|
pub async fn start(params: ServerParams) -> Result<(), Box<dyn Error>> {
|
|
let ServerParams {
|
|
config: config_path,
|
|
skey: skey_hex,
|
|
listen_on,
|
|
} = params;
|
|
|
|
let c = config::Config::read(&config_path)?;
|
|
println!("CONFIG ");
|
|
println!("{:?}", c);
|
|
if true {
|
|
let _ = tracing_subscriber::fmt()
|
|
.with_env_filter(EnvFilter::from_default_env())
|
|
.try_init();
|
|
}
|
|
let keypair = keys::libp2p_kp_from_hex(skey_hex)?;
|
|
let peer_id = libp2p_identity::PublicKey::from(keypair.public()).to_peer_id();
|
|
println!("PEER_ID : {}", peer_id);
|
|
|
|
// let (keychain, pool) = db::start_db().await?;
|
|
|
|
let mut swarm = mk_swarm(&keypair.clone().into(), ProtocolSupport::Inbound)?;
|
|
swarm.listen_on(listen_on.parse()?)?;
|
|
|
|
loop {
|
|
match swarm.select_next_some().await {
|
|
// MDNS BEHAVIOR
|
|
SwarmEvent::Behaviour(MyBehaviourEvent::Mdns(mdns::Event::Expired(list))) => {
|
|
for (peer_id, _multiaddr) in list {
|
|
println!("mDNS discover peer has expired: {peer_id}");
|
|
// swarm.behaviour_mut().gossipsub.remove_explicit_peer(&peer_id);
|
|
}
|
|
}
|
|
SwarmEvent::Behaviour(MyBehaviourEvent::ReqRes(request_response::Event::Message {
|
|
peer: _peer,
|
|
message:
|
|
libp2p::request_response::Message::Request {
|
|
request, channel, ..
|
|
},
|
|
})) => {
|
|
let res = match request {
|
|
api::Req::Global(api::global::Req::Terms) => {
|
|
api::Res::Global(api::global::Res::Terms(api::global::terms::Res::Okay(
|
|
api::global::terms::Terms::default(),
|
|
)))
|
|
}
|
|
api::Req::Channel => todo!("Not yet implemented"),
|
|
_ => api::Res::Global(api::global::Res::Terms(api::global::terms::Res::Okay(
|
|
api::global::terms::Terms::default(),
|
|
))),
|
|
};
|
|
let _ = swarm.behaviour_mut().req_res.send_response(channel, res);
|
|
}
|
|
e => {
|
|
println!("OTHER {:?}", e)
|
|
}
|
|
}
|
|
}
|
|
}
|