apkeep 1.0.0

A command-line tool for downloading APK files from various sources
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
//! # Installation
//!
//! Precompiled binaries for `apkeep` on various platforms can be downloaded
//! [here](https://github.com/EFForg/apkeep/releases).
//!
//! To install from `crates.io`, simply [install rust](https://www.rust-lang.org/tools/install) and
//! run
//!
//! ```shell
//! cargo install apkeep
//! ```
//!
//! Or to install from the latest commit in our repository, run
//!
//! ```shell
//! cargo install --git https://github.com/EFForg/apkeep.git
//! ```
//!
//! If using on an Android platform, [`termux`](https://termux.org/) must be installed first.
//! Upgrade to the latest packages with `pkg update`, then install the `apkeep` precompiled binary
//! as described above or run `pkg install apkeep` to install from the `termux` repository.
//!
//! Docker images are also available through the GitHub Container Registry. Aside from using a
//! specific release version, the following floating tags are available:
//!
//! - stable: tracks the latest stable release (recommended)
//! - latest: tracks the latest release, including pre-releases
//! - edge: tracks the latest commit
//!
//! # Usage
//!
//! See [`USAGE`](https://github.com/EFForg/apkeep/blob/master/USAGE).
//!
//! # Examples
//!
//! The simplest example is to download a single APK to the current directory:
//!
//! ```shell
//! apkeep -a com.instagram.android .
//! ```
//!
//! This downloads from the default source, APKPure, which does not require credentials. For more
//! APKPure usage examples, such as specifying a package architecture, refer to the
//! [`USAGE-apkpure.md`](USAGE-apkpure.md) document.
//!
//! To download directly from the google play store, you will first have to [obtain an AAS token](USAGE-google-play.md).
//! Then,
//!
//! ```shell
//! apkeep -a com.instagram.android -d google-play -e 'someone@gmail.com' -t aas_token .
//! ```
//!
//! For more google play usage examples, such as specifying a device configuration, timezone or
//! locale, refer to the [`USAGE-google-play.md`](USAGE-google-play.md) document.
//!
//! To download from the F-Droid open source repository:
//!
//! ```shell
//! apkeep -a org.mozilla.fennec_fdroid -d f-droid .
//! ```
//!
//! For more F-Droid usage examples, such as downloading from F-Droid mirrors or other F-Droid
//! repositories, refer to the [`USAGE-fdroid.md`](USAGE-fdroid.md) document.
//!
//! Or, to download from the Huawei AppGallery:
//!
//! ```shell
//! apkeep -a com.elysiumlabs.newsbytes -d huawei-app-gallery .
//! ```
//!
//! To download a specific version of an APK (possible for APKPure or F-Droid), use the `@version`
//! convention:
//!
//! ```shell
//! apkeep -a com.instagram.android@1.2.3 .
//! ```
//!
//! Or, to list what versions are available, use `-l`:
//!
//! ```shell
//! apkeep -l -a org.mozilla.fennec_fdroid -d f-droid
//! ```
//!
//! Refer to [`USAGE`](https://github.com/EFForg/apkeep/blob/master/USAGE) to download multiple
//! APKs in a single run.
//!
//! All the above examples can also be used in Docker with minimal changes. For example, to
//! download a single APK to your chosen output directory:
//!
//! ```shell
//! docker run --rm -v output_path:/output ghcr.io/efforg/apkeep:stable -a com.instagram.android
//! /output
//! ```
//!
//! # Specify a CSV file or individual app ID
//!
//! You can either specify a CSV file which lists the apps to download, or an individual app ID.
//! If you specify a CSV file and the app ID is not specified by the first column, you'll have to
//! use the --field option as well.  If you have a simple file with one app ID per line, you can
//! just treat it as a CSV with a single field.
//!
//! # Download Sources
//!
//! You can use this tool to download from a few distinct sources.
//!
//! * The Google Play Store (`-d google-play`), given an email address and AAS token
//! * APKPure (`-d apk-pure`), a third-party site hosting APKs available on the Play Store
//! * F-Droid (`-d f-droid`), a repository for free and open-source Android apps. `apkeep`
//! verifies that these APKs are signed by the F-Droid maintainers, and alerts the user if an APK
//! was downloaded but could not be verified
//! * The Huawei AppGallery (`-d huawei-app-gallery`), an app store popular in China
//!
//! # Usage Note
//!
//! Users should not use app lists or choose so many parallel APK fetches as to place unreasonable
//! or disproportionately large load on the infrastructure of the app distributor.
//!
//! When using with the Google Play Store as the download source, a few considerations should be
//! made:
//!
//! * Google may terminate your Google account based on Terms of Service violations.  Read their
//! [Terms of Service](https://play.google.com/about/play-terms/index.html), avoid violating it,
//! and choose an account where this outcome is acceptable.
//! * Paid and DRM apps will not be available.
//! * Using Tor will make it a lot more likely that the download will fail.

