package org.pihole.dnsproxy; import android.net.VpnService; import android.os.ParcelFileDescriptor; import android.util.Log; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.InetSocketAddress; import java.net.SocketAddress; import java.nio.channels.DatagramChannel; public class DNSProxyRunner implements Runnable { public interface OnEstablishListener { void onEstablish(ParcelFileDescriptor networkInterface); } private OnEstablishListener onEstablishListener; private DNSProxyService service; DNSProxyRunner(DNSProxyService service) { this.service = service; } @Override public void run() { try { 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); } } public void setOnEstablishListener(OnEstablishListener listener) { this.onEstablishListener = listener; } }