1
0
mirror of https://github.com/moparisthebest/SSLDroid synced 2024-11-27 03:12:18 -05:00

Implemented explicit stop feature - stops the service until it is

started manually

Signed-off-by: Balint Kovacs <blint@blint.hu>
This commit is contained in:
Balint Kovacs 2011-10-19 11:44:43 +02:00
parent d59aa907a8
commit 8e3cd92b69
12 changed files with 130 additions and 78 deletions

View File

@ -12,6 +12,7 @@
<activity android:name=".SSLDroidTunnelDetails" <activity android:name=".SSLDroidTunnelDetails"
android:windowSoftInputMode="stateVisible|adjustResize" /> android:windowSoftInputMode="stateVisible|adjustResize" />
<activity android:name=".SSLDroidReadLogs" /> <activity android:name=".SSLDroidReadLogs" />
<activity android:name=".SSLDroidProvisioning" />
<service android:enabled="true" android:name="SSLDroid"> <service android:enabled="true" android:name="SSLDroid">
<intent-filter> <intent-filter>
<action android:name="hu.blint.ssldroid.SSLDroid" /> <action android:name="hu.blint.ssldroid.SSLDroid" />

View File

@ -8,4 +8,4 @@
# project structure. # project structure.
# Project target. # Project target.
target=android-7 target=android-8

View File

@ -2,7 +2,9 @@
<menu <menu
xmlns:android="http://schemas.android.com/apk/res/android"> xmlns:android="http://schemas.android.com/apk/res/android">
<item android:enabled="true" android:title="@string/add_tunnel" android:id="@+id/addtunnel" android:icon="@android:drawable/ic_menu_add"></item> <item android:enabled="true" android:title="@string/add_tunnel" android:id="@+id/addtunnel" android:icon="@android:drawable/ic_menu_add"></item>
<item android:enabled="true" android:id="@+id/startservice" android:title="@string/start_service" android:icon="@android:drawable/ic_menu_manage"></item>
<item android:enabled="true" android:id="@+id/startservice" android:title="@string/start_service" android:icon="@android:drawable/ic_menu_manage"></item><item android:title="@string/stop_service" android:enabled="true" android:id="@+id/stopservice" android:icon="@android:drawable/ic_menu_close_clear_cancel"></item> <item android:title="@string/stop_service" android:enabled="true" android:id="@+id/stopservice" android:icon="@android:drawable/ic_menu_close_clear_cancel"></item>
<item android:title="@string/stop_service_for_good" android:enabled="true" android:id="@+id/stopserviceforgood" android:icon="@android:drawable/ic_menu_close_clear_cancel"></item>
<item android:title="@string/menu_readlogs" android:id="@+id/readlogs" android:icon="@android:drawable/ic_menu_agenda" android:enabled="true"></item> <item android:title="@string/menu_readlogs" android:id="@+id/readlogs" android:icon="@android:drawable/ic_menu_agenda" android:enabled="true"></item>
<!-- <item android:title="@string/menu_provision" android:id="@+id/provision" android:icon="@android:drawable/ic_menu_mapmode" android:enabled="true"></item> -->
</menu> </menu>

View File

@ -1,57 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="tunnels">
<xs:complexType>
<xs:sequence>
<xs:element name="tunnel" type="tunnelType" minOccurs="0" maxOccurs="unbounded">
<xs:annotation>
<xs:documentation>
A container to group tunnel settings.
</xs:documentation>
</xs:annotation>
</xs:element>
</xs:sequence>
<xs:attribute name="version" type="xs:NMTOKEN" use="required" fixed="1">
<xs:annotation>
<xs:documentation>
The schema version of the tunnel setting.
The current version is '1'.
</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
<xs:unique name="tunnel_name">
<xs:selector xpath="tunnel"/>
<xs:field xpath="@name"/>
</xs:unique>
<xs:unique name="localports">
<xs:selector xpath="tunnel/localport"/>
<xs:field xpath="."/>
</xs:unique>
</xs:element>
<xs:complexType name="tunnelType">
<xs:all>
<xs:element name="localport" minOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minExclusive value="1024"/>
<xs:maxInclusive value="65535"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="remotehost" type="xs:string" minOccurs="1" />
<xs:element name="remoteport" type="xs:unsignedShort" minOccurs="1" />
<xs:element name="pkcsfile" type="xs:string" minOccurs="1" />
<xs:element name="pkcspass" type="xs:string" minOccurs="0" />
</xs:all>
<xs:attribute name="name" type="xs:string" use="required">
<xs:annotation>
<xs:documentation>
The name of the tunnel.
</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
</xs:schema>

View File

@ -11,6 +11,7 @@
<string name="connection_name">Tunnel name</string> <string name="connection_name">Tunnel name</string>
<string name="add_tunnel">Add tunnel</string> <string name="add_tunnel">Add tunnel</string>
<string name="stop_service">Stop service</string> <string name="stop_service">Stop service</string>
<string name="stop_service_for_good">Stop service until started</string>
<string name="start_service">Start service</string> <string name="start_service">Start service</string>
<string name="no_tunnels">No tunnels configured yet</string> <string name="no_tunnels">No tunnels configured yet</string>
<string name="menu_delete">Delete tunnel</string> <string name="menu_delete">Delete tunnel</string>
@ -18,6 +19,8 @@
<string name="alert_sdcard_absent">No SD card present, please insert one to continue</string> <string name="alert_sdcard_absent">No SD card present, please insert one to continue</string>
<string name="menu_readlogs">Read logs</string> <string name="menu_readlogs">Read logs</string>
<string name="reading_logs">Reading log messages...</string> <string name="reading_logs">Reading log messages...</string>
<string name="menu_provision">Provisioning</string>
<string name="provision">Please enter the URL for remote XML configuration</string>
<string name="back">Back</string> <string name="back">Back</string>
<string name="refresh">Refresh</string> <string name="refresh">Refresh</string>
</resources> </resources>

View File

@ -1,16 +1,44 @@
package hu.blint.ssldroid; package hu.blint.ssldroid;
import hu.blint.ssldroid.db.SSLDroidDbAdapter;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
public class BootStartupReceiver extends BroadcastReceiver { public class BootStartupReceiver extends BroadcastReceiver {
private boolean isStopped(Context context){
Boolean stopped = false;
SSLDroidDbAdapter dbHelper;
dbHelper = new SSLDroidDbAdapter(context);
dbHelper.open();
Cursor cursor = dbHelper.getStopStatus();
int tunnelcount = cursor.getCount();
Log.d("SSLDroid", "Tunnelcount: "+tunnelcount);
//don't start if the stop status field is available
if (tunnelcount != 0){
stopped = true;
}
cursor.close();
dbHelper.close();
return stopped;
}
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent(); Intent i = new Intent();
i.setAction("hu.blint.ssldroid.SSLDroid"); i.setAction("hu.blint.ssldroid.SSLDroid");
if (!isStopped(context))
context.startService(i); context.startService(i);
else
Log.w("SSLDroid", "Not starting service as directed by explicit stop");
} }
} }
} }

View File

@ -1,14 +1,37 @@
package hu.blint.ssldroid; package hu.blint.ssldroid;
import hu.blint.ssldroid.db.SSLDroidDbAdapter;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.database.Cursor;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.util.Log; import android.util.Log;
public class NetworkChangeReceiver extends BroadcastReceiver { public class NetworkChangeReceiver extends BroadcastReceiver {
private boolean isStopped(Context context){
Boolean stopped = false;
SSLDroidDbAdapter dbHelper;
dbHelper = new SSLDroidDbAdapter(context);
dbHelper.open();
Cursor cursor = dbHelper.getStopStatus();
int tunnelcount = cursor.getCount();
Log.d("SSLDroid", "Tunnelcount: "+tunnelcount);
//don't start if the stop status field is available
if (tunnelcount != 0){
stopped = true;
}
cursor.close();
dbHelper.close();
return stopped;
}
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE ); ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );
@ -24,7 +47,10 @@ public class NetworkChangeReceiver extends BroadcastReceiver {
Intent i = new Intent(); Intent i = new Intent();
i.setAction("hu.blint.ssldroid.SSLDroid"); i.setAction("hu.blint.ssldroid.SSLDroid");
context.stopService(i); context.stopService(i);
if (!isStopped(context))
context.startService(i); context.startService(i);
else
Log.w("SSLDroid", "Not starting service as directed by explicit stop");
} }
} }
} }

