Commit b60a5eb7 authored by Daniel Wolf's avatar Daniel Wolf
Browse files

Added the option for servers to be started at boot

parent 9040dbb3
......@@ -56,6 +56,10 @@ public class DNSServerSetting extends SingletonEntity implements Serializable{
private boolean resolveLocal;
@Named(name = "UDP")
private boolean udp = true;
@Named(name = "IPWhitelist")
private boolean ipWhitelist = false;
@Named(name = "StartOnBoot")
private boolean startOnBoot = false;
@Ignore
private boolean serverRunning;
......@@ -102,6 +106,10 @@ public class DNSServerSetting extends SingletonEntity implements Serializable{
this.localRedirectPort = localRedirectPort;
}
public boolean shouldStartOnBoot(){
return startOnBoot;
}
public boolean isUdp() {
return udp;
}
......
package com.frostnerd.dnsserver.receivers;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import com.frostnerd.dnsserver.database.entities.main.DNSServerSetting;
import com.frostnerd.dnsserver.services.ServerService;
import com.frostnerd.dnsserver.util.Util;
import java.util.ArrayList;
/**
* Copyright Daniel Wolf 2018
* All rights reserved.
* Code may NOT be used without proper permission, neither in binary nor in source form.
* All redistributions of this software in source code must retain this copyright header
* All redistributions of this software in binary form must visibly inform users about usage of this software
* <p>
* development@frostnerd.com
*/
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent != null && intent.getAction() != null && (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED) || intent.getAction().equals(Intent.ACTION_LOCKED_BOOT_COMPLETED))){
ArrayList<String> servers = new ArrayList<>();
for(DNSServerSetting setting: Util.getMainDatabase(context).getAll(DNSServerSetting.class)){
if(setting.shouldStartOnBoot())servers.add(setting.getName());
}
Intent serviceIntent = new Intent(context, ServerService.class).putExtra(ServerService.COMMAND_START_MULTIPLE_SERVERS, servers);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)context.startForegroundService(serviceIntent);
else context.startService(serviceIntent);
}
}
}
......@@ -10,11 +10,13 @@ import android.support.v4.app.NotificationCompat;
import com.frostnerd.dnsserver.R;
import com.frostnerd.dnsserver.activities.MainActivity;
import com.frostnerd.dnsserver.database.DatabaseHelper;
import com.frostnerd.dnsserver.database.entities.main.DNSServerSetting;
import com.frostnerd.dnsserver.server.DNSServer;
import com.frostnerd.dnsserver.server.TCPServer;
import com.frostnerd.dnsserver.server.UDPServer;
import com.frostnerd.dnsserver.util.Util;
import com.frostnerd.utils.database.orm.parser.Column;
import com.frostnerd.utils.database.orm.statementoptions.queryoptions.WhereCondition;
import com.frostnerd.utils.services.NotificationService;
......@@ -32,7 +34,7 @@ import java.util.HashMap;
public class ServerService extends NotificationService {
private NotificationCompat.Builder notificationBuilder;
private HashMap<DNSServer, Thread> servers = new HashMap<>();
public static final String COMMAND_START_SERVER = "start_server", COMMAND_STOP_SERVER = "stop_server";
public static final String COMMAND_START_SERVER = "start_server", COMMAND_STOP_SERVER = "stop_server", COMMAND_START_MULTIPLE_SERVERS = "start_servers";
@Override
public void onCreate() {
......@@ -66,6 +68,18 @@ public class ServerService extends NotificationService {
break;
}
}
}else if(intent.hasExtra(COMMAND_START_MULTIPLE_SERVERS)){
DatabaseHelper db = Util.getMainDatabase(this);
Column<DNSServerSetting> column = db.findColumn(DNSServerSetting.class, "name");
for(String name: intent.getStringArrayListExtra(COMMAND_START_MULTIPLE_SERVERS)){
DNSServerSetting settings = db.getSQLHandler(DNSServerSetting.class).
selectFirstRow(Util.getMainDatabase(this), true,
WhereCondition.equal(column, name));
DNSServer server = settings.isUdp() ? new UDPServer(this, settings) : new TCPServer(this, settings);
Thread thread = new Thread(server);
servers.put(server, thread);
thread.start();
}
}
if(servers.size() == 0){
stopSelf();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment