Analyzer.java 18 KB

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