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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
pub mod apk;
pub mod consts;
pub mod protos;

extern crate base64;
extern crate byteorder;
extern crate hex;
extern crate openssl;
extern crate protobuf;
extern crate reqwest;

#[macro_use]
extern crate serde_derive;
extern crate serde;

use protos::googleplay::{
    BulkDetailsRequest, BulkDetailsResponse, BuyResponse, DeliveryResponse, DetailsResponse,
    ResponseWrapper,
};
use reqwest::header::{HeaderMap, HeaderValue};
use reqwest::Url;
use std::collections::HashMap;
use std::error::Error;

pub use protobuf::{Message, SingularPtrField};

pub const STATUS_PURCHASE_UNAVAIL: i32 = 2;
pub const STATUS_PURCHASE_REQD: i32 = 3;
pub const STATUS_PURCHASE_ERR: i32 = 5;

#[derive(Debug)]
pub struct Gpapi {
    pub username: String,
    pub password: String,
    pub gsf_id: String,
    pub token: String,
    pub client: Box<reqwest::Client>,
}

impl Gpapi {
    pub fn new<S: Into<String>>(username: S, password: S, gsf_id: S) -> Self {
        Gpapi {
            username: username.into(),
            password: password.into(),
            gsf_id: gsf_id.into(),
            token: String::from(""),
            client: Box::new(reqwest::Client::new()),
        }
    }

    /// Handles logging into Google Play Store, retrieving a set of tokens from
    /// the server that can be used for future requests.
    /// The `gsf_id` is obtained by retrieving your
    /// [GSF id](https://blog.onyxbits.de/what-exactly-is-a-gsf-id-where-do-i-get-it-from-and-why-should-i-care-2-12/).
    /// You can also get your **GSF ID**  using this following [device id app](https://play.google.com/store/apps/details?id=com.evozi.deviceid&hl=en)
    /// Note that you don't want the Android ID here, but the GSF id.
    fn login(&self) -> Result<HashMap<String, String>, Box<Error>> {
        use consts::defaults::DEFAULT_LOGIN_URL;

        let login_req = ::build_login_request(&self.username, &self.password, &self.gsf_id);

        let mut headers = HeaderMap::new();
        headers.insert(
            reqwest::header::USER_AGENT,
            HeaderValue::from_str(&consts::defaults::DEFAULT_AUTH_USER_AGENT)?,
        );
        headers.insert(
            reqwest::header::CONTENT_TYPE,
            HeaderValue::from_static("application/x-www-form-urlencoded; charset=UTF-8"),
        );

        let form_body = login_req.form_post();
        let mut res = (*self.client)
            .post(DEFAULT_LOGIN_URL)
            .headers(headers)
            .body(form_body)
            .send()?;

        let mut buf = Vec::new();
        res.copy_to(&mut buf)?;
        let reply = parse_form_reply(&std::str::from_utf8(&buf).unwrap());
        Ok(reply)
    }

    /// Play Store package detail request (provides more detail than bulk requests).
    pub fn details(&self, pkg_name: &str) -> Result<Option<DetailsResponse>, Box<Error>> {
        let mut req = HashMap::new();
        req.insert("doc", pkg_name);
        let resp = self.execute_request_v2("details", Some(req), None, None)?;
        if let Some(payload) = resp.payload.into_option() {
            Ok(payload.detailsResponse.into_option())
        } else {
            Ok(None)
        }
    }

    pub fn bulk_details(
        &self,
        pkg_names: &[&str],
    ) -> Result<Option<BulkDetailsResponse>, Box<Error>> {
        let mut req = BulkDetailsRequest::new();
        req.docid = pkg_names.into_iter().cloned().map(String::from).collect();
        req.includeDetails = Some(true);
        req.includeChildDocs = Some(false);
        let bytes = req.write_to_bytes()?;
        let resp = self.execute_request_v2(
            "bulkDetails",
            None,
            Some(&bytes),
            Some("application/x-protobuf"),
        )?;
        if let Some(payload) = resp.payload.into_option() {
            Ok(payload.bulkDetailsResponse.into_option())
        } else {
            Ok(None)
        }
    }

