Handle UDP/TCP

This commit is contained in:
Quentin 2020-05-22 16:27:43 +02:00
parent ec777652c1
commit 5fafc1cb64
3 changed files with 25 additions and 11 deletions

View file

@ -59,6 +59,8 @@ To test the Consul Catalog part, you can do:
```bash
consul agent -dev #in a separate terminal, if not already running
consul services register -name=example -port=1337 -tag="(diplonat (port 1337) (port 1338 1339))"
consul services register -name=fake_leet -tag="(diplonat (tcp_port 1337) (tcp_port 1338 1339))"
consul services register -name=fake_dns -tag="(diplonat (udp_port 53) (tcp_port 53))"
consul services register -name=fake_irc -tag="(diplonat (udp_port 6667 6666))"
consul services -id=example
```

View file

@ -11,7 +11,8 @@ use crate::consul;
#[derive(Serialize, Deserialize, Debug)]
pub enum DiplonatParameter {
port(Vec<u16>)
tcp_port(Vec<u16>),
udp_port(Vec<u16>)
}
#[derive(Serialize, Deserialize, Debug)]
@ -20,12 +21,12 @@ pub enum DiplonatConsul {
}
pub struct ConsulActor {
pub rx_open_ports: watch::Receiver<messages::OpenPorts>,
pub rx_open_ports: watch::Receiver<messages::PublicExposedPorts>,
consul: consul::Consul,
node: String,
retries: u32,
tx_open_ports: watch::Sender<messages::OpenPorts>
tx_open_ports: watch::Sender<messages::PublicExposedPorts>
}
fn retry_to_time(retries: u32, max_time: Duration) -> Duration {
@ -34,8 +35,12 @@ fn retry_to_time(retries: u32, max_time: Duration) -> Duration {
return Duration::from_secs(cmp::min(max_time.as_secs(), 1.2f64.powf(retries as f64) as u64))
}
fn from_catalog_to_open_ports(catalog: &consul::CatalogNode) -> messages::OpenPorts {
let mut op = messages::OpenPorts { ports: Vec::new() };
fn from_catalog_to_open_ports(catalog: &consul::CatalogNode) -> messages::PublicExposedPorts {
let mut op = messages::PublicExposedPorts {
tcp_ports: Vec::new(),
udp_ports: Vec::new()
};
for (_, service_info) in &catalog.Services {
for tag in &service_info.Tags {
let diplo_conf: error::Result<DiplonatConsul> = from_str(tag);
@ -43,8 +48,10 @@ fn from_catalog_to_open_ports(catalog: &consul::CatalogNode) -> messages::OpenPo
Ok(conf) => {
let DiplonatConsul::diplonat(c) = conf;
for parameter in &c {
let DiplonatParameter::port(p) = parameter;
op.ports.extend(p);
match parameter {
DiplonatParameter::tcp_port(p) => op.tcp_ports.extend(p),
DiplonatParameter::udp_port(p) => op.udp_ports.extend(p),
}
}
}
Err(e) => debug!("Failed to parse entry {}. {}", tag, e),
@ -56,7 +63,11 @@ fn from_catalog_to_open_ports(catalog: &consul::CatalogNode) -> messages::OpenPo
impl ConsulActor {
pub fn new(url: &str, node: &str) -> Self {
let (tx, rx) = watch::channel(messages::OpenPorts{ports: Vec::new() });
let (tx, rx) = watch::channel(messages::PublicExposedPorts{
tcp_ports: Vec::new(),
udp_ports: Vec::new()
});
return Self {
consul: consul::Consul::new(url),
rx_open_ports: rx,

View file

@ -1,4 +1,5 @@
#[derive(Debug, Clone)]
pub struct OpenPorts {
pub ports: Vec<u16>
pub struct PublicExposedPorts {
pub tcp_ports: Vec<u16>,
pub udp_ports: Vec<u16>
}