* Check WiFi before ServerStarter

* Retored 'ps dropbear' quick parsing to check if ServerStarter has to kill before starting
This commit is contained in:
Alan SCHNEIDER 2012-06-13 10:52:11 +02:00 committed by Alan SCHNEIDER
parent 210df7d4dd
commit aeaeb115c9
4 changed files with 56 additions and 10 deletions

View File

@ -224,7 +224,7 @@ public class ServerPage extends Activity implements OnClickListener, DropbearIns
}
mInfosLabel.setText(infos);
if (SettingsHelper.getInstance(mContext).getNotification() == true) {
Log.d(TAG, "ServerPage: updateServerStatus(): Notification");
Notification notification = new Notification(R.drawable.ic_launcher, "DropBear Server is running", System.currentTimeMillis());
@ -267,9 +267,15 @@ public class ServerPage extends Activity implements OnClickListener, DropbearIns
mServerStatusCode = STATUS_STARTING;
updateServerStatus();
mListeningPort = SettingsHelper.getInstance(mContext).getListeningPort();
// StartServer
ServerStarter serverStarter = new ServerStarter(mContext, this);
serverStarter.execute();
if (SettingsHelper.getInstance(mContext).getOnlyOverWifi() == true && Utils.isConnectedToWiFi(mContext) == false) {
Toast.makeText(mContext, "You are not over WiFi network", Toast.LENGTH_LONG).show();
}
else {
// StartServer
ServerStarter serverStarter = new ServerStarter(mContext, this);
serverStarter.execute();
}
break;
case STATUS_STARTING:
mServerStatusCode = STATUS_STARTED;

View File

@ -15,7 +15,8 @@ import android.util.Log;
public class ServerStarter extends AsyncTask<Void, String, Boolean> {
private static final String TAG = "DropBearServer";
private static final int SDCARD_RW = 1015;
private static final int ID_ROOT = 0;
private static final int ID_SDCARD_RW = 1015;
private Context mContext = null;
private ProgressDialog mProgressDialog = null;
@ -58,14 +59,20 @@ public class ServerStarter extends AsyncTask<Void, String, Boolean> {
return falseWithError("You are not over WiFi network");
}
if (ServerUtils.isDropbearRunning() == true) {
Log.i(TAG, "ServerStopper: Killing processes");
if (ShellUtils.killall("dropbear") == false)
return falseWithError("killall(dropbear)");
}
String login = (SettingsHelper.getInstance(mContext).getCredentialsLogin() ? "root" : "android");
String passwd = SettingsHelper.getInstance(mContext).getCredentialsPasswd();
String banner = ServerUtils.getLocalDir(mContext) + "/banner";
String hostRsa = ServerUtils.getLocalDir(mContext) + "/host_rsa";
String hostDss = ServerUtils.getLocalDir(mContext) + "/host_dss";
String authorizedKeys = ServerUtils.getLocalDir(mContext) + "/authorized_keys";
String listeningPort = "" + SettingsHelper.getInstance(mContext).getListeningPort();
//String lockFile = ServerUtils.getLocalDir(mContext) + "/lock";
Integer listeningPort = SettingsHelper.getInstance(mContext).getListeningPort();
String pidFile = ServerUtils.getLocalDir(mContext) + "/pid";
String command = "/system/xbin/dropbear";
command = command.concat(" -A -N " + login);
@ -73,14 +80,14 @@ public class ServerStarter extends AsyncTask<Void, String, Boolean> {
command = command.concat(" -r " + hostRsa + " -d " + hostDss);
command = command.concat(" -R " + authorizedKeys);
if (login.equals("root")) {
command = command.concat(" -U 0 -G 0");
command = command.concat(" -U " + ID_ROOT + " -G " + ID_ROOT);
}
else {
// TODO: uid=app gid=app groups=1015(sdcard_rw),3003(inet)
command = command.concat(" -U " + mContext.getApplicationInfo().uid + " -G " + SDCARD_RW);
command = command.concat(" -U " + mContext.getApplicationInfo().uid + " -G " + ID_SDCARD_RW);
}
command = command.concat(" -p " + listeningPort);
//command = command.concat(" -P " + lockFile);
command = command.concat(" -P " + pidFile);
if (SettingsHelper.getInstance(mContext).getDisallowRootLogins() == true) {
command = command.concat(" -w");

View File

@ -48,6 +48,9 @@ public class ServerStopper extends AsyncTask<Void, String, Boolean> {
protected Boolean doInBackground(Void... params) {
Log.i(TAG, "ServerStopper: doInBackground()");
String pidFile = ServerUtils.getLocalDir(mContext) + "/pid";
ShellUtils.rm(pidFile);
String lockFile = ServerUtils.getLocalDir(mContext) + "/lock";
if (ShellUtils.echoToFile("0", lockFile) == false)
return falseWithError("echoToFile(0, " + lockFile + ")");

View File

@ -131,6 +131,36 @@ public abstract class ServerUtils {
return 0;
}
// WARNING: this is not threaded
public static final Boolean isDropbearRunning() {
try {
Process suProcess = Runtime.getRuntime().exec("su");
// stdin
DataOutputStream stdin = new DataOutputStream(suProcess.getOutputStream());
Log.d(TAG, "ServerUtils: getServerPidFromPs(): # ps dropbear");
stdin.writeBytes("ps dropbear\n");
stdin.flush();
stdin.writeBytes("exit\n");
stdin.flush();
// stdout
BufferedReader reader = new BufferedReader(new InputStreamReader(suProcess.getInputStream()));
ArrayList<String> output = new ArrayList<String>();
String line;
while ((line = reader.readLine()) != null) {
output.add(line);
}
// parsing
return (output.size() >= 2);
}
catch (Exception e) {
Log.e(TAG, "ServerUtils: isDropbearRunning(): " + e.getMessage());
}
return false;
}
// WARNING: this is not threaded
public static final Boolean generateRsaPrivateKey(String path) {
ShellUtils.commands.add("/system/xbin/dropbearkey -t rsa -f " + path);