-
Notifications
You must be signed in to change notification settings - Fork 9
feat(netwatch): Add browser support for rough network changes (online/offline) #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2e21113
Use `n0-future` in netwatch
matheus23 956e00d
feat(netwatch): Allow compiling to Wasm
matheus23 6e5616c
feat(netwatch): Implement checking `navigator.onLine` and reacting to…
matheus23 e382353
Fix errors
matheus23 451d4f6
Fix runtime errors
matheus23 394a13c
Export `ip` module, but without `LocalAddresses`
matheus23 c353457
Fix unused import
matheus23 c68d57c
Merge remote-tracking branch 'origin/main' into matheus23/netwatch-wasm
matheus23 a1410ad
code review: use `std::future::ready`
matheus23 addec6e
Add a smoke test for netwatch wasm
matheus23 7da1bd1
Disable `LocalAddresses` test in Wasm
matheus23 b9d3774
Add `wasm_browser` cfg alias
matheus23 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| use std::{collections::HashMap, fmt}; | ||
|
|
||
| use js_sys::{JsString, Reflect}; | ||
|
|
||
| pub const BROWSER_INTERFACE: &str = "browserif"; | ||
|
|
||
| /// Represents a network interface. | ||
| #[derive(Debug, PartialEq, Eq)] | ||
| pub struct Interface { | ||
| is_up: bool, | ||
| } | ||
|
|
||
| impl fmt::Display for Interface { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| write!(f, "navigator.onLine={}", self.is_up) | ||
| } | ||
| } | ||
|
|
||
| impl Interface { | ||
| async fn new() -> Self { | ||
| let is_up = Self::is_up(); | ||
| tracing::debug!(onLine = is_up, "Fetched globalThis.navigator.onLine"); | ||
| Self { | ||
| is_up: is_up.unwrap_or(true), | ||
| } | ||
| } | ||
|
|
||
| fn is_up() -> Option<bool> { | ||
| let navigator = Reflect::get( | ||
| js_sys::global().as_ref(), | ||
| JsString::from("navigator").as_ref(), | ||
| ) | ||
| .ok()?; | ||
|
|
||
| let is_up = Reflect::get(&navigator, JsString::from("onLine").as_ref()).ok()?; | ||
|
|
||
| is_up.as_bool() | ||
| } | ||
|
|
||
| /// The name of the interface. | ||
| pub(crate) fn name(&self) -> &str { | ||
| BROWSER_INTERFACE | ||
| } | ||
| } | ||
|
|
||
| /// Intended to store the state of the machine's network interfaces, routing table, and | ||
| /// other network configuration. For now it's pretty basic. | ||
| #[derive(Debug, PartialEq, Eq)] | ||
| pub struct State { | ||
| /// Maps from an interface name interface. | ||
| pub interfaces: HashMap<String, Interface>, | ||
|
|
||
| /// Whether this machine has an IPv6 Global or Unique Local Address | ||
| /// which might provide connectivity. | ||
| pub have_v6: bool, | ||
|
|
||
| /// Whether the machine has some non-localhost, non-link-local IPv4 address. | ||
| pub have_v4: bool, | ||
|
|
||
| //// Whether the current network interface is considered "expensive", which currently means LTE/etc | ||
| /// instead of Wifi. This field is not populated by `get_state`. | ||
| pub(crate) is_expensive: bool, | ||
|
|
||
| /// The interface name for the machine's default route. | ||
| /// | ||
| /// It is not yet populated on all OSes. | ||
| /// | ||
| /// When set, its value is the map key into `interface` and `interface_ips`. | ||
| pub(crate) default_route_interface: Option<String>, | ||
|
|
||
| /// The HTTP proxy to use, if any. | ||
| pub(crate) http_proxy: Option<String>, | ||
|
|
||
| /// The URL to the Proxy Autoconfig URL, if applicable. | ||
| pub(crate) pac: Option<String>, | ||
| } | ||
|
|
||
| impl fmt::Display for State { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| for iface in self.interfaces.values() { | ||
| write!(f, "{iface}")?; | ||
| if let Some(ref default_if) = self.default_route_interface { | ||
| if iface.name() == default_if { | ||
| write!(f, " (default)")?; | ||
| } | ||
| } | ||
| if f.alternate() { | ||
| writeln!(f)?; | ||
| } else { | ||
| write!(f, "; ")?; | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| impl State { | ||
| /// Returns the state of all the current machine's network interfaces. | ||
| /// | ||
| /// It does not set the returned `State.is_expensive`. The caller can populate that. | ||
| pub async fn new() -> Self { | ||
| let mut interfaces = HashMap::new(); | ||
| let have_v6 = false; | ||
| let have_v4 = false; | ||
|
|
||
| interfaces.insert(BROWSER_INTERFACE.to_string(), Interface::new().await); | ||
|
|
||
| State { | ||
| interfaces, | ||
| have_v4, | ||
| have_v6, | ||
| is_expensive: false, | ||
| default_route_interface: Some(BROWSER_INTERFACE.to_string()), | ||
| http_proxy: None, | ||
| pac: None, | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,16 @@ | ||
| //! Networking related utilities | ||
|
|
||
| #[cfg_attr( | ||
| all(target_family = "wasm", target_os = "unknown"), | ||
| path = "interfaces/wasm_browser.rs" | ||
| )] | ||
| pub mod interfaces; | ||
| pub mod ip; | ||
| mod ip_family; | ||
| pub mod netmon; | ||
| #[cfg(not(all(target_family = "wasm", target_os = "unknown")))] | ||
| mod udp; | ||
|
|
||
| pub use self::{ip_family::IpFamily, udp::UdpSocket}; | ||
| pub use self::ip_family::IpFamily; | ||
| #[cfg(not(all(target_family = "wasm", target_os = "unknown")))] | ||
| pub use self::udp::UdpSocket; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.