Commit c690372f authored by Daniel Wolf's avatar Daniel Wolf

Implemented the test app as DNSTunnelProxy

parent 91e375fd
......@@ -47,6 +47,7 @@ allprojects {
jcenter()
google()
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
maven { url = 'https://dl.bintray.com/michaelbull/maven' }
maven {
url 'https://artifactory.frostnerd.com/artifactory/libs-release-local'
credentials {
......
......@@ -23,6 +23,17 @@ android {
lintOptions {
abortOnError false
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/notice.txt'
exclude 'META-INF/ASL2.0'
exclude 'META-INF/library_release.kotlin_module'
}
}
dependencies {
......@@ -39,6 +50,9 @@ dependencies {
implementation 'com.frostnerd.utils:preferenceexport:1.0.9'
implementation 'com.frostnerd.utils:api:1.0.5'
implementation 'com.frostnerd.utils:encrypteddnstunnelproxy:1.0.10'
implementation "androidx.appcompat:appcompat:1.0.0"
implementation "androidx.cardview:cardview:1.0.0"
implementation "androidx.recyclerview:recyclerview:1.0.0"
......
package com.frostnerd.dnschangertests;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.VpnService;
import android.os.Build;
import android.os.IBinder;
import android.os.ParcelFileDescriptor;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import kotlin.Unit;
import kotlin.jvm.functions.Function0;
import android.system.ErrnoException;
import android.system.OsConstants;
import android.util.Base64;
import com.frostnerd.dnstunnelproxy.DnsPacketProxy;
import com.frostnerd.dnstunnelproxy.UpstreamAddress;
import com.frostnerd.encrypteddnstunnelproxy.AbstractHttpsDNSHandle;
import com.frostnerd.encrypteddnstunnelproxy.Scheduler;
import com.frostnerd.encrypteddnstunnelproxy.ServerConfiguration;
import com.frostnerd.encrypteddnstunnelproxy.UrlCreator;
import com.frostnerd.networking.NetworkUtil;
import com.frostnerd.vpntunnelproxy.PacketProxy;
import com.frostnerd.vpntunnelproxy.ReceivedAnswer;
import com.frostnerd.vpntunnelproxy.VPNTunnelProxy;
import org.jetbrains.annotations.NotNull;
import org.minidns.dnsmessage.DnsMessage;
import org.pcap4j.packet.IpPacket;
import java.io.IOException;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Copyright Daniel Wolf 2017
......@@ -22,6 +46,7 @@ import com.frostnerd.networking.NetworkUtil;
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class TestVPNService extends VpnService implements Runnable {
private ParcelFileDescriptor fd;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
......@@ -38,12 +63,20 @@ public class TestVPNService extends VpnService implements Runnable {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder.setBlocking(true);
}
builder.addRoute("8.8.8.8", 32);
builder.setSession("DNS Test");
try {
builder.addDisallowedApplication(getPackageName());
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder.allowFamily(OsConstants.AF_INET);
builder.allowFamily(OsConstants.AF_INET6);
}
ParcelFileDescriptor fd = builder.establish();
fd = builder.establish();
System.out.println(">>>>>>>>>>>>>>>>");
System.out.println("Established");
new Thread(this).start();
}
......@@ -55,9 +88,99 @@ public class TestVPNService extends VpnService implements Runnable {
@Override
public void run() {
System.out.println("Run...");
https();
}
private void normal() {
PacketProxy packetProxy = new PacketProxy(this) {
@Override
public void processUpstreamResponse(@NotNull ReceivedAnswer receivedAnswer) {
}
@Override
public void processDevicePacket(@NotNull byte[] bytes) {
}
};
VPNTunnelProxy proxy = new VPNTunnelProxy(packetProxy);
try {
while (true) Thread.sleep(250);
} catch (InterruptedException e) {
proxy.run(fd);
} catch (IOException e) {
e.printStackTrace();
} catch (ErrnoException e) {
e.printStackTrace();
}
}
private void https() {
final ServerConfiguration serverConfiguration = new ServerConfiguration(new UrlCreator() {
@NotNull
@Override
public URL createUrl(@NotNull DnsMessage dnsMessage, @NotNull UpstreamAddress upstreamAddress) {
String encoded = Base64.encodeToString(dnsMessage.toArray(), Base64.DEFAULT);
try {
URL url = new URL("https://cloudflare-dns.com/dns-query?dns=" + encoded);
System.out.println(url);
return url;
} catch (MalformedURLException e) {
e.printStackTrace();
}
return null;
}
}, false, null);
final VPNTunnelProxy proxy = new VPNTunnelProxy(new DnsPacketProxy(new AbstractHttpsDNSHandle(serverConfiguration, 5000, new Scheduler() {
@Override
public void schedule(@NotNull final Function0<Unit> function0) {
new Thread() {
@Override
public void run() {
function0.invoke();
}
}.start();
}
}) {
@Override
public boolean shouldHandleDestination(@NotNull InetAddress inetAddress, int i) {
System.out.println("SHould handle: " + inetAddress);
return true;
}
@NotNull
@Override
public UpstreamAddress remapDestination(@NotNull InetAddress inetAddress, int i) {
System.out.println("Remapping " + inetAddress + ":" + i);
return new UpstreamAddress(inetAddress, i);
}
@NotNull
@Override
public DnsMessage modifyUpstreamResponse(@NotNull DnsMessage dnsMessage) {
return null;
}
@Override
public boolean shouldModifyUpstreamResponse(@NotNull ReceivedAnswer receivedAnswer, @NotNull byte[] bytes) {
return false;
}
@Override
public void forwardDnsQuestion(@NotNull DnsMessage dnsMessage, @NotNull IpPacket originalEnvelope, @NotNull UpstreamAddress realDestination) {
System.out.println("Forwarding " + dnsMessage);
System.out.println("Stats: " + getDnsPacketProxy().getTunnelHandle().getTrafficStats());
super.forwardDnsQuestion(dnsMessage, originalEnvelope, realDestination);
}
}, this, null, null));
try {
System.out.println("Now running proxy");
proxy.run(fd);
} catch (IOException e) {
e.printStackTrace();
} catch (ErrnoException e) {
e.printStackTrace();
}
}
......
Markdown is supported
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