NetGuard/app/src/main/java/eu/faircode/netguard/LogAdapter.java

228 lines
9.0 KiB
Java
Raw Normal View History

2016-01-05 12:40:02 +00:00
package eu.faircode.netguard;
import android.content.Context;
2016-01-23 15:48:51 +00:00
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
2016-01-05 12:40:02 +00:00
import android.database.Cursor;
2016-01-06 10:12:37 +00:00
import android.net.Uri;
import android.os.AsyncTask;
2016-01-23 15:48:51 +00:00
import android.preference.PreferenceManager;
import android.util.Log;
2016-01-05 12:40:02 +00:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
2016-01-05 12:40:02 +00:00
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.net.InetAddress;
import java.net.UnknownHostException;
2016-01-05 12:40:02 +00:00
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
2016-01-05 12:40:02 +00:00
public class LogAdapter extends CursorAdapter {
2016-01-23 15:48:51 +00:00
private static String TAG = "NetGuard.Log";
private boolean resolve;
2016-01-05 12:40:02 +00:00
private int colTime;
private int colVersion;
private int colProtocol;
private int colFlags;
private int colSource;
private int colSPort;
private int colDest;
private int colDPort;
private int colUid;
private int colAllowed;
private int colConnection;
private int colInteractive;
2016-01-23 15:48:51 +00:00
private InetAddress vpn4 = null;
private InetAddress vpn6 = null;
private InetAddress dns = null;
private Map<String, String> mapIPHost = new HashMap<String, String>();
2016-01-05 12:40:02 +00:00
public LogAdapter(Context context, Cursor cursor, boolean resolve) {
2016-01-05 12:40:02 +00:00
super(context, cursor, 0);
this.resolve = resolve;
2016-01-05 12:40:02 +00:00
colTime = cursor.getColumnIndex("time");
colVersion = cursor.getColumnIndex("version");
colProtocol = cursor.getColumnIndex("protocol");
colFlags = cursor.getColumnIndex("flags");
colSource = cursor.getColumnIndex("saddr");
colSPort = cursor.getColumnIndex("sport");
colDest = cursor.getColumnIndex("daddr");
colDPort = cursor.getColumnIndex("dport");
colUid = cursor.getColumnIndex("uid");
colAllowed = cursor.getColumnIndex("allowed");
colConnection = cursor.getColumnIndex("connection");
colInteractive = cursor.getColumnIndex("interactive");
2016-01-23 15:48:51 +00:00
try {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
vpn4 = InetAddress.getByName(prefs.getString("vpn4", "10.1.10.1"));
vpn6 = InetAddress.getByName(prefs.getString("vpn6", "fd00:1:fd00:1:fd00:1:fd00:1"));
2016-01-25 09:46:47 +00:00
dns = InetAddress.getByName(prefs.getString("dns", Util.getDefaultDNS(context)));
2016-01-23 15:48:51 +00:00
} catch (UnknownHostException ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
2016-01-05 12:40:02 +00:00
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.log, parent, false);
}
@Override
public void bindView(final View view, final Context context, final Cursor cursor) {
// Get values
2016-01-06 10:12:37 +00:00
long time = cursor.getLong(colTime);
int version = (cursor.isNull(colVersion) ? -1 : cursor.getInt(colVersion));
int protocol = (cursor.isNull(colProtocol) ? -1 : cursor.getInt(colProtocol));
String flags = cursor.getString(colFlags);
String source = cursor.getString(colSource);
int sport = (cursor.isNull(colSPort) ? -1 : cursor.getInt(colSPort));
final String dest = cursor.getString(colDest);
int dport = (cursor.isNull(colDPort) ? -1 : cursor.getInt(colDPort));
2016-01-10 13:26:37 +00:00
int uid = (cursor.isNull(colUid) ? -1 : cursor.getInt(colUid));
int allowed = (cursor.isNull(colAllowed) ? -1 : cursor.getInt(colAllowed));
int connection = (cursor.isNull(colConnection) ? -1 : cursor.getInt(colConnection));
int interactive = (cursor.isNull(colInteractive) ? -1 : cursor.getInt(colInteractive));
2016-01-05 12:40:02 +00:00
// Get views
TextView tvTime = (TextView) view.findViewById(R.id.tvTime);
TextView tvProtocol = (TextView) view.findViewById(R.id.tvProtocol);
TextView tvFlags = (TextView) view.findViewById(R.id.tvFlags);
final TextView tvSource = (TextView) view.findViewById(R.id.tvSource);
TextView tvSPort = (TextView) view.findViewById(R.id.tvSPort);
final TextView tvDest = (TextView) view.findViewById(R.id.tvDest);
TextView tvDPort = (TextView) view.findViewById(R.id.tvDPort);
2016-01-08 16:21:50 +00:00
ImageView ivIcon = (ImageView) view.findViewById(R.id.ivIcon);
TextView tvUid = (TextView) view.findViewById(R.id.tvUid);
ImageView ivConnection = (ImageView) view.findViewById(R.id.ivConnection);
ImageView ivInteractive = (ImageView) view.findViewById(R.id.ivInteractive);
2016-01-08 16:21:50 +00:00
// Set values
tvTime.setText(new SimpleDateFormat("HH:mm:ss").format(time));
if (connection <= 0)
ivConnection.setImageDrawable(null);
2016-01-19 19:58:51 +00:00
else {
if (allowed > 0)
ivConnection.setImageResource(connection == 1 ? R.drawable.wifi_on : R.drawable.other_on);
else
ivConnection.setImageResource(connection == 1 ? R.drawable.wifi_off : R.drawable.other_off);
}
if (interactive <= 0)
ivInteractive.setImageDrawable(null);
else
ivInteractive.setImageResource(R.drawable.screen_on);
2016-01-08 16:21:50 +00:00
2016-01-22 10:28:29 +00:00
// https://en.wikipedia.org/wiki/List_of_IP_protocol_numbers
if (protocol == 0) // HOPOPT
tvProtocol.setText("HOPO");
else if (protocol == 1) // ICMP
tvProtocol.setText("ICMP");
2016-01-09 18:53:28 +00:00
else if (protocol == 6) // TCP
2016-01-22 10:28:29 +00:00
tvProtocol.setText("TCP");
2016-01-09 18:53:28 +00:00
else if (protocol == 17) // UDP
2016-01-22 10:28:29 +00:00
tvProtocol.setText("UDP");
2016-01-08 16:21:50 +00:00
else
tvProtocol.setText(protocol < 0 ? "" : Integer.toString(protocol));
2016-01-25 15:41:54 +00:00
tvFlags.setText(version + flags);
2016-01-05 12:40:02 +00:00
tvSPort.setText(sport < 0 ? "" : Integer.toString(sport));
tvDPort.setText(dport < 0 ? "" : Integer.toString(dport));
// Application icon
ApplicationInfo info = null;
PackageManager pm = context.getPackageManager();
String[] pkg = pm.getPackagesForUid(uid);
if (pkg != null && pkg.length > 0)
try {
info = pm.getApplicationInfo(pkg[0], 0);
} catch (PackageManager.NameNotFoundException ignored) {
}
2016-01-10 13:13:36 +00:00
if (info == null)
2016-01-08 11:14:31 +00:00
ivIcon.setImageDrawable(null);
2016-01-10 13:13:36 +00:00
else if (info.icon == 0)
Picasso.with(context).load(android.R.drawable.sym_def_app_icon).into(ivIcon);
else {
Uri uri = Uri.parse("android.resource://" + info.packageName + "/" + info.icon);
Picasso.with(context).load(uri).into(ivIcon);
}
2016-01-10 13:26:37 +00:00
// https://android.googlesource.com/platform/system/core/+/master/include/private/android_filesystem_config.h
uid = uid % 100000; // strip off user ID
if (uid == -1)
tvUid.setText("");
else if (uid == 0)
tvUid.setText("root");
else if (uid == 9999)
tvUid.setText("-"); // nobody
else
tvUid.setText(Integer.toString(uid));
// TODO resolve source when inbound
2016-01-23 15:48:51 +00:00
tvSource.setText(getKnownAddress(source));
2016-01-23 15:48:51 +00:00
if (resolve && !isKnownAddress(dest))
synchronized (mapIPHost) {
if (mapIPHost.containsKey(dest))
tvDest.setText(mapIPHost.get(dest));
else {
tvDest.setText(dest);
new AsyncTask<String, Object, String>() {
@Override
protected String doInBackground(String... args) {
try {
// This requires internet permission
return InetAddress.getByName(args[0]).getHostName();
} catch (UnknownHostException ignored) {
return dest;
}
}
@Override
protected void onPostExecute(String host) {
synchronized (mapIPHost) {
if (!mapIPHost.containsKey(host))
mapIPHost.put(host, host);
}
tvDest.setText(host);
}
}.execute(dest);
}
}
else
2016-01-23 15:48:51 +00:00
tvDest.setText(getKnownAddress(dest));
}
private boolean isKnownAddress(String addr) {
try {
InetAddress a = InetAddress.getByName(addr);
if (a.equals(vpn4) || a.equals(vpn6) || a.equals(dns))
return true;
} catch (UnknownHostException ignored) {
}
return false;
}
private String getKnownAddress(String addr) {
try {
InetAddress a = InetAddress.getByName(addr);
if (a.equals(vpn4) || a.equals(vpn6))
return "vpn";
else if (a.equals(dns))
return "dns";
} catch (UnknownHostException ignored) {
}
return addr;
2016-01-05 12:40:02 +00:00
}
}