Giter Site home page Giter Site logo

mirsamantajbakhsh / torandroid Goto Github PK

View Code? Open in Web Editor NEW
7.0 2.0 6.0 32.14 MB

Tor Binary in Android as a Library

Home Page: https://mstajbakhsh.ir/tor-android-library/

Java 100.00%
tor-android-library tor hidden-services android library torrc

torandroid's Introduction

Credits from: https://android.gadgethacks.com/how-to/tor-for-android-stay-anonymous-your-phone-0163360/

Tor Android

YATLA (Yet Another Tor Library for Android) Version 2

Tor Android is a library which included Tor binary inside it. Tor Binary is grabbed from Guardian Project's maven. Some helper libraries added in order to make life easier. In the current version, 2 main features added including support for multiple hidden services, and ability to load former torrc file.

Changelog

  • Bug fixed #4 : Now the library works when there is no hidden service available.
  • Connect Mode added to README.

How To Import

It is very simple. Just implement the following package in your application Gradle file:

implementation 'ir.mstajbakhsh:tor-android:2.0.0447'

# Or Use JitPack

allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}

dependencies {
	        implementation 'com.github.mirsamantajbakhsh:TorAndroid:2.0.0447'
	}

Code Example

Creation Mode

In creation mode, every previous configuration will be overwritten. Any previous Hidden Services (private keys) will be lost. This action can not be undone. It is recommended to use creation mode for first time.

HiddenService hs1 = new HiddenService();
hs1.addHiddenServiceMapping(8080, new Address("127.0.0.1", 8080));
hs1.addHiddenServiceMapping(8123, new Address("127.0.0.1", 8123));

HiddenService hs2 = new HiddenService();
hs2.addHiddenServiceMapping(80, new Address("192.168.1.24", 80));

List<HiddenService> hss = new ArrayList<>();
hss.add(hs1);
hss.add(hs2);

TorProxy.TorBuilder tb = new TorProxy.TorBuilder()
	.setDebuggable(true)
	.setSOCKsPort(9050)
	.setServices(hss);

final TorProxy tp = tb.build(appContext);
tp.init();
try {
	tp.start(new IConnectionDone() {
	@Override
	public void onSuccess() {
		Log.d("MSTTOR", "Everything is OK!");
		String addresses = "";

		for (HiddenService hs : tp.getHiddenServices()) {
			addresses += hs.getHiddenServiceAddress() + ":\r\n";

			for(Map.Entry<Integer, Address> entry : hs.getHiddenServiceMapping().entrySet()) {
				int key = entry.getKey();
				Address value = entry.getValue();

				addresses += "\t" + key + " -> " + value.toString() + "\r\n";
			}
		}
	Log.d("MSTTOR", "Hidden Addresses: " + addresses);
}

	@Override
	public void onFailure(Exception ex) {

	}
});
} catch (IOException ex) {

}

Sample output of the above code is here:

2020-12-11 22:00:19.615 5390-5430/ir.mstajbakhsh.torandroid.test D/MSTTOR:
Hidden Addresses:
	l666a7nafbfw2csklnsrigmvyj4nqvitdtqy6kyrcuncwrktqfs3h6qd.onion:
    	8080 -> 127.0.0.1:8080
    	8123 -> 127.0.0.1:8123
    cvv6sb22aseo2sgkv6iixpnkriplerjgnyhpsubonbkpspxpxnh6xlid.onion:
    	80 -> 192.168.1.24:80

Reload Mode

In this mode, TOR will be reloaded with previously configured settings. Use the following code to achieve:

TorProxy tp = new TorProxy(appContext, TorUtils.getTorrc(appContext));
tp.init();
	try {
    	tp.start(new IConnectionDone() {
        	@Override
        	public void onSuccess() {
        		Log.d("MSTTOR", "Everything is OK!");
			}

    	    @Override
        	public void onFailure(Exception ex) {
        		Log.d("MSTTOR", "Error!");
			}
		});
	} catch (IOException e) {
    	e.printStackTrace();
	}

In this case, the previously configured torrc file (located in torconfig directory of private app storage), will be loaded.

Connect Mode

In this mode, you can only start Tor in order to connect to normal/hidden services. It will open a SOCKS5 port available in the application. All the connections in your application can be proxied through the SOCKS5 proxy. Here is the code example:

public void startTorInBackground(Context cntx) {
        HandlerThread mHandlerThread = new HandlerThread("TorThread");
        mHandlerThread.start();
        Handler mHandler = new Handler(mHandlerThread.getLooper());
        mHandler.postDelayed(() -> {
            final TorProxy tb = new TorProxy.TorBuilder()
                    .setSOCKsPort(9150)
                    .setUseBrideges(false)
                    .setDebuggable(false)
                    .build(cntx.getApplicationContext());
            tb.init();
            try {
                final IConnectionDone icd = new IConnectionDone() {
                    @Override
                    public void onSuccess() {
                        Log.d("TorAndroid", "Tor Started Successfully.");
                    }

                    @Override
                    public void onFailure(Exception ex) {
                        Log.e("TorAndroid", "Error in Starting Tor.\r\n" + ex.getMessage());
                    }
                };
                tb.start(icd);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }, 1000);
    }

Donate

If you liked the project, buy me a cup of coffee:

BitCoin Wallet: 1F5uiEmdCLJX5KktWHE1wkc63feKJYMmxS

Contact

You can reach me at my web site available at: https://mstajbakhsh.ir

torandroid's People

Contributors

mirsamantajbakhsh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

torandroid's Issues

App Freeze or Hang

When I call your Method then I check on Android studio log everything is fine tor connected successfully, but my application is hang on or freeze please help me with this

TorAndroid does not work on Android 10 device

Someone sent me an email about this issue and thankfully donated to the project.

He himself fixed the issue and I mentioning it here due to he has not GitHub account.

The fix was this:

Adding a tag to manifest application section:

android:extractNativeLibs = "true"

Cannot run when no HiddenService added to the library

When someone wants to start the library without hidden service, an exception occurs as follow:

2021-04-23 16:43:29.371 7971-8021/ir.mstajbakhsh.torsample E/AndroidRuntime: FATAL EXCEPTION: TorThread
    Process: ir.mstajbakhsh.torsample, PID: 7971
    java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference
        at ir.mstajbakhsh.torandroid.TorProxy.start(TorProxy.java:193)
        at ir.mstajbakhsh.torsample.MainActivity.lambda$startTorInBackground$0$MainActivity(MainActivity.java:50)
        at ir.mstajbakhsh.torsample.-$$Lambda$MainActivity$Yl-3ZrA2Mjs_HuWqnqvfI51vhCk.run(Unknown Source:4)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.os.HandlerThread.run(HandlerThread.java:67)

This is because no hidden service defined and exception occurs in TorProxy.java:193 -> for (HiddenService hs : services) {

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.