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::*; pub use ErrorCode::*;
match code { match code {
ProtocolError => "Protocol error", ProtocolError => "Protocol error",
TryAnotherAP => "Try another AP", TryAnotherAP => "Try another access point",
BadConnectionId => "Bad connection id", BadConnectionId => "Bad connection ID",
TravelRestriction => "Travel restriction", TravelRestriction => "Travel restriction",
PremiumAccountRequired => "Premium account required", PremiumAccountRequired => "Premium account required",
BadCredentials => "Bad credentials", BadCredentials => "Bad credentials",

View file

@ -32,6 +32,7 @@ use crate::{
http_client::HttpClient, http_client::HttpClient,
mercury::MercuryManager, mercury::MercuryManager,
packet::PacketType, packet::PacketType,
protocol::keyexchange::ErrorCode,
spclient::SpClient, spclient::SpClient,
token::TokenProvider, token::TokenProvider,
Error, Error,
@ -131,12 +132,33 @@ impl Session {
credentials: Credentials, credentials: Credentials,
store_credentials: bool, store_credentials: bool,
) -> Result<(), Error> { ) -> Result<(), Error> {
let (reusable_credentials, transport) = loop {
let ap = self.apresolver().resolve("accesspoint").await?; let ap = self.apresolver().resolve("accesspoint").await?;
info!("Connecting to AP \"{}:{}\"", ap.0, ap.1); info!("Connecting to AP \"{}:{}\"", ap.0, ap.1);
let mut transport = connection::connect(&ap.0, ap.1, self.config().proxy.as_ref()).await?; 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); info!("Authenticated as \"{}\" !", reusable_credentials.username);
self.set_username(&reusable_credentials.username); self.set_username(&reusable_credentials.username);
if let Some(cache) = self.cache() { if let Some(cache) = self.cache() {