Skip to content

Commit

Permalink
Optimization for guardianproject#1046 only extract geoip and geoip6 f…
Browse files Browse the repository at this point in the history
…rom zip

if those files dont already exist or if the app has been updated

added very lightweight logic in Application subclass to set flags for geoip
and other events if and only if an app update has occured
  • Loading branch information
bitmold authored and syphyr committed Dec 22, 2023
1 parent daae526 commit 6168383
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
9 changes: 9 additions & 0 deletions app/src/main/java/org/torproject/android/OrbotApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ public void onCreate() {

deleteDatabase("hidden_services"); // if it exists, remove v2 onion service data

// this code only runs on first install and app updates
if (Prefs.getCurrentVersionForUpdate() < BuildConfig.VERSION_CODE) {
Prefs.setCurrentVersionForUpdate(BuildConfig.VERSION_CODE);
// don't do anything resource intensive here, instead set a flag to do the task later

// tell OrbotService it needs to reinstall geoip
Prefs.setIsGeoIpReinstallNeeded(true);
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -490,14 +490,19 @@ public void onCreate() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
createNotificationChannel();

try {
CustomTorResourceInstaller installer = new CustomTorResourceInstaller(this, appBinHome);
installer.installGeoIP();
}
catch (IOException io) {
Log.e(TAG, "Error installing geoip files", io);
logNotice(getString(R.string.log_notice_geoip_error));
}
var hasGeoip = new File(appBinHome, GEOIP_ASSET_KEY).exists();
var hasGeoip6 = new File(appBinHome, GEOIP6_ASSET_KEY).exists();

// only write out geoip files if there's an app update or they don't exist
if (!hasGeoip || !hasGeoip6 || Prefs.isGeoIpReinstallNeeded()) {
try {
new CustomTorResourceInstaller(this, appBinHome).installGeoIP();
Prefs.setIsGeoIpReinstallNeeded(false);
} catch (IOException io) {
Log.e(TAG, "Error installing geoip files", io);
logNotice(getString(R.string.log_notice_geoip_error));
}
} else Log.d(TAG, "no need to write geoip");

pluggableTransportInstall();

Expand Down Expand Up @@ -1079,11 +1084,10 @@ private StringBuffer processSettingsImpl(StringBuffer extraLines) throws IOExcep
}
}

//only apply GeoIP if you need it
var fileGeoIP = new File(appBinHome, GEOIP_ASSET_KEY);
var fileGeoIP6 = new File(appBinHome, GEOIP6_ASSET_KEY);

if (fileGeoIP.exists()) {
if (fileGeoIP.exists()) { // only apply geoip if it exists
extraLines.append("GeoIPFile" + ' ').append(fileGeoIP.getCanonicalPath()).append('\n');
extraLines.append("GeoIPv6File" + ' ').append(fileGeoIP6.getCanonicalPath()).append('\n');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,27 @@ public class Prefs {

private final static String PREF_SNOWFLAKES_SERVED_COUNT = "pref_snowflakes_served";

private static final String PREF_CURRENT_VERSION = "pref_current_version";

private static SharedPreferences prefs;
private static Context sContext;

public static int getCurrentVersionForUpdate() {
return prefs.getInt(PREF_CURRENT_VERSION, 0);
}

public static void setCurrentVersionForUpdate(int version) {
putInt(PREF_CURRENT_VERSION, version);
}

private static final String PREF_REINSTALL_GEOIP = "pref_geoip";
public static boolean isGeoIpReinstallNeeded() {
return prefs.getBoolean(PREF_REINSTALL_GEOIP, true);
}
public static void setIsGeoIpReinstallNeeded(boolean reinstallNeeded) {
putBoolean(PREF_REINSTALL_GEOIP, reinstallNeeded);
}

public static void setContext(Context context) {
if (prefs == null)
prefs = getSharedPrefs(context);
Expand Down

0 comments on commit 6168383

Please sign in to comment.