Added protocol and uid to blocked traffic log

This commit is contained in:
M66B 2016-01-07 07:47:00 +01:00
parent 071f50b9e5
commit 9c26fd2b58
9 changed files with 113 additions and 32 deletions

View File

@ -65,6 +65,7 @@
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/renderscript" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/binaries" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" />
@ -74,8 +75,8 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/objectFiles" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/pre-dexed" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/proguard-rules" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />

View File

@ -16,7 +16,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = "NetGuard.Database";
private static final String DB_NAME = "Netguard";
private static final int DB_VERSION = 1;
private static final int DB_VERSION = 2;
private static List<LogChangedListener> logChangedListeners = new ArrayList<LogChangedListener>();
@ -37,7 +37,10 @@ public class DatabaseHelper extends SQLiteOpenHelper {
db.execSQL("CREATE TABLE log (" +
" ID INTEGER PRIMARY KEY AUTOINCREMENT" +
", time INTEGER NOT NULL" +
", version INTEGER NULL" +
", ip TEXT" +
", protocol INTEGER NULL" +
", uid INTEGER NULL" +
");");
db.execSQL("CREATE INDEX idx_log_time ON log(time)");
}
@ -48,6 +51,13 @@ public class DatabaseHelper extends SQLiteOpenHelper {
db.beginTransaction();
try {
if (oldVersion < 2) {
db.execSQL("ALTER TABLE log ADD COLUMN version INTEGER NULL");
db.execSQL("ALTER TABLE log ADD COLUMN protocol INTEGER NULL");
db.execSQL("ALTER TABLE log ADD COLUMN uid INTEGER NULL");
oldVersion = 2;
}
db.setVersion(DB_VERSION);
db.setTransactionSuccessful();
@ -60,14 +70,25 @@ public class DatabaseHelper extends SQLiteOpenHelper {
// Location
public DatabaseHelper insertLog(String ip) {
public DatabaseHelper insertLog(int version, String ip, int protocol, int uid) {
synchronized (mContext.getApplicationContext()) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("time", new Date().getTime());
cv.put("version", version);
cv.put("ip", ip);
if (protocol < 0)
cv.putNull("protocol");
else
cv.put("protocol", protocol);
if (uid < 0)
cv.putNull("uid");
else
cv.put("uid", uid);
if (db.insert("log", null, cv) == -1)
Log.e(TAG, "Insert log failed");
}

View File

@ -15,12 +15,18 @@ import java.text.SimpleDateFormat;
public class LogAdapter extends CursorAdapter {
private int colTime;
private int colVersion;
private int colIP;
private int colProtocol;
private int colUid;
public LogAdapter(Context context, Cursor cursor) {
super(context, cursor, 0);
colTime = cursor.getColumnIndex("time");
colVersion = cursor.getColumnIndex("version");
colIP = cursor.getColumnIndex("ip");
colProtocol = cursor.getColumnIndex("protocol");
colUid = cursor.getColumnIndex("uid");
}
@Override
@ -32,17 +38,38 @@ public class LogAdapter extends CursorAdapter {
public void bindView(final View view, final Context context, final Cursor cursor) {
// Get values
long time = cursor.getLong(colTime);
int version = (cursor.isNull(colVersion) ? -1 : cursor.getInt(colVersion));
String ip = cursor.getString(colIP);
int protocol = (cursor.isNull(colProtocol) ? -1 : cursor.getInt(colProtocol));
final int uid = (cursor.isNull(colUid) ? -1 : cursor.getInt(colUid));
final String whois = (ip.length() > 1 && ip.charAt(0) == '/' ? ip.substring(1) : ip);
// Get views
TextView tvTime = (TextView) view.findViewById(R.id.tvTime);
TextView tvIP = (TextView) view.findViewById(R.id.tvIP);
TextView tvProtocol = (TextView) view.findViewById(R.id.tvProtocol);
TextView tvUid = (TextView) view.findViewById(R.id.tvUid);
// Set values
tvTime.setText(SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.SHORT, SimpleDateFormat.MEDIUM).format(time));
tvIP.setText(ip);
tvTime.setText(new SimpleDateFormat("dd").format(time) + " " +
SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM).format(time));
tvIP.setText(whois);
if (version == 4)
if (protocol == Packet.IPv4Header.ICMP)
tvProtocol.setText("ICMP");
else if (protocol == Packet.IPv4Header.TCP)
tvProtocol.setText("TCP");
else if (protocol == Packet.IPv4Header.UDP)
tvProtocol.setText("UDP");
else
tvProtocol.setText(protocol < 0 ? "" : Integer.toString(protocol));
else
tvProtocol.setText("");
tvUid.setText(uid < 0 ? "" : Integer.toString(uid % 100000));
final String whois = (ip.length() > 1 && ip.charAt(0) == '/' ? ip.substring(1) : ip);
tvIP.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
@ -53,5 +80,16 @@ public class LogAdapter extends CursorAdapter {
}
}
});
tvUid.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (uid > 0) {
Intent main = new Intent(context, ActivityMain.class);
main.putExtra(ActivityMain.EXTRA_SEARCH, Integer.toString(uid));
context.startActivity(main);
}
}
});
}
}

View File

@ -50,7 +50,6 @@ public class Packet {
if (IPv4.protocol == IPv4.UDP) {
UDP = new UDPHeader(buffer);
UDP.validate();
throw new IOException("UDP not supported");
} else if (IPv4.protocol == IPv4.TCP) {
TCP = new TCP(IPv4.sourceAddress, IPv4.destinationAddress, buffer);
TCP.validate();
@ -494,15 +493,18 @@ public class Packet {
}
public int getUid4() {
String addr = "";
if (this.TCP == null)
return -1;
StringBuilder addr = new StringBuilder();
byte[] b = this.IPv4.sourceAddress.getAddress();
for (int i = b.length - 1; i >= 0; i--)
addr += String.format("%02X", b[i]);
addr += ":" + String.format("%04X", this.TCP.sourcePort);
addr.append(String.format("%02X", b[i]));
addr.append(':').append(String.format("%04X", this.TCP.sourcePort));
int uid = scanUid("0000000000000000FFFF0000" + addr, "/proc/net/tcp6");
int uid = scanUid("0000000000000000FFFF0000" + addr.toString(), "/proc/net/tcp6");
if (uid < 0)
uid = scanUid(addr, "/proc/net/tcp");
uid = scanUid(addr.toString(), "/proc/net/tcp");
return uid;
}

View File

@ -114,10 +114,11 @@ public class Receiver extends BroadcastReceiver {
throw new PackageManager.NameNotFoundException(Integer.toString(uid));
ApplicationInfo info = pm.getApplicationInfo(packages[0], 0);
String name = (String) pm.getApplicationLabel(info);
boolean internet = (pm.checkPermission("android.permission.INTERNET", info.packageName) == PackageManager.PERMISSION_GRANTED);
// Build notification
Intent main = new Intent(context, ActivityMain.class);
main.putExtra(ActivityMain.EXTRA_SEARCH, name);
main.putExtra(ActivityMain.EXTRA_SEARCH, Integer.toString(info.uid));
PendingIntent pi = PendingIntent.getActivity(context, 999, main, PendingIntent.FLAG_UPDATE_CURRENT);
Util.setTheme(context);
@ -169,7 +170,14 @@ public class Receiver extends BroadcastReceiver {
);
// Show notification
NotificationManagerCompat.from(context).notify(uid, notification.build());
if (internet)
NotificationManagerCompat.from(context).notify(uid, notification.build());
else {
NotificationCompat.BigTextStyle expanded = new NotificationCompat.BigTextStyle(notification);
expanded.bigText(context.getString(R.string.msg_installed, name));
expanded.setSummaryText(context.getString(R.string.title_internet));
NotificationManagerCompat.from(context).notify(uid, expanded.build());
}
} catch (PackageManager.NameNotFoundException ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));

View File

@ -306,12 +306,8 @@ public class RuleAdapter extends RecyclerView.Adapter<RuleAdapter.ViewHolder> im
holder.llConfiguration.setVisibility(rule.expanded ? View.VISIBLE : View.GONE);
holder.tvUid.setVisibility(debuggable ? View.VISIBLE : View.GONE);
holder.tvUid.setText(rule.info.applicationInfo == null ? "?" : Integer.toString(rule.info.applicationInfo.uid));
holder.tvPackage.setText(rule.info.packageName);
holder.tvVersion.setVisibility(debuggable ? View.VISIBLE : View.GONE);
holder.tvVersion.setText(rule.info.versionName + '/' + rule.info.versionCode);
holder.tvDisabled.setVisibility(rule.enabled ? View.GONE : View.VISIBLE);
@ -518,8 +514,7 @@ public class RuleAdapter extends RecyclerView.Adapter<RuleAdapter.ViewHolder> im
for (Rule rule : listAll)
if (rule.info.packageName.toLowerCase().contains(query) ||
(rule.name != null && rule.name.toLowerCase().contains(query)) ||
(debuggable && rule.info.applicationInfo != null &&
Integer.toString(rule.info.applicationInfo.uid).contains(query)))
(rule.info.applicationInfo != null && Integer.toString(rule.info.applicationInfo.uid).contains(query)))
listResult.add(rule);
}

