refactoring, more config, packaging

This commit is contained in:
John Smith
2022-05-16 11:52:48 -04:00
parent 444f65d76d
commit ef1f5d7b52
42 changed files with 1329 additions and 368 deletions

View File

@@ -79,10 +79,16 @@ impl VeilidAPI {
Ok(routing_table.debug_info_buckets(min_state))
}
async fn debug_dialinfo(&self, _args: String) -> Result<String, VeilidAPIError> {
async fn debug_dialinfo(&self, args: String) -> Result<String, VeilidAPIError> {
let args: Vec<String> = args.split_whitespace().map(|s| s.to_owned()).collect();
let is_txt = if args.len() == 1 {
args[0] == "txt"
} else {
false
};
// Dump routing table dialinfo
let routing_table = self.network_manager()?.routing_table();
Ok(routing_table.debug_info_dialinfo())
Ok(routing_table.debug_info_dialinfo(is_txt))
}
async fn debug_entries(&self, args: String) -> Result<String, VeilidAPIError> {
@@ -147,7 +153,7 @@ impl VeilidAPI {
// Must be detached
if !matches!(
self.get_state().await?.attachment,
self.get_state().await?.attachment.state,
AttachmentState::Detached
) {
return Err(VeilidAPIError::Internal {
@@ -168,7 +174,7 @@ impl VeilidAPI {
if args[0] == "buckets" {
// Must be detached
if matches!(
self.get_state().await?.attachment,
self.get_state().await?.attachment.state,
AttachmentState::Detached | AttachmentState::Detaching
) {
return Err(VeilidAPIError::Internal {
@@ -194,7 +200,7 @@ impl VeilidAPI {
async fn debug_attach(&self, _args: String) -> Result<String, VeilidAPIError> {
if !matches!(
self.get_state().await?.attachment,
self.get_state().await?.attachment.state,
AttachmentState::Detached
) {
return Err(VeilidAPIError::Internal {
@@ -209,7 +215,7 @@ impl VeilidAPI {
async fn debug_detach(&self, _args: String) -> Result<String, VeilidAPIError> {
if matches!(
self.get_state().await?.attachment,
self.get_state().await?.attachment.state,
AttachmentState::Detaching
) {
return Err(VeilidAPIError::Internal {

View File

@@ -167,22 +167,37 @@ impl VeilidLogLevel {
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VeilidStateLog {
pub log_level: VeilidLogLevel,
pub message: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VeilidStateAttachment {
pub state: AttachmentState,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VeilidStateNetwork {
pub started: bool,
pub bps_down: u64,
pub bps_up: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind")]
pub enum VeilidUpdate {
Log {
log_level: VeilidLogLevel,
message: String,
},
Attachment {
state: AttachmentState,
},
Log(VeilidStateLog),
Attachment(VeilidStateAttachment),
Network(VeilidStateNetwork),
Shutdown,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VeilidState {
pub attachment: AttachmentState,
pub attachment: VeilidStateAttachment,
pub network: VeilidStateNetwork,
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -745,8 +760,37 @@ impl fmt::Display for DialInfo {
match self {
DialInfo::UDP(di) => write!(f, "udp|{}", di.socket_address),
DialInfo::TCP(di) => write!(f, "tcp|{}", di.socket_address),
DialInfo::WS(di) => write!(f, "ws|{}|{}", di.socket_address, di.request),
DialInfo::WSS(di) => write!(f, "wss|{}|{}", di.socket_address, di.request),
DialInfo::WS(di) => {
let url = format!("ws://{}", di.request);
let split_url = SplitUrl::from_str(&url).unwrap();
match split_url.host {
SplitUrlHost::Hostname(_) => {
write!(f, "ws|{}|{}", di.socket_address.to_ip_addr(), di.request)
}
SplitUrlHost::IpAddr(a) => {
if di.socket_address.to_ip_addr() == a {
write!(f, "ws|{}", di.request)
} else {
panic!("resolved address does not match url: {}", di.request);
}
}
}
}
DialInfo::WSS(di) => {
let url = format!("wss://{}", di.request);
let split_url = SplitUrl::from_str(&url).unwrap();
match split_url.host {
SplitUrlHost::Hostname(_) => {
write!(f, "wss|{}|{}", di.socket_address.to_ip_addr(), di.request)
}
SplitUrlHost::IpAddr(_) => {
panic!(
"secure websockets can not use ip address in request: {}",
di.request
);
}
}
}
}
}
}
@@ -767,18 +811,50 @@ impl FromStr for DialInfo {
Ok(DialInfo::tcp(socket_address))
}
"ws" => {
let (sa, rest) = rest.split_once('|').ok_or_else(|| {
parse_error!("DialInfo::from_str missing socket address '|' separator", s)
})?;
let socket_address = SocketAddress::from_str(sa)?;
DialInfo::try_ws(socket_address, format!("ws://{}", rest))
let url = format!("ws://{}", rest);
let split_url = SplitUrl::from_str(&url)
.map_err(|e| parse_error!(format!("unable to split WS url: {}", e), url))?;
if split_url.scheme != "ws" || !url.starts_with("ws://") {
return Err(parse_error!("incorrect scheme for WS dialinfo", url));
}
let url_port = split_url.port.unwrap_or(80u16);
match rest.split_once('|') {
Some((sa, rest)) => {
let address = Address::from_str(sa)?;
DialInfo::try_ws(
SocketAddress::new(address, url_port),
format!("ws://{}", rest),
)
}
None => {
let address = Address::from_str(&split_url.host.to_string())?;
DialInfo::try_ws(
SocketAddress::new(address, url_port),
format!("ws://{}", rest),
)
}
}
}
"wss" => {
let (sa, rest) = rest.split_once('|').ok_or_else(|| {
let url = format!("wss://{}", rest);
let split_url = SplitUrl::from_str(&url)
.map_err(|e| parse_error!(format!("unable to split WSS url: {}", e), url))?;
if split_url.scheme != "wss" || !url.starts_with("wss://") {
return Err(parse_error!("incorrect scheme for WSS dialinfo", url));
}
let url_port = split_url.port.unwrap_or(443u16);
let (a, rest) = rest.split_once('|').ok_or_else(|| {
parse_error!("DialInfo::from_str missing socket address '|' separator", s)
})?;
let socket_address = SocketAddress::from_str(sa)?;
DialInfo::try_wss(socket_address, format!("wss://{}", rest))
let address = Address::from_str(a)?;
DialInfo::try_wss(
SocketAddress::new(address, url_port),
format!("wss://{}", rest),
)
}
_ => Err(parse_error!("DialInfo::from_str has invalid scheme", s)),
}
@@ -819,6 +895,14 @@ impl DialInfo {
url
));
}
if let SplitUrlHost::IpAddr(a) = split_url.host {
if socket_address.to_ip_addr() != a {
return Err(parse_error!(
format!("request address does not match socket address: {}", a),
socket_address
));
}
}
Ok(Self::WS(DialInfoWS {
socket_address: socket_address.to_canonical(),
request: url[5..].to_string(),
@@ -1497,11 +1581,19 @@ impl VeilidAPI {
// get a full copy of the current state
pub async fn get_state(&self) -> Result<VeilidState, VeilidAPIError> {
let attachment_manager = self.attachment_manager()?;
let network_manager = attachment_manager.network_manager();
let attachment = attachment_manager.get_veilid_state();
let network = network_manager.get_veilid_state();
Ok(VeilidState {
attachment: attachment_manager.get_state(),
attachment,
network,
})
}
// get network connectedness
// connect to the network
pub async fn attach(&self) -> Result<(), VeilidAPIError> {
let attachment_manager = self.attachment_manager()?;