Commit 7c818da6 authored by Daniel Wolf's avatar Daniel Wolf
Browse files

Added logging

parent 4b987b8c
...@@ -6,6 +6,7 @@ import android.os.ParcelFileDescriptor; ...@@ -6,6 +6,7 @@ import android.os.ParcelFileDescriptor;
import android.support.annotation.RequiresApi; import android.support.annotation.RequiresApi;
import android.system.ErrnoException; import android.system.ErrnoException;
import com.frostnerd.dnschanger.LogFactory;
import com.frostnerd.dnschanger.database.entities.IPPortPair; import com.frostnerd.dnschanger.database.entities.IPPortPair;
import com.frostnerd.dnschanger.util.PreferencesAccessor; import com.frostnerd.dnschanger.util.PreferencesAccessor;
...@@ -26,6 +27,7 @@ import java.util.Set; ...@@ -26,6 +27,7 @@ import java.util.Set;
* development@frostnerd.com * development@frostnerd.com
*/ */
public abstract class DNSProxy { public abstract class DNSProxy {
private static final String LOG_TAG = "[DNSPROXY]";
public static final String IPV4_LOOPBACK_REPLACEMENT = "1.1.1.1", public static final String IPV4_LOOPBACK_REPLACEMENT = "1.1.1.1",
IPV6_LOOPBACK_REPLACEMENT = "fdce:b45b:8dd7:6e47:1:2:3:4"; IPV6_LOOPBACK_REPLACEMENT = "fdce:b45b:8dd7:6e47:1:2:3:4";
static InetAddress LOOPBACK_IPV4, LOOPBACK_IPV6; static InetAddress LOOPBACK_IPV4, LOOPBACK_IPV6;
...@@ -45,10 +47,13 @@ public abstract class DNSProxy { ...@@ -45,10 +47,13 @@ public abstract class DNSProxy {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public static DNSProxy createProxy(VpnService context, ParcelFileDescriptor parcelFileDescriptor, public static DNSProxy createProxy(VpnService context, ParcelFileDescriptor parcelFileDescriptor,
Set<IPPortPair> upstreamDNSServers, boolean resolveLocalRules, boolean queryLogging, boolean tcp) { Set<IPPortPair> upstreamDNSServers, boolean resolveLocalRules, boolean queryLogging, boolean tcp) {
LogFactory.writeMessage(context, LOG_TAG, "Creating a proxy with upstreamservers: " + upstreamDNSServers + " and file descriptor: " + parcelFileDescriptor);
if (tcp) { if (tcp) {
LogFactory.writeMessage(context, LOG_TAG, "Creating a TCP proxy");
return new DNSTCPProxy(context, parcelFileDescriptor, upstreamDNSServers, return new DNSTCPProxy(context, parcelFileDescriptor, upstreamDNSServers,
resolveLocalRules, queryLogging, PreferencesAccessor.getTCPTimeout(context)); resolveLocalRules, queryLogging, PreferencesAccessor.getTCPTimeout(context));
} else { } else {
LogFactory.writeMessage(context, LOG_TAG, "Creating an UDP proxy");
return new DNSUDPProxy(context, parcelFileDescriptor, upstreamDNSServers, return new DNSUDPProxy(context, parcelFileDescriptor, upstreamDNSServers,
resolveLocalRules, queryLogging); resolveLocalRules, queryLogging);
} }
......
...@@ -10,6 +10,7 @@ import android.system.Os; ...@@ -10,6 +10,7 @@ import android.system.Os;
import android.system.OsConstants; import android.system.OsConstants;
import android.system.StructPollfd; import android.system.StructPollfd;
import com.frostnerd.dnschanger.LogFactory;
import com.frostnerd.dnschanger.database.DatabaseHelper; import com.frostnerd.dnschanger.database.DatabaseHelper;
import com.frostnerd.dnschanger.database.accessors.DNSResolver; import com.frostnerd.dnschanger.database.accessors.DNSResolver;
import com.frostnerd.dnschanger.database.accessors.QueryLogger; import com.frostnerd.dnschanger.database.accessors.QueryLogger;
...@@ -64,6 +65,7 @@ import de.measite.minidns.record.Data; ...@@ -64,6 +65,7 @@ import de.measite.minidns.record.Data;
*/ */
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class DNSTCPProxy extends DNSProxy{ public class DNSTCPProxy extends DNSProxy{
private static final String LOG_TAG = "[DNSUDPProxy]";
private FileDescriptor interruptedDescriptor = null; private FileDescriptor interruptedDescriptor = null;
private FileDescriptor blockingDescriptor = null; private FileDescriptor blockingDescriptor = null;
private ParcelFileDescriptor parcelFileDescriptor; private ParcelFileDescriptor parcelFileDescriptor;
...@@ -114,29 +116,44 @@ public class DNSTCPProxy extends DNSProxy{ ...@@ -114,29 +116,44 @@ public class DNSTCPProxy extends DNSProxy{
public DNSTCPProxy(VpnService context, ParcelFileDescriptor parcelFileDescriptor, public DNSTCPProxy(VpnService context, ParcelFileDescriptor parcelFileDescriptor,
Set<IPPortPair> upstreamDNSServers, boolean resolveLocalRules, boolean queryLogging, int timeout){ Set<IPPortPair> upstreamDNSServers, boolean resolveLocalRules, boolean queryLogging, int timeout){
if(parcelFileDescriptor == null)throw new IllegalStateException("The ParcelFileDescriptor passed to DNSTCPProxy is null."); LogFactory.writeMessage(context, LOG_TAG, "Creating the proxy...");
if(parcelFileDescriptor == null)throw new IllegalStateException("The ParcelFileDescriptor passed to DNSUDPProxy is null.");
if(context == null)throw new IllegalStateException("The DNSVPNService passed to DNSTCPProxy is null."); if(context == null)throw new IllegalStateException("The DNSVPNService passed to DNSTCPProxy is null.");
this.parcelFileDescriptor = parcelFileDescriptor; this.parcelFileDescriptor = parcelFileDescriptor;
this.vpnService = context; this.vpnService = context;
LogFactory.writeMessage(context, LOG_TAG, "Parsing the upstream servers...");
for(IPPortPair pair: upstreamDNSServers){ for(IPPortPair pair: upstreamDNSServers){
if(pair != IPPortPair.getEmptyPair() && !pair.getAddress().equals(""))this.upstreamServers.put(pair.getAddress(), pair.getPort()); if(pair != IPPortPair.getEmptyPair() && !pair.getAddress().equals("")) this.upstreamServers.put(pair.getAddress(), pair.getPort());
} }
LogFactory.writeMessage(context, LOG_TAG, "Upstream servers parsed to: " + this.upstreamServers);
this.resolveLocalRules = resolveLocalRules; this.resolveLocalRules = resolveLocalRules;
this.queryLogging = queryLogging; this.queryLogging = queryLogging;
if(queryLogging)queryLogger = new QueryLogger(DatabaseHelper.getInstance(context)); if(queryLogging) {
if(resolveLocalRules)resolver = new DNSResolver(context); queryLogger = new QueryLogger(DatabaseHelper.getInstance(context));
LogFactory.writeMessage(context, LOG_TAG, "Created the query logger.");
}
if(resolveLocalRules) {
resolver = new DNSResolver(context);
LogFactory.writeMessage(context, LOG_TAG, "Created the rule resolver.");
}
LogFactory.writeMessage(context, LOG_TAG, "Created the proxy.");
this.timeout = timeout; this.timeout = timeout;
} }
@Override @Override
public void run() throws IOException, ErrnoException { public void run() throws IOException, ErrnoException {
LogFactory.writeMessage(vpnService, LOG_TAG, "Starting the proxy");
if(!shouldRun){
LogFactory.writeMessage(vpnService, LOG_TAG, "Not running as shouldRun is false.");
return;
}
FileDescriptor[] pipes = Os.pipe(); FileDescriptor[] pipes = Os.pipe();
interruptedDescriptor = pipes[0]; interruptedDescriptor = pipes[0];
blockingDescriptor = pipes[1]; blockingDescriptor = pipes[1];
if(!shouldRun)return;
FileInputStream inputStream = new FileInputStream(parcelFileDescriptor.getFileDescriptor()); FileInputStream inputStream = new FileInputStream(parcelFileDescriptor.getFileDescriptor());
FileOutputStream outputStream = new FileOutputStream(parcelFileDescriptor.getFileDescriptor()); FileOutputStream outputStream = new FileOutputStream(parcelFileDescriptor.getFileDescriptor());
byte[] packet = new byte[32767]; byte[] packet = new byte[32767];
LogFactory.writeMessage(vpnService, LOG_TAG, "Entering the while loop");
while(shouldRun){ while(shouldRun){
StructPollfd structFd = new StructPollfd(); StructPollfd structFd = new StructPollfd();
structFd.fd = inputStream.getFD(); structFd.fd = inputStream.getFD();
...@@ -188,6 +205,7 @@ public class DNSTCPProxy extends DNSProxy{ ...@@ -188,6 +205,7 @@ public class DNSTCPProxy extends DNSProxy{
Os.poll(polls, timeout/pollTries); Os.poll(polls, timeout/pollTries);
pollTries = 0; pollTries = 0;
} catch(ErrnoException ex){ } catch(ErrnoException ex){
LogFactory.writeMessage(vpnService, LOG_TAG, "Polling failed with exception: " + ex.getMessage() + "(Cause: " + ex.getCause() + ")");
if(pollTries < 3) poll(polls, timeout); if(pollTries < 3) poll(polls, timeout);
else throw ex; else throw ex;
} }
...@@ -254,6 +272,7 @@ public class DNSTCPProxy extends DNSProxy{ ...@@ -254,6 +272,7 @@ public class DNSTCPProxy extends DNSProxy{
} }
}catch(IOException exception){ }catch(IOException exception){
if(!(exception instanceof SocketTimeoutException))handleUpstreamDNSResponse(ipPacket, outgoingPacket.getData()); if(!(exception instanceof SocketTimeoutException))handleUpstreamDNSResponse(ipPacket, outgoingPacket.getData());
LogFactory.writeStackTrace(vpnService, LOG_TAG, exception);
} }
} }
...@@ -265,6 +284,7 @@ public class DNSTCPProxy extends DNSProxy{ ...@@ -265,6 +284,7 @@ public class DNSTCPProxy extends DNSProxy{
handleUpstreamDNSResponse(parsedPacket, data); handleUpstreamDNSResponse(parsedPacket, data);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
LogFactory.writeStackTrace(vpnService, LOG_TAG, e);
} }
} }
...@@ -303,11 +323,14 @@ public class DNSTCPProxy extends DNSProxy{ ...@@ -303,11 +323,14 @@ public class DNSTCPProxy extends DNSProxy{
@Override @Override
public void stop() { public void stop() {
LogFactory.writeMessage(vpnService, LOG_TAG, "Stopping the proxy");
shouldRun = false; shouldRun = false;
try { try {
LogFactory.writeMessage(vpnService, LOG_TAG, "Closing the descriptors.");
if(interruptedDescriptor != null) Os.close(interruptedDescriptor); if(interruptedDescriptor != null) Os.close(interruptedDescriptor);
if(blockingDescriptor != null) Os.close(blockingDescriptor); if(blockingDescriptor != null) Os.close(blockingDescriptor);
} catch (Exception ignored) { } catch (Exception ignored) {
LogFactory.writeMessage(vpnService, LOG_TAG, "An error occurred when closing the descriptors: " + ignored.getMessage() + "(Cause: " + ignored.getCause() + ")");
} }
synchronized (futureSocketAnswers){ synchronized (futureSocketAnswers){
try { try {
...@@ -324,6 +347,7 @@ public class DNSTCPProxy extends DNSProxy{ ...@@ -324,6 +347,7 @@ public class DNSTCPProxy extends DNSProxy{
writeToDevice.clear(); writeToDevice.clear();
if(resolver != null) resolver.destroy(); if(resolver != null) resolver.destroy();
if(queryLogger != null) queryLogger.destroy(); if(queryLogger != null) queryLogger.destroy();
LogFactory.writeMessage(vpnService, LOG_TAG, "Everything was destructed.");
parcelFileDescriptor = null; parcelFileDescriptor = null;
resolver = null; resolver = null;
vpnService = null; vpnService = null;
...@@ -335,12 +359,12 @@ public class DNSTCPProxy extends DNSProxy{ ...@@ -335,12 +359,12 @@ public class DNSTCPProxy extends DNSProxy{
private IpPacket packet; private IpPacket packet;
private final long time; private final long time;
public PacketWrap(IpPacket packet) { PacketWrap(IpPacket packet) {
this.packet = packet; this.packet = packet;
this.time = System.currentTimeMillis(); this.time = System.currentTimeMillis();
} }
public IpPacket getPacket() { IpPacket getPacket() {
return packet; return packet;
} }
......
...@@ -8,7 +8,9 @@ import android.system.ErrnoException; ...@@ -8,7 +8,9 @@ import android.system.ErrnoException;
import android.system.Os; import android.system.Os;
import android.system.OsConstants; import android.system.OsConstants;
import android.system.StructPollfd; import android.system.StructPollfd;
import android.util.Log;
import com.frostnerd.dnschanger.LogFactory;
import com.frostnerd.dnschanger.database.DatabaseHelper; import com.frostnerd.dnschanger.database.DatabaseHelper;
import com.frostnerd.dnschanger.database.accessors.DNSResolver; import com.frostnerd.dnschanger.database.accessors.DNSResolver;
import com.frostnerd.dnschanger.database.accessors.QueryLogger; import com.frostnerd.dnschanger.database.accessors.QueryLogger;
...@@ -60,6 +62,7 @@ import de.measite.minidns.record.Data; ...@@ -60,6 +62,7 @@ import de.measite.minidns.record.Data;
*/ */
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class DNSUDPProxy extends DNSProxy{ public class DNSUDPProxy extends DNSProxy{
private static final String LOG_TAG = "[DNSUDPProxy]";
private FileDescriptor interruptedDescriptor = null; private FileDescriptor interruptedDescriptor = null;
private FileDescriptor blockingDescriptor = null; private FileDescriptor blockingDescriptor = null;
private ParcelFileDescriptor parcelFileDescriptor; private ParcelFileDescriptor parcelFileDescriptor;
...@@ -105,28 +108,43 @@ public class DNSUDPProxy extends DNSProxy{ ...@@ -105,28 +108,43 @@ public class DNSUDPProxy extends DNSProxy{
public DNSUDPProxy(VpnService context, ParcelFileDescriptor parcelFileDescriptor, public DNSUDPProxy(VpnService context, ParcelFileDescriptor parcelFileDescriptor,
Set<IPPortPair> upstreamDNSServers, boolean resolveLocalRules, boolean queryLogging){ Set<IPPortPair> upstreamDNSServers, boolean resolveLocalRules, boolean queryLogging){
LogFactory.writeMessage(context, LOG_TAG, "Creating the proxy...");
if(parcelFileDescriptor == null)throw new IllegalStateException("The ParcelFileDescriptor passed to DNSUDPProxy is null."); if(parcelFileDescriptor == null)throw new IllegalStateException("The ParcelFileDescriptor passed to DNSUDPProxy is null.");
if(context == null)throw new IllegalStateException("The DNSVPNService passed to DNSUDPProxy is null."); if(context == null)throw new IllegalStateException("The DNSVPNService passed to DNSUDPProxy is null.");
this.parcelFileDescriptor = parcelFileDescriptor; this.parcelFileDescriptor = parcelFileDescriptor;
this.vpnService = context; this.vpnService = context;
LogFactory.writeMessage(context, LOG_TAG, "Parsing the upstream servers...");
for(IPPortPair pair: upstreamDNSServers){ for(IPPortPair pair: upstreamDNSServers){
if(pair != IPPortPair.getEmptyPair() && !pair.getAddress().equals(""))this.upstreamServers.put(pair.getAddress(), pair.getPort()); if(pair != IPPortPair.getEmptyPair() && !pair.getAddress().equals("")) this.upstreamServers.put(pair.getAddress(), pair.getPort());
} }
LogFactory.writeMessage(context, LOG_TAG, "Upstream servers parsed to: " + this.upstreamServers);
this.resolveLocalRules = resolveLocalRules; this.resolveLocalRules = resolveLocalRules;
this.queryLogging = queryLogging; this.queryLogging = queryLogging;
if(queryLogging) queryLogger = new QueryLogger(DatabaseHelper.getInstance(context)); if(queryLogging) {
if(resolveLocalRules) resolver = new DNSResolver(context); queryLogger = new QueryLogger(DatabaseHelper.getInstance(context));
LogFactory.writeMessage(context, LOG_TAG, "Created the query logger.");
}
if(resolveLocalRules) {
resolver = new DNSResolver(context);
LogFactory.writeMessage(context, LOG_TAG, "Created the rule resolver.");
}
LogFactory.writeMessage(context, LOG_TAG, "Created the proxy.");
} }
@Override @Override
public void run() throws IOException, ErrnoException { public void run() throws IOException, ErrnoException {
LogFactory.writeMessage(vpnService, LOG_TAG, "Starting the proxy");
if(!shouldRun){
LogFactory.writeMessage(vpnService, LOG_TAG, "Not running as shouldRun is false.");
return;
}
FileDescriptor[] pipes = Os.pipe(); FileDescriptor[] pipes = Os.pipe();
interruptedDescriptor = pipes[0]; interruptedDescriptor = pipes[0];
blockingDescriptor = pipes[1]; blockingDescriptor = pipes[1];
if(!shouldRun)return;
FileInputStream inputStream = new FileInputStream(parcelFileDescriptor.getFileDescriptor()); FileInputStream inputStream = new FileInputStream(parcelFileDescriptor.getFileDescriptor());
FileOutputStream outputStream = new FileOutputStream(parcelFileDescriptor.getFileDescriptor()); FileOutputStream outputStream = new FileOutputStream(parcelFileDescriptor.getFileDescriptor());
byte[] packet = new byte[32767]; byte[] packet = new byte[32767];
LogFactory.writeMessage(vpnService, LOG_TAG, "Entering the while loop");
outer: while(shouldRun){ outer: while(shouldRun){
StructPollfd structFd = new StructPollfd(); StructPollfd structFd = new StructPollfd();
structFd.fd = inputStream.getFD(); structFd.fd = inputStream.getFD();
...@@ -179,6 +197,7 @@ public class DNSUDPProxy extends DNSProxy{ ...@@ -179,6 +197,7 @@ public class DNSUDPProxy extends DNSProxy{
Os.poll(polls, timeout/pollTries); Os.poll(polls, timeout/pollTries);
pollTries = 0; pollTries = 0;
} catch(ErrnoException ex){ } catch(ErrnoException ex){
LogFactory.writeMessage(vpnService, LOG_TAG, "Polling failed with exception: " + ex.getMessage() + "(Cause: " + ex.getCause() + ")");
if(pollTries < 3) poll(polls, timeout); if(pollTries < 3) poll(polls, timeout);
else throw ex; else throw ex;
} }
...@@ -236,6 +255,7 @@ public class DNSUDPProxy extends DNSProxy{ ...@@ -236,6 +255,7 @@ public class DNSUDPProxy extends DNSProxy{
if(ipPacket != null) futureSocketAnswers.put(socket, new PacketWrap(ipPacket)); if(ipPacket != null) futureSocketAnswers.put(socket, new PacketWrap(ipPacket));
else socket.close(); else socket.close();
}catch(IOException exception){ }catch(IOException exception){
LogFactory.writeStackTrace(vpnService, LOG_TAG, exception);
handleUpstreamDNSResponse(ipPacket, outgoingPacket.getData()); handleUpstreamDNSResponse(ipPacket, outgoingPacket.getData());
} }
} }
...@@ -247,7 +267,7 @@ public class DNSUDPProxy extends DNSProxy{ ...@@ -247,7 +267,7 @@ public class DNSUDPProxy extends DNSProxy{
dnsSocket.receive(replyPacket); dnsSocket.receive(replyPacket);
handleUpstreamDNSResponse(parsedPacket, datagramData); handleUpstreamDNSResponse(parsedPacket, datagramData);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); LogFactory.writeStackTrace(vpnService, LOG_TAG, e);
} }
} }
...@@ -285,11 +305,14 @@ public class DNSUDPProxy extends DNSProxy{ ...@@ -285,11 +305,14 @@ public class DNSUDPProxy extends DNSProxy{
@Override @Override
public void stop() { public void stop() {
LogFactory.writeMessage(vpnService, LOG_TAG, "Stopping the proxy");
shouldRun = false; shouldRun = false;
try { try {
LogFactory.writeMessage(vpnService, LOG_TAG, "Closing the descriptors.");
if(interruptedDescriptor != null) Os.close(interruptedDescriptor); if(interruptedDescriptor != null) Os.close(interruptedDescriptor);
if(blockingDescriptor != null) Os.close(blockingDescriptor); if(blockingDescriptor != null) Os.close(blockingDescriptor);
} catch (Exception ignored) { } catch (Exception ignored) {
LogFactory.writeMessage(vpnService, LOG_TAG, "An error occurred when closing the descriptors: " + ignored.getMessage() + "(Cause: " + ignored.getCause() + ")");
} }
synchronized (futureSocketAnswers){ synchronized (futureSocketAnswers){
for(Map.Entry<DatagramSocket, PacketWrap> entry: futureSocketAnswers.entrySet()){ for(Map.Entry<DatagramSocket, PacketWrap> entry: futureSocketAnswers.entrySet()){
...@@ -302,6 +325,7 @@ public class DNSUDPProxy extends DNSProxy{ ...@@ -302,6 +325,7 @@ public class DNSUDPProxy extends DNSProxy{
writeToDevice.clear(); writeToDevice.clear();
if(resolver != null) resolver.destroy(); if(resolver != null) resolver.destroy();
if(queryLogger != null) queryLogger.destroy(); if(queryLogger != null) queryLogger.destroy();
LogFactory.writeMessage(vpnService, LOG_TAG, "Everything was destructed.");
parcelFileDescriptor = null; parcelFileDescriptor = null;
resolver = null; resolver = null;
vpnService = null; vpnService = null;
...@@ -313,12 +337,12 @@ public class DNSUDPProxy extends DNSProxy{ ...@@ -313,12 +337,12 @@ public class DNSUDPProxy extends DNSProxy{
private IpPacket packet; private IpPacket packet;
private final long time; private final long time;
public PacketWrap(IpPacket packet) { PacketWrap(IpPacket packet) {
this.packet = packet; this.packet = packet;
this.time = System.currentTimeMillis(); this.time = System.currentTimeMillis();
} }
public IpPacket getPacket() { IpPacket getPacket() {
return packet; return packet;
} }
......
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