use std::collections::HashMap;
use std::error::Error;
use std::fs::{self, File};
use std::io::{self, Write, Read};
use std::path::{Path, PathBuf};

use configparser::ini::Ini;

mod cli;
use cli::DownloadSource;

mod config;
mod consts;
mod util;

mod download_sources;
use download_sources::google_play;
use download_sources::fdroid;
use download_sources::apkpure;
use download_sources::huawei_app_gallery;

type CSVList = Vec<(String, Option<String>)>;
fn fetch_csv_list(csv: &str, field: usize, version_field: Option<usize>) -> Result<CSVList, Box<dyn Error>> {
    Ok(parse_csv_text(fs::read_to_string(csv)?, field, version_field))
}

fn parse_csv_text(text: String, field: usize, version_field: Option<usize>) -> Vec<(String, Option<String>)> {
    let field = field - 1;
    let version_field = version_field.map(|version_field| version_field - 1);
    text.split('\n')
        .filter_map(|l| {
            let entry = l.trim();
            let mut entry_vec = entry.split(',').collect::<Vec<&str>>();
            if entry_vec.len() > field && !(entry_vec.len() == 1 && entry_vec[0].is_empty()) {
                match version_field {
                    Some(mut version_field) if entry_vec.len() > version_field => {
                        if version_field > field {
                            version_field -= 1;
                        }
                        let app_id = String::from(entry_vec.remove(field));
                        let app_version = String::from(entry_vec.remove(version_field));
                        if !app_version.is_empty() {
                            Some((app_id, Some(app_version)))
                        } else {
                            Some((app_id, None))
                        }
                    },
                    _ => Some((String::from(entry_vec.remove(field)), None)),
                }
            } else {
                None
            }
        })
        .collect()
}

fn load_config(ini_file: Option<PathBuf>) -> Result<Ini, Box<dyn Error>> {
    let mut conf = Ini::new();
    let config_path = match ini_file {
        Some(ini_file) => ini_file,
        None => {
            let mut config_path = config::config_dir()?;
            config_path.push("apkeep.ini");
            config_path
        }
    };
    let mut config_fp = File::open(&config_path)?;
    let mut contents = String::new();
    config_fp.read_to_string(&mut contents)?;
    conf.read(contents)?;
    Ok(conf)
}

