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 Jan 26, 2024
1 parent 9c2be4c commit d89f678
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
10 changes: 9 additions & 1 deletion app/src/main/java/org/torproject/android/OrbotApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import org.torproject.android.service.OrbotConstants
import org.torproject.android.service.util.Prefs
import java.util.Locale


class OrbotApp : Application(), OrbotConstants {


Expand All @@ -35,6 +34,15 @@ class OrbotApp : Application(), OrbotConstants {

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

// 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 fun onConfigurationChanged(newConfig: Configuration) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,13 +460,20 @@ 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 @@ -1096,11 +1103,10 @@ private StringBuffer processSettingsImpl(StringBuffer extraLines) throws IOExcep
extraLines = processSettingsImplObfs4(extraLines);
}
}
//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 @@ -38,6 +38,8 @@ public class Prefs {
private final static String PREF_SNOWFLAKES_SERVED_COUNT = "pref_snowflakes_served";
private final static String PREF_SNOWFLAKES_SERVED_COUNT_WEEKLY = "pref_snowflakes_served_weekly";

private static final String PREF_CURRENT_VERSION = "pref_current_version";

private static final String PREF_CONNECTION_PATHWAY = "pref_connection_pathway";
public static final String PATHWAY_SMART = "smart", PATHWAY_DIRECT = "direct",
PATHWAY_SNOWFLAKE = "snowflake", PATHWAY_SNOWFLAKE_AMP = "snowflake_amp", PATHWAY_CUSTOM = "custom";
Expand All @@ -46,6 +48,22 @@ public class Prefs {

private static SharedPreferences prefs;

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 d89f678

Please sign in to comment.