Skip to content

Commit 9c937c6

Browse files
committed
Add tracker blocking to Android 10.
1 parent d9bc296 commit 9c937c6

File tree

5 files changed

+49
-41
lines changed

5 files changed

+49
-41
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ android {
2525
applicationId 'net.kollnig.missioncontrol'
2626
minSdkVersion 22
2727
targetSdkVersion 29
28-
versionCode 6
29-
versionName "1.0.0-alpha5"
28+
versionCode 7
29+
versionName "1.0.0-alpha6"
3030
}
3131

3232
buildTypes {

app/src/main/java/net/kollnig/missioncontrol/vpn/InConsumer.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,9 @@
1717
package net.kollnig.missioncontrol.vpn;
1818

1919
import android.content.Context;
20-
import android.util.Log;
21-
22-
import java.nio.ByteBuffer;
2320

2421
import edu.uci.calit2.antmonitor.lib.logging.PacketConsumer;
2522
import edu.uci.calit2.antmonitor.lib.logging.PacketProcessor.TrafficType;
26-
import edu.uci.calit2.antmonitor.lib.util.IpDatagram;
2723
import edu.uci.calit2.antmonitor.lib.util.PacketDumpInfo;
2824

2925
public class InConsumer extends PacketConsumer {
@@ -43,13 +39,6 @@ public InConsumer (Context c, TrafficType trafficType) {
4339
@Override
4440
protected void consumePacket (PacketDumpInfo packetDumpInfo) {
4541
// Parse IP packet
46-
byte[] packet = packetDumpInfo.getDump();
47-
IpDatagram ipDatagram = new IpDatagram(ByteBuffer.wrap(packet));
48-
String remoteIp = ipDatagram.getSourceIP().getHostAddress();
49-
50-
if (remoteIp.equals("8.8.8.8")) {
51-
Log.d(TAG, remoteIp);
52-
}
5342
}
5443

5544
/**

app/src/main/java/net/kollnig/missioncontrol/vpn/OutConsumer.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,11 @@
3838
import static android.system.OsConstants.IPPROTO_UDP;
3939

4040
public class OutConsumer extends PacketConsumer {
41-
private final String TAG = OutConsumer.class.getSimpleName();
41+
static PackageManager pm;
4242
private final Context mContext;
4343
private final Database database;
44-
private final PackageManager pm;
45-
46-
ConnectivityManager connectivityManager;
44+
static ConnectivityManager connectivityManager;
45+
private static String TAG = OutConsumer.class.getSimpleName();
4746

4847
public OutConsumer (Context c, TrafficType trafficType) {
4948
super(c, trafficType, null);
@@ -58,6 +57,7 @@ static String getHostname (String remoteIp) {
5857
return VpnController.retrieveHostname(remoteIp);
5958
}
6059

60+
6161
/**
6262
* Logs outgoing packets of apps.
6363
*
@@ -121,7 +121,7 @@ protected void onStop () {
121121
* @return the name of the package of the app with the given uid, or "Unknown" if
122122
* no name could be found for the uid.
123123
*/
124-
public String getAppName (int uid) {
124+
static String getAppName (int uid) {
125125
/* IMPORTANT NOTE:
126126
* From https://source.android.com/devices/tech/security/ : "The Android
127127
* system assigns a unique user ID (UID) to each Android application and

app/src/main/java/net/kollnig/missioncontrol/vpn/OutFilter.java

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,21 @@
2424
import net.kollnig.missioncontrol.data.Database;
2525
import net.kollnig.missioncontrol.main.AppBlocklistController;
2626

27+
import java.net.InetSocketAddress;
28+
import java.net.UnknownHostException;
2729
import java.nio.ByteBuffer;
2830

2931
import edu.uci.calit2.antmonitor.lib.logging.ConnectionValue;
3032
import edu.uci.calit2.antmonitor.lib.logging.PacketAnnotation;
3133
import edu.uci.calit2.antmonitor.lib.util.IpDatagram;
3234
import edu.uci.calit2.antmonitor.lib.vpn.OutPacketFilter;
3335

36+
import static android.os.Process.INVALID_UID;
37+
import static android.system.OsConstants.IPPROTO_TCP;
38+
import static android.system.OsConstants.IPPROTO_UDP;
39+
import static net.kollnig.missioncontrol.vpn.OutConsumer.connectivityManager;
40+
import static net.kollnig.missioncontrol.vpn.OutConsumer.getAppName;
41+
3442
public class OutFilter extends OutPacketFilter {
3543
private final String TAG = OutFilter.class.getSimpleName();
3644
private final AppBlocklistController appBlocklist;
@@ -63,17 +71,43 @@ public PacketAnnotation acceptIPDatagram (final ByteBuffer packet) {
6371
if (tracker == null)
6472
return ALLOW;
6573

66-
ConnectionValue v = mapDatagramToApp(packet);
67-
String appId = v.getAppName();
68-
if (appId == null)
74+
String appname;
75+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
76+
// Only UDP and TCP are supported
77+
short protocol = IpDatagram.readProtocol(packet);
78+
if (protocol != IpDatagram.UDP && protocol != IpDatagram.TCP)
79+
return ALLOW;
80+
81+
int lookupProtocol = (protocol == IpDatagram.TCP) ? IPPROTO_TCP : IPPROTO_UDP;
82+
83+
InetSocketAddress local, remote;
84+
try {
85+
local = new InetSocketAddress
86+
(IpDatagram.readSourceIP(packet), IpDatagram.readSourcePort(packet));
87+
remote = new InetSocketAddress
88+
(IpDatagram.readDestinationIP(packet), IpDatagram.readDestinationPort(packet));
89+
} catch (UnknownHostException e) {
90+
return ALLOW;
91+
}
92+
93+
int uid = connectivityManager.getConnectionOwnerUid(lookupProtocol, local, remote);
94+
if (uid == INVALID_UID)
95+
return ALLOW;
96+
97+
appname = getAppName(uid);
98+
} else {
99+
ConnectionValue cv = mapDatagramToApp(packet);
100+
appname = cv.getAppName();
101+
}
102+
if (appname == null)
69103
return ALLOW;
70104

71-
if (appBlocklist.blockedApp(appId)
72-
&& appBlocklist.blockedTracker(appId, tracker.getRoot())
105+
if (appBlocklist.blockedApp(appname)
106+
&& appBlocklist.blockedTracker(appname, tracker.getRoot())
73107
)
74108
return BLOCK;
75109

76-
// DATABASE.logPacketAsyncTask(mContext, appId, remoteIp, hostname);
110+
// DATABASE.logPacketAsyncTask(mContext, appname, remoteIp, hostname);
77111
return ALLOW;
78112
}
79113
}

build.gradle

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,6 @@
11
ext {
2-
var = '3.4.2'
3-
}/*
4-
* Copyright (C) 2019 Konrad Kollnig, University of Oxford
5-
*
6-
* TrackerControl is free software: you can redistribute it and/or modify
7-
* it under the terms of the GNU General Public License as published by
8-
* the Free Software Foundation, either version 2 of the License, or
9-
* (at your option) any later version.
10-
*
11-
* TrackerControl is distributed in the hope that it will be useful,
12-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14-
* GNU General Public License for more details.
15-
*
16-
* You should have received a copy of the GNU General Public License
17-
* along with TrackerControl. If not, see <http://www.gnu.org/licenses/>.
18-
*/
2+
var = '3.5.2'
3+
}
194

205
// Top-level build file where you can add configuration options common to all sub-projects/modules.
216

0 commit comments

Comments
 (0)