1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use crate::{
    handle_error, handle_error2, Error, ErrorResponse, Result, Tarkov, GAME_VERSION,
    LAUNCHER_ENDPOINT, LAUNCHER_VERSION, PROD_ENDPOINT,
};
use flate2::read::ZlibDecoder;
use hyper::body::Buf;
use hyper::client::connect::dns::GaiResolver;
use hyper::client::{Client, HttpConnector};
use hyper::Request;
use hyper::StatusCode;
use hyper::{Body, Method};
use hyper_tls::HttpsConnector;
use log::debug;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::io::Read;

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct LoginRequest<'a> {
    email: &'a str,
    pass: &'a str,
    hw_code: &'a str,
    captcha: Option<&'a str>,
}

#[derive(Debug, Deserialize)]
pub(crate) struct Auth {
    pub aid: String,
    pub lang: String,
    pub region: Option<String>,
    #[serde(rename = "gameVersion")]
    pub game_version: Option<String>,
    #[serde(rename = "dataCenters")]
    pub data_centers: Vec<String>,
    #[serde(rename = "ipRegion")]
    pub ip_region: String,
    pub token_type: String,
    pub expires_in: u64,
    pub access_token: String,
    pub refresh_token: String,
}

#[derive(Debug, Deserialize)]
struct LoginResponse {
    #[serde(flatten)]
    error: ErrorResponse,
    #[serde(default)]
    data: serde_json::Value,
}

/// Login error
#[derive(Debug, err_derive::Error)]
pub enum LoginError {
    /// Bad login, invalid email or password.
    #[error(display = "bad login, wrong email or password.")]
    BadLogin,
    /// 2FA code is required to continue authentication.
    #[error(display = "2fa is required")]
    TwoFactorRequired,
    /// Captcha response is required to continue authentication.
    #[error(display = "captcha is required")]
    CaptchaRequired,
    /// Incorrect 2FA code.
    #[error(display = "incorrect 2FA code")]
    BadTwoFactorCode,
    /// Rate limited after too many bad login attempts.
    #[error(display = "too many login attempts")]
    RateLimited,
}

pub(crate) async fn login(
    client: &Client<HttpsConnector<HttpConnector<GaiResolver>>, Body>,
    email: &str,
    password: &str,
    captcha: Option<&str>,
    hwid: &str,
) -> Result<Auth> {
    let url = format!(
        "{}/launcher/login?launcherVersion={}&branch=live",
        LAUNCHER_ENDPOINT, LAUNCHER_VERSION
    );
    let password = format!("{:x}", md5::compute(&password));

    let body = LoginRequest {
        email,
        pass: &password,
        hw_code: hwid,
        captcha,
    };

    let res: LoginResponse = post_json(client, &url, &body).await?;
    handle_error2(res.error)?;

    Ok(Deserialize::deserialize(res.data)?)
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct SecurityLoginRequest<'a> {
    email: &'a str,
    hw_code: &'a str,
    activate_code: &'a str,
}

#[derive(Debug, Deserialize)]
struct SecurityLoginResponse {
    #[serde(flatten)]
    error: ErrorResponse,
}

pub(crate) async fn activate_hardware(
    client: &Client<HttpsConnector<HttpConnector<GaiResolver>>, Body>,
    email: &str,
    code: &str,
    hwid: &str,
) -> Result<()> {
    let url = format!(
        "{}/launcher/hardwareCode/activate?launcherVersion={}",
        LAUNCHER_ENDPOINT, LAUNCHER_VERSION
    );

    let body = SecurityLoginRequest {
        email,
        hw_code: hwid,
        activate_code: code,
    };

    let res: SecurityLoginResponse = post_json(client, &url, &body).await?;
    handle_error2(res.error)
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct ExchangeRequest<'a> {
    version: ExchangeVersion<'a>,
    hw_code: &'a str,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct ExchangeVersion<'a> {
    major: &'a str,
    game: &'a str,
    backend: &'a str,
}

#[derive(Debug, Deserialize)]
struct ExchangeResponse {
    #[serde(flatten)]
    error: ErrorResponse,
    data: Option<Session>,
}

/// Authenticated user session.
#[derive(Debug, Deserialize, Clone, PartialEq)]
pub struct Session {
    queued: bool,
    /// Session cookie.
    pub session: String,
}

pub(crate) async fn exchange_access_token(
    client: &Client<HttpsConnector<HttpConnector<GaiResolver>>, Body>,
    access_token: &str,
    hwid: &str,
) -> Result<Session> {
    let url = format!(
        "{}/launcher/game/start?launcherVersion={}&branch=live",
        PROD_ENDPOINT, LAUNCHER_VERSION
    );

    let body = ExchangeRequest {
        version: ExchangeVersion {
            major: GAME_VERSION,
            game: "live",
            backend: "6",
        },
        hw_code: hwid,
    };

    debug!("Sending request to {} ({:?})", url, body);
    let req = Request::builder()
        .uri(url)
        .method(Method::POST)
        .header("Content-Type", "application/json")
        .header("User-Agent", format!("BSG Launcher {}", LAUNCHER_VERSION))
        .header("Authorization", access_token)
        .body(Body::from(serde_json::to_string(&body)?))?;
    let res = client.request(req).await?;

    match res.status() {
        StatusCode::OK => {
            let body = hyper::body::to_bytes(res.into_body()).await?;
            let mut decode = ZlibDecoder::new(body.bytes());
            let mut body = String::new();
            decode.read_to_string(&mut body)?;
            debug!("Response: {}", body);

            let res = serde_json::from_slice::<ExchangeResponse>(body.as_bytes())?;
            handle_error(res.error, res.data)
        }
        _ => Err(Error::Status(res.status())),
    }
}

async fn post_json<S: serde::Serialize + ?Sized + std::fmt::Debug, T: DeserializeOwned>(
    client: &Client<HttpsConnector<HttpConnector<GaiResolver>>, Body>,
    url: &str,
    body: &S,
) -> Result<T> {
    debug!("Sending request to {} ({:?})", url, body);
    let req = Request::builder()
        .uri(url)
        .method(Method::POST)
        .header("Content-Type", "application/json")
        .header("User-Agent", format!("BSG Launcher {}", LAUNCHER_VERSION))
        .body(Body::from(serde_json::to_string(&body)?))?;
    let res = client.request(req).await?;

    match res.status() {
        StatusCode::OK => {
            let body = hyper::body::to_bytes(res.into_body()).await?;
            let mut decode = ZlibDecoder::new(body.bytes());
            let mut body = String::new();
            decode.read_to_string(&mut body)?;
            debug!("Response: {}", body);

            Ok(serde_json::from_slice::<T>(body.as_bytes())?)
        }
        _ => Err(Error::Status(res.status())),
    }
}

impl Tarkov {
    /// Keep the current session alive. Session expires after 30 seconds of idling.
    pub async fn keep_alive(&self) -> Result<()> {
        let url = format!("{}/client/game/keepalive", PROD_ENDPOINT);
        let res: ErrorResponse = self.post_json(&url, &{}).await?;

        match res.code {
            0 => Ok(()),
            _ => Err(Error::UnknownAPIError(res.code)),
        }
    }
}