enable local DNS listen on 10.0.0.1 for VPN service

also add support for stopping VPNBuilder instance
This commit is contained in:
Nathan Freitas 2015-02-04 14:55:57 -05:00
parent ec4350ee40
commit 1852cde041
2 changed files with 62 additions and 20 deletions

View File

@ -141,6 +141,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst
private boolean mTransProxyTethering = false; private boolean mTransProxyTethering = false;
private boolean mTransProxyNetworkRefresh = false; private boolean mTransProxyNetworkRefresh = false;
private boolean mUseVPN = false;
private ExecutorService mExecutor = Executors.newFixedThreadPool(1); private ExecutorService mExecutor = Executors.newFixedThreadPool(1);
public void debug(String msg) public void debug(String msg)
@ -368,7 +370,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst
if (action!=null){ if (action!=null){
if(action.equals(Intent.ACTION_BOOT_COMPLETED)||action.equals(CMD_START)){ if(action.equals(Intent.ACTION_BOOT_COMPLETED)||action.equals(CMD_START)){
clearVpnProxy();
setTorProfile(STATUS_ON); setTorProfile(STATUS_ON);
}else if (action.equals(CMD_STOP)){ }else if (action.equals(CMD_STOP)){
setTorProfile(STATUS_OFF); setTorProfile(STATUS_OFF);
@ -381,7 +382,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
flushTransparentProxyRules(); flushTransparentProxyRules();
}else if (action.equals(CMD_UPDATE)){ }else if (action.equals(CMD_UPDATE)){
processSettings(); processSettings();
}else if (action.equals(CMD_VPN)){ }else if (action.equals(CMD_VPN)){
enableVpnProxy(); enableVpnProxy();
} }
else if (action.equals(CMD_VPN_CLEAR)){ else if (action.equals(CMD_VPN_CLEAR)){
@ -1422,6 +1423,10 @@ public class TorService extends Service implements TorServiceConstants, TorConst
public void enableVpnProxy () { public void enableVpnProxy () {
debug ("enabling VPN Proxy");
mUseVPN = true;
SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext()); SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext());
Editor ePrefs = prefs.edit(); Editor ePrefs = prefs.edit();
@ -1434,12 +1439,18 @@ public class TorService extends Service implements TorServiceConstants, TorConst
processSettings(); processSettings();
Intent intent = new Intent(TorService.this, OrbotVpnService.class); Intent intent = new Intent(TorService.this, OrbotVpnService.class);
intent.setAction("start");
startService(intent); startService(intent);
} }
public void clearVpnProxy () public void clearVpnProxy ()
{ {
debug ("clearing VPN Proxy");
mUseVPN = false;
SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext()); SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext());
Editor ePrefs = prefs.edit(); Editor ePrefs = prefs.edit();
ePrefs.remove("pref_proxy_type"); ePrefs.remove("pref_proxy_type");
@ -1449,6 +1460,10 @@ public class TorService extends Service implements TorServiceConstants, TorConst
ePrefs.remove("pref_proxy_password"); ePrefs.remove("pref_proxy_password");
ePrefs.commit(); ePrefs.commit();
processSettings(); processSettings();
Intent intent = new Intent(TorService.this, OrbotVpnService.class);
intent.setAction("stop");
startService(intent);
} }
@ -2314,6 +2329,12 @@ public class TorService extends Service implements TorServiceConstants, TorConst
updateConfiguration("HiddenServiceDir","", false); updateConfiguration("HiddenServiceDir","", false);
} }
if (mUseVPN)
{
updateConfiguration("DNSListenAddress","10.0.0.1:" + TorServiceConstants.TOR_DNS_PORT_DEFAULT,false);
}
saveConfiguration(); saveConfiguration();

View File

@ -41,7 +41,7 @@ import com.runjva.sourceforge.jsocks.server.ServerAuthenticatorNone;
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public class OrbotVpnService extends VpnService implements Handler.Callback { public class OrbotVpnService extends VpnService implements Handler.Callback {
private static final String TAG = "DrobotVpnService"; private static final String TAG = "OrbotVpnService";
private PendingIntent mConfigureIntent; private PendingIntent mConfigureIntent;
@ -60,20 +60,32 @@ public class OrbotVpnService extends VpnService implements Handler.Callback {
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
// The handler is only used to show messages. String action = intent.getAction();
if (mHandler == null) {
mHandler = new Handler(this); if (action.equals("start"))
} {
Log.d(TAG,"starting OrbotVPNService service!");
// Stop the previous session by interrupting the thread.
if (mThreadVPN == null || (!mThreadVPN.isAlive())) // The handler is only used to show messages.
{ if (mHandler == null) {
enableAppRouting (); mHandler = new Handler(this);
setupTun2Socks(); }
}
// Stop the previous session by interrupting the thread.
if (mThreadVPN == null || (!mThreadVPN.isAlive()))
{
enableAppRouting ();
setupTun2Socks();
}
}
else if (action.equals("stop"))
{
stopVPN();
mHandler.postDelayed(new Runnable () { public void run () { stopSelf(); }}, 1000);
}
return START_STICKY; return START_NOT_STICKY;
} }
private void enableAppRouting () private void enableAppRouting ()
@ -113,12 +125,21 @@ public class OrbotVpnService extends VpnService implements Handler.Callback {
@Override @Override
public void onDestroy() { public void onDestroy() {
stopVPN();
}
private void stopVPN ()
{
if (mProxyServer != null){ if (mProxyServer != null){
mProxyServer.stop(); mProxyServer.stop();
mProxyServer = null;
} }
if (mInterface != null){ if (mInterface != null){
try { try {
Log.d(TAG,"closing interface, destroying VPN interface");
mInterface.close(); mInterface.close();
mInterface = null;
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
@ -154,14 +175,14 @@ public class OrbotVpnService extends VpnService implements Handler.Callback {
// (i.e., Farsi and Arabic).^M // (i.e., Farsi and Arabic).^M
Locale.setDefault(new Locale("en")); Locale.setDefault(new Locale("en"));
String localhost = InetAddress.getLocalHost().getHostAddress(); //String localhost = InetAddress.getLocalHost().getHostAddress();
String vpnName = "OrbotVPN"; String vpnName = "OrbotVPN";
String virtualGateway = "10.0.0.1"; String virtualGateway = "10.0.0.1";
String virtualIP = "10.0.0.2"; String virtualIP = "10.0.0.2";
String virtualNetMask = "255.255.255.0"; String virtualNetMask = "255.255.255.0";
String localSocks = localhost + ':' + TorServiceConstants.PORT_SOCKS_DEFAULT; String localSocks = "127.0.0.1" + ':' + TorServiceConstants.PORT_SOCKS_DEFAULT;
String localDNS = localhost + ':' + TorServiceConstants.TOR_DNS_PORT_DEFAULT; String localDNS = "10.0.0.1" + ':' + TorServiceConstants.TOR_DNS_PORT_DEFAULT;
Builder builder = new Builder(); Builder builder = new Builder();
@ -170,7 +191,7 @@ public class OrbotVpnService extends VpnService implements Handler.Callback {
builder.addAddress(virtualGateway,28); builder.addAddress(virtualGateway,28);
builder.setSession(vpnName); builder.setSession(vpnName);
builder.addRoute("0.0.0.0",0); builder.addRoute("0.0.0.0",0);
builder.addDnsServer("8.8.8.8"); // builder.addDnsServer("8.8.8.8");
// Create a new interface using the builder and save the parameters. // Create a new interface using the builder and save the parameters.
mInterface = builder.setSession(mSessionName) mInterface = builder.setSession(mSessionName)