package org.pihole.dnsproxy; import android.net.VpnService; import android.os.ParcelFileDescriptor; import android.util.Log; 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); } } /** * Callback when proxy connection is established */ public void setOnEstablishListener(OnEstablishListener listener) { this.onEstablishListener = listener; } }