Skip to content

Commit

Permalink
check password before login
Browse files Browse the repository at this point in the history
  • Loading branch information
NaThousand committed May 24, 2024
1 parent cd02d42 commit 47e95e9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/net/bupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ impl fmt::Debug for BuptAccount {
}
}

const CHECK_URL: &str = "http://connect.rom.miui.com/generate_204?cmd=redirect&arubalp=12345";
pub const CHECK_URL: &str = "http://connect.rom.miui.com/generate_204?cmd=redirect&arubalp=12345";

enum BuptNetStatus {
pub enum BuptNetStatus {
Authenticated,
NotAuthenticated(Option<String>),
}

fn check(url: impl AsRef<str>) -> Result<BuptNetStatus> {
pub fn check(url: impl AsRef<str>) -> Result<BuptNetStatus> {
log::debug!("checking bupt network status with url: {}", url.as_ref());
let connection = EspHttpConnection::new(&Configuration {
follow_redirects_policy: FollowRedirectsPolicy::FollowNone,
Expand Down
69 changes: 53 additions & 16 deletions src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,31 @@ fn connect_wifi_with_config(
log::info!("Retrying...");
}
}

log::info!("Waiting for DHCP lease...");
wifi.wait_netif_up()?;

let ip_info = wifi.wifi().sta_netif().get_ip_info()?;
log::info!("Wifi DHCP info: {:?}", ip_info);

if let Some(account) = bupt_account {

for retry in 0..10 {
match bupt::login(&account) {
Ok(_) => break,

Err(e) => {
log::warn!(
if e.to_string().contains("incorrect password") {

This comment has been minimized.

Copy link
@YouXam

YouXam May 24, 2024

Member

我记得密码错误的报错应该不是这个

log::error!("Failed to login to BUPT-portal: {}, retrying with new password",
e);
bail!("Failed to login to BUPT-portal because of wrong password");
//密码错误 重新配网

}
else {
log::warn!(
"Failed to login to BUPT-portal: {}, will retry after 10 seconds...",
e
);
);
}
}
}
delay.delay_ms(1000 * 10);
Expand Down Expand Up @@ -139,23 +148,51 @@ pub fn generate_random_mac() -> [u8; 6] {
pub fn connect() -> Result<Box<EspWifi<'static>>> {
set_target_level("wifi", log::LevelFilter::Warn)?;
set_target_level("wifi_init", log::LevelFilter::Warn)?;

#[cfg(feature = "clean_nvs")]
crate::nvs::remove::<NetConfig>()?;

This comment has been minimized.

Copy link
@YouXam

YouXam May 24, 2024

Member

这个不应该删除,这是可选项,当加入 clean_nvs features 的时候清空,默认是不清空的



let nvs = crate::nvs::nvs();
match crate::nvs::load::<NetConfig>()? {
Some(config) => {
log::info!("Loaded NetConfig: {:?}", &config);
connect_wifi_with_config(
config,
Peripherals::take()?.modem,
EspSystemEventLoop::take()?,
)
Some(config) => {//有配置
match bupt::check(bupt::CHECK_URL) { //判断是否已经登陆过,如果已经登陆进入第一个块

This comment has been minimized.

Copy link
@YouXam

YouXam May 24, 2024

Member

check 函数需要先连上 BUPT-portal 才能运行

Ok(bupt::BuptNetStatus::Authenticated) => {
let bupt_wifi = EspWifi::new(Peripherals::take()?.modem, EspSystemEventLoop::take()?.clone(), Some(nvs))?;//bupt—portal wifi配置?

This comment has been minimized.

Copy link
@YouXam

YouXam May 24, 2024

Member

这里逻辑应该是有问题的,connect_wifi_with_config 里面已经处理了已登录的情况。应当不用修改。需要检查的是配网函数那块,如果已经登录了就不启动 http 和 dns 服务器了,直接返回。

顺便一提,你这块直接创建一个 esp_wifi 也没用啊,既没有配置,也没有真的去连接。

log::info!("BUPT-portal is already authenticated");
Ok(Box::new(bupt_wifi))//返回bupt-portal实例
}

_ => {//未登录进入第二个块
log::info!("Loaded NetConfig: {:?}", &config);
let wifi_result = connect_wifi_with_config(//用已经储存的用户名密码登录,如果密码修改,无法登录,需要重新进行配网
config,
Peripherals::take()?.modem,
EspSystemEventLoop::take()?,
);
check_error_and_reconnect(wifi_result)

}
}
}
None => {
None => {//没有配置
let p = provisioning::Provisioner::new()?;
p.wait();
Ok(p.wifi)
}
}
}

fn check_error_and_reconnect(result: Result<Box<EspWifi<'static>>>) -> Result<Box<EspWifi<'static>>> {
match result {
Ok(wifi) => Ok(wifi),
Err(e) => {
if e.to_string().contains("Failed to login to BUPT-portal because of wrong password") {

This comment has been minimized.

Copy link
@YouXam

YouXam May 24, 2024

Member

建议不要用读取错误的错误信息的方式判断是什么错误,会有性能损耗,并且不便于后续维护。

可以加一个 enum,比如说:

enum ConnectResult {PasswordError,OtherError(anyhow::Error)}
// 密码错误,读取到返回的错误消息中特定报错,当做没有配置,重新进行配网
log::info!("Re-provisioning the network");
let new_config = provisioning::Provisioner::new()?;
new_config.wait();
Ok(new_config.wifi)
} else {
// 其他错误,直接返回原始错误
Err(e)
}
}
}
}

1 comment on commit 47e95e9

@YouXam
Copy link
Member

@YouXam YouXam commented on 47e95e9 May 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

提交前记得 cargo fmt,你这个格式检查应该没有通过。

Please sign in to comment.