commit b7d100cac5beb4b68c211d027acff5f4ab17b93c Author: n8fr8 nathan@guardianproject.info Date: Mon Jul 8 14:07:33 2019 -0400
fixes #239 #229 #227 #205 and other VPN / DNS issues moved PDNSD daemon to load from the .so native path --- .../android/service/util/CustomNativeLoader.java | 48 ++++-- .../service/util/CustomTorResourceInstaller.java | 4 +- .../android/service/vpn/OrbotVpnManager.java | 11 +- .../android/service/vpn/PDNSDInstaller.java | 185 --------------------- orbotservice/src/main/jni/Android.mk | 1 + 5 files changed, 47 insertions(+), 202 deletions(-)
diff --git a/orbotservice/src/main/java/org/torproject/android/service/util/CustomNativeLoader.java b/orbotservice/src/main/java/org/torproject/android/service/util/CustomNativeLoader.java index 0df6784a..e44340b4 100644 --- a/orbotservice/src/main/java/org/torproject/android/service/util/CustomNativeLoader.java +++ b/orbotservice/src/main/java/org/torproject/android/service/util/CustomNativeLoader.java @@ -1,6 +1,7 @@ package org.torproject.android.service.util;
import android.content.Context; +import android.content.pm.ApplicationInfo; import android.os.Build; import android.util.Log;
@@ -13,12 +14,9 @@ import java.util.zip.ZipFile;
public class CustomNativeLoader {
- private final static String LIB_NAME = "tor"; - private final static String LIB_SO_NAME = "tor.so"; + private final static String TAG = "CNL";
- private final static String TAG = "TorNativeLoader"; - - private static boolean loadFromZip(Context context, File destLocalFile, String arch) { + private static boolean loadFromZip(Context context, String libname, File destLocalFile, String arch) {
ZipFile zipFile = null; @@ -26,9 +24,9 @@ public class CustomNativeLoader {
try { zipFile = new ZipFile(context.getApplicationInfo().sourceDir); - ZipEntry entry = zipFile.getEntry("lib/" + arch + "/" + LIB_SO_NAME); + ZipEntry entry = zipFile.getEntry("lib/" + arch + "/" + libname + ".so"); if (entry == null) { - throw new Exception("Unable to find file in apk:" + "lib/" + arch + "/" + LIB_NAME); + throw new Exception("Unable to find file in apk:" + "lib/" + arch + "/" + libname); }
//how we wrap this in another stream because the native .so is zipped itself @@ -69,9 +67,26 @@ public class CustomNativeLoader { return false; }
- public static synchronized File initNativeLibs(Context context, File destLocalFile) { + public static File loadNativeBinary(Context context, String libname, File destLocalFile) {
try { + + + File fileNativeBin = new File(getNativeLibraryDir(context),libname + ".so"); + + if (fileNativeBin.exists()) + { + if (fileNativeBin.canExecute()) + return fileNativeBin; + else + { + setExecutable(fileNativeBin); + + if (fileNativeBin.canExecute()) + return fileNativeBin; + } + } + String folder = Build.CPU_ABI;
@@ -80,8 +95,7 @@ public class CustomNativeLoader { folder = "x86"; }
- - if (loadFromZip(context, destLocalFile, folder)) { + if (loadFromZip(context, libname, destLocalFile, folder)) { return destLocalFile; }
@@ -92,5 +106,19 @@ public class CustomNativeLoader {
return null; } + + private static void setExecutable(File fileBin) { + fileBin.setReadable(true); + fileBin.setExecutable(true); + fileBin.setWritable(false); + fileBin.setWritable(true, true); + } + + // Return Full path to the directory where native JNI libraries are stored. + private static String getNativeLibraryDir(Context context) { + ApplicationInfo appInfo = context.getApplicationInfo(); + return appInfo.nativeLibraryDir; + } + }
diff --git a/orbotservice/src/main/java/org/torproject/android/service/util/CustomTorResourceInstaller.java b/orbotservice/src/main/java/org/torproject/android/service/util/CustomTorResourceInstaller.java index 7e9dcc55..a2561238 100644 --- a/orbotservice/src/main/java/org/torproject/android/service/util/CustomTorResourceInstaller.java +++ b/orbotservice/src/main/java/org/torproject/android/service/util/CustomTorResourceInstaller.java @@ -74,9 +74,7 @@ public class CustomTorResourceInstaller implements TorServiceConstants { }
//let's try another approach - fileTor = new File(installFolder, TOR_ASSET_KEY); - //fileTor = NativeLoader.initNativeLibs(context,fileTor); - CustomNativeLoader.initNativeLibs(context,fileTor); + fileTor = CustomNativeLoader.loadNativeBinary(context,TOR_ASSET_KEY,new File(installFolder, TOR_ASSET_KEY));
setExecutable(fileTor);
diff --git a/orbotservice/src/main/java/org/torproject/android/service/vpn/OrbotVpnManager.java b/orbotservice/src/main/java/org/torproject/android/service/vpn/OrbotVpnManager.java index 541fd12f..7233159f 100644 --- a/orbotservice/src/main/java/org/torproject/android/service/vpn/OrbotVpnManager.java +++ b/orbotservice/src/main/java/org/torproject/android/service/vpn/OrbotVpnManager.java @@ -36,6 +36,7 @@ import com.runjva.sourceforge.jsocks.protocol.ProxyServer; import com.runjva.sourceforge.jsocks.server.ServerAuthenticatorNone;
import org.torproject.android.service.R; +import org.torproject.android.service.util.CustomNativeLoader;
import java.io.BufferedReader; import java.io.File; @@ -79,6 +80,7 @@ public class OrbotVpnManager implements Handler.Callback {
File filePdnsd = null;
+ private final static String PDNSD_BIN = "pdnsd"; private final static int PDNSD_PORT = 8091;
private boolean isRestart = false; @@ -89,7 +91,8 @@ public class OrbotVpnManager implements Handler.Callback { public OrbotVpnManager (VpnService service) throws IOException, TimeoutException { mService = service;
- filePdnsd = new PDNSDInstaller(service.getApplicationContext(),service.getFilesDir()).installResources(); + + filePdnsd = CustomNativeLoader.loadNativeBinary(service.getApplicationContext(),PDNSD_BIN,new File(service.getFilesDir(),PDNSD_BIN));
Tun2Socks.init();
@@ -343,7 +346,7 @@ public class OrbotVpnManager implements Handler.Callback { isRestart = false;
//start PDNSD daemon pointing to actual DNS - startDNS("127.0.0.1",localDns); + startDNS(filePdnsd.getCanonicalPath(), "127.0.0.1",localDns);
Tun2Socks.Start(mInterface, VPN_MTU, virtualIP, virtualNetMask, localSocks , localDNS , localDnsTransparentProxy);
@@ -404,14 +407,14 @@ public class OrbotVpnManager implements Handler.Callback { }
- private void startDNS (String dns, int port) throws IOException, TimeoutException + private void startDNS (String pdnsPath, String dns, int port) throws IOException, TimeoutException {
File fileConf = makePdnsdConf(mService, dns, port,mService.getFilesDir()); // ArrayList<String> customEnv = new ArrayList<String>();
- String[] cmdString = {filePdnsd.getCanonicalPath(),"-c",fileConf.toString()}; + String[] cmdString = {pdnsPath,"-c",fileConf.toString()}; ProcessBuilder pb = new ProcessBuilder(cmdString); pb.redirectErrorStream(true); Process proc = pb.start(); diff --git a/orbotservice/src/main/java/org/torproject/android/service/vpn/PDNSDInstaller.java b/orbotservice/src/main/java/org/torproject/android/service/vpn/PDNSDInstaller.java deleted file mode 100644 index d19b7212..00000000 --- a/orbotservice/src/main/java/org/torproject/android/service/vpn/PDNSDInstaller.java +++ /dev/null @@ -1,185 +0,0 @@ -package org.torproject.android.service.vpn; - -import android.content.Context; -import android.content.pm.ApplicationInfo; -import android.util.Log; - -import org.torproject.android.service.util.CustomNativeLoader; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.concurrent.TimeoutException; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; - -import static org.torproject.android.service.vpn.VpnConstants.FILE_WRITE_BUFFER_SIZE; - -public class PDNSDInstaller { - - private final static String LIB_NAME = "pdnsd"; - private final static String LIB_SO_NAME = "pdnsd.so"; - - private final static String TAG = "TorNativeLoader"; - - private File installFolder; - private Context context; - private File filePdnsd; - - public PDNSDInstaller (Context context, File installFolder) - { - this.installFolder = installFolder; - this.context = context; - } - - // - /* - * Extract the Tor resources from the APK file using ZIP - * - * @File path to the Tor executable - */ - public File installResources () throws IOException, TimeoutException - { - - filePdnsd = new File(installFolder, LIB_NAME); - - if (!installFolder.exists()) - installFolder.mkdirs(); - - File fileNativeDir = new File(getNativeLibraryDir(context)); - filePdnsd = new File(fileNativeDir,LIB_NAME + ".so"); - - if (filePdnsd.exists()) - { - if (filePdnsd.canExecute()) - return filePdnsd; - else - { - setExecutable(filePdnsd); - - if (filePdnsd.canExecute()) - return filePdnsd; - } - } - - if (filePdnsd.exists()) { - InputStream is = new FileInputStream(filePdnsd); - streamToFile(is, filePdnsd, false, true); - setExecutable(filePdnsd); - - if (filePdnsd.exists() && filePdnsd.canExecute()) - return filePdnsd; - } - - //let's try another approach - filePdnsd = new File(installFolder, LIB_NAME); - //fileTor = NativeLoader.initNativeLibs(context,fileTor); - CustomNativeLoader.initNativeLibs(context,filePdnsd); - - setExecutable(filePdnsd); - - if (filePdnsd != null && filePdnsd.exists() && filePdnsd.canExecute()) - return filePdnsd; - - return null; - } - - - // Return Full path to the directory where native JNI libraries are stored. - private static String getNativeLibraryDir(Context context) { - ApplicationInfo appInfo = context.getApplicationInfo(); - return appInfo.nativeLibraryDir; - } - - - - - - - /* - * Reads file from assetPath/assetKey writes it to the install folder - */ - private File assetToFile(String assetPath, String assetKey, boolean isZipped, boolean isExecutable) throws IOException { - InputStream is = context.getAssets().open(assetPath); - File outFile = new File(installFolder, assetKey); - streamToFile(is, outFile, false, isZipped); - if (isExecutable) { - setExecutable(outFile); - } - return outFile; - } - - - /* - * Write the inputstream contents to the file - */ - private static boolean streamToFile(InputStream stm, File outFile, boolean append, boolean zip) throws IOException - - { - byte[] buffer = new byte[FILE_WRITE_BUFFER_SIZE]; - - int bytecount; - - OutputStream stmOut = new FileOutputStream(outFile.getAbsolutePath(), append); - ZipInputStream zis = null; - - if (zip) - { - zis = new ZipInputStream(stm); - ZipEntry ze = zis.getNextEntry(); - stm = zis; - - } - - while ((bytecount = stm.read(buffer)) > 0) - { - - stmOut.write(buffer, 0, bytecount); - - } - - stmOut.close(); - stm.close(); - - if (zis != null) - zis.close(); - - - return true; - - } - - - - private void setExecutable(File fileBin) { - fileBin.setReadable(true); - fileBin.setExecutable(true); - fileBin.setWritable(false); - fileBin.setWritable(true, true); - } - - private static File[] listf(String directoryName) { - - // .............list file - File directory = new File(directoryName); - - // get all the files from a directory - File[] fList = directory.listFiles(); - - if (fList != null) - for (File file : fList) { - if (file.isFile()) { - Log.d(TAG,file.getAbsolutePath()); - } else if (file.isDirectory()) { - listf(file.getAbsolutePath()); - } - } - - return fList; - } - - -} diff --git a/orbotservice/src/main/jni/Android.mk b/orbotservice/src/main/jni/Android.mk index c4448dbc..10accb58 100644 --- a/orbotservice/src/main/jni/Android.mk +++ b/orbotservice/src/main/jni/Android.mk @@ -29,6 +29,7 @@ LOCAL_MODULE := pdnsd LOCAL_SRC_FILES := $(PDNSD_SOURCES:$(LOCAL_PATH)/%=%) LOCAL_CFLAGS := -Wall -O2 -I$(LOCAL_PATH)/pdnsd -DHAVE_STPCPY
+ include $(BUILD_EXECUTABLE)
########################################################