    pub fn get_download_url(&self, pkg_name: &str, vc: u64) -> Result<Option<String>, Box<Error>> {
        if let Ok(Some(ref app_delivery_resp)) = self.app_delivery_data(pkg_name, vc) {
            match app_delivery_resp {
                DeliveryResponse {
                    status: None,
                    appDeliveryData: app_delivery_data,
                    ..
                } => Ok(app_delivery_data.clone().unwrap().downloadUrl.into_option()),
                DeliveryResponse {
                    status: Some(STATUS_PURCHASE_UNAVAIL),
                    ..
                } => Err(format!("can't locate {}", pkg_name).into()),
                DeliveryResponse {
                    status: Some(STATUS_PURCHASE_REQD),
                    ..
                } => match self.purchase(pkg_name, vc) {
                    Ok(Some(purchase_resp)) => Ok(purchase_resp
                        .purchaseStatusResponse
                        .unwrap_or_default()
                        .appDeliveryData
                        .unwrap_or_default()
                        .downloadUrl
                        .into_option()),
                    Err(err) => Err(format!("error purchasing {:?}", err).into()),
                    _ => unimplemented!(),
                },
                _ => {
                    unimplemented!()
                }
            }
        } else {
            if let Ok(Some(purchase_resp)) = self.purchase(pkg_name, vc) {
                Ok(purchase_resp
                    .purchaseStatusResponse
                    .unwrap()
                    .appDeliveryData
                    .unwrap()
                    .downloadUrl
                    .into_option())
            } else {
                Err("didn't get purchase data".into())
            }
        }
    }

    pub fn app_delivery_data(
        &self,
        pkg_name: &str,
        vc: u64,
    ) -> Result<Option<DeliveryResponse>, Box<dyn Error>> {
        let vc = vc.to_string();

        let mut req = HashMap::new();

        req.insert("doc", pkg_name);
        req.insert("vc", &vc);
        req.insert("ot", "1");

        let delivery_resp = self.execute_request_v2("delivery", Some(req), None, None)?;
        if let Some(payload) = delivery_resp.payload.into_option() {
            Ok(payload.deliveryResponse.into_option())
        } else {
            Ok(None)
        }
    }

    pub fn purchase(&self, pkg_name: &str, vc: u64) -> Result<Option<BuyResponse>, Box<dyn Error>> {
        let vc = vc.to_string();
        let body = format!("ot=1&doc={}&vc={}", pkg_name, vc);
        let body_bytes = body.into_bytes();

        let resp = self.execute_request_v2("purchase", None, Some(&body_bytes), None)?;
        match resp {
            ResponseWrapper {
                commands, payload, ..
            } => match (commands.into_option(), payload.into_option()) {
                (_, Some(payload)) => Ok(payload.buyResponse.into_option()),
                (Some(commands), _) => Err(commands.displayErrorMessage.unwrap().into()),
                _ => unimplemented!(),
            }, // ResponseWrapper { commands: SingularPtrField<ServerCommands>::some(commands), .. } => Err(commands.displayErrorMessage)
        }
        // if let Some(payload) = resp.payload.into_option() {
        //     Ok(payload.buyResponse.into_option())
        // } else {
        //     Ok(None)
        // }
    }

    pub fn authenticate(&mut self) -> Result<(), Box<Error>> {
        let form = self.login()?;
        if let Some(token) = form.get("auth") {
            self.token = token.to_string();
            Ok(())
        } else {
            Err("No GSF auth token".into())
        }
    }

