feat/web (Patchset 2) (#444)

This patch implement a restful server without any auth.

usage:

```bash
# run easytier-web, which acts as an gateway and registry for all easytier-core
$> easytier-web

# run easytier-core and connect to easytier-web with a token
$> easytier-core --config-server udp://127.0.0.1:22020/fdsafdsa

# use restful api to list session
$> curl -H "Content-Type: application/json" -X GET 127.0.0.1:11211/api/v1/sessions
[{"token":"fdsafdsa","client_url":"udp://127.0.0.1:48915","machine_id":"de3f5b8f-0f2f-d9d0-fb30-a2ac8951d92f"}]%

# use restful api to run a network instance
$> curl -H "Content-Type: application/json" -X POST 127.0.0.1:11211/api/v1/network/de3f5b8f-0f2f-d9d0-fb30-a2ac8951d92f -d '{"config": "listeners = [\"udp://0.0.0.0:12344\"]"}'

# use restful api to get network instance info
$> curl -H "Content-Type: application/json" -X GET 127.0.0.1:11211/api/v1/network/de3f5b8f-0f2f-d9d0-fb30-a2ac8951d92f/65437e50-b286-4098-a624-74429f2cb839 
```
This commit is contained in:
Sijie.Sun
2024-10-26 00:04:22 +08:00
committed by GitHub
parent b5c3726e67
commit a78b759741
33 changed files with 1539 additions and 263 deletions
+18 -1
View File
@@ -1,9 +1,10 @@
use std::sync::{Arc, Mutex};
use std::sync::{atomic::AtomicBool, Arc, Mutex};
use futures::{SinkExt as _, StreamExt};
use tokio::{task::JoinSet, time::timeout};
use crate::{
defer,
proto::rpc_types::error::Error,
tunnel::{packet_def::PacketType, ring::create_ring_tunnel_pair, Tunnel},
};
@@ -17,6 +18,7 @@ pub struct BidirectRpcManager {
rx_timeout: Option<std::time::Duration>,
error: Arc<Mutex<Option<Error>>>,
tunnel: Mutex<Option<Box<dyn Tunnel>>>,
running: Arc<AtomicBool>,
tasks: Mutex<Option<JoinSet<()>>>,
}
@@ -30,6 +32,7 @@ impl BidirectRpcManager {
rx_timeout: None,
error: Arc::new(Mutex::new(None)),
tunnel: Mutex::new(None),
running: Arc::new(AtomicBool::new(false)),
tasks: Mutex::new(None),
}
@@ -50,6 +53,8 @@ impl BidirectRpcManager {
let mut tasks = JoinSet::new();
self.rpc_client.run();
self.rpc_server.run();
self.running
.store(true, std::sync::atomic::Ordering::Relaxed);
let (server_tx, mut server_rx) = (
self.rpc_server.get_transport_sink(),
@@ -64,7 +69,11 @@ impl BidirectRpcManager {
self.tunnel.lock().unwrap().replace(inner);
let e_clone = self.error.clone();
let r_clone = self.running.clone();
tasks.spawn(async move {
defer! {
r_clone.store(false, std::sync::atomic::Ordering::Relaxed);
}
loop {
let packet = tokio::select! {
Some(Ok(packet)) = server_rx.next() => {
@@ -90,7 +99,11 @@ impl BidirectRpcManager {
let recv_timeout = self.rx_timeout;
let e_clone = self.error.clone();
let r_clone = self.running.clone();
tasks.spawn(async move {
defer! {
r_clone.store(false, std::sync::atomic::Ordering::Relaxed);
}
loop {
let ret = if let Some(recv_timeout) = recv_timeout {
match timeout(recv_timeout, inner_rx.next()).await {
@@ -161,4 +174,8 @@ impl BidirectRpcManager {
tasks.abort_all();
}
}
pub fn is_running(&self) -> bool {
self.running.load(std::sync::atomic::Ordering::Relaxed)
}
}