Skip to content

Commit 1937c85

Browse files
committed
Refactor host information generation and clean up tagging with host information
1 parent 8e016f3 commit 1937c85

File tree

3 files changed

+113
-83
lines changed

3 files changed

+113
-83
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package fi.helsinki.cs.tmc.spyware;
2+
3+
import fi.helsinki.cs.tmc.events.TmcEventBus;
4+
import fi.helsinki.cs.tmc.utilities.JsonMaker;
5+
import java.net.NetworkInterface;
6+
import java.nio.charset.Charset;
7+
import java.util.ArrayList;
8+
import java.util.Enumeration;
9+
import java.util.List;
10+
import java.util.logging.Level;
11+
import java.util.logging.Logger;
12+
13+
/**
14+
* Generates host information used by spyware to identify requests coming from single host.
15+
*
16+
* HostInformation is linked by hopefully unique enough small identifier which is calculated from
17+
* host information.
18+
*/
19+
public class HostInformationGenerator {
20+
21+
private static final Logger log = Logger.getLogger(HostInformationGenerator.class.getName());
22+
23+
public int updateHostInformation() {
24+
JsonMaker data = getStaticHostInformation();
25+
// Should be unique enough not to collapse among singe users machines.
26+
int hostId = data.toString().hashCode();
27+
28+
data.add("hostId", hostId);
29+
30+
LoggableEvent event =
31+
new LoggableEvent(
32+
"host_information_update",
33+
data.toString().getBytes(Charset.forName("UTF-8")));
34+
TmcEventBus.getDefault().post(event);
35+
36+
return hostId;
37+
}
38+
39+
private static JsonMaker getStaticHostInformation() {
40+
JsonMaker builder = JsonMaker.create();
41+
42+
try {
43+
java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
44+
builder.add("hostAddress", localMachine.getHostAddress());
45+
builder.add("hostName", localMachine.getHostName());
46+
} catch (Exception ex) {
47+
log.log(Level.WARNING, "Exception while getting host name information: {0}", ex);
48+
}
49+
50+
try {
51+
Enumeration<NetworkInterface> iterator = NetworkInterface.getNetworkInterfaces();
52+
List<String> macs = new ArrayList<String>(2);
53+
while (iterator.hasMoreElements()) {
54+
NetworkInterface networkInterface = iterator.nextElement();
55+
if (networkInterface.isUp() && !networkInterface.isLoopback()) {
56+
byte[] mac = networkInterface.getHardwareAddress();
57+
StringBuilder sb = new StringBuilder();
58+
for (int i = 0; i < mac.length; i++) {
59+
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : ""));
60+
}
61+
macs.add(sb.toString());
62+
}
63+
}
64+
builder.add("mac_addresses", macs);
65+
66+
} catch (Exception ex) {
67+
log.log(Level.WARNING, "Exception while getting host mac information: {0}", ex);
68+
}
69+
70+
try {
71+
builder.add("user.name", System.getProperty("user.name"));
72+
builder.add("java.runtime.version", System.getProperty("java.runtime.version"));
73+
builder.add("os.name", System.getProperty("os.name"));
74+
builder.add("os.version", System.getProperty("os.version"));
75+
} catch (Exception e) {
76+
log.log(Level.WARNING, "Exception while getting basic host information: {0}", e);
77+
}
78+
79+
return builder;
80+
}
81+
}

