a78b759741
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
```
114 lines
2.8 KiB
Rust
114 lines
2.8 KiB
Rust
include!(concat!(env!("OUT_DIR"), "/cli.rs"));
|
|
|
|
impl PeerRoutePair {
|
|
pub fn get_latency_ms(&self) -> Option<f64> {
|
|
let mut ret = u64::MAX;
|
|
let p = self.peer.as_ref()?;
|
|
for conn in p.conns.iter() {
|
|
let Some(stats) = &conn.stats else {
|
|
continue;
|
|
};
|
|
ret = ret.min(stats.latency_us);
|
|
}
|
|
|
|
if ret == u64::MAX {
|
|
None
|
|
} else {
|
|
Some(f64::from(ret as u32) / 1000.0)
|
|
}
|
|
}
|
|
|
|
pub fn get_rx_bytes(&self) -> Option<u64> {
|
|
let mut ret = 0;
|
|
let p = self.peer.as_ref()?;
|
|
for conn in p.conns.iter() {
|
|
let Some(stats) = &conn.stats else {
|
|
continue;
|
|
};
|
|
ret += stats.rx_bytes;
|
|
}
|
|
|
|
if ret == 0 {
|
|
None
|
|
} else {
|
|
Some(ret)
|
|
}
|
|
}
|
|
|
|
pub fn get_tx_bytes(&self) -> Option<u64> {
|
|
let mut ret = 0;
|
|
let p = self.peer.as_ref()?;
|
|
for conn in p.conns.iter() {
|
|
let Some(stats) = &conn.stats else {
|
|
continue;
|
|
};
|
|
ret += stats.tx_bytes;
|
|
}
|
|
|
|
if ret == 0 {
|
|
None
|
|
} else {
|
|
Some(ret)
|
|
}
|
|
}
|
|
|
|
pub fn get_loss_rate(&self) -> Option<f64> {
|
|
let mut ret = 0.0;
|
|
let p = self.peer.as_ref()?;
|
|
for conn in p.conns.iter() {
|
|
ret += conn.loss_rate;
|
|
}
|
|
|
|
if ret == 0.0 {
|
|
None
|
|
} else {
|
|
Some(ret as f64)
|
|
}
|
|
}
|
|
|
|
pub fn get_conn_protos(&self) -> Option<Vec<String>> {
|
|
let mut ret = vec![];
|
|
let p = self.peer.as_ref()?;
|
|
for conn in p.conns.iter() {
|
|
let Some(tunnel_info) = &conn.tunnel else {
|
|
continue;
|
|
};
|
|
// insert if not exists
|
|
if !ret.contains(&tunnel_info.tunnel_type) {
|
|
ret.push(tunnel_info.tunnel_type.clone());
|
|
}
|
|
}
|
|
|
|
if ret.is_empty() {
|
|
None
|
|
} else {
|
|
Some(ret)
|
|
}
|
|
}
|
|
|
|
pub fn get_udp_nat_type(self: &Self) -> String {
|
|
use crate::proto::common::NatType;
|
|
let mut ret = NatType::Unknown;
|
|
if let Some(r) = &self.route.clone().unwrap_or_default().stun_info {
|
|
ret = NatType::try_from(r.udp_nat_type).unwrap();
|
|
}
|
|
format!("{:?}", ret)
|
|
}
|
|
}
|
|
|
|
pub fn list_peer_route_pair(peers: Vec<PeerInfo>, routes: Vec<Route>) -> Vec<PeerRoutePair> {
|
|
let mut pairs: Vec<PeerRoutePair> = vec![];
|
|
|
|
for route in routes.iter() {
|
|
let peer = peers.iter().find(|peer| peer.peer_id == route.peer_id);
|
|
let pair = PeerRoutePair {
|
|
route: Some(route.clone()),
|
|
peer: peer.cloned(),
|
|
};
|
|
|
|
pairs.push(pair);
|
|
}
|
|
|
|
pairs
|
|
}
|