Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build update #45

Merged
merged 3 commits into from Feb 13, 2018
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Prev

Introduce TorConfig and TorInstaller

  • Loading branch information...
sisbell committed Feb 10, 2018
commit 0ebdd33a287a5d7b10f40a0bea7bcec7d76d257f
@@ -68,6 +68,8 @@ task sourceJar(type: Jar) {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.msopentech.thali:universal:0.0.3'
implementation 'org.slf4j:slf4j-api:1.7.25'
implementation 'org.slf4j:slf4j-android:1.7.25'
implementation 'org.torproject:tor-android-binary:0.3.1.9a'
implementation 'com.android.support:appcompat-v7:26.1.0'
testImplementation 'junit:junit:4.12'
@@ -114,15 +114,15 @@ public void testHiddenServiceRecycleTime() throws IOException, InterruptedExcept
// Note: Normally you never want to call anything like deleteTorWorkingDirectory since this
// is where all the cached data about the Tor network is kept and it makes connectivity
// must faster. We are deleting it here just to make sure we are running clean tests.
deleteTorWorkingDirectory(hiddenServiceManager.getWorkingDirectory());
deleteTorWorkingDirectory(hiddenServiceManager.getContext().getConfig().getConfigDir());
hiddenServiceManager.setup();

assertTrue(hiddenServiceManager.startWithRepeat(TOTAL_SECONDS_PER_TOR_STARTUP, TOTAL_TRIES_PER_TOR_STARTUP, true));

LOG.warn("Hidden Service Manager is running.");

clientManager = getOnionProxyManager(clientManagerDirectoryName);
deleteTorWorkingDirectory(clientManager.getWorkingDirectory());
deleteTorWorkingDirectory(clientManager.getContext().getConfig().getConfigDir());
clientManager.setup();

assertTrue(clientManager.startWithRepeat(TOTAL_SECONDS_PER_TOR_STARTUP, TOTAL_TRIES_PER_TOR_STARTUP, true));
@@ -141,7 +141,7 @@ public void testHiddenServiceRecycleTime() throws IOException, InterruptedExcept
// in which case we would actually be testing against a new hidden service which would
// remove the point of this test. So we check that they are the same.
assertEquals(runHiddenServiceTest(hiddenServiceManager, clientManager), onionAddress);
} finally {
} finally {
if (hiddenServiceManager != null) {
hiddenServiceManager.stop();
}
@@ -13,32 +13,23 @@

package com.msopentech.thali.android.toronionproxy;

import android.content.Context;
import com.msopentech.thali.toronionproxy.OnionProxyContext;
import com.msopentech.thali.toronionproxy.TorInstaller;
import com.msopentech.thali.toronionproxy.WriteObserver;

import org.torproject.android.binary.TorResourceInstaller;
import android.content.Context;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeoutException;

import static android.content.Context.MODE_PRIVATE;
import static org.torproject.android.binary.TorResourceInstaller.streamToFile;
import static org.torproject.android.binary.TorServiceConstants.TORRC_ASSET_KEY;
import static org.torproject.android.binary.TorServiceConstants.TOR_ASSET_KEY;

public class AndroidOnionProxyContext extends OnionProxyContext {

private final Context context;

private final TorResourceInstaller resourceInstaller;

public AndroidOnionProxyContext(Context context, String workingSubDirectoryName) {
super(context.getDir(workingSubDirectoryName, MODE_PRIVATE));
public AndroidOnionProxyContext(Context context, String installDir) {
super(AndroidTorConfig.createConfig(context.getDir(installDir, MODE_PRIVATE)));
this.context = context;
this.resourceInstaller = new TorResourceInstaller(context, getWorkingDirectory());
}

@Override
@@ -47,22 +38,8 @@ public WriteObserver generateWriteObserver(File file) {
}

@Override
public String getTorExecutableFileName() {
return TOR_ASSET_KEY;
}

@Override
public boolean setup() throws IOException {
try {
if(resourceInstaller.installResources()) {
InputStream is = context.getAssets().open(TORRC_ASSET_KEY);
File outFile = new File(getWorkingDirectory(), TORRC_ASSET_KEY);
return streamToFile(is, outFile, false, false);
}
return false;
} catch (TimeoutException e) {
throw new IOException(e);
}
public TorInstaller getInstaller() {
return new AndroidTorInstaller(context, getConfig().getConfigDir());
}

@Override
@@ -48,9 +48,11 @@
import static android.net.ConnectivityManager.EXTRA_NO_CONNECTIVITY;

public class AndroidOnionProxyManager extends OnionProxyManager {

private static final Logger LOG = LoggerFactory.getLogger(AndroidOnionProxyManager.class);

private volatile BroadcastReceiver networkStateReceiver;

private final Context context;

public AndroidOnionProxyManager(Context context, String workingSubDirectoryName) {
@@ -59,15 +61,12 @@ public AndroidOnionProxyManager(Context context, String workingSubDirectoryName)
}

@Override
public synchronized boolean start(boolean enableLogs) throws IOException {
if (super.start(enableLogs)) {
// Register to receive network status events
networkStateReceiver = new NetworkStateReceiver();
IntentFilter filter = new IntentFilter(CONNECTIVITY_ACTION);
context.registerReceiver(networkStateReceiver, filter);
return true;
}
return false;
public synchronized void start(boolean enableLogs) throws IOException {
super.start(enableLogs);
// Register to receive network status events
networkStateReceiver = new NetworkStateReceiver();
IntentFilter filter = new IntentFilter(CONNECTIVITY_ACTION);
context.registerReceiver(networkStateReceiver, filter);
}

@Override
@@ -78,37 +77,44 @@ public synchronized void stop() throws IOException {
if (networkStateReceiver != null) {
try {
context.unregisterReceiver(networkStateReceiver);
} catch(IllegalArgumentException e) {
} catch (IllegalArgumentException e) {
// There is a race condition where if someone calls stop before installAndStartTorOp is done
// then we could get an exception because the network state receiver might not be properly
// registered.
LOG.info("Someone tried to call stop before we had finished registering the receiver", e);
LOG.info(
"Someone tried to call stop before we had finished registering the receiver",
e);
}
}
}
}

private class NetworkStateReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context ctx, Intent i) {
try {
if(!isRunning()) return;
if (!isRunning()) {
return;
}
} catch (IOException e) {
LOG.info("Did someone call before Tor was ready?", e);
return;
}
boolean online = !i.getBooleanExtra(EXTRA_NO_CONNECTIVITY, false);
if(online) {
if (online) {
// Some devices fail to set EXTRA_NO_CONNECTIVITY, double check
Object o = ctx.getSystemService(CONNECTIVITY_SERVICE);
ConnectivityManager cm = (ConnectivityManager) o;
NetworkInfo net = cm.getActiveNetworkInfo();
if(net == null || !net.isConnected()) online = false;
if (net == null || !net.isConnected()) {
online = false;
}
}
LOG.info("Online: " + online);
try {
enableNetwork(online);
} catch(IOException e) {
} catch (IOException e) {
LOG.warn(e.toString(), e);
}
}
@@ -0,0 +1,29 @@
/*
Copyright (c) Microsoft Open Technologies, Inc.
All Rights Reserved
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED,
INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache 2 License for the specific language governing permissions and limitations under the License.
*/
package com.msopentech.thali.android.toronionproxy;

import com.msopentech.thali.toronionproxy.TorConfig;

import java.io.File;

/**
* Creates config file that is compatible with Android.
*/
public class AndroidTorConfig {

public static TorConfig createConfig(File configDir) {
TorConfig.Builder builder = new TorConfig.Builder(configDir);
builder.dataDir(new File(configDir, ".tor"));
return builder.build();
}
}
@@ -0,0 +1,67 @@
/*
Copyright (c) Microsoft Open Technologies, Inc.
All Rights Reserved
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED,
INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache 2 License for the specific language governing permissions and limitations under the License.
*/
package com.msopentech.thali.android.toronionproxy;

import com.msopentech.thali.toronionproxy.TorInstaller;

import org.torproject.android.binary.TorResourceInstaller;

import android.content.Context;
import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeoutException;

import static org.torproject.android.binary.TorResourceInstaller.streamToFile;
import static org.torproject.android.binary.TorServiceConstants.TORRC_ASSET_KEY;

/**
* Installs Tor for an Android app. This is a wrapper around the <code>TorResourceInstaller</code>.
*
* This installer will override the torrc file that is provided by <code>org.torproject.android.binary</code>
* with the torrc in the toronionproxy assets folder.
*/
public class AndroidTorInstaller extends TorInstaller {

private final TorResourceInstaller resourceInstaller;

private final Context context;

private final File installDir;

private static final String TAG = "TorInstaller";

public AndroidTorInstaller(Context context, File installDir) {
this.context = context;
this.installDir = installDir;
this.resourceInstaller = new TorResourceInstaller(context, installDir);
}

@Override
public boolean setup() throws IOException {
try {
if (resourceInstaller.installResources()) {
InputStream is = context.getAssets().open(TORRC_ASSET_KEY);
File outFile = new File(installDir, TORRC_ASSET_KEY);
return streamToFile(is, outFile, false, false);
}
Log.w(TAG, "Failed to setup tor");
return false;
} catch (TimeoutException e) {
Log.w(TAG, "Failed to setup tor: " + e.getMessage());
throw new IOException(e);
}
}
}
@@ -29,9 +29,10 @@

package com.msopentech.thali.android.toronionproxy;

import android.os.FileObserver;
import com.msopentech.thali.toronionproxy.WriteObserver;

import android.os.FileObserver;

import java.io.File;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@@ -13,10 +13,11 @@ repositories {
}

dependencies {
implementation 'org.slf4j:slf4j-api:1.7.7'
implementation 'net.freehaven.tor.control:jtorctl:0.2'
implementation project(':universal')
testCompile group: 'junit', name: 'junit', version: '4.12'
implementation 'org.slf4j:slf4j-api:1.7.25'
implementation 'org.slf4j:slf4j-simple:1.7.25'
implementation 'net.freehaven.tor.control:jtorctl:0.2'
implementation project(':universal')
testCompile group: 'junit', name: 'junit', version: '4.12'
}


ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.