tor-android/src/org/torproject/android/vpn/OrbotVpnService.java

240 lines
6.7 KiB
Java
Raw Normal View History

/*
* Copyright (C) 2011 The Android Open Source Project
*
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.torproject.android.vpn;
import java.io.IOException;
import java.net.InetAddress;
2015-02-02 22:21:04 +01:00
import java.net.UnknownHostException;
import java.util.Locale;
import org.torproject.android.service.TorServiceConstants;
import android.annotation.TargetApi;
2015-01-20 15:47:24 +01:00
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.VpnService;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import android.widget.Toast;
import com.runjva.sourceforge.jsocks.protocol.ProxyServer;
import com.runjva.sourceforge.jsocks.server.ServerAuthenticatorNone;
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public class OrbotVpnService extends VpnService implements Handler.Callback {
private static final String TAG = "OrbotVpnService";
private PendingIntent mConfigureIntent;
private Handler mHandler;
private Thread mThreadVPN;
private String mSessionName = "OrbotVPN";
private ParcelFileDescriptor mInterface;
private int mSocksProxyPort = 9999;
2015-01-20 15:47:24 +01:00
private ProxyServer mProxyServer;
private Thread mThreadProxy;
2014-10-28 06:05:53 +01:00
private final static int VPN_MTU = 1500;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String action = intent.getAction();
if (action.equals("start"))
{
Log.d(TAG,"starting OrbotVPNService service!");
// The handler is only used to show messages.
if (mHandler == null) {
mHandler = new Handler(this);
}
// Stop the previous session by interrupting the thread.
if (mThreadVPN == null || (!mThreadVPN.isAlive()))
{
enableAppRouting ();
setupTun2Socks();
}
}
else if (action.equals("stop"))
{
stopVPN();
2015-02-04 22:28:40 +01:00
if (mHandler != null)
mHandler.postDelayed(new Runnable () { public void run () { stopSelf(); }}, 1000);
}
return START_NOT_STICKY;
}
2015-02-02 17:04:32 +01:00
private void enableAppRouting ()
{
2015-02-02 17:04:32 +01:00
boolean isLollipop = false;
if (isLollipop)
{
//allow for specific apps to be sent through VPN based on list selection
}
else
{
//do socks bypass trick
2015-02-02 22:21:04 +01:00
startSocksBypass();
2015-02-02 17:04:32 +01:00
}
}
2015-01-20 15:47:24 +01:00
private void startSocksBypass(){
mThreadProxy = new Thread ()
{
public void run ()
{
try {
mProxyServer = new ProxyServer(new ServerAuthenticatorNone(null, null));
ProxyServer.setVpnService(OrbotVpnService.this);
mProxyServer.start(mSocksProxyPort, 5, InetAddress.getLocalHost());
} catch (Exception e) {
Log.d(TAG,"proxy server error: " + e.getLocalizedMessage(),e);
}
}
};
mThreadProxy.start();
}
@Override
public void onDestroy() {
stopVPN();
}
private void stopVPN ()
{
2015-01-20 15:47:24 +01:00
if (mProxyServer != null){
mProxyServer.stop();
mProxyServer = null;
2015-01-20 15:47:24 +01:00
}
if (mInterface != null){
2015-02-14 06:44:02 +01:00
onRevoke();
2015-01-20 15:47:24 +01:00
}
}
@Override
public boolean handleMessage(Message message) {
if (message != null) {
Toast.makeText(this, message.what, Toast.LENGTH_SHORT).show();
}
return true;
}
private void setupTun2Socks() {
mThreadVPN = new Thread ()
{
public void run ()
{
if (mInterface == null)
{
2015-02-04 17:55:36 +01:00
try
{
2015-02-04 17:55:36 +01:00
// Set the locale to English (or probably any other language that^M
// uses Hindu-Arabic (aka Latin) numerals).^M
// We have found that VpnService.Builder does something locale-dependent^M
// internally that causes errors when the locale uses its own numerals^M
// (i.e., Farsi and Arabic).^M
Locale.setDefault(new Locale("en"));
//String localhost = InetAddress.getLocalHost().getHostAddress();
2015-02-04 17:55:36 +01:00
String vpnName = "OrbotVPN";
String virtualGateway = "10.0.0.1";
String virtualIP = "10.0.0.2";
String virtualNetMask = "255.255.255.0";
String localSocks = "127.0.0.1" + ':' + TorServiceConstants.PORT_SOCKS_DEFAULT;
String localDNS = "10.0.0.1" + ':' + TorServiceConstants.TOR_DNS_PORT_DEFAULT;
2015-02-04 17:55:36 +01:00
Builder builder = new Builder();
builder.setMtu(VPN_MTU);
builder.addAddress(virtualGateway,28);
builder.setSession(vpnName);
builder.addRoute("0.0.0.0",0);
// builder.addDnsServer("8.8.8.8");
2015-02-04 17:55:36 +01:00
// Create a new interface using the builder and save the parameters.
mInterface = builder.setSession(mSessionName)
.setConfigureIntent(mConfigureIntent)
.establish();
2015-02-02 05:36:19 +01:00
Tun2Socks.Start(mInterface, VPN_MTU, virtualIP, virtualNetMask, localSocks , localDNS , true);
}
catch (Exception e)
{
Log.d(TAG,"tun2Socks has stopped",e);
}
}
}
};
mThreadVPN.start();
}
2015-01-20 15:47:24 +01:00
@Override
public void onRevoke() {
new Thread ()
{
public void run()
{
try
{
2015-02-14 06:44:02 +01:00
Log.d(TAG,"closing interface, destroying VPN interface");
if (mInterface != null)
{
mInterface.close();
mInterface = null;
}
2015-01-20 15:47:24 +01:00
Tun2Socks.Stop();
}
catch (Exception e)
{
Log.d(TAG,"error stopping tun2socks",e);
}
}
}.start();
super.onRevoke();
}
}