Native is numeric address, reverse DNS for numeric access addresses

This commit is contained in:
M66B 2016-02-09 16:31:56 +01:00
parent d6e9f711f4
commit 8eba191c43
9 changed files with 68 additions and 11 deletions

View File

@ -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<String, Object, String>() {
@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);

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -562,7 +562,7 @@ public class RuleAdapter extends RecyclerView.Adapter<RuleAdapter.ViewHolder> 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));

View File

@ -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();

View File

@ -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)

View File

@ -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");

View File

@ -7,10 +7,12 @@
#include <pthread.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <dlfcn.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>