Try another access point if so instructed

This commit is contained in:
Roderick van Domburg 2022-08-29 23:51:29 +02:00
parent 6c2127bfcd
commit 16dbade516
No known key found for this signature in database
GPG key ID: 87F5FDE8A56219F4
2 changed files with 29 additions and 7 deletions

View file

@ -23,8 +23,8 @@ fn login_error_message(code: &ErrorCode) -> &'static str {
pub use ErrorCode::*;
match code {
ProtocolError => "Protocol error",
TryAnotherAP => "Try another AP",
BadConnectionId => "Bad connection id",
TryAnotherAP => "Try another access point",
BadConnectionId => "Bad connection ID",
TravelRestriction => "Travel restriction",
PremiumAccountRequired => "Premium account required",
BadCredentials => "Bad credentials",

View file

@ -32,6 +32,7 @@ use crate::{
http_client::HttpClient,
mercury::MercuryManager,
packet::PacketType,
protocol::keyexchange::ErrorCode,
spclient::SpClient,
token::TokenProvider,
Error,
@ -131,12 +132,33 @@ impl Session {
credentials: Credentials,
store_credentials: bool,
) -> Result<(), Error> {
let ap = self.apresolver().resolve("accesspoint").await?;
info!("Connecting to AP \"{}:{}\"", ap.0, ap.1);
let mut transport = connection::connect(&ap.0, ap.1, self.config().proxy.as_ref()).await?;
let (reusable_credentials, transport) = loop {
let ap = self.apresolver().resolve("accesspoint").await?;
info!("Connecting to AP \"{}:{}\"", ap.0, ap.1);
let mut transport =
connection::connect(&ap.0, ap.1, self.config().proxy.as_ref()).await?;
match connection::authenticate(
&mut transport,
credentials.clone(),
&self.config().device_id,
)
.await
{
Ok(creds) => break (creds, transport),
Err(e) => {
if let Some(AuthenticationError::LoginFailed(ErrorCode::TryAnotherAP)) =
e.error.downcast_ref::<AuthenticationError>()
{
warn!("Instructed to try another access point...");
continue;
} else {
return Err(e);
}
}
}
};
let reusable_credentials =
connection::authenticate(&mut transport, credentials, &self.config().device_id).await?;
info!("Authenticated as \"{}\" !", reusable_credentials.username);
self.set_username(&reusable_credentials.username);
if let Some(cache) = self.cache() {