Commit 0a7f15e9 authored by Daniel Wolf's avatar Daniel Wolf
Browse files

Each server has its own database now

parent 6434a8a8
......@@ -2,10 +2,9 @@ package com.frostnerd.dnsserver.database;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.frostnerd.dnsserver.database.entities.DNSServerSetting;
import com.frostnerd.dnsserver.database.entities.IPPortPair;
import com.frostnerd.dnsserver.database.entities.main.DNSServerSetting;
import com.frostnerd.dnsserver.database.entities.main.IPPortPair;
import com.frostnerd.utils.database.orm.Entity;
import java.util.HashSet;
......@@ -20,10 +19,9 @@ import java.util.Set;
* <p>
* development@frostnerd.com
*/
public class DatabaseHelper extends com.frostnerd.utils.database.DatabaseHelper {
public final class DatabaseHelper extends com.frostnerd.utils.database.DatabaseHelper {
private static final String DATABASE_NAME = "main";
private static final int DATABASE_VERSION = 1;
private SQLiteDatabase currentDB;
private static final Set<Class<? extends Entity>> entities = new HashSet<>();
static{
entities.add(IPPortPair.class);
......
package com.frostnerd.dnsserver.database;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.frostnerd.dnsserver.database.entities.main.DNSServerSetting;
import com.frostnerd.dnsserver.database.entities.server.DNSEntry;
import com.frostnerd.dnsserver.database.entities.server.DNSQuery;
import com.frostnerd.dnsserver.database.entities.server.UpstreamResponse;
import com.frostnerd.utils.database.orm.Entity;
import java.util.HashSet;
import java.util.Set;
/**
* Copyright Daniel Wolf 2017
* 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 final class ServerDatabaseHelper extends com.frostnerd.utils.database.DatabaseHelper {
private static final int DATABASE_VERSION = 1;
private static final Set<Class<? extends Entity>> entities = new HashSet<>();
static{
entities.add(DNSQuery.class);
entities.add(DNSEntry.class);
entities.add(UpstreamResponse.class);
}
public ServerDatabaseHelper(Context context, DNSServerSetting settings){
super(context, settings.getName(), DATABASE_VERSION, entities);
}
@Override
public void onBeforeCreate(SQLiteDatabase db) {
}
@Override
public void onAfterCreate(SQLiteDatabase db) {
}
@Override
public void onBeforeUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
@Override
public void onAfterUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
package com.frostnerd.dnsserver.database.entities;
package com.frostnerd.dnsserver.database.entities.main;
import com.frostnerd.dnsserver.server.DNSServer;
......
package com.frostnerd.dnsserver.database.entities;
package com.frostnerd.dnsserver.database.entities.main;
import com.frostnerd.dnsserver.util.Util;
import com.frostnerd.utils.database.orm.Entity;
......
package com.frostnerd.dnsserver.database.entities.server;
import com.frostnerd.utils.database.orm.Entity;
import com.frostnerd.utils.database.orm.annotations.Table;
/**
* Copyright Daniel Wolf 2017
* 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
*/
@Table
public class DNSEntry extends Entity{
}
package com.frostnerd.dnsserver.database.entities.server;
import com.frostnerd.utils.database.orm.Entity;
import com.frostnerd.utils.database.orm.annotations.Table;
/**
* Copyright Daniel Wolf 2017
* 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
*/
@Table(name = "DNSQuery")
public class DNSQuery extends Entity {
}
package com.frostnerd.dnsserver.database.entities.server;
import com.frostnerd.utils.database.orm.Entity;
/**
* Copyright Daniel Wolf 2017
* 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 UpstreamResponse extends Entity{
}
......@@ -2,7 +2,8 @@ package com.frostnerd.dnsserver.server;
import android.content.Context;
import com.frostnerd.dnsserver.database.entities.DNSServerSetting;
import com.frostnerd.dnsserver.database.ServerDatabaseHelper;
import com.frostnerd.dnsserver.database.entities.main.DNSServerSetting;
import com.frostnerd.dnsserver.util.DNSResolver;
import com.frostnerd.dnsserver.util.QueryLogger;
import com.frostnerd.dnsserver.util.Util;
......@@ -27,16 +28,17 @@ public abstract class DNSServer implements Runnable {
InetAddressWithPort primaryAddress, secondaryAddress;
QueryLogger queryLogger;
QueryListener queryListener;
ServerDatabaseHelper serverDatabase;
DNSServer(Context context, DNSServerSetting settings) {
this.resolver = Util.getDnsResolver(context);
this.resolver = new DNSResolver(context, serverDatabase = new ServerDatabaseHelper(context, settings));
this.settings = settings;
try {
this.primaryAddress = new InetAddressWithPort(InetAddress.getByName(settings.getUpstreamPrimary().getAddress()), settings.getUpstreamPrimary().getPort());
this.secondaryAddress = new InetAddressWithPort(InetAddress.getByName(settings.getUpstreamSecondary().getAddress()), settings.getUpstreamSecondary().getPort());
} catch (UnknownHostException ignored) {}
if(settings.isQueryLoggingEnabled() || settings.isQueryResponseLoggingEnabled())
queryLogger = new QueryLogger(context);
queryLogger = new QueryLogger(context, serverDatabase);
if(settings.isQueryLoggingEnabled() && settings.getQueryListener() != null){
queryListener = new QueryListener() {
......@@ -76,6 +78,8 @@ public abstract class DNSServer implements Runnable {
stopServer();
resolver.cleanup();
if(queryLogger != null)queryLogger.cleanup();
serverDatabase.close();
serverDatabase = null;
resolver = null;
queryLogger = null;
primaryAddress = null;
......
......@@ -2,7 +2,7 @@ package com.frostnerd.dnsserver.server;
import android.content.Context;
import com.frostnerd.dnsserver.database.entities.DNSServerSetting;
import com.frostnerd.dnsserver.database.entities.main.DNSServerSetting;
import com.frostnerd.dnsserver.util.DNSResolver;
import java.net.DatagramPacket;
......
......@@ -5,6 +5,7 @@ import android.database.Cursor;
import android.text.TextUtils;
import com.frostnerd.dnsserver.database.DatabaseHelper;
import com.frostnerd.dnsserver.database.ServerDatabaseHelper;
import java.io.IOException;
import java.io.InputStream;
......@@ -34,11 +35,11 @@ public class DNSResolver {
"SELECT Target FROM DNSRules WHERE IPv6=? AND Wildcard=1 AND ? REGEXP Domain LIMIT 1";
private static final String NON_WILDCARD_QUERY = "SELECT Target FROM DNSRules WHERE Domain=? AND IPv6=? AND Wildcard=0";
private static final String SUM_WILDCARD_QUERY = "SELECT SUM(Wildcard) FROM DNSRules";
private DatabaseHelper db;
private ServerDatabaseHelper db;
private int wildcardCount;
public DNSResolver(Context context) {
db = Util.getDatabaseHelper(context);
public DNSResolver(Context context, ServerDatabaseHelper serverDatabase) {
db = serverDatabase;
Cursor cursor = db.getReadableDatabase().rawQuery(SUM_WILDCARD_QUERY, null);
if (cursor.moveToFirst()) {
wildcardCount = cursor.getInt(0);
......@@ -47,7 +48,6 @@ public class DNSResolver {
}
public void cleanup(){
db.close();
db = null;
}
......
......@@ -3,6 +3,7 @@ package com.frostnerd.dnsserver.util;
import android.content.Context;
import com.frostnerd.dnsserver.database.DatabaseHelper;
import com.frostnerd.dnsserver.database.ServerDatabaseHelper;
import de.measite.minidns.Record;
......@@ -11,10 +12,10 @@ import de.measite.minidns.Record;
*/
public class QueryLogger {
private DatabaseHelper databaseHelper;
private ServerDatabaseHelper serverDatabase;
public QueryLogger(Context context){
this.databaseHelper = Util.getDatabaseHelper(context);
public QueryLogger(Context context, ServerDatabaseHelper serverDatabase){
this.serverDatabase = serverDatabase;
}
public void logQuery(String host, String query, Record.TYPE type, boolean wasForwarded){
......@@ -26,7 +27,6 @@ public class QueryLogger {
}
public void cleanup(){
databaseHelper.close();
databaseHelper = null;
serverDatabase = null;
}
}
......@@ -3,30 +3,28 @@ package com.frostnerd.dnsserver.util;
import android.content.Context;
import com.frostnerd.dnsserver.database.DatabaseHelper;
import com.frostnerd.dnsserver.database.entities.IPPortPair;
import com.frostnerd.dnsserver.database.ServerDatabaseHelper;
import com.frostnerd.dnsserver.database.entities.main.DNSServerSetting;
import com.frostnerd.dnsserver.database.entities.main.IPPortPair;
import com.frostnerd.utils.networking.NetworkUtil;
import java.util.HashMap;
import java.util.regex.Pattern;
/**
* Created by Daniel on 25.10.2017.
*/
public class Util {
public final class Util {
private static DatabaseHelper databaseHelper;
private static DNSResolver dnsResolver;
private static Pattern ipv6WithPort = Pattern.compile("(\\[[0-9a-f:]+\\]:[0-9]{1,5})|([0-9a-f:]+)");
private static Pattern ipv4WithPort = Pattern.compile("([0-9]{1,3}\\.){3}[0-9]{1,3}(:[0-9]{1,5})?");
private static final Pattern ipv6WithPort = Pattern.compile("(\\[[0-9a-f:]+\\]:[0-9]{1,5})|([0-9a-f:]+)");
private static final Pattern ipv4WithPort = Pattern.compile("([0-9]{1,3}\\.){3}[0-9]{1,3}(:[0-9]{1,5})?");
public static DatabaseHelper getDatabaseHelper(Context context){
public static DatabaseHelper getMainDatabase(Context context){
return databaseHelper == null ? databaseHelper = new DatabaseHelper(context) : databaseHelper;
}
public static DNSResolver getDnsResolver(Context context){
return dnsResolver == null ? dnsResolver = new DNSResolver(context) : dnsResolver;
}
public static IPPortPair validateInput(String input, boolean iPv6, boolean allowEmpty, boolean allowLoopback) {
if (allowEmpty && input.equals("")) return new IPPortPair("", -1, iPv6);
if (iPv6) {
......
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