2016-06-29 06:20:20 +02:00
|
|
|
/* Copyright (c) 2009, Nathan Freitas, Orbot / The Guardian Project - http://openideals.com/guardian */
|
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
2017-11-17 16:40:46 +01:00
|
|
|
package org.torproject.android.binary;
|
2016-06-29 06:20:20 +02:00
|
|
|
|
|
|
|
import java.io.DataInputStream;
|
|
|
|
import java.io.DataOutputStream;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.io.PrintStream;
|
|
|
|
import java.io.StringBufferInputStream;
|
|
|
|
import java.util.concurrent.TimeoutException;
|
|
|
|
import java.util.zip.ZipEntry;
|
|
|
|
import java.util.zip.ZipInputStream;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.os.Build;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2016-09-26 18:03:30 +02:00
|
|
|
|
2016-06-29 06:20:20 +02:00
|
|
|
public class TorResourceInstaller implements TorServiceConstants {
|
|
|
|
|
|
|
|
|
|
|
|
File installFolder;
|
|
|
|
Context context;
|
|
|
|
|
|
|
|
public TorResourceInstaller (Context context, File installFolder)
|
|
|
|
{
|
|
|
|
this.installFolder = installFolder;
|
|
|
|
|
|
|
|
this.context = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void deleteDirectory(File file) {
|
|
|
|
if( file.exists() ) {
|
|
|
|
if (file.isDirectory()) {
|
|
|
|
File[] files = file.listFiles();
|
|
|
|
for(int i=0; i<files.length; i++) {
|
|
|
|
if(files[i].isDirectory()) {
|
|
|
|
deleteDirectory(files[i]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
files[i].delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
file.delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private final static String MP3_EXT = ".mp3";
|
|
|
|
//
|
|
|
|
/*
|
|
|
|
* Extract the Tor resources from the APK file using ZIP
|
|
|
|
*/
|
2018-01-04 04:49:40 +01:00
|
|
|
public boolean installResources () throws IOException, TimeoutException
|
2016-06-29 06:20:20 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
InputStream is;
|
|
|
|
File outFile;
|
|
|
|
|
|
|
|
String cpuPath = "armeabi";
|
2017-11-17 16:58:42 +01:00
|
|
|
|
2016-06-29 06:20:20 +02:00
|
|
|
if (Build.CPU_ABI.contains("x86"))
|
|
|
|
cpuPath = "x86";
|
2016-09-19 22:11:30 +02:00
|
|
|
|
2016-06-29 06:20:20 +02:00
|
|
|
deleteDirectory(installFolder);
|
|
|
|
|
|
|
|
installFolder.mkdirs();
|
|
|
|
|
2017-11-17 16:58:42 +01:00
|
|
|
is = context.getAssets().open(COMMON_ASSET_KEY + TORRC_ASSET_KEY);
|
2016-06-29 06:20:20 +02:00
|
|
|
outFile = new File(installFolder, TORRC_ASSET_KEY);
|
|
|
|
streamToFile(is,outFile, false, false);
|
2017-11-17 16:58:42 +01:00
|
|
|
|
|
|
|
is = context.getAssets().open(COMMON_ASSET_KEY + POLIPOCONFIG_ASSET_KEY);
|
2016-06-29 06:20:20 +02:00
|
|
|
outFile = new File(installFolder, POLIPOCONFIG_ASSET_KEY);
|
|
|
|
streamToFile(is,outFile, false, false);
|
2016-09-19 22:11:30 +02:00
|
|
|
|
2016-06-29 06:20:20 +02:00
|
|
|
is = context.getAssets().open(cpuPath + '/' + TOR_ASSET_KEY + MP3_EXT);
|
|
|
|
outFile = new File(installFolder, TOR_ASSET_KEY);
|
|
|
|
streamToFile(is,outFile, false, true);
|
|
|
|
setExecutable(outFile);
|
|
|
|
|
|
|
|
is = context.getAssets().open(cpuPath + '/' + POLIPO_ASSET_KEY + MP3_EXT);
|
|
|
|
outFile = new File(installFolder, POLIPO_ASSET_KEY);
|
|
|
|
streamToFile(is,outFile, false, true);
|
|
|
|
setExecutable(outFile);
|
2017-11-17 16:58:42 +01:00
|
|
|
|
2016-06-29 06:20:20 +02:00
|
|
|
installGeoIP();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean updateTorConfigCustom (File fileTorRcCustom, String extraLines) throws IOException, FileNotFoundException, TimeoutException
|
|
|
|
{
|
|
|
|
if (fileTorRcCustom.exists())
|
|
|
|
{
|
|
|
|
fileTorRcCustom.delete();
|
|
|
|
Log.d("torResources","deleting existing torrc.custom");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
fileTorRcCustom.createNewFile();
|
|
|
|
|
|
|
|
FileOutputStream fos = new FileOutputStream(fileTorRcCustom, false);
|
|
|
|
PrintStream ps = new PrintStream(fos);
|
|
|
|
ps.print(extraLines);
|
|
|
|
ps.close();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean updatePolipoConfig (File filePolipo, String extraLines) throws IOException, FileNotFoundException, TimeoutException
|
|
|
|
{
|
|
|
|
|
|
|
|
InputStream is;
|
|
|
|
|
2017-11-17 16:58:42 +01:00
|
|
|
is = context.getAssets().open(COMMON_ASSET_KEY + POLIPOCONFIG_ASSET_KEY);
|
2016-06-29 06:20:20 +02:00
|
|
|
streamToFile(is,filePolipo, false, false);
|
|
|
|
|
|
|
|
if (extraLines != null && extraLines.length() > 0)
|
|
|
|
{
|
|
|
|
StringBufferInputStream sbis = new StringBufferInputStream('\n' + extraLines + '\n');
|
|
|
|
streamToFile(sbis,filePolipo,true,false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean installPolipoConf () throws IOException, FileNotFoundException, TimeoutException
|
|
|
|
{
|
|
|
|
|
|
|
|
InputStream is;
|
|
|
|
File outFile;
|
|
|
|
|
2017-11-17 16:58:42 +01:00
|
|
|
is = context.getAssets().open(COMMON_ASSET_KEY + POLIPOCONFIG_ASSET_KEY);
|
2016-06-29 06:20:20 +02:00
|
|
|
outFile = new File(installFolder, POLIPOCONFIG_ASSET_KEY);
|
|
|
|
streamToFile(is,outFile, false, false);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Extract the Tor binary from the APK file using ZIP
|
|
|
|
*/
|
|
|
|
|
2018-01-04 04:49:40 +01:00
|
|
|
private boolean installGeoIP () throws IOException
|
2016-06-29 06:20:20 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
InputStream is;
|
|
|
|
File outFile;
|
|
|
|
|
|
|
|
outFile = new File(installFolder, GEOIP_ASSET_KEY);
|
2018-01-04 04:49:40 +01:00
|
|
|
is = context.getAssets().open(COMMON_ASSET_KEY + GEOIP_ASSET_KEY);
|
|
|
|
streamToFile(is, outFile, false, false);
|
2017-11-17 16:58:42 +01:00
|
|
|
|
2018-01-04 04:49:40 +01:00
|
|
|
is = context.getAssets().open(COMMON_ASSET_KEY + GEOIP6_ASSET_KEY);
|
2016-06-29 06:20:20 +02:00
|
|
|
outFile = new File(installFolder, GEOIP6_ASSET_KEY);
|
2018-01-04 04:49:40 +01:00
|
|
|
streamToFile(is, outFile, false, false);
|
2016-06-29 06:20:20 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-17 16:58:42 +01:00
|
|
|
|
2016-06-29 06:20:20 +02:00
|
|
|
/*
|
|
|
|
* Write the inputstream contents to the file
|
|
|
|
*/
|
|
|
|
public 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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-11-17 18:37:08 +01:00
|
|
|
|
2016-06-29 06:20:20 +02:00
|
|
|
|
|
|
|
private void setExecutable(File fileBin) {
|
|
|
|
fileBin.setReadable(true);
|
|
|
|
fileBin.setExecutable(true);
|
|
|
|
fileBin.setWritable(false);
|
|
|
|
fileBin.setWritable(true, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|