View File

@ -4,6 +4,7 @@ import hu.blint.ssldroid.TcpProxy;
import android.app.*; import android.app.*;
import android.content.Intent; import android.content.Intent;
import android.database.Cursor; import android.database.Cursor;
import android.os.Bundle;
import android.os.IBinder; import android.os.IBinder;
import android.util.Log; import android.util.Log;
import hu.blint.ssldroid.db.SSLDroidDbAdapter; import hu.blint.ssldroid.db.SSLDroidDbAdapter;

View File

@ -54,8 +54,14 @@ public class SSLDroidGui extends ListActivity {
Log.d("SSLDroid", "Stopping service"); Log.d("SSLDroid", "Stopping service");
stopService(new Intent(this, SSLDroid.class)); stopService(new Intent(this, SSLDroid.class));
return true; return true;
case R.id.stopserviceforgood:
Log.d("SSLDroid", "Stopping service until explicitly started");
dbHelper.setStopStatus();
stopService(new Intent(this, SSLDroid.class));
return true;
case R.id.startservice: case R.id.startservice:
Log.d("SSLDroid", "Starting service"); Log.d("SSLDroid", "Starting service");
dbHelper.delStopStatus();
startService(new Intent(this, SSLDroid.class)); startService(new Intent(this, SSLDroid.class));
return true; return true;
case R.id.readlogs: case R.id.readlogs:
@ -75,8 +81,14 @@ public class SSLDroidGui extends ListActivity {
Log.d("SSLDroid", "Stopping service"); Log.d("SSLDroid", "Stopping service");
stopService(new Intent(this, SSLDroid.class)); stopService(new Intent(this, SSLDroid.class));
return true; return true;
case R.id.stopserviceforgood:
Log.d("SSLDroid", "Stopping service until explicitly started");
dbHelper.setStopStatus();
stopService(new Intent(this, SSLDroid.class));
return true;
case R.id.startservice: case R.id.startservice:
Log.d("SSLDroid", "Starting service"); Log.d("SSLDroid", "Starting service");
dbHelper.delStopStatus();
startService(new Intent(this, SSLDroid.class)); startService(new Intent(this, SSLDroid.class));
return true; return true;
case R.id.readlogs: case R.id.readlogs:
@ -124,7 +136,6 @@ public class SSLDroidGui extends ListActivity {
Intent i = new Intent(this, SSLDroidTunnelDetails.class); Intent i = new Intent(this, SSLDroidTunnelDetails.class);
i.putExtra(SSLDroidDbAdapter.KEY_ROWID, id); i.putExtra(SSLDroidDbAdapter.KEY_ROWID, id);
// Activity returns an result if called with startActivityForResult // Activity returns an result if called with startActivityForResult
startActivityForResult(i, ACTIVITY_EDIT); startActivityForResult(i, ACTIVITY_EDIT);
} }
@ -159,4 +170,12 @@ public class SSLDroidGui extends ListActivity {
super.onCreateContextMenu(menu, v, menuInfo); super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, DELETE_ID, 0, R.string.menu_delete); menu.add(0, DELETE_ID, 0, R.string.menu_delete);
} }
@Override
public void onDestroy (){
cursor.close();
dbHelper.close();
super.onDestroy();
}
} }

View File

