Added port and flags to blocked traffic log

This commit is contained in:
M66B 2016-01-08 08:03:04 +01:00
parent 52ec25302f
commit 653261743f
4 changed files with 95 additions and 26 deletions

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 = 2;
private static final int DB_VERSION = 3;
private static List<LogChangedListener> logChangedListeners = new ArrayList<LogChangedListener>();
@ -40,6 +40,8 @@ public class DatabaseHelper extends SQLiteOpenHelper {
", version INTEGER NULL" +
", ip TEXT" +
", protocol INTEGER NULL" +
", port INTEGER NULL" +
", flags TEXT" +
", uid INTEGER NULL" +
");");
db.execSQL("CREATE INDEX idx_log_time ON log(time)");
@ -57,6 +59,11 @@ public class DatabaseHelper extends SQLiteOpenHelper {
db.execSQL("ALTER TABLE log ADD COLUMN uid INTEGER NULL");
oldVersion = 2;
}
if (oldVersion < 3) {
db.execSQL("ALTER TABLE log ADD COLUMN port INTEGER NULL");
db.execSQL("ALTER TABLE log ADD COLUMN flags TEXT");
oldVersion = 3;
}
db.setVersion(DB_VERSION);
@ -70,7 +77,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
// Location
public DatabaseHelper insertLog(int version, String ip, int protocol, int uid) {
public DatabaseHelper insertLog(int version, String ip, int protocol, int port, String flags, int uid) {
synchronized (mContext.getApplicationContext()) {
SQLiteDatabase db = this.getWritableDatabase();
@ -84,6 +91,13 @@ public class DatabaseHelper extends SQLiteOpenHelper {
else
cv.put("protocol", protocol);
if (port < 0)
cv.putNull("port");
else
cv.put("port", port);
cv.put("flags", flags);
if (uid < 0)
cv.putNull("uid");
else

View File

@ -18,6 +18,8 @@ public class LogAdapter extends CursorAdapter {
private int colVersion;
private int colIP;
private int colProtocol;
private int colPort;
private int colFlags;
private int colUid;
public LogAdapter(Context context, Cursor cursor) {
@ -26,6 +28,8 @@ public class LogAdapter extends CursorAdapter {
colVersion = cursor.getColumnIndex("version");
colIP = cursor.getColumnIndex("ip");
colProtocol = cursor.getColumnIndex("protocol");
colPort = cursor.getColumnIndex("port");
colFlags = cursor.getColumnIndex("flags");
colUid = cursor.getColumnIndex("uid");
}
@ -41,13 +45,18 @@ public class LogAdapter extends CursorAdapter {
int version = (cursor.isNull(colVersion) ? -1 : cursor.getInt(colVersion));
String ip = cursor.getString(colIP);
int protocol = (cursor.isNull(colProtocol) ? -1 : cursor.getInt(colProtocol));
int port = (cursor.isNull(colPort) ? -1 : cursor.getInt(colPort));
String flags = cursor.getString(colFlags);
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 tvPort = (TextView) view.findViewById(R.id.tvPort);
TextView tvFlags = (TextView) view.findViewById(R.id.tvFlags);
TextView tvUid = (TextView) view.findViewById(R.id.tvUid);
// Set values
@ -68,6 +77,8 @@ public class LogAdapter extends CursorAdapter {
else
tvProtocol.setText("");
tvPort.setText(port < 0 ? "" : Integer.toString(port));
tvFlags.setText(flags);
tvUid.setText(uid < 0 ? "" : Integer.toString(uid % 100000));
tvIP.setOnClickListener(new View.OnClickListener() {

View File

@ -699,28 +699,7 @@ public class SinkholeService extends VpnService {
int length = in.read(buffer.array());
if (length > 0) {
buffer.limit(length);
byte version = (byte) (buffer.get() >> 4);
if (version == 4) {
buffer.position(0);
IPv4Packet pkt = new IPv4Packet(buffer);
Log.i(TAG, "Packet to " + pkt.IPv4.destinationAddress.toString());
new DatabaseHelper(SinkholeService.this).insertLog(
version,
pkt.IPv4.destinationAddress.toString(),
pkt.IPv4.protocol,
pkt.getUid()).close();
} else if (version == 6) {
buffer.position(24);
byte[] addressBytes = new byte[16];
buffer.get(addressBytes, 0, 16);
InetAddress ina = Inet6Address.getByAddress(addressBytes);
Log.i(TAG, "Packet to " + ina);
new DatabaseHelper(SinkholeService.this).insertLog(
version,
ina.toString(), -1, -1).close();
}
processPacket(buffer);
}
} catch (Throwable ex) {
Log.e(TAG, ex.toString());
@ -750,6 +729,57 @@ public class SinkholeService extends VpnService {
receiveThread.start();
}
private void processPacket(ByteBuffer buffer) throws IOException {
byte version = (byte) (buffer.get() >> 4);
if (version == 4) {
buffer.position(0);
IPv4Packet pkt = new IPv4Packet(buffer);
int port = -1;
if (pkt.TCP != null)
port = pkt.TCP.destinationPort;
else if (pkt.UDP != null)
port = pkt.UDP.destinationPort;
String flags = "";
if (pkt.TCP != null) {
if (pkt.TCP.SYN)
flags += "S";
if (pkt.TCP.ACK)
flags += "A";
if (pkt.TCP.PSH)
flags += "P";
if (pkt.TCP.FIN)
flags += "F";
if (pkt.TCP.RST)
flags += "R";
}
Log.i(TAG, "Packet to " + pkt.IPv4.destinationAddress.toString() + ":" + port + " " + flags);
new DatabaseHelper(SinkholeService.this).insertLog(
version,
pkt.IPv4.destinationAddress.toString(),
pkt.IPv4.protocol,
port,
flags,
pkt.getUid()).close();
} else if (version == 6) {
buffer.position(24);
byte[] addressBytes = new byte[16];
buffer.get(addressBytes, 0, 16);
InetAddress ina = Inet6Address.getByAddress(addressBytes);
Log.i(TAG, "Packet to " + ina);
new DatabaseHelper(SinkholeService.this).insertLog(
version,
ina.toString(),
-1,
-1,
"",
-1).close();
}
}
private void stopReceiving() {
if (receiveThread != null)
receiveThread.interrupt();

View File

@ -14,13 +14,27 @@
<TextView
android:id="@+id/tvProtocol"
android:layout_width="35dp"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:textAppearance="@android:style/TextAppearance.Material.Small" />
<TextView
android:id="@+id/tvPort"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:textAlignment="textEnd"
android:textAppearance="@android:style/TextAppearance.Material.Small" />
<TextView
android:id="@+id/tvFlags"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:textAlignment="textEnd"
android:textAppearance="@android:style/TextAppearance.Material.Small" />
<TextView
android:id="@+id/tvUid"
android:layout_width="50dp"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:textAlignment="textEnd"
android:textAppearance="@android:style/TextAppearance.Material.Small" />