Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Initializer
package com.example.stins.orbotcamera;
import android.app.Application;
import android.util.Log;
import net.gotev.uploadservice.UploadService;
import net.gotev.uploadservice.okhttp.BuildConfig;
import net.gotev.uploadservice.okhttp.OkHttpStack;
import info.guardianproject.netcipher.client.StrongBuilder;
import info.guardianproject.netcipher.client.StrongOkHttpClientBuilder;
import info.guardianproject.netcipher.proxy.OrbotHelper;
import okhttp3.OkHttpClient;
/**
* Created by stins on 7/19/2017.
*/
public class Initializer extends Application {
public static volatile boolean orbotConnected = false;
@Override
public void onCreate() {
super.onCreate();
// setup the broadcast action namespace string which will
// be used to notify upload status.
// Gradle automatically generates proper variable as below.
UploadService.NAMESPACE = BuildConfig.APPLICATION_ID;
// Or, you can define it manually.
UploadService.NAMESPACE = "com.example.stins.orbotcamera";
initNetCipher();
}
private void initNetCipher() {
Log.i("NetCipherClient", "Initializing NetCipher client");
OrbotHelper.get(this).init();
try {
StrongOkHttpClientBuilder.forMaxSecurity(this).build(new StrongBuilder.Callback<OkHttpClient>() {
@Override
public void onConnected(OkHttpClient okHttpClient) {
UploadService.HTTP_STACK = new OkHttpStack(okHttpClient);
orbotConnected = true;
Log.i("NetCipherClient", "Connection to orbot established!");
// from now on, you can create upload requests as usual, and they
// will be proxied through TOR
}
@Override
public void onConnectionException(Exception e) {
// handle exception
orbotConnected = false;
Log.e("NetCipherClient", "onConnectionException()", e);
}
@Override
public void onTimeout() {
// do something on timeout
orbotConnected = false;
Log.e("NetCipherClient", "onTimeout()");
}
@Override
public void onInvalid() {
// do something on invalid
orbotConnected = false;
Log.e("NetCipherClient", "onInvalid()");
}
});
} catch (Exception exc) {
Log.e("Error", "Error while initializing TOR Proxy OkHttpClient", exc);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment