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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
//! An unofficial client library for the [Escape from Tarkov](https://escapefromtarkov.com) (EFT) API.
//!
//! To get started, login to EFT with `Tarkov::login`, `from_access_token`, or `from_session`.
//! Additionally, on a new session, a profile must be selected with `select_profile` before continuing.
//!
//! Once authenticated, the resulting value can be used to make further API requests.
//!
//! See [Tarkov](struct.Tarkov.html) for a list of available methods.
//!
//! For examples, see the `examples` directory in the source tree.

#![warn(missing_docs)]

use crate::auth::LoginError;
use crate::hwid::generate_hwid;
use crate::profile::ProfileError;
use crate::ragfair::RagfairError;
use crate::trading::TradingError;
use err_derive::Error;
use flate2::read::ZlibDecoder;
use hyper::body::Buf;
use hyper::client::connect::dns::GaiResolver;
use hyper::client::{Client, HttpConnector};
use hyper::Body;
use hyper::Request;
use hyper::{Method, StatusCode};
use hyper_tls::HttpsConnector;
use log::debug;
use serde::de::DeserializeOwned;
use serde::Deserialize;
use std::io::Read;

const GAME_VERSION: &str = "0.12.2.5707";
const LAUNCHER_VERSION: &str = "0.9.2.970";
const UNITY_VERSION: &str = "2018.4.13f1";

const LAUNCHER_ENDPOINT: &str = "https://launcher.escapefromtarkov.com";
const PROD_ENDPOINT: &str = "https://prod.escapefromtarkov.com";
const TRADING_ENDPOINT: &str = "https://trading.escapefromtarkov.com";
const RAGFAIR_ENDPOINT: &str = "https://ragfair.escapefromtarkov.com";

mod bad_json;

/// Structs for authentication.
pub mod auth;
/// Structs for game constants API.
pub mod constant;
/// Structs for the Friend API.
pub mod friend;
/// Helper functions for hardware ID.
pub mod hwid;
/// Structs for inventory and items.
pub mod inventory;
/// Flea market search helpers.
pub mod market_filter;
/// Structs for the Profile API.
pub mod profile;
/// Structs for the Flea Market (Ragfair) API.
pub mod ragfair;
/// Structs for the Trading API.
pub mod trading;

