diff --git a/app/src/main/java/eu/faircode/netguard/AccessAdapter.java b/app/src/main/java/eu/faircode/netguard/AccessAdapter.java index d48a7c9f..d9681bb6 100644 --- a/app/src/main/java/eu/faircode/netguard/AccessAdapter.java +++ b/app/src/main/java/eu/faircode/netguard/AccessAdapter.java @@ -23,6 +23,7 @@ import android.content.Context; import android.content.res.TypedArray; import android.database.Cursor; import android.graphics.drawable.Drawable; +import android.os.AsyncTask; import android.os.Build; import android.support.v4.graphics.drawable.DrawableCompat; import android.util.TypedValue; @@ -33,6 +34,8 @@ import android.widget.CursorAdapter; import android.widget.ImageView; import android.widget.TextView; +import java.net.InetAddress; +import java.net.UnknownHostException; import java.text.SimpleDateFormat; public class AccessAdapter extends CursorAdapter { @@ -82,10 +85,10 @@ public class AccessAdapter extends CursorAdapter { @Override public void bindView(final View view, final Context context, final Cursor cursor) { // Get values - int version = cursor.getInt(colVersion); - int protocol = cursor.getInt(colProtocol); - String daddr = cursor.getString(colDaddr); - int dport = cursor.getInt(colDPort); + final int version = cursor.getInt(colVersion); + final int protocol = cursor.getInt(colProtocol); + final String daddr = cursor.getString(colDaddr); + final int dport = cursor.getInt(colDPort); long time = cursor.getLong(colTime); int allowed = cursor.getInt(colAllowed); int block = cursor.getInt(colBlock); @@ -106,9 +109,29 @@ public class AccessAdapter extends CursorAdapter { DrawableCompat.setTint(wrap, block > 0 ? colorOff : colorOn); } } + tvDest.setText( - Util.getProtocolName(protocol, version, true) + " " + - daddr + (dport > 0 ? ":" + dport : "")); + Util.getProtocolName(protocol, version, true) + + " " + daddr + (dport > 0 ? "/" + dport : "")); + + if (Util.isNumericAddress(daddr)) + new AsyncTask() { + @Override + protected String doInBackground(String... args) { + try { + return InetAddress.getByName(args[0]).getHostName(); + } catch (UnknownHostException ignored) { + return args[0]; + } + } + + @Override + protected void onPostExecute(String addr) { + tvDest.setText( + Util.getProtocolName(protocol, version, true) + + " " + addr + (dport > 0 ? "/" + dport : "")); + } + }.execute(daddr); if (allowed < 0) tvDest.setTextColor(colorText); diff --git a/app/src/main/java/eu/faircode/netguard/ActivityForwardApproval.java b/app/src/main/java/eu/faircode/netguard/ActivityForwardApproval.java index 699520bf..bb898bc0 100644 --- a/app/src/main/java/eu/faircode/netguard/ActivityForwardApproval.java +++ b/app/src/main/java/eu/faircode/netguard/ActivityForwardApproval.java @@ -82,7 +82,7 @@ am start -a eu.faircode.netguard.START_PORT_FORWARD \ --ei ruid 1 \ --user 0 */ - Log.i(TAG, "Start forwarding protocol " + protocol + " port " + dport + " to " + raddr + ":" + rport + " uid " + ruid); + Log.i(TAG, "Start forwarding protocol " + protocol + " port " + dport + " to " + raddr + "/" + rport + " uid " + ruid); DatabaseHelper dh = new DatabaseHelper(ActivityForwardApproval.this); dh.deleteForward(protocol, dport); dh.addForward(protocol, dport, raddr, rport, ruid); diff --git a/app/src/main/java/eu/faircode/netguard/Forward.java b/app/src/main/java/eu/faircode/netguard/Forward.java index 07c849ce..f27cf5e4 100644 --- a/app/src/main/java/eu/faircode/netguard/Forward.java +++ b/app/src/main/java/eu/faircode/netguard/Forward.java @@ -9,6 +9,6 @@ public class Forward { @Override public String toString() { - return "protocol=" + protocol + " port " + dport + " to " + raddr + ":" + rport + " uid " + ruid; + return "protocol=" + protocol + " port " + dport + " to " + raddr + "/" + rport + " uid " + ruid; } } diff --git a/app/src/main/java/eu/faircode/netguard/Packet.java b/app/src/main/java/eu/faircode/netguard/Packet.java index 5fc97200..3e4331d9 100644 --- a/app/src/main/java/eu/faircode/netguard/Packet.java +++ b/app/src/main/java/eu/faircode/netguard/Packet.java @@ -37,6 +37,6 @@ public class Packet { @Override public String toString() { - return "uid=" + uid + " v" + version + " p" + protocol + " " + daddr + ":" + dport; + return "uid=" + uid + " v" + version + " p" + protocol + " " + daddr + "/" + dport; } } diff --git a/app/src/main/java/eu/faircode/netguard/RuleAdapter.java b/app/src/main/java/eu/faircode/netguard/RuleAdapter.java index f930b1e9..5c2195a5 100644 --- a/app/src/main/java/eu/faircode/netguard/RuleAdapter.java +++ b/app/src/main/java/eu/faircode/netguard/RuleAdapter.java @@ -562,7 +562,7 @@ public class RuleAdapter extends RecyclerView.Adapter im popup.inflate(R.menu.access); popup.getMenu().findItem(R.id.menu_host).setTitle( Util.getProtocolName(protocol, version, false) + " " + - daddr + (dport > 0 ? ":" + dport : "")); + daddr + (dport > 0 ? "/" + dport : "")); popup.getMenu().findItem(R.id.menu_time).setTitle( SimpleDateFormat.getDateTimeInstance().format(time)); diff --git a/app/src/main/java/eu/faircode/netguard/SinkholeService.java b/app/src/main/java/eu/faircode/netguard/SinkholeService.java index 2e56fd4c..680afe88 100644 --- a/app/src/main/java/eu/faircode/netguard/SinkholeService.java +++ b/app/src/main/java/eu/faircode/netguard/SinkholeService.java @@ -1180,7 +1180,7 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS allowed = new Allowed(); } else { allowed = new Allowed(fwd.raddr, fwd.rport); - packet.data = "> " + fwd.raddr + ":" + fwd.rport; + packet.data = "> " + fwd.raddr + "/" + fwd.rport; } } else allowed = new Allowed(); @@ -1694,6 +1694,11 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS sb.append(df.format(cursor.getLong(colTime))).append(' '); String daddr = cursor.getString(colDAddr); + if (Util.isNumericAddress(daddr)) + try { + daddr = InetAddress.getByName(daddr).getHostName(); + } catch (UnknownHostException ignored) { + } sb.append(daddr); int allowed = cursor.getInt(colAllowed); @@ -1703,6 +1708,7 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS ForegroundColorSpan fgsp = new ForegroundColorSpan(allowed > 0 ? colorOn : colorOff); sp.setSpan(fgsp, pos, pos + daddr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } + notification.addLine(sp); } cursor.close(); diff --git a/app/src/main/java/eu/faircode/netguard/Util.java b/app/src/main/java/eu/faircode/netguard/Util.java index 9693e963..4ad9fed4 100644 --- a/app/src/main/java/eu/faircode/netguard/Util.java +++ b/app/src/main/java/eu/faircode/netguard/Util.java @@ -77,6 +77,8 @@ public class Util { private static native String jni_getprop(String name); + private static native boolean is_numeric_address(String ip); + static { System.loadLibrary("netguard"); } @@ -274,6 +276,10 @@ public class Util { return jni_getprop("net.dns1"); } + public static boolean isNumericAddress(String ip) { + return is_numeric_address(ip); + } + public static boolean isInteractive(Context context) { PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT_WATCH) diff --git a/app/src/main/jni/netguard/netguard.c b/app/src/main/jni/netguard/netguard.c index 0a4c60c4..f66ef0c9 100644 --- a/app/src/main/jni/netguard/netguard.c +++ b/app/src/main/jni/netguard/netguard.c @@ -270,6 +270,26 @@ Java_eu_faircode_netguard_Util_jni_1getprop(JNIEnv *env, jclass type, jstring na return (*env)->NewStringUTF(env, value); } +JNIEXPORT jboolean JNICALL +Java_eu_faircode_netguard_Util_is_1numeric_1address(JNIEnv *env, jclass type, jstring ip_) { + jboolean numeric = 0; + const char *ip = (*env)->GetStringUTFChars(env, ip_, 0); + + struct addrinfo hints; + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = AF_UNSPEC; + hints.ai_flags = AI_NUMERICHOST; + struct addrinfo *result; + int err = getaddrinfo(ip, NULL, &hints, &result); + if (err) + log_android(ANDROID_LOG_WARN, "getaddrinfo(%s) error %d: %s", ip, err, gai_strerror(err)); + else + numeric = (result != NULL); + + (*env)->ReleaseStringUTFChars(env, ip_, ip); + return numeric; +} + void report_exit(const struct arguments *args, const char *fmt, ...) { jclass cls = (*args->env)->GetObjectClass(args->env, args->instance); jmethodID mid = jniGetMethodID(args->env, cls, "nativeExit", "(Ljava/lang/String;)V"); diff --git a/app/src/main/jni/netguard/netguard.h b/app/src/main/jni/netguard/netguard.h index bc21ce12..ea5baf14 100644 --- a/app/src/main/jni/netguard/netguard.h +++ b/app/src/main/jni/netguard/netguard.h @@ -7,10 +7,12 @@ #include #include #include +#include #include #include #include +#include #include #include #include