#[tokio::main]
async fn main() {
    let usage = {
        cli::app().render_usage()
    };
    let matches = cli::app().get_matches();

    let mut download_source = *matches.get_one::<DownloadSource>("download_source").unwrap();
    let options: HashMap<&str, &str> = match matches.get_one::<String>("options") {
        Some(options) => {
            let mut options_map = HashMap::new();
            for option in options.split(",") {
                match option.split_once("=") {
                    Some((key, value)) => {
                        options_map.insert(key, value);
                    },
                    None => {}
                }
            }
            options_map
        },
        None => HashMap::new()
    };

    let oauth_token = matches.get_one::<String>("google_oauth_token").map(|v| v.to_string());
    if oauth_token.is_some() {
        download_source = DownloadSource::GooglePlay;
    }
    let list: Vec<(String, Option<String>)> = if oauth_token.is_none() {
        match matches.get_one::<String>("app") {
            Some(app) => {
                let mut app_vec: Vec<String> = app.splitn(2, '@').map(String::from).collect();
                let app_id = app_vec.remove(0);
                let app_version = match app_vec.len() {
                    1 => Some(app_vec.remove(0)),
                    _ => None,
                };
                vec![(app_id, app_version)]
            },
            None => {
                let csv = matches.get_one::<String>("csv").unwrap();
                let field = *matches.get_one::<usize>("field").unwrap();
                let version_field = matches.get_one::<usize>("version_field").map(|v| *v);
                if field < 1 {
                    println!("{}\n\nApp ID field must be 1 or greater", usage);
                    std::process::exit(1);
                }
                if let Some(version_field) = version_field {
                    if version_field < 1 {
                        println!("{}\n\nVersion field must be 1 or greater", usage);
                        std::process::exit(1);
                    }
                    if field == version_field {
                        println!("{}\n\nApp ID and Version fields must be different", usage);
                        std::process::exit(1);
                    }
                }
                match fetch_csv_list(csv, field, version_field) {
                    Ok(csv_list) => csv_list,
                    Err(err) => {
                        println!("{}\n\n{:?}", usage, err);
                        std::process::exit(1);
                    }
                }
            }
        }
    } else { Vec::new() };

    if let Some(true) = matches.get_one::<bool>("list_versions") {
        match download_source {
            DownloadSource::APKPure => {
                apkpure::list_versions(list, options).await;
            }
            DownloadSource::GooglePlay => {
                google_play::list_versions(list);
            }
            DownloadSource::FDroid => {
                fdroid::list_versions(list, options).await;
            }
            DownloadSource::HuaweiAppGallery => {
                huawei_app_gallery::list_versions(list).await;
            }
        }
    } else {
        let parallel = matches.get_one::<usize>("parallel").map(|v| *v).unwrap();
        let sleep_duration = matches.get_one::<u64>("sleep_duration").map(|v| *v).unwrap();
        let outpath = matches.get_one::<String>("OUTPATH").map_or_else(|| {
            if oauth_token.is_none() {
                println!("{}\n\nOUTPATH must be specified when downloading files", usage);
                std::process::exit(1);
            }
            None
        }, |outpath| {
            match fs::canonicalize(outpath) {
                Ok(outpath) if Path::new(&outpath).is_dir() => {
                    Some(outpath)
                },
                _ => {
                    println!("{}\n\nOUTPATH is not a valid directory", usage);
                    std::process::exit(1);
                }
            }
        });

        match download_source {
            DownloadSource::APKPure => {
                apkpure::download_apps(
                    list,
                    parallel,
                    sleep_duration,
                    &outpath.unwrap(),
                    options,
                ).await;
            }
            DownloadSource::GooglePlay => {
                let mut email = matches.get_one::<String>("google_email").map(|v| v.to_string());

                if email.is_some() && oauth_token.is_some() {
                    google_play::request_aas_token(
                        &email.unwrap(),
                        &oauth_token.unwrap(),
                        options,
                    ).await;
                } else {
                    let mut aas_token = matches.get_one::<String>("google_aas_token").map(|v| v.to_string());
                    let mut auth_token = matches.get_one::<String>("google_auth_token").map(|v| v.to_string());
                    let accept_tos = match matches.get_one::<bool>("google_accept_tos") {
                        Some(true) => true,
                        _ => false,
                    };

                    let ini_file = matches.get_one::<String>("ini").map(|ini_file| {
                        match fs::canonicalize(ini_file) {
                            Ok(ini_file) if Path::new(&ini_file).is_file() => {
                                ini_file
                            },
                            _ => {
                                println!("{}\n\nSpecified ini is not a valid file", usage);
                                std::process::exit(1);
                            },
                        }
                    });

                    if email.is_none() || (aas_token.is_none() && auth_token.is_none()) {
                        if let Ok(conf) = load_config(ini_file) {
                            if email.is_none() {
                                email = conf.get("google", "email");
                            }
                            if aas_token.is_none() && auth_token.is_none() {
                                aas_token = conf.get("google", "aas_token");
                                if aas_token.is_none() {
                                    auth_token = conf.get("google", "auth_token");
                                }
                            }
                        }
                    }

                    if email.is_none() {
                        let mut prompt_email = String::new();
                        print!("Email: ");
                        io::stdout().flush().unwrap();
                        io::stdin().read_line(&mut prompt_email).unwrap();
                        email = Some(prompt_email.trim().to_string());
                    }

                    if aas_token.is_none() && auth_token.is_none() {
                        let mut prompt_token = String::new();
                        print!("AAS Token (or AUTH Token): ");
                        io::stdout().flush().unwrap();
                        io::stdin().read_line(&mut prompt_token).unwrap();
                        let token = prompt_token.trim().to_string();
                        // Heuristic: AUTH tokens typically start with "ya29."
                        if token.starts_with("ya29.") {
                            auth_token = Some(token);
                        } else {
                            aas_token = Some(token);
                        }
                    }

                    google_play::download_apps(
                        list,
                        parallel,
                        sleep_duration,
                        &email.unwrap(),
                        aas_token.as_deref(),
                        auth_token.as_deref(),
                        &outpath.unwrap(),
                        accept_tos,
                        options,
                    )
                    .await;
                }
            }
            DownloadSource::FDroid => {
                fdroid::download_apps(list,
                    parallel,
                    sleep_duration,
                    &outpath.unwrap(),
                    options,
                ).await;
            }
            DownloadSource::HuaweiAppGallery => {
                huawei_app_gallery::download_apps(list, parallel, sleep_duration, &outpath.unwrap()).await;
            }
        }
    }
}