Analyzer.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. package analyzer;
  2. import analyzer.models.Client;
  3. import analyzer.models.Hashtag;
  4. import analyzer.models.Round;
  5. import analyzer.models.User;
  6. import java.io.IOException;
  7. import java.util.*;
  8. import java.util.concurrent.ExecutionException;
  9. import java.util.concurrent.ExecutorService;
  10. import java.util.concurrent.Executors;
  11. import java.util.concurrent.Future;
  12. import java.util.concurrent.atomic.AtomicInteger;
  13. /**
  14. * This class calls the parsers running parallel, then performs intersection attacks from the loaded objects.
  15. */
  16. public class Analyzer {
  17. private static final int threadPoolSize = 8;
  18. public static final int roundLength = 1800;
  19. public static final int clientNo = 1000000;
  20. public static final String datasetName = "reddit"; //;
  21. public static final boolean coverTraffic = false;
  22. public static final int delay = 0;
  23. public static String childPath = "";
  24. public static String logType = ""; //"top-" "random_"
  25. public static String logPath = "";
  26. public static String clientLogPath = "";
  27. public static String serverLogPath = "";
  28. //TEST
  29. //private static final User toTraceUser = new User(306637085);
  30. private static final String toTraceHashtag = "InstantFollowBack";
  31. private static final String intersectingClientID = "client-937851";
  32. private static final long targetUserID = 398449825L;
  33. public static void main(String[] args) throws InterruptedException, ExecutionException {
  34. //a.traceHashtag();
  35. //traceClientHashtagLink();
  36. traceClientUserLink();
  37. }
  38. public Analyzer () throws InterruptedException {
  39. pathConfig();
  40. Thread slogs = new Thread(() -> {
  41. try {
  42. ServerLogParser.run();
  43. } catch (IOException e) {
  44. e.printStackTrace();
  45. }
  46. });
  47. Thread clogs = new Thread(ClientLogParser::run);
  48. slogs.start();
  49. clogs.start();
  50. slogs.join();
  51. clogs.join();
  52. //checkParsing();
  53. System.out.println("round length: " + Analyzer.roundLength);
  54. //new ServerLogParser(serverLogPath).traceUser();
  55. }
  56. private static void pathConfig() {
  57. String currentDirectory;
  58. currentDirectory = System.getProperty("user.dir");
  59. logPath = currentDirectory + "sample\\";
  60. childPath = coverTraffic ? "ct" + "\\" + coverTraffic : delay > 0 ? "delay" + "\\" + delay : roundLength + "";
  61. if(datasetName.equals("twitter")) {
  62. logType = "twitter\\random_";
  63. logPath = logPath + logType + clientNo + "\\" + childPath;
  64. }
  65. else if(datasetName.equals("reddit")) {
  66. logType = "reddit\\";
  67. logPath = logPath + logType + "\\" + "ct\\0\\";
  68. }
  69. clientLogPath = logPath + "\\clogs.txt";
  70. serverLogPath = logPath + "\\slogs.txt";
  71. }
  72. private static void traceClientUserLink() throws InterruptedException, ExecutionException {
  73. Analyzer privateInst = new Analyzer();
  74. int roundNum = privateInst.intersectUsers(ClientLogParser.clients.get(intersectingClientID), 1, true);
  75. System.out.println("roundNum: " + roundNum);
  76. }
  77. private static void traceClientHashtagLink() throws ExecutionException, InterruptedException {
  78. Analyzer privateInst = new Analyzer();
  79. List<Map.Entry<String, Integer>> intersection = privateInst.intersectHashtags(ClientLogParser.clients.get(intersectingClientID), 1000, true);
  80. Hashtag[] hashtags = new Hashtag[intersection.size()];
  81. for(int i = 0; i < hashtags.length; i++)
  82. hashtags[i] = ServerLogParser.hashtags.get(intersection.get(i).getKey());
  83. System.out.println();
  84. List<Map.Entry<Hashtag, Integer>> results = privateInst.calculatePointsGivenHashtags(ClientLogParser.clients.get(intersectingClientID), hashtags, 1, true);
  85. System.out.println("Result size: " + results.size());
  86. //User targetUser = ServerLogParser.users.get(targetUserID);
  87. //System.out.println();
  88. //targetUser.getHashtags().forEach((k, v) -> System.out.println(k.getName()));
  89. }
  90. public void traceHashtag() throws InterruptedException, ExecutionException {
  91. Hashtag staticTargetHashtag = ServerLogParser.hashtags.get(toTraceHashtag);
  92. Client staticTargetClient = ClientLogParser.clients.get(intersectingClientID);
  93. System.out.println(staticTargetHashtag.getName() + " size: " + staticTargetHashtag.getRoundMap().size());
  94. long startCalculatingMaxPoint = System.currentTimeMillis();
  95. calculatePointsAllClients(staticTargetHashtag, 0, true);
  96. long calculatingMaxPointTime = System.currentTimeMillis() - startCalculatingMaxPoint;
  97. System.out.println("CalculatingMaxPointTime: " + calculatingMaxPointTime + " ms");
  98. System.out.println();
  99. System.out.println(staticTargetClient.getId() + " " + staticTargetClient.postingTraces());
  100. //print traced hashtag
  101. System.out.println("Hashtag " + staticTargetHashtag.getName() + " " + staticTargetHashtag.hashtagTraces());
  102. long startIntersecting = System.currentTimeMillis();
  103. intersectHashtags(staticTargetClient, 0, true);
  104. long intersectingTime = System.currentTimeMillis() - startIntersecting;
  105. System.out.println("Intersecting Time: " + intersectingTime + " ms");
  106. }
  107. /**
  108. * This method calculates points between a given hashtag and all clients, and prints out these points if needed.
  109. * @param hashtag hashtag to calculate points against all clients
  110. * @param returnNum number of returned highest point 2-tuples (client, point)
  111. * @param printOut true if log print-out is needed
  112. * @return list of highest point 2-tuples (client, point)
  113. * @throws InterruptedException
  114. * @throws ExecutionException
  115. */
  116. public List<Map.Entry<Client, Integer>> calculatePointsAllClients(Hashtag hashtag, int returnNum, boolean printOut) throws InterruptedException, ExecutionException {
  117. Hashtable<Client, Integer> clientPointsWithHashtag = new Hashtable<>(ClientLogParser.clients.size(), 1);
  118. ClientLogParser.clients.values().forEach(c -> clientPointsWithHashtag.put(c, 0));
  119. ExecutorService executor = Executors.newFixedThreadPool(threadPoolSize);
  120. List<Future<?>> tasks = new ArrayList<>();
  121. for(Client c : clientPointsWithHashtag.keySet()) {
  122. Future<?> task = executor.submit(new PointCalculatorTask(c, hashtag, clientPointsWithHashtag, null));
  123. tasks.add(task);
  124. }
  125. for(Future<?> task : tasks)
  126. task.get();
  127. executor.shutdown();
  128. long startSortTime = System.currentTimeMillis();
  129. List<Map.Entry<Client, Integer>> sortedList = new ArrayList<>(clientPointsWithHashtag.entrySet());
  130. sortedList.sort(((o1, o2) -> o2.getValue().compareTo(o1.getValue())));
  131. long sortTime = System.currentTimeMillis() - startSortTime;
  132. if(printOut) {
  133. System.out.println("Points for hashtag " + hashtag.getName());
  134. System.out.println("Sorting time : " + sortTime + " ms");
  135. System.out.println("Client-ID\tPoints");
  136. int maxPrintOut = Math.min(20, sortedList.size());
  137. for(int i=0; i < maxPrintOut; i++) {
  138. Map.Entry<Client, Integer> e = sortedList.get(i);
  139. System.out.println(e.getKey().getId() + "\t" + e.getValue());
  140. }
  141. }
  142. if(returnNum > 0) {
  143. if(returnNum >= sortedList.size())
  144. return sortedList;
  145. else {
  146. List<Map.Entry<Client, Integer>> returnList = new ArrayList<>();
  147. int maxVal = sortedList.get(0).getValue();
  148. for (Map.Entry<Client, Integer> entry : sortedList) {
  149. if (entry.getValue().equals(maxVal)) {
  150. returnList.add(entry);
  151. } else {
  152. if (returnList.size() < returnNum) {
  153. maxVal = entry.getValue();
  154. returnList.add(entry);
  155. } else
  156. break;
  157. }
  158. }
  159. return returnList;
  160. }
  161. }
  162. return null;
  163. }
  164. /**
  165. * This method calculates points between a client and a set of hashtags and prints out these points if needed.
  166. * @param client client to calculate point against a set of hashtags
  167. * @param hashtags set of hashtags to calculate point against client
  168. * @param returnNum number of returned highest point 2-tuples (hashtag, point)
  169. * @param printOut true if log print-out is needed
  170. * @return list of highest point 2-tuples (hashtag, point)
  171. * @throws InterruptedException
  172. * @throws ExecutionException
  173. */
  174. public List<Map.Entry<Hashtag, Integer>> calculatePointsGivenHashtags(Client client, Hashtag[] hashtags, int returnNum, boolean printOut) throws InterruptedException, ExecutionException {
  175. Hashtable<Hashtag, Integer> hashtagPointMap = new Hashtable<>(hashtags.length, 1);
  176. Arrays.stream(hashtags).forEach(h -> hashtagPointMap.put(h, 0));
  177. ExecutorService executor = Executors.newFixedThreadPool(threadPoolSize);
  178. List<Future<?>> tasks = new ArrayList<>();
  179. for(Hashtag h : hashtags) {
  180. Future<?> task = executor.submit(new PointCalculatorTask(client, h, null, hashtagPointMap));
  181. tasks.add(task);
  182. }
  183. for(Future<?> task : tasks)
  184. task.get();
  185. executor.shutdown();
  186. long startSortTime = System.currentTimeMillis();
  187. List<Map.Entry<Hashtag, Integer>> sortedList = new ArrayList<>(hashtagPointMap.entrySet());
  188. sortedList.sort(((o1, o2) -> o2.getValue().compareTo(o1.getValue())));
  189. long sortTime = System.currentTimeMillis() - startSortTime;
  190. if(returnNum > 0) {
  191. if(returnNum >= sortedList.size())
  192. return sortedList;
  193. else {
  194. List<Map.Entry<Hashtag, Integer>> returnList = new ArrayList<>();
  195. int maxVal = sortedList.get(0).getValue();
  196. for (Map.Entry<Hashtag, Integer> entry : sortedList) {
  197. if (entry.getValue().equals(maxVal)) {
  198. returnList.add(entry);
  199. } else {
  200. if (returnList.size() < returnNum) {
  201. maxVal = entry.getValue();
  202. returnList.add(entry);
  203. } else
  204. break;
  205. }
  206. }
  207. if(printOut) {
  208. System.out.println("Points for Client " + client.getId());
  209. System.out.println("Sorting time : " + sortTime + " ms");
  210. System.out.println("Client-ID\tPoints");
  211. for(int i=0; i < returnList.size(); i++) {
  212. Map.Entry<Hashtag, Integer> e = returnList.get(i);
  213. System.out.println(e.getKey().getName() + "\t" + e.getValue());
  214. }
  215. }
  216. return returnList;
  217. }
  218. }
  219. return null;
  220. }
  221. /**
  222. * This method performs intersection attacks by calculating the correlation points from a given targetClient against hashtags based on active rounds
  223. * @param targetClient target client to perform the attack
  224. * @param returnNum number of returned 2-tuples of most relevant suspects
  225. * @param printOut true if log print-out is needed
  226. * @return list of highest point 2-tuples (hashtag, point)
  227. */
  228. public List<Map.Entry<String, Integer>> intersectHashtags(Client targetClient, int returnNum, boolean printOut) {
  229. //initialize a mapping of all possible hashtags with their no of occurrences
  230. Map<String, Integer> results = new Hashtable<>();
  231. List<Round> targetRounds = new ArrayList<>(targetClient.getRounds().keySet());
  232. targetRounds.sort(Comparator.comparingInt(Round::getNo));
  233. int targetNoRounds = targetRounds.size();
  234. int lastRound = 1;
  235. long lastResult = ServerLogParser.hashtags.size();
  236. for(int i = 0; i < targetNoRounds; i++) {
  237. results.forEach((k, v) -> results.replace(k, v - 1));
  238. //results.entrySet().removeIf(e -> (e.getValue() < 0));
  239. int targetRoundNo = targetRounds.get(i).getNo();
  240. List<Hashtag> hashtagsOfARound = new ArrayList<>(ServerLogParser.rounds.get(targetRoundNo).getHashtags().keySet());
  241. for(Hashtag hashtag: hashtagsOfARound) {
  242. if(results.containsKey(hashtag.getName()))
  243. results.replace(hashtag.getName(), results.get(hashtag.getName()) + 2);
  244. else
  245. results.put(hashtag.getName(), -(i + 1) + 1);
  246. }
  247. //long resultSize = Collections.frequency(results.values(), Collections.max(results.values(), Integer::compare));
  248. //for (int x = lastRound + 1; x < targetRoundNo; x++) {
  249. // System.out.print(lastResult+ ",");
  250. //}
  251. //lastRound = targetRoundNo;
  252. //lastResult = resultSize;
  253. //System.out.print(resultSize + ",");
  254. }
  255. //System.out.println();
  256. //results.entrySet().removeIf(e -> (e.getValue() < targetNoRound /2));
  257. List<Map.Entry<String, Integer>> sortedList = new ArrayList<>(results.entrySet());
  258. sortedList.sort(Map.Entry.comparingByValue((v1, v2) -> Integer.compare(v2, v1)));
  259. if(returnNum > 0) {
  260. if(returnNum >= sortedList.size())
  261. return sortedList;
  262. else {
  263. List<Map.Entry<String, Integer>> returnList = new ArrayList<>();
  264. int maxVal = sortedList.get(0).getValue();
  265. for (Map.Entry<String, Integer> entry : sortedList) {
  266. if (entry.getValue().equals(maxVal)) {
  267. returnList.add(entry);
  268. } else {
  269. if (returnList.size() < returnNum) {
  270. maxVal = entry.getValue();
  271. returnList.add(entry);
  272. } else
  273. break;
  274. }
  275. }
  276. if(printOut) {
  277. System.out.print(targetClient.getId() + ": ");
  278. System.out.println("no of rounds of " + targetClient.getId() + ": " + targetNoRounds);
  279. for(int i = 0; i < returnList.size(); i++) {
  280. System.out.print(returnList.get(i).getKey() + "|" + returnList.get(i).getValue() + " ");
  281. }
  282. //sortedList.forEach(e -> System.out.print(e.getKey() + "|" + e.getValue() + " "));
  283. System.out.println();
  284. }
  285. return returnList;
  286. }
  287. }
  288. return null;
  289. }
  290. /**
  291. * This method performs intersection attacks by matching the behaviour of targetClient with all other clients.
  292. * @param targetClient target client to perform the attacks
  293. * @param returnNum number of returned 2-tuples of most relevant suspects
  294. * @param printOut true if log print-out is needed
  295. * @return number of rounds needed until the attacks finish
  296. */
  297. public int intersectUsers(Client targetClient, int returnNum, boolean printOut) {
  298. int roundProcessed = -1;
  299. List<User> userList = new ArrayList<>(ServerLogParser.users.values());
  300. List<Round> roundList = new ArrayList<>(ServerLogParser.rounds.values());
  301. roundList.sort(Comparator.comparingInt(Round::getNo));
  302. for(Round r: roundList) {
  303. if(!targetClient.getRounds().containsKey(r)) {
  304. userList.removeIf(u -> u.getRounds().containsKey(r));
  305. } else {
  306. userList.removeIf(u -> !u.getRounds().containsKey(r) || !u.getRounds().get(r).equals(targetClient.getRounds().get(r)));
  307. }
  308. //System.out.print(userList.size() + ", ");
  309. if(userList.size() <= 1) {
  310. roundProcessed = r.getNo();
  311. break;
  312. }
  313. }
  314. //System.out.println();
  315. if(userList.size() != 1) {
  316. roundProcessed = -1;
  317. }
  318. if(printOut) {
  319. System.out.println("Suspect(s): ");
  320. userList.forEach(e -> System.out.println(e.getId()));
  321. System.out.println();
  322. System.out.println("rounds processed: " + roundProcessed);
  323. System.out.println();
  324. System.out.println();
  325. }
  326. return roundProcessed;
  327. }
  328. /**
  329. * This method checks the parsing processes, exits the JVM if there is an error.
  330. */
  331. private static void checkParsing() {
  332. if(ClientLogParser.clientRoundNo != ServerLogParser.serverRoundNo) {
  333. System.out.println("Parsing Error: Round numbers are not equals");
  334. System.exit(1);
  335. }
  336. if(ClientLogParser.clients.size() != ServerLogParser.users.size()) {
  337. System.out.println("Parsing Error: Client/User numbers are not equals");
  338. System.exit(1);
  339. }
  340. }
  341. /**
  342. * This class models the parallel execution of point calculations
  343. */
  344. private static class PointCalculatorTask implements Runnable {
  345. private final Client client;
  346. private final Hashtag hashtag;
  347. private final Hashtable<Client, Integer> clientMap;
  348. private final Hashtable<Hashtag, Integer> hashtagMap;
  349. private PointCalculatorTask(Client c, Hashtag hashtag, Hashtable<Client, Integer> clientMap, Hashtable<Hashtag, Integer> hashtagMap) {
  350. this.client = c;
  351. this.hashtag = hashtag;
  352. this.clientMap = clientMap;
  353. this.hashtagMap = hashtagMap;
  354. }
  355. @Override
  356. public void run() {
  357. calculatePoint();
  358. }
  359. private void calculatePoint() {
  360. Map<Round, Integer> clone = new Hashtable<>(client.getRounds());
  361. AtomicInteger n = new AtomicInteger();
  362. clone.keySet().retainAll(this.hashtag.getRoundMap().keySet());
  363. clone.forEach((k, v) -> n.addAndGet((Math.min(v, this.hashtag.getRoundMap().get(k)))));
  364. if(clientMap != null)
  365. clientMap.put(this.client, n.get());
  366. if(hashtagMap != null)
  367. hashtagMap.put(this.hashtag, n.get());
  368. }
  369. }
  370. }