-
Notifications
You must be signed in to change notification settings - Fork 363
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
Rust language reimplementation #105
Conversation
fc3e475
to
1a31569
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looking good!
Cargo.toml
Outdated
@@ -0,0 +1,28 @@ | |||
[package] | |||
name = "resin-wifi-connect" | |||
version = "0.1.0" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the version should match
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will place a matching "2.0.8"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@majorz this will be 3.0.0
- the last coffeescript version we release will be 2.0.8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@josephroberts, it will be bumped to 3.0.0
by VersionBot automatically when 104-rust
lands in master
. So I think it is better to keep the Cargo version with the changelog one in sync.
Cargo.toml
Outdated
[package] | ||
name = "resin-wifi-connect" | ||
version = "0.1.0" | ||
authors = ["Joe Roberts <joe@resin.io>"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add yourself
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:D
env_logger = "0.4" | ||
|
||
[dependencies.network_manager] | ||
git = "https://github.com/resin-io-modules/network_manager" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should release network_managed as a crate before merging this in
balena-io-modules/network-manager#57
balena-io-modules/network-manager#56
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good - let's leave that for a next PR
|
||
let interface: Option<String> = matches.value_of("interface").map_or_else( | ||
|| { | ||
env::var("PORTAL_INTERFACE").ok() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is there no default?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If no device is specified, we default to the first Wi-Fi device. Do we really need to default to 'wlan0'? For example on my laptop the internal one is called 'wlo1' and this one makes more sense as a default one for my system. Or I am missing something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, that makes total sense. ignore me :)
|
||
let password: Option<String> = matches.value_of("password").map_or_else( | ||
|| { | ||
env::var("PORTAL_PASSPHRASE").ok() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is there no default?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is the same as in the current implementation - the default is for creating an open hotspot.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
- if [ $TRAVIS_RUST_VERSION == nightly ]; then | ||
cargo clippy -- -D warnings; | ||
fi | ||
- cargo build |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
travis should build for the required architectures, push to S3 and create the github releases
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to update the docker file to always pull the latest release for the correct architecture. If this PR was merged today it would break resin-wifi-connect?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to update the readme. We also need to release the 2.x version with the warnings we spoke about in the arch call before this can be merged
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we talked on the chat about those items - the PR will go into 104-rust
, not master
and we will make the changes in consecutive PRs.
@@ -0,0 +1,1003 @@ | |||
[root] | |||
name = "resin-wifi-connect" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should a binary have this lock file? I thought it was only recommended for libraries
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is the other way around: http://doc.crates.io/faq.html#why-do-binaries-have-cargolock-in-version-control-but-not-libraries
hotspot_connection = None; | ||
} | ||
|
||
let access_points = match get_access_points(&device) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is some code duplication throughout, for example this section is repeated on line 95. It would be good to go through and try to extract these parts into functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have used this for go code - not sure if there is something similar for rust. I am surprised clippy does not catch this to be honest
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are actually some small changes (like a missing let
in one of the cases) and maybe this is why clippy does not catch it. It is still a duplication, but I suggest that we leave it like that for now and address it all at once when we refactor that whole function with a better design.
unsafe impl Send for RequestSharedState {} | ||
unsafe impl Sync for RequestSharedState {} | ||
|
||
#[derive(Debug)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we remove this, you have the display trait implemented below
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to remove it and the compiler complains with required by "std::error::Error"
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, any idea why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Error trait is defined as dependent on the Debug and Display traits: pub trait Error: Debug + Display {...}
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A good start! Appears to be coming along nicely. :)
|
||
use config::Config; | ||
|
||
pub fn start_dnsmasq(config: &Config, device: &Device) -> Result<Child, String> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String
errors make me sad. Have you looked at using error_chain
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We discussed that earlier on during one of the network_manager PRs, but left it for a later stage. @josephroberts, what's your opinion on the improved error reporting - should we pursue that before release or after?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should release now and have this as one of the first things we "fix" - unless its like a couple of hours of work in which case we should do it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely it will take more time to get it done in both repos. We will address it later on then.
src/network.rs
Outdated
ssid, | ||
password, | ||
} => { | ||
if hotspot_connection.is_some() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be an if let Some(value)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice catch!
src/network.rs
Outdated
let command = match network_rx.recv() { | ||
Ok(command) => command, | ||
Err(e) => { | ||
return exit_with_error( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think instead of calling exit_with_error
all over the place, it might make sense to convert all this main loop logic into a dispatch function that returns a Result
. You can then use the handy ?
operator inside the function to avoid all the match
ing. The main loop can then check for an Err
result and send the exit command.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I am not happy with that long function either. It definitely needs a better design and I was thinking that we can address that in the next development phase. Joe had some ideas about incorporating some form of state machine as well. @josephroberts, what's your opinion on this - should we try to come up with a better design for the network related thread function, or we should address that at a post-3.0 phase?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
address it later on
ssid: &str, | ||
gateway: &Ipv4Addr, | ||
password: &Option<&str>, | ||
) -> Result<Connection, String> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the rustfmt
approved way of formatting a function? It looks really wonky to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is how rustfmt
is configured now for us to avoid right-side drifting (I am not really sure what is the current rustfmt
default, since they improved many aspects of those recently). We need to catch up with rustfmt
soon, since they introduced a new crate that runs on nightly only, which will be the maintained version: http://www.ncameron.org/blog/rustfmt-changes/
src/server.rs
Outdated
format!("{}", request_state.gateway) | ||
}; | ||
|
||
let host = req.headers.get::<headers::Host>().unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could a client crash the server by not sending a Host
header?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another nice catch, will fix that.
src/server.rs
Outdated
let host = req.headers.get::<headers::Host>().unwrap(); | ||
|
||
if host.hostname != gateway { | ||
let url = Url::parse(&format!("http://{}/", gateway)).unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same with sending a malformed Url
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will fix that too :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked at this one and it will actually never fail, since the gateway is already parsed Ipv4Addr instance.
I updated the source code and recommitted with all the changes discussed above. Additionally I ported the following functionality from the existing implementation:
|
src/config.rs
Outdated
const DEFAULT_GATEWAY: &str = "192.168.42.1"; | ||
const DEFAULT_DHCP_RANGE: &str = "192.168.42.2,192.168.42.254"; | ||
const DEFAULT_SSID: &str = "ResinAP"; | ||
const DEFAULT_TIMEOUT: &str = "15000"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should specify that this timeout is in ms, either with a comment or by naming the variable DEFAULT_TIMEOUT_MS
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I changed it to DEFAULT_TIMEOUT_MS
.
|
||
pub fn get_config() -> Config { | ||
let matches = App::new(env!("CARGO_PKG_NAME")) | ||
.version(env!("CARGO_PKG_VERSION")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't know about CARGO_PKG_...
- very nice :)
unsafe impl Send for RequestSharedState {} | ||
unsafe impl Sync for RequestSharedState {} | ||
|
||
#[derive(Debug)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, any idea why?
a82d236
to
c5c6a55
Compare
Connects-To: #104 Change-Type: major
In addition to the When clippy was compiled with an older Rust nightly (like from the previous day), but no new clippy version was found, then clippy is not working with the new Rust nightly (because it links to the compiled Rust binary libs). In that case although a new clippy version is not available, we still need to recompile the current clippy version towards the new nightly. The check I did is run |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
Connects-To: #104
Change-Type: minor
---- Autogenerated Waffleboard Connection: Connects to #104