From 7f0436aa2a782d926a81bd5bdf76baf0253582da Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Fri, 12 May 2023 13:12:57 +0200 Subject: working commit --- app/java/src/DNSProxyRunner.java | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) (limited to 'app/java/src/DNSProxyRunner.java') diff --git a/app/java/src/DNSProxyRunner.java b/app/java/src/DNSProxyRunner.java index 0c136e8..6a45413 100644 --- a/app/java/src/DNSProxyRunner.java +++ b/app/java/src/DNSProxyRunner.java @@ -1,5 +1,7 @@ package org.pihole.dnsproxy; +import android.net.VpnService; + import android.os.ParcelFileDescriptor; import android.util.Log; @@ -14,6 +16,7 @@ import java.net.SocketAddress; import java.nio.channels.DatagramChannel; public class DNSProxyRunner implements Runnable { + public interface OnEstablishListener { void onEstablish(ParcelFileDescriptor networkInterface); } @@ -28,22 +31,15 @@ public class DNSProxyRunner implements Runnable { @Override public void run() { try { - DatagramChannel tunnel = DatagramChannel.open(); - - if(!this.service.protect(tunnel.socket())) { - throw new IllegalStateException("Cannot protect tunnel"); - } - - // SocketAddress server = new InetSocketAddress("servername", "serverport"); - // - // tunnel.connect(server); - // tunnel.configureBlocking(false); - // - // ParcelFileDescriptor networkInterface = this.handshake(tunnel); - // FileInputStream in = new FileInputStream(networkInterface.getFileDescriptor()); - // FileOutputStream out = new FileOutputStream(networkInterface.getFileDescriptor()); - } catch (IOException exception) { - Log.e(DNSProxyService.LOG_TAG, "Cannot use socket", exception); + VpnService.Builder builder = this.service.newBuilder() + .setSession("Pihole DNS Proxy") + .addAddress("10.111.222.1", 24) + .addDnsServer(DNSProxyService.PIHOLE_ADDRESS) + .setBlocking(true); + + this.onEstablishListener.onEstablish(builder.establish()); + } catch (Exception exception) { + Log.e(DNSProxyService.LOG_TAG, "Failed to establish VPN connection", exception); } } -- cgit v1.2.3