2014-04-16 20:21:42 +00:00
|
|
|
package org.torproject.android;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import org.sufficientlysecure.rootcommands.Shell;
|
|
|
|
import org.sufficientlysecure.rootcommands.command.SimpleCommand;
|
|
|
|
import org.torproject.android.service.TorResourceInstaller;
|
|
|
|
import org.torproject.android.service.TorServiceConstants;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Environment;
|
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Message;
|
|
|
|
import android.os.StatFs;
|
|
|
|
import android.text.format.Formatter;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
|
|
|
|
public class OrbotDiagnosticsActivity extends Activity {
|
|
|
|
|
|
|
|
private TextView mTextView = null;
|
|
|
|
private final static String TAG = "OrbotDiag";
|
2014-04-17 17:39:49 +00:00
|
|
|
private StringBuffer log = new StringBuffer();
|
2014-04-18 10:22:10 +00:00
|
|
|
Process mProcess;
|
2014-04-16 20:21:42 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
setContentView(R.layout.layout_diag);
|
|
|
|
|
|
|
|
mTextView = (TextView)findViewById(R.id.diaglog);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getFreeStorage ()
|
|
|
|
{
|
|
|
|
File path = Environment.getDataDirectory();
|
|
|
|
StatFs stat = new StatFs(path.getPath());
|
|
|
|
long blockSize = stat.getBlockSize();
|
|
|
|
long availableBlocks = stat.getAvailableBlocks();
|
|
|
|
return Formatter.formatFileSize(this, availableBlocks * blockSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPause() {
|
|
|
|
super.onPause();
|
2014-06-12 00:24:21 +00:00
|
|
|
|
|
|
|
stopTor();
|
2014-04-16 20:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
|
|
|
|
|
|
|
super.onDestroy();
|
2014-04-18 10:22:10 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void stopTor ()
|
|
|
|
{
|
2014-04-16 20:21:42 +00:00
|
|
|
File appBinHome = this.getDir("bin", Context.MODE_PRIVATE);
|
|
|
|
|
|
|
|
File fileTor= new File(appBinHome, TorServiceConstants.TOR_ASSET_KEY);
|
2014-04-18 10:22:10 +00:00
|
|
|
|
|
|
|
if (mProcess != null)
|
|
|
|
mProcess.destroy();
|
|
|
|
|
|
|
|
|
2014-04-16 20:21:42 +00:00
|
|
|
}
|
2014-04-18 10:22:10 +00:00
|
|
|
|
|
|
|
|
2014-04-16 20:21:42 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
|
2014-04-17 14:16:25 +00:00
|
|
|
|
|
|
|
log("Hello, Orbot!");
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
log(android.os.Build.DEVICE);
|
|
|
|
log(android.os.Build.HARDWARE);
|
|
|
|
log(android.os.Build.MANUFACTURER);
|
|
|
|
log(android.os.Build.MODEL);
|
|
|
|
log(android.os.Build.VERSION.CODENAME);
|
|
|
|
log(android.os.Build.VERSION.RELEASE);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
log("error getting device info");
|
|
|
|
}
|
|
|
|
|
2014-04-16 20:21:42 +00:00
|
|
|
showFileTree ();
|
|
|
|
|
|
|
|
runTorTest();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void runTorTest ()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
File appBinHome = this.getDir("bin", Context.MODE_PRIVATE);
|
|
|
|
File appDataHome = this.getDir("data", Context.MODE_PRIVATE);
|
|
|
|
|
|
|
|
File fileTor= new File(appBinHome, TorServiceConstants.TOR_ASSET_KEY);
|
|
|
|
enableBinExec (fileTor, appBinHome);
|
2014-04-18 10:22:10 +00:00
|
|
|
|
2014-07-14 13:19:30 +00:00
|
|
|
InputStream is = getResources().openRawResource(R.raw.torrc);
|
2014-04-16 20:21:42 +00:00
|
|
|
File fileTorrc = new File(appBinHome, TorServiceConstants.TORRC_ASSET_KEY + "diag");
|
|
|
|
TorResourceInstaller.streamToFile(is,fileTorrc, false, false);
|
|
|
|
|
|
|
|
/**
|
|
|
|
ArrayList<String> alEnv = new ArrayList<String>();
|
|
|
|
alEnv.add("HOME=" + appBinHome.getAbsolutePath());
|
|
|
|
Shell shell = Shell.startShell(alEnv,appBinHome.getAbsolutePath());
|
|
|
|
SimpleCommand cmdTor = new SimpleCommand(fileTor.getAbsolutePath() + " DataDirectory " + appDataHome.getAbsolutePath() + " -f " + fileTorrc.getAbsolutePath());
|
|
|
|
shell.add(cmdTor);
|
|
|
|
**/
|
|
|
|
|
|
|
|
String cmd = fileTor.getAbsolutePath() + " DataDirectory " + appDataHome.getAbsolutePath() + " -f " + fileTorrc.getAbsolutePath();
|
|
|
|
|
|
|
|
log ("Executing command> " + cmd);
|
|
|
|
|
2014-04-18 10:22:10 +00:00
|
|
|
mProcess = Runtime.getRuntime().exec(cmd);
|
2014-04-17 14:16:25 +00:00
|
|
|
|
2014-04-18 10:22:10 +00:00
|
|
|
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(mProcess.getInputStream()));
|
2014-04-16 20:21:42 +00:00
|
|
|
StreamGobbler sg = new StreamGobbler();
|
|
|
|
sg.reader = bufferedReader;
|
2014-04-18 10:22:10 +00:00
|
|
|
sg.process = mProcess;
|
2014-04-16 20:21:42 +00:00
|
|
|
new Thread(sg).start();
|
|
|
|
|
2014-04-18 10:22:10 +00:00
|
|
|
if (mProcess.getErrorStream() != null)
|
2014-04-17 14:16:25 +00:00
|
|
|
{
|
2014-04-18 10:22:10 +00:00
|
|
|
bufferedReader = new BufferedReader(new InputStreamReader(mProcess.getErrorStream()));
|
2014-04-17 14:16:25 +00:00
|
|
|
sg = new StreamGobbler();
|
|
|
|
sg.reader = bufferedReader;
|
2014-04-18 10:22:10 +00:00
|
|
|
sg.process = mProcess;
|
2014-04-17 14:16:25 +00:00
|
|
|
new Thread(sg).start();
|
|
|
|
}
|
2014-04-16 20:21:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Log.d(TAG,"runTorTest exception",e);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class StreamGobbler implements Runnable
|
|
|
|
{
|
|
|
|
BufferedReader reader;
|
|
|
|
Process process;
|
|
|
|
|
|
|
|
public void run ()
|
|
|
|
{
|
|
|
|
String line = null;
|
|
|
|
try {
|
|
|
|
while ( (line = reader.readLine()) != null)
|
|
|
|
{
|
|
|
|
Message msg = mHandler.obtainMessage(0);
|
|
|
|
msg.getData().putString("log", line);
|
|
|
|
mHandler.sendMessage(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.d(TAG, "error reading line",e);
|
|
|
|
}
|
|
|
|
|
2014-04-18 10:22:10 +00:00
|
|
|
//log("Tor exit code=" + process.exitValue() + ";");
|
|
|
|
|
2014-04-16 20:21:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean enableBinExec (File fileBin, File appBinHome) throws Exception
|
|
|
|
{
|
|
|
|
|
|
|
|
log(fileBin.getName() + ": PRE: Is binary exec? " + fileBin.canExecute());
|
|
|
|
|
|
|
|
if (!fileBin.canExecute())
|
|
|
|
{
|
|
|
|
log("(re)Setting permission on binary: " + fileBin.getAbsolutePath());
|
|
|
|
Shell shell = Shell.startShell(new ArrayList<String>(), appBinHome.getAbsolutePath());
|
|
|
|
|
|
|
|
shell.add(new SimpleCommand("chmod " + TorServiceConstants.CHMOD_EXE_VALUE + ' ' + fileBin.getAbsolutePath())).waitForFinish();
|
|
|
|
|
|
|
|
File fileTest = new File(fileBin.getAbsolutePath());
|
|
|
|
log(fileTest.getName() + ": POST: Is binary exec? " + fileTest.canExecute());
|
|
|
|
|
|
|
|
shell.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileBin.canExecute();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void showFileTree ()
|
|
|
|
{
|
|
|
|
|
|
|
|
File fileDir = this.getDir("bin", Context.MODE_PRIVATE);
|
|
|
|
|
2014-04-17 14:16:25 +00:00
|
|
|
if (fileDir.exists())
|
|
|
|
{
|
|
|
|
log("checking file tree: " + fileDir.getAbsolutePath());
|
|
|
|
printDir (fileDir.getName(), fileDir);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
log("app_bin does not exist");
|
|
|
|
}
|
2014-04-16 20:21:42 +00:00
|
|
|
|
2014-04-17 14:16:25 +00:00
|
|
|
fileDir = this.getDir("data", Context.MODE_PRIVATE);
|
|
|
|
if (fileDir.exists())
|
|
|
|
{
|
|
|
|
log("checking file tree: " + fileDir.getAbsolutePath());
|
|
|
|
printDir (fileDir.getName(), fileDir);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
log ("app_data does not exist");
|
|
|
|
}
|
2014-04-16 20:21:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void printDir (String path, File fileDir)
|
|
|
|
{
|
|
|
|
File[] files = fileDir.listFiles();
|
|
|
|
|
2014-04-17 14:16:25 +00:00
|
|
|
if (files != null && files.length > 0)
|
2014-04-16 20:21:42 +00:00
|
|
|
{
|
2014-04-17 14:16:25 +00:00
|
|
|
for (File file : files)
|
2014-04-16 20:21:42 +00:00
|
|
|
{
|
2014-04-17 14:16:25 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (file.isDirectory())
|
|
|
|
{
|
|
|
|
printDir(path + '/' + file.getName(), file);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
log(path + '/' + file.getName() + " len:" + file.length() + " exec:" + file.canExecute());
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
log("problem printing out file information");
|
|
|
|
}
|
|
|
|
|
2014-04-16 20:21:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Handler mHandler = new Handler ()
|
|
|
|
{
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void handleMessage(Message msg) {
|
|
|
|
|
|
|
|
super.handleMessage(msg);
|
|
|
|
|
|
|
|
String logMsg = msg.getData().getString("log");
|
|
|
|
log(logMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
private void log (String msg)
|
|
|
|
{
|
|
|
|
Log.d(TAG, msg);
|
|
|
|
mTextView.append(msg + '\n');
|
2014-04-17 17:39:49 +00:00
|
|
|
log.append(msg + '\n');
|
2014-04-16 20:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
// Inflate menu resource file.
|
|
|
|
getMenuInflater().inflate(R.menu.share_menu, menu);
|
|
|
|
|
|
|
|
// Locate MenuItem with ShareActionProvider
|
|
|
|
MenuItem item = menu.findItem(R.id.menu_item_share);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
|
|
|
|
case R.id.menu_item_share:
|
|
|
|
sendLog();
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void sendLog ()
|
|
|
|
{
|
2014-04-17 17:39:49 +00:00
|
|
|
int maxLength = 5000;
|
|
|
|
|
|
|
|
String logShare = null;
|
|
|
|
|
|
|
|
if (log.length() > maxLength)
|
|
|
|
logShare = log.substring(0, maxLength);
|
|
|
|
else
|
|
|
|
logShare = log.toString();
|
|
|
|
|
2014-04-16 20:21:42 +00:00
|
|
|
Intent sendIntent = new Intent();
|
|
|
|
sendIntent.setAction(Intent.ACTION_SEND);
|
2014-04-17 17:39:49 +00:00
|
|
|
sendIntent.putExtra(Intent.EXTRA_TEXT, logShare);
|
2014-04-16 20:21:42 +00:00
|
|
|
sendIntent.setType("text/plain");
|
|
|
|
startActivity(sendIntent);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|