mirror of
https://github.com/EasyTier/EasyTier.git
synced 2026-10-10 04:36:14 -08:00
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
```
49 lines
1.4 KiB
Rust
49 lines
1.4 KiB
Rust
use std::sync::Arc;
|
|
|
|
use crate::{common::scoped_task::ScopedTask, tunnel::TunnelConnector};
|
|
|
|
pub mod controller;
|
|
pub mod session;
|
|
|
|
pub struct WebClient {
|
|
controller: Arc<controller::Controller>,
|
|
tasks: ScopedTask<()>,
|
|
}
|
|
|
|
impl WebClient {
|
|
pub fn new<T: TunnelConnector + 'static, S: ToString>(connector: T, token: S) -> Self {
|
|
let controller = Arc::new(controller::Controller::new(token.to_string()));
|
|
|
|
let controller_clone = controller.clone();
|
|
let tasks = ScopedTask::from(tokio::spawn(async move {
|
|
Self::routine(controller_clone, Box::new(connector)).await;
|
|
}));
|
|
|
|
WebClient { controller, tasks }
|
|
}
|
|
|
|
async fn routine(
|
|
controller: Arc<controller::Controller>,
|
|
mut connector: Box<dyn TunnelConnector>,
|
|
) {
|
|
loop {
|
|
let conn = match connector.connect().await {
|
|
Ok(conn) => conn,
|
|
Err(e) => {
|
|
println!(
|
|
"Failed to connect to the server ({}), retrying in 5 seconds...",
|
|
e
|
|
);
|
|
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
|
continue;
|
|
}
|
|
};
|
|
|
|
println!("Successfully connected to {:?}", conn.info());
|
|
|
|
let mut session = session::Session::new(conn, controller.clone());
|
|
session.wait().await;
|
|
}
|
|
}
|
|
}
|