Main.java: cleanup - leave only code for extracting traffic generated by a device...
[pingpong.git] / Code / Projects / SmartPlugDetector / src / main / java / edu / uci / iotproject / Main.java
1 package edu.uci.iotproject;
2
3 import edu.uci.iotproject.analysis.TriggerTrafficExtractor;
4 import edu.uci.iotproject.io.TriggerTimesFileReader;
5 import org.pcap4j.core.*;
6 import org.pcap4j.packet.namednumber.DataLinkType;
7
8 import java.io.EOFException;
9 import java.net.UnknownHostException;
10 import java.time.Instant;
11 import java.util.*;
12 import java.util.concurrent.TimeoutException;
13
14 /**
15  * This is a system that reads PCAP files to compare
16  * patterns of DNS hostnames, packet sequences, and packet
17  * lengths with training data to determine certain events
18  * or actions for smart home devices.
19  *
20  * @author Janus Varmarken
21  * @author Rahmadi Trimananda (rtrimana@uci.edu)
22  * @version 0.1
23  */
24 public class Main {
25
26
27     public static void main(String[] args) throws PcapNativeException, NotOpenException, EOFException, TimeoutException, UnknownHostException {
28         // -------------------------------------------------------------------------------------------------------------
29         // ------------ # Code for extracting traffic generated by a device within x seconds of a trigger # ------------
30         // Paths to input and output files (consider supplying these as arguments instead)
31         final String inputPcapFile = "/Users/varmarken/temp/UCI IoT Project/June2018 experiments/tplink/tplink.wlan1.local.pcap";
32         final String outputPcapFile = "/Users/varmarken/temp/UCI IoT Project/June2018 experiments/tplink/tplink-filtered.pcap";
33         final String triggerTimesFile = "/Users/varmarken/temp/UCI IoT Project/June2018 experiments/tplink/tplink-june-14-2018-timestamps.txt";
34         // IP of the device for which traffic is to be extracted
35         final String deviceIp = "192.168.1.159";
36
37         TriggerTimesFileReader ttfr = new TriggerTimesFileReader();
38         List<Instant> triggerTimes = ttfr.readTriggerTimes(triggerTimesFile, false);
39         TriggerTrafficExtractor tte = new TriggerTrafficExtractor(inputPcapFile, triggerTimes, deviceIp);
40         final PcapDumper outputter = Pcaps.openDead(DataLinkType.EN10MB, 65536).dumpOpen(outputPcapFile);
41         DnsMap dnsMap = new DnsMap();
42         TcpReassembler tcpReassembler = new TcpReassembler();
43         tte.performExtraction(pkt -> {
44             try {
45                 outputter.dump(pkt);
46             } catch (NotOpenException e) {
47                 e.printStackTrace();
48             }
49         }, dnsMap, tcpReassembler);
50         outputter.flush();
51         outputter.close();
52         // -------------------------------------------------------------------------------------------------------------
53         // -------------------------------------------------------------------------------------------------------------
54     }
55
56 }
57
58
59 // TP-Link MAC 50:c7:bf:33:1f:09 and usually IP 192.168.1.159 (remember to verify per file)