summaryrefslogtreecommitdiff
path: root/app/java/src/DNSProxyService.java
diff options
context:
space:
mode:
Diffstat (limited to 'app/java/src/DNSProxyService.java')
-rw-r--r--app/java/src/DNSProxyService.java76
1 files changed, 72 insertions, 4 deletions
diff --git a/app/java/src/DNSProxyService.java b/app/java/src/DNSProxyService.java
index 117e6fc..8911036 100644
--- a/app/java/src/DNSProxyService.java
+++ b/app/java/src/DNSProxyService.java
@@ -1,23 +1,45 @@
package org.pihole.dnsproxy;
+import android.app.Notification;
+import android.app.NotificationChannel;
+import android.app.NotificationManager;
+import android.app.PendingIntent;
import android.app.Service;
+import android.content.Context;
import android.content.Intent;
+import android.net.DhcpInfo;
import android.net.VpnService;
+import android.net.wifi.WifiManager;
+
+import android.util.Log;
+
+import java.net.InetAddress;
public class DNSProxyService extends VpnService {
public static String LOG_TAG = "org.pihole.dnsproxy.log";
public static String NOTIFICATION = "org.pihole.dnsproxy.service";
+ public static String NOTIFICATION_CHANNEL_ID = "PiholeDNSProxy";
+ public static String ACTION_START = "org.pihole.dnsproxy.service.START";
+ public static String ACTION_STOP = "org.pihole.dnsproxy.service.STOP";
+
+ public static String PIHOLE_ADDRESS = "";
private static DNSProxyConnection connection;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
- this.start();
+ if (intent.getAction().equals(DNSProxyService.ACTION_START)) {
+ this.start();
- return Service.START_STICKY;
+ return Service.START_STICKY;
+ } else {
+ this.stop();
+
+ return Service.START_NOT_STICKY;
+ }
}
@Override
@@ -27,14 +49,22 @@ public class DNSProxyService extends VpnService {
super.onDestroy();
}
+ public VpnService.Builder newBuilder() {
+ return new VpnService.Builder();
+ }
+
public static boolean isRunning()
{
return DNSProxyService.connection != null;
}
public void start() {
+ this.setPiholeAddress();
+
DNSProxyService.connection = new DNSProxyConnection(this);
- // DNSProxyService.connection.start();
+ DNSProxyService.connection.start();
+
+ this.startForeground();
// send notification when started
Intent notification = new Intent(DNSProxyService.NOTIFICATION);
@@ -42,11 +72,49 @@ public class DNSProxyService extends VpnService {
}
public void stop() {
- // DNSProxyService.connection.stop();
+ DNSProxyService.connection.stop();
DNSProxyService.connection = null;
+ stopForeground(true);
+
// send notification when stopped
Intent notification = new Intent(DNSProxyService.NOTIFICATION);
sendBroadcast(notification);
}
+
+ private void setPiholeAddress() {
+ WifiManager manager = (WifiManager) getSystemService(WIFI_SERVICE);
+ DhcpInfo info = manager.getDhcpInfo();
+
+ byte[] addressBytes = {
+ (byte) (0xff & info.dns1),
+ (byte) (0xff & (info.dns1 >> 8)),
+ (byte) (0xff & (info.dns1 >> 16)),
+ (byte) (0xff & (info.dns1 >> 24)),
+ };
+
+ try {
+ DNSProxyService.PIHOLE_ADDRESS = InetAddress.getByAddress(addressBytes).toString().replaceAll("/", "");
+ } catch (Exception e) {
+ DNSProxyService.PIHOLE_ADDRESS = "";
+ }
+ }
+
+ private void startForeground() {
+ NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
+
+ NotificationChannel channel = new NotificationChannel(
+ DNSProxyService.NOTIFICATION_CHANNEL_ID, DNSProxyService.NOTIFICATION_CHANNEL_ID, NotificationManager.IMPORTANCE_DEFAULT
+ );
+ manager.createNotificationChannel(channel);
+
+ Notification notification = new Notification.Builder(this, DNSProxyService.NOTIFICATION_CHANNEL_ID)
+ .setSmallIcon(R.drawable.logo)
+ .setContentTitle("Pihole DNS Proxy")
+ .setContentText("Running with " + DNSProxyService.PIHOLE_ADDRESS)
+ // .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(MainActivity.class), PendingIntent.FLAG_IMMUTABLE))
+ .build();
+
+ startForeground(1, notification);
+ }
}