/// Common error enum returned by most functions.
#[derive(Debug, Error)]
pub enum Error {
    /// A `std::io` error
    #[error(display = "io error: {}", _0)]
    Io(#[error(source)] std::io::Error),
    /// HTTP request error.
    #[error(display = "http error: {}", _0)]
    Http(#[error(source)] http::Error),
    /// A `hyper` crate error.
    #[error(display = "hyper error: {}", _0)]
    Hyper(#[error(source)] hyper::Error),
    /// A `serde_json` error.
    #[error(display = "json error: {}", _0)]
    Json(#[error(source)] serde_json::error::Error),
    /// Generic non-success response from the API.
    #[error(display = "non-success response from api: {}", _0)]
    Status(StatusCode),
    /// Invalid or missing parameters.
    #[error(display = "invalid or missing login parameters")]
    InvalidParameters,

    /// Unidentified error within the EFT API.
    #[error(display = "unidentified login error with error code: {}", _0)]
    UnknownAPIError(u64),
    /// Not authorized to API or profile is not selected.
    #[error(display = "not authorized or game profile not selected")]
    NotAuthorized,
    /// EFT API is down for maintenance.
    #[error(display = "api is down for maintenance")]
    Maintenance,
    /// Backend error. No other information is given.
    #[error(display = "backend error")]
    BackendError,
    /// Authentication API error.
    #[error(display = "login api error: {}", _0)]
    LoginError(#[error(source)] LoginError),
    /// Profile API error.
    #[error(display = "profile api error: {}", _0)]
    ProfileError(#[error(source)] ProfileError),
    /// Trading API error.
    #[error(display = "trading api error: {}", _0)]
    TradingError(#[error(source)] TradingError),
    /// Ragfair API error.
    #[error(display = "trading api error: {}", _0)]
    RagfairError(#[error(source)] RagfairError),
}

/// `Result` alias type.
pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug, Deserialize, Clone, PartialEq)]
struct ErrorResponse {
    #[serde(rename = "err")]
    code: u64,
    #[serde(rename = "errmsg")]
    message: Option<String>,
}

/// Client for the EFT API.
pub struct Tarkov {
    client: Client<HttpsConnector<HttpConnector<GaiResolver>>, Body>,
    /// Hardware ID
    pub hwid: String,
    /// Session cookie
    pub session: String,
}

impl Tarkov {
    /// Login with email and password.
    pub async fn login(email: &str, password: &str, hwid: &str) -> Result<Self> {
        if email.is_empty() || password.is_empty() || hwid.is_empty() {
            return Err(Error::InvalidParameters);
        }

        let https = HttpsConnector::new();
        let client = Client::builder().build::<_, Body>(https);

        let user = auth::login(&client, email, password, None, &hwid).await?;
        let session = auth::exchange_access_token(&client, &user.access_token, &hwid).await?;

        Ok(Tarkov {
            client,
            hwid: hwid.to_string(),
            session: session.session,
        })
    }

    /// Login with email, password and captcha.
    pub async fn login_with_captcha(
        email: &str,
        password: &str,
        captcha: &str,
        hwid: &str,
    ) -> Result<Self> {
        if email.is_empty() || password.is_empty() || captcha.is_empty() || hwid.is_empty() {
            return Err(Error::InvalidParameters);
        }

        let https = HttpsConnector::new();
        let client = Client::builder().build::<_, Body>(https);

        let user = auth::login(&client, email, password, Some(captcha), &hwid).await?;
        let session = auth::exchange_access_token(&client, &user.access_token, &hwid).await?;

        Ok(Tarkov {
            client,
            hwid: hwid.to_string(),
            session: session.session,
        })
    }

    /// Login with email, password and 2FA code.
    pub async fn login_with_2fa(
        email: &str,
        password: &str,
        code: &str,
        hwid: &str,
    ) -> Result<Self> {
        if email.is_empty() || password.is_empty() || code.is_empty() || hwid.is_empty() {
            return Err(Error::InvalidParameters);
        }

        let https = HttpsConnector::new();
        let client = Client::builder().build::<_, Body>(https);

        let _ = auth::activate_hardware(&client, email, code, &hwid).await?;
        let user = auth::login(&client, email, password, None, &hwid).await?;
        let session = auth::exchange_access_token(&client, &user.access_token, &hwid).await?;

        Ok(Tarkov {
            client,
            hwid: hwid.to_string(),
            session: session.session,
        })
    }

    /// Login with a Bearer token.
    pub async fn from_access_token(access_token: &str, hwid: &str) -> Result<Self> {
        if access_token.is_empty() || hwid.is_empty() {
            return Err(Error::InvalidParameters);
        }

        let https = HttpsConnector::new();
        let client = Client::builder().build::<_, Body>(https);
        let session = auth::exchange_access_token(&client, &access_token, &hwid).await?;

        Ok(Tarkov {
            client,
            hwid: hwid.to_string(),
            session: session.session,
        })
    }

    /// Login with a cookie session (AKA `PHPSESSID`).
    pub fn from_session(session: &str) -> Self {
        let https = HttpsConnector::new();
        let client = Client::builder().build::<_, Body>(https);

        Tarkov {
            client,
            hwid: generate_hwid(),
            session: session.to_string(),
        }
    }

    async fn post_json<S: serde::Serialize + ?Sized + std::fmt::Debug, T: DeserializeOwned>(
        &self,
        url: &str,
        body: &S,
    ) -> Result<T> {
        debug!("Sending request to {} ({:?})", url, body);
        let body = match serde_json::to_string(&body) {
            Ok(body) => Ok(Body::from(if body == "null" {
                "{}".to_string()
            } else {
                body
            })),
            Err(e) => Err(e),
        }?;

        let req = Request::builder()
            .uri(url)
            .method(Method::POST)
            .header("Content-Type", "application/json")
            .header(
                "User-Agent",
                format!(
                    "UnityPlayer/{} (UnityWebRequest/1.0, libcurl/7.52.0-DEV)",
                    UNITY_VERSION
                ),
            )
            .header("App-Version", format!("EFT Client {}", GAME_VERSION))
            .header("X-Unity-Version", UNITY_VERSION)
            .header("Cookie", format!("PHPSESSID={}", self.session))
            .body(body)?;
        let res = self.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())),
        }
    }
}

pub(crate) fn handle_error<T: DeserializeOwned>(error: ErrorResponse, ret: Option<T>) -> Result<T> {
    handle_error2(error)?;
    Ok(ret.expect("API returned no errors but `data` is unavailable."))
}

pub(crate) fn handle_error2(error: ErrorResponse) -> Result<()> {
    match error.code {
        0 => Ok(()),
        201 => Err(Error::NotAuthorized)?,
        205 => Err(ProfileError::InvalidUserID)?,
        206 => Err(LoginError::BadLogin)?,
        207 => Err(Error::InvalidParameters)?,
        209 => Err(LoginError::TwoFactorRequired)?,
        211 => Err(LoginError::BadTwoFactorCode)?,
        214 => Err(LoginError::CaptchaRequired)?,
        228 => Err(RagfairError::InvalidBarterItems)?,
        230 => Err(LoginError::RateLimited)?,
        263 => Err(Error::Maintenance)?,
        1000 => Err(Error::BackendError)?,
        1501 => Err(RagfairError::MaxOfferCount)?,
        1502 => Err(RagfairError::InsufficientTaxFunds)?,
        1507 => Err(RagfairError::OfferNotFound)?,
        1510 => Err(TradingError::BadLoyaltyLevel)?,
        1512 => Err(RagfairError::OfferNotAvailableYet)?,
        1514 => Err(TradingError::TransactionError)?,
        _ => Err(Error::UnknownAPIError(error.code)),
    }
}