tmc-plugin/src/fi/helsinki/cs/tmc/spyware/LoggableEvent.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ public class LoggableEvent implements TmcEvent {
1818

1919
private static final Logger log = Logger.getLogger(LoggableEvent.class.getName());
2020

21-
private static int GLOBAL_HOST_ID;
22-
2321
private String courseName;
2422
private String exerciseName;
2523
private String eventType;
@@ -62,7 +60,6 @@ public LoggableEvent(String courseName, String exerciseName, String eventType, b
6260
this.happenedAt = System.currentTimeMillis();
6361
this.systemNanotime = System.nanoTime();
6462

65-
this.hostId = LoggableEvent.GLOBAL_HOST_ID;
6663
this.key = courseName + "|" + exerciseName + "|" + eventType;
6764
}
6865

@@ -90,14 +87,6 @@ public void setHostId(int hostId) {
9087
this.hostId = hostId;
9188
}
9289

93-
public static void setGlobalHostId(int globalHostId) {
94-
LoggableEvent.GLOBAL_HOST_ID = globalHostId;
95-
}
96-
97-
public static int getGlobalHostId() {
98-
return GLOBAL_HOST_ID;
99-
}
100-
10190
/**
10291
* Optional JSON metadata.
10392
*/
Lines changed: 32 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,18 @@
11
package fi.helsinki.cs.tmc.spyware;
22

33
import fi.helsinki.cs.tmc.spyware.eventsources.WindowStatechangesEventSource;
4-
import fi.helsinki.cs.tmc.data.Exercise;
54
import fi.helsinki.cs.tmc.events.TmcEvent;
65
import fi.helsinki.cs.tmc.events.TmcEventBus;
76
import fi.helsinki.cs.tmc.model.CourseDb;
87
import fi.helsinki.cs.tmc.model.ServerAccess;
9-
import fi.helsinki.cs.tmc.model.TmcProjectInfo;
108
import fi.helsinki.cs.tmc.model.TmcSettings;
119
import fi.helsinki.cs.tmc.spyware.eventsources.TextInsertEventSource;
1210
import fi.helsinki.cs.tmc.spyware.eventsources.ProjectActionCaptor;
1311
import fi.helsinki.cs.tmc.spyware.eventsources.ProjectActionEventSource;
1412
import fi.helsinki.cs.tmc.spyware.eventsources.SourceSnapshotEventSource;
1513
import fi.helsinki.cs.tmc.spyware.eventsources.TmcEventBusEventSource;
16-
import fi.helsinki.cs.tmc.utilities.JsonMaker;
1714
import fi.helsinki.cs.tmc.utilities.TmcSwingUtilities;
18-
import java.net.NetworkInterface;
19-
import java.net.SocketException;
20-
import java.net.UnknownHostException;
21-
import java.nio.charset.Charset;
22-
import java.util.ArrayList;
23-
import java.util.Enumeration;
24-
import java.util.List;
25-
import java.util.logging.Level;
15+
import java.io.IOException;
2616
import java.util.logging.Logger;
2717

2818
public class SpywareFacade implements SpywareSettings {
@@ -38,7 +28,6 @@ public static void start() {
3828
instance = new SpywareFacade();
3929
TmcEventBus.getDefault().post(new InvokedEvent("spyware_loaded"));
4030

41-
updateHostInformation();
4231
}
4332

4433
public static void close() {
@@ -68,6 +57,7 @@ public void run() {
6857
private TmcSettings settings;
6958

7059
private EventSendBuffer sender;
60+
private EventReceiver taggingSender;
7161

7262
private EventDeduplicater sourceSnapshotDedup;
7363

@@ -77,26 +67,51 @@ public void run() {
7767
private TextInsertEventSource textInsertEventSource;
7868
private WindowStatechangesEventSource windowStatechangesEventSource;
7969

70+
private static final class TaggingEventReceiver implements EventReceiver {
71+
72+
private final EventReceiver nextReceiver;
73+
private final int hostId;
74+
75+
public TaggingEventReceiver(EventReceiver nextReceiver, int hostId) {
76+
this.nextReceiver = nextReceiver;
77+
this.hostId = hostId;
78+
}
79+
80+
@Override
81+
public void receiveEvent(LoggableEvent event) {
82+
event.setHostId(hostId);
83+
nextReceiver.receiveEvent(event);
84+
}
85+
86+
@Override
87+
public void close() throws IOException {
88+
nextReceiver.close();
89+
}
90+
}
91+
92+
8093
public SpywareFacade() {
8194
settings = TmcSettings.getDefault();
8295

8396
sender = new EventSendBuffer(this, new ServerAccess(), CourseDb.getInstance(), new EventStore());
8497
sender.sendNow();
8598

86-
sourceSnapshotDedup = new EventDeduplicater(sender);
99+
int hostId = new HostInformationGenerator().updateHostInformation();
100+
taggingSender = new TaggingEventReceiver(sender, hostId);
101+
sourceSnapshotDedup = new EventDeduplicater(taggingSender);
87102
sourceSnapshotSource = new SourceSnapshotEventSource(this, sourceSnapshotDedup);
88103
sourceSnapshotSource.startListeningToFileChanges();
89104

90-
projectActionSource = new ProjectActionEventSource(sender);
91-
tmcEventBusSource = new TmcEventBusEventSource(sender);
105+
projectActionSource = new ProjectActionEventSource(taggingSender);
106+
tmcEventBusSource = new TmcEventBusEventSource(taggingSender);
92107

93-
windowStatechangesEventSource = new WindowStatechangesEventSource(sender);
108+
windowStatechangesEventSource = new WindowStatechangesEventSource(taggingSender);
94109
TmcSwingUtilities.ensureEdt(new Runnable() {
95110
@Override
96111
public void run() {
97112
ProjectActionCaptor.addListener(projectActionSource);
98113
TmcEventBus.getDefault().subscribeStrongly(tmcEventBusSource);
99-
textInsertEventSource = new TextInsertEventSource(sender);
114+
textInsertEventSource = new TextInsertEventSource(taggingSender);
100115
}
101116
});
102117
}
@@ -138,59 +153,4 @@ public InvokedEvent(String message) {
138153
this.message = message;
139154
}
140155
}
141-
142-
private static void updateHostInformation() {
143-
JsonMaker data = getStaticHostInformation();
144-
// Should be unique enough not to collapse among singe users machines.
145-
int hostId = data.toString().hashCode();
146-
LoggableEvent.setGlobalHostId(hostId);
147-
148-
data.add("hostId", hostId);
149-
150-
LoggableEvent event = new LoggableEvent("host_information_update", data.toString().getBytes(Charset.forName("UTF-8")));
151-
TmcEventBus.getDefault().post(event);
152-
}
153-
154-
private static JsonMaker getStaticHostInformation() {
155-
JsonMaker builder = JsonMaker.create();
156-
157-
try {
158-
java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
159-
builder.add("hostAddress", localMachine.getHostAddress());
160-
builder.add("hostName", localMachine.getHostName());
161-
} catch (Exception ex) {
162-
log.log(Level.WARNING, "Exception while getting host name information: {0}", ex);
163-
}
164-
165-
try {
166-
Enumeration<NetworkInterface> iterator = NetworkInterface.getNetworkInterfaces();
167-
List<String> macs = new ArrayList<String>(2);
168-
while (iterator.hasMoreElements()) {
169-
NetworkInterface networkInterface = iterator.nextElement();
170-
if (networkInterface.isUp() && !networkInterface.isLoopback()) {
171-
byte[] mac = networkInterface.getHardwareAddress();
172-
StringBuilder sb = new StringBuilder();
173-
for (int i = 0; i < mac.length; i++) {
174-
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : ""));
175-
}
176-
macs.add(sb.toString());
177-
}
178-
}
179-
builder.add("mac_addresses", macs);
180-
181-
} catch (Exception ex) {
182-
log.log(Level.WARNING, "Exception while getting host mac information: {0}", ex);
183-
}
184-
185-
try {
186-
builder.add("user.name", System.getProperty("user.name"));
187-
builder.add("java.runtime.version", System.getProperty("java.runtime.version"));
188-
builder.add("os.name", System.getProperty("os.name"));
189-
builder.add("os.version", System.getProperty("os.version"));
190-
} catch (Exception e) {
191-
log.log(Level.WARNING, "Exception while getting basic host information: {0}", e);
192-
}
193-
194-
return builder;
195-
}
196156
}

0 commit comments

Comments
 (0)