@ -16,7 +16,10 @@ public class SSLDroidDbAdapter {
public static final String KEY_REMOTEPORT = "remoteport"; public static final String KEY_REMOTEPORT = "remoteport";
public static final String KEY_PKCSFILE = "pkcsfile"; public static final String KEY_PKCSFILE = "pkcsfile";
public static final String KEY_PKCSPASS = "pkcspass"; public static final String KEY_PKCSPASS = "pkcspass";
public static final String KEY_STATUS_NAME = "name";
public static final String KEY_STATUS_VALUE = "value";
private static final String DATABASE_TABLE = "tunnels"; private static final String DATABASE_TABLE = "tunnels";
private static final String STATUS_TABLE = "status";
private Context context; private Context context;
private SQLiteDatabase database; private SQLiteDatabase database;
private SSLDroidDbHelper dbHelper; private SSLDroidDbHelper dbHelper;
@ -93,6 +96,30 @@ public class SSLDroidDbAdapter {
/** /**
* Return a Cursor positioned at the defined tunnel * Return a Cursor positioned at the defined tunnel
*/ */
public Cursor fetchStatus(String valuename) throws SQLException {
return database.query(STATUS_TABLE, new String[] {
KEY_STATUS_NAME, KEY_STATUS_VALUE
},
KEY_STATUS_NAME + "='" + valuename + "'", null, null, null, null);
}
public Cursor getStopStatus() {
return fetchStatus("stopped");
}
public boolean setStopStatus() {
ContentValues stopStatus = new ContentValues();
stopStatus.put(KEY_STATUS_NAME, "stopped");
stopStatus.put(KEY_STATUS_VALUE, "yes");
if (getStopStatus().getCount() == 0)
database.insert(STATUS_TABLE, null, stopStatus);
return true;
}
public boolean delStopStatus() {
return database.delete(STATUS_TABLE, KEY_STATUS_NAME+"= 'stopped'", null) > 0;
}
public Cursor fetchTunnel(long rowId) throws SQLException { public Cursor fetchTunnel(long rowId) throws SQLException {
Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] { Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] {
KEY_ROWID, KEY_NAME, KEY_LOCALPORT, KEY_REMOTEHOST, KEY_REMOTEPORT, KEY_ROWID, KEY_NAME, KEY_LOCALPORT, KEY_REMOTEHOST, KEY_REMOTEPORT,

View File

@ -3,16 +3,17 @@ package hu.blint.ssldroid.db;
import android.content.Context; import android.content.Context;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class SSLDroidDbHelper extends SQLiteOpenHelper { public class SSLDroidDbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "applicationdata"; private static final String DATABASE_NAME = "applicationdata";
private static final int DATABASE_VERSION = 2;
private static final int DATABASE_VERSION = 1;
// Database creation sql statement // Database creation sql statement
private static final String DATABASE_CREATE = "create table tunnels (_id integer primary key autoincrement, " private static final String DATABASE_CREATE = "CREATE TABLE IF NOT EXISTS tunnels (_id integer primary key autoincrement, "
+ "name text not null, localport integer not null, remotehost text not null, " + "name text not null, localport integer not null, remotehost text not null, "
+ "remoteport integer not null, pkcsfile text not null, pkcspass text );"; + "remoteport integer not null, pkcsfile text not null, pkcspass text );";
private static final String STATUS_CREATE = "CREATE TABLE IF NOT EXISTS status (name text, value text);";
public SSLDroidDbHelper(Context context) { public SSLDroidDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION); super(context, DATABASE_NAME, null, DATABASE_VERSION);
@ -22,6 +23,7 @@ public class SSLDroidDbHelper extends SQLiteOpenHelper {
@Override @Override
public void onCreate(SQLiteDatabase database) { public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE); database.execSQL(DATABASE_CREATE);
database.execSQL(STATUS_CREATE);
} }
// Method is called during an update of the database, e.g. if you increase // Method is called during an update of the database, e.g. if you increase
@ -29,11 +31,11 @@ public class SSLDroidDbHelper extends SQLiteOpenHelper {
@Override @Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, public void onUpgrade(SQLiteDatabase database, int oldVersion,
int newVersion) { int newVersion) {
/* Log.w(SSLDroidDbHelper.class.getName(), Log.w(SSLDroidDbHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to " "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data"); + newVersion + ", which will add a status table");
database.execSQL("DROP TABLE IF EXISTS todo"); database.execSQL("CREATE TABLE IF NOT EXISTS status (name text, value text);");
onCreate(database); */ onCreate(database);
} }
} }