Skip to content

Commit

Permalink
allow incoming requests from arbitrary ports (#78)
Browse files Browse the repository at this point in the history
closes #77
  • Loading branch information
feschber authored Jan 18, 2024
1 parent b3caba9 commit 6674af8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 33 deletions.
11 changes: 4 additions & 7 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ pub struct Client {
/// This way any event consumer / producer backend does not
/// need to know anything about a client other than its handle.
pub handle: ClientHandle,
/// all socket addresses associated with a particular client
/// all ip addresses associated with a particular client
/// e.g. Laptops usually have at least an ethernet and a wifi port
/// which have different ip addresses
pub addrs: HashSet<SocketAddr>,
pub ips: HashSet<IpAddr>,
/// both active_addr and addrs can be None / empty so port needs to be stored seperately
pub port: u16,
/// position of a client on screen
Expand Down Expand Up @@ -134,15 +134,12 @@ impl ClientManager {
// store fix ip addresses
let fix_ips = ips.iter().cloned().collect();

// map ip addresses to socket addresses
let addrs = HashSet::from_iter(ips.into_iter().map(|ip| SocketAddr::new(ip, port)));

// store the client
let client = Client {
hostname,
fix_ips,
handle,
addrs,
ips,
port,
pos,
};
Expand Down Expand Up @@ -173,7 +170,7 @@ impl ClientManager {
.iter()
.position(|c| {
if let Some(c) = c {
c.active && c.client.addrs.contains(&addr)
c.active && c.client.ips.contains(&addr.ip())
} else {
false
}
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub fn run() -> Result<()> {
client.hostname.as_deref().unwrap_or(""),
if active { "yes" } else { "no" },
client
.addrs
.ips
.into_iter()
.map(|a| a.to_string())
.collect::<Vec<String>>()
Expand Down
38 changes: 13 additions & 25 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,19 +247,11 @@ impl Server {
}
};
if let Some(state) = server.client_manager.borrow_mut().get_mut(client) {
let port = state.client.port;
let mut addrs = HashSet::from_iter(
state
.client
.fix_ips
.iter()
.map(|a| SocketAddr::new(*a, port)),
);
let mut addrs = HashSet::from_iter(state.client.fix_ips.iter().cloned());
for ip in ips {
let sock_addr = SocketAddr::new(ip, port);
addrs.insert(sock_addr);
addrs.insert(ip);
}
state.client.addrs = addrs;
state.client.ips = addrs;
}
}
});
Expand Down Expand Up @@ -347,10 +339,16 @@ impl Server {
.iter()
.flat_map(|&c| client_manager.get(c))
.flat_map(|state| {
if let Some(a) = state.active_addr {
vec![a]
if state.alive && state.active_addr.is_some() {
vec![state.active_addr.unwrap()]
} else {
state.client.addrs.iter().cloned().collect()
state
.client
.ips
.iter()
.cloned()
.map(|ip| SocketAddr::new(ip, state.client.port))
.collect()
}
})
.collect()
Expand Down Expand Up @@ -602,22 +600,12 @@ impl Server {
// update port
if state.client.port != port {
state.client.port = port;
state.client.addrs = state
.client
.addrs
.iter()
.cloned()
.map(|mut a| {
a.set_port(port);
a
})
.collect();
state.active_addr = state.active_addr.map(|a| SocketAddr::new(a.ip(), port));
}

// update hostname
if state.client.hostname != hostname {
state.client.addrs = HashSet::new();
state.client.ips = HashSet::new();
state.active_addr = None;
state.client.hostname = hostname;
}
Expand Down

0 comments on commit 6674af8

Please sign in to comment.