View File

@ -701,12 +701,14 @@ public class SinkholeService extends VpnService {
byte version = (byte) (buffer.get() >> 4);
if (version == 4) {
buffer.position(16);
byte[] addressBytes = new byte[4];
buffer.get(addressBytes, 0, 4);
InetAddress ina = Inet4Address.getByAddress(addressBytes);
Log.i(TAG, "Packet to " + ina);
new DatabaseHelper(SinkholeService.this).insertLog(ina.toString()).close();
buffer.position(0);
Packet pkt = new Packet(buffer);
Log.i(TAG, "Packet to " + pkt.IPv4.destinationAddress.toString());
new DatabaseHelper(SinkholeService.this).insertLog(
version,
pkt.IPv4.destinationAddress.toString(),
pkt.IPv4.protocol,
pkt.getUid4()).close();
} else if (version == 6) {
buffer.position(24);
@ -714,7 +716,9 @@ public class SinkholeService extends VpnService {
buffer.get(addressBytes, 0, 16);
InetAddress ina = Inet6Address.getByAddress(addressBytes);
Log.i(TAG, "Packet to " + ina);
new DatabaseHelper(SinkholeService.this).insertLog(ina.toString()).close();
new DatabaseHelper(SinkholeService.this).insertLog(
version,
ina.toString(), -1, -1).close();
}
}
} catch (Throwable ex) {

View File

@ -6,16 +6,29 @@
<TextView
android:id="@+id/tvTime"
android:layout_width="0dp"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="@android:style/TextAppearance.Material.Small" />
<TextView
android:id="@+id/tvProtocol"
android:layout_width="35dp"
android:layout_height="wrap_content"
android:textAppearance="@android:style/TextAppearance.Material.Small" />
<TextView
android:id="@+id/tvUid"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:textAlignment="textEnd"
android:textAppearance="@android:style/TextAppearance.Material.Small" />
<TextView
android:id="@+id/tvIP"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:textAppearance="@android:style/TextAppearance.Material.Small"
android:textIsSelectable="true" />
</LinearLayout>

View File

@ -158,8 +158,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:textAppearance="@android:style/TextAppearance.Material.Small"
android:visibility="gone" />
android:textAppearance="@android:style/TextAppearance.Material.Small" />
<TextView
android:id="@+id/tvInternet"