    /// Lower level Play Store request, used by APIs but exposed for specialized
    /// requests. Returns a `ResponseWrapper` which depending on the request
    /// populates different fields/values.
    pub fn execute_request_v2(
        &self,
        endpoint: &str,
        query: Option<HashMap<&str, &str>>,
        msg: Option<&[u8]>,
        content_type: Option<&str>,
    ) -> Result<ResponseWrapper, Box<Error>> {
        let mut url = Url::parse(&format!(
            "{}/{}",
            "https://android.clients.google.com/fdfe", endpoint
        ))?;

        let config = BuildConfiguration {
            ..Default::default()
        };

        let mut headers = HeaderMap::new();
        headers.insert(
            reqwest::header::USER_AGENT,
            HeaderValue::from_str(&config.user_agent())?,
        );
        headers.insert(
            reqwest::header::ACCEPT_LANGUAGE,
            HeaderValue::from_static("en_US"),
        );
        headers.insert(
            reqwest::header::AUTHORIZATION,
            HeaderValue::from_str(&format!("GoogleLogin auth={}", self.token))?,
        );
        headers.insert(
            "X-DFE-Enabled-Experiments",
            HeaderValue::from_static("cl:billing.select_add_instrument_by_default"),
        );
        headers.insert("X-DFE-Unsupported-Experiments", HeaderValue::from_static("nocache:billing.use_charging_poller,market_emails,buyer_currency,prod_baseline,checkin.set_asset_paid_app_field,shekel_test,content_ratings,buyer_currency_in_app,nocache:encrypted_apk,recent_changes"));
        headers.insert("X-DFE-Device-Id", HeaderValue::from_str(&self.gsf_id)?);
        headers.insert(
            "X-DFE-Client-Id",
            HeaderValue::from_static("am-android-google"),
        );
        headers.insert(
            "X-DFE-SmallestScreenWidthDp",
            HeaderValue::from_static("320"),
        );
        headers.insert("X-DFE-Filter-Level", HeaderValue::from_static("3"));
        headers.insert("X-DFE-No-Prefetch", HeaderValue::from_static("true"));

        if let Some(content_type) = content_type {
            headers.insert(
                reqwest::header::CONTENT_TYPE,
                HeaderValue::from_str(content_type)?,
            );
        } else {
            headers.insert(
                reqwest::header::CONTENT_TYPE,
                HeaderValue::from_static("application/x-www-form-urlencoded; charset=UTF-8"),
            );
        }

        if let Some(query) = query {
            let mut queries = url.query_pairs_mut();
            for (key, val) in query {
                queries.append_pair(key, val);
            }
        }

        let mut res = if let Some(msg) = msg {
            (*self.client)
                .post(url)
                .headers(headers)
                .body(msg.to_owned())
                .send()?
        } else {
            (*self.client).get(url).headers(headers).send()?
        };

        let mut buf = Vec::new();
        res.copy_to(&mut buf)?;
        let mut resp = ResponseWrapper::new();
        resp.merge_from_bytes(&buf)?;
        Ok(resp)
    }
}

/// Play Store API endpoints supported
#[derive(Debug)]
pub enum Endpoint {
    Details,
    BulkDetails,
}

impl Endpoint {
    pub fn as_str(&self) -> &'static str {
        match self {
            Endpoint::Details => "details",
            Endpoint::BulkDetails => "bulkDetails",
        }
    }
}

#[derive(Debug)]
pub struct PubKey {
    pub modulus: Vec<u8>,
    pub exp: Vec<u8>,
}

pub fn parse_form_reply(data: &str) -> HashMap<String, String> {
    let mut form_resp = HashMap::new();
    let lines: Vec<&str> = data.split_terminator('\n').collect();
    for line in lines.iter() {
        let kv: Vec<&str> = line.split_terminator('=').collect();
        form_resp.insert(String::from(kv[0]).to_lowercase(), String::from(kv[1]));
    }
    form_resp
}

/// Handles encrypting your login/password using Google's public key
/// Produces something of the format:
/// |00|4 bytes of sha1(publicKey)|rsaEncrypt(publicKeyPem, "login\x00password")|
pub fn encrypt_login(login: &str, password: &str) -> Option<Vec<u8>> {
    let raw = base64::decode(consts::GOOGLE_PUB_KEY_B64).unwrap();
    if let Ok(Some(pubkey)) = extract_pubkey(&raw) {
        let rsa = build_openssl_rsa(&pubkey);

        let data = format!("{login}\x00{password}", login = login, password = password);
        let mut to = vec![0u8; rsa.size() as usize];
        let padding = openssl::rsa::Padding::PKCS1_OAEP;

        if let Ok(_sz) = rsa.public_encrypt(data.as_bytes(), &mut to, padding) {
            let sha1 = openssl::sha::sha1(&raw);
            let mut res = vec![];
            res.push(0x00);
            res.extend(&sha1[0..4]);
            res.extend(&to);
            Some(res)
        } else {
            None
        }
    } else {
        None
    }
}

///
/// Base64 encode w/ URL safe characters.
///
pub fn base64_urlsafe(input: &[u8]) -> String {
    base64::encode_config(input, base64::URL_SAFE_NO_PAD)
}

///
/// Gen up an `openssl::rsa::Rsa` from a `PubKey`.
///
pub fn build_openssl_rsa(p: &PubKey) -> openssl::rsa::Rsa<openssl::pkey::Public> {
    use openssl::bn::BigNum;
    use openssl::rsa::Rsa;

    let modulus = BigNum::from_hex_str(&hex::encode(&p.modulus)).unwrap();
    let exp = BigNum::from_hex_str(&hex::encode(&p.exp)).unwrap();
    let rsa = Rsa::from_public_components(modulus, exp).unwrap();

    rsa
}

///
/// Extract public key (PEM) from a raw buffer.
///
fn extract_pubkey(buf: &[u8]) -> Result<Option<PubKey>, Box<Error>> {
    use byteorder::{NetworkEndian, ReadBytesExt};
    use std::io::{Cursor, Read};
    let mut cur = Cursor::new(&buf);

    let sz = cur.read_u32::<NetworkEndian>()?;
    let mut modulus = vec![0u8; sz as usize];
    cur.read_exact(&mut modulus)?;

    let sz = cur.read_u32::<NetworkEndian>()?;
    let mut exp = vec![0u8; sz as usize];
    cur.read_exact(&mut exp)?;

    Ok(Some(PubKey { modulus, exp }))
}

#[derive(Debug, Clone)]
pub struct LoginRequest {
    email: String,
    encrypted_password: String,
    service: String,
    account_type: String,
    has_permission: String,
    source: String,
    gsf_id: String,
    app: String,
    device_country: String,
    operator_country: String,
    lang: String,
    sdk_version: String,
    build_config: Option<BuildConfiguration>,
}

impl LoginRequest {
    pub fn form_post(&self) -> String {
        format!("Email={}&EncryptedPasswd={}&service={}&accountType={}&has_permission={}&source={}&androidId={}&app={}&device_country={}&operatorCountry={}&lang={}&sdk_version={}",
         self.email, self.encrypted_password, self.service, self.account_type, self.has_permission, self.source, self.gsf_id, self.app,self.device_country, self.operator_country, self.lang, self.sdk_version)
    }
}

#[derive(Debug, Clone)]
pub struct BuildConfiguration {
    pub finsky_agent: String,
    pub finsky_version: String,
    pub api: String,
    pub version_code: String,
    pub sdk: String,
    pub device: String,
    pub hardware: String,
    pub product: String,
    pub platform_version_release: String,
    pub model: String,
    pub build_id: String,
    pub is_wide_screen: String,
}

impl BuildConfiguration {
    pub fn user_agent(&self) -> String {
        format!("{}/{} (api={},versionCode={},sdk={},device={},hardware={},product={},platformVersionRelease={},model={},buildId={},isWideScreen={})", 
          self.finsky_agent, self.finsky_version, self.api, self.version_code, self.sdk,
          self.device, self.hardware, self.product,
          self.platform_version_release, self.model, self.build_id,
          self.is_wide_screen
        )
    }
}

impl Default for BuildConfiguration {
    fn default() -> BuildConfiguration {
        use consts::defaults::api_user_agent::{
            DEFAULT_API, DEFAULT_BUILD_ID, DEFAULT_DEVICE, DEFAULT_HARDWARE,
            DEFAULT_IS_WIDE_SCREEN, DEFAULT_MODEL, DEFAULT_PLATFORM_VERSION_RELEASE,
            DEFAULT_PRODUCT, DEFAULT_SDK, DEFAULT_VERSION_CODE,
        };
        use consts::defaults::{DEFAULT_FINSKY_AGENT, DEFAULT_FINSKY_VERSION};

        BuildConfiguration {
            finsky_agent: DEFAULT_FINSKY_AGENT.to_string(),
            finsky_version: DEFAULT_FINSKY_VERSION.to_string(),
            api: DEFAULT_API.to_string(),
            version_code: DEFAULT_VERSION_CODE.to_string(),
            sdk: DEFAULT_SDK.to_string(),
            device: DEFAULT_DEVICE.to_string(),
            hardware: DEFAULT_HARDWARE.to_string(),
            product: DEFAULT_PRODUCT.to_string(),
            platform_version_release: DEFAULT_PLATFORM_VERSION_RELEASE.to_string(),
            model: DEFAULT_MODEL.to_string(),
            build_id: DEFAULT_BUILD_ID.to_string(),
            is_wide_screen: DEFAULT_IS_WIDE_SCREEN.to_string(),
        }
    }
}

impl Default for LoginRequest {
    fn default() -> Self {
        LoginRequest {
            email: String::from(""),
            encrypted_password: String::from(""),
            service: String::from(consts::defaults::DEFAULT_SERVICE),
            account_type: String::from(consts::defaults::DEFAULT_ACCOUNT_TYPE),
            has_permission: String::from("1"),
            source: String::from("android"),
            gsf_id: String::from(""),
            app: String::from(consts::defaults::DEFAULT_ANDROID_VENDING),
            device_country: String::from(consts::defaults::DEFAULT_DEVICE_COUNTRY),
            operator_country: String::from(consts::defaults::DEFAULT_COUNTRY_CODE),
            lang: String::from(consts::defaults::DEFAULT_LANGUAGE),
            sdk_version: String::from(consts::defaults::DEFAULT_SDK_VERSION),
            build_config: None,
        }
    }
}

pub fn build_login_request(username: &str, password: &str, gsf_id: &str) -> LoginRequest {
    let login = encrypt_login(username, password).unwrap();
    let encrypted_password = base64_urlsafe(&login);
    let build_config = BuildConfiguration {
        ..Default::default()
    };
    LoginRequest {
        email: String::from(username),
        encrypted_password,
        gsf_id: String::from(gsf_id),
        build_config: Some(build_config),
        ..Default::default()
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn login() {
        let enc = ::encrypt_login("foo", "bar").unwrap();
        println!("encrypted: {:?}", base64::encode(&enc));
        println!("base64_urlsafe: {:?}", ::base64_urlsafe(&enc));
    }

    #[test]
    fn parse_form() {
        let form_reply = "FOO=BAR\nbaz=qux";
        let x = ::parse_form_reply(&form_reply);
        println!("form (parsed): {:?}", x);
    }

    #[test]
    fn foobar() {
        assert!(1 == 1);
    }

    mod gpapi {

        use std::env;
        use Gpapi;

        #[test]
        fn create_gpapi() {
            match (
                env::var("GOOGLE_LOGIN"),
                env::var("GOOGLE_PASSWORD"),
                env::var("ANDROID_ID"),
            ) {
                (Ok(username), Ok(password), Ok(gsf_id)) => {
                    let mut api = Gpapi::new(username, password, gsf_id);
                    api.authenticate().ok();
                    assert!(api.token != "");

                    let details = api.details("com.viber.voip").ok();
                    let pkg_names = ["com.viber.voip", "air.WatchESPN"];
                    let bulk_details = api.bulk_details(&pkg_names).ok();
                }
                _ => panic!("require login/password/gsf_id for test"),
            }
        }

        #[test]
        fn test_protobuf() {
            use protos::googleplay::BulkDetailsRequest;
            let mut x = BulkDetailsRequest::new();
            x.docid = vec!["test".to_string()].into();
            x.includeDetails = Some(true);
            x.includeChildDocs = Some(true);
        }
    }
}