main.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**
  2. * TraCINg-Server - Gathering and visualizing cyber incidents on the world
  3. *
  4. * Copyright 2013 Matthias Gazzari, Annemarie Mattmann, André Wolski
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var socket = io.connect("/"); // connect to the current server/port
  19. var tableCallback = null;
  20. var currentTableFilter = null;
  21. /*
  22. * startup configuration
  23. * default view if no link is given: live 2D map (includes: "Live View" button toggled (state "active" in index.html), "2D View" tab in navbar toggled, no side/database window, table and statistics hidden)
  24. */
  25. // do on startup
  26. $(function () {
  27. // activate tooltips
  28. $("[rel=tooltip]").tooltip();
  29. // no advanced marker info on default
  30. advInfo = false;
  31. // prevent having lots of blue space above and below the map if the window is narrow
  32. $("#map").css("height", function() {return Math.min($("#map").width()/2+50, $(window).height()*0.8);});
  33. // hide left window
  34. $(leftWin).hide();
  35. // center the map
  36. toggleCenterDivs();
  37. // if a special url is given show the corresponding div
  38. goToLocationHash();
  39. $(window).on("hashchange", goToLocationHash);
  40. disableRequestControl();
  41. // based on http://stackoverflow.com/questions/5082235/automatically-set-jquery-ui-datepicker-on-load
  42. // set datepicker to current date on pageload
  43. $("#inputDateDay").datepicker("setDate", "null");
  44. $("#inputDateNoDay").datepicker("setDate", "null");
  45. // zoom tooltips for jVectorMap
  46. $("div.jvectormap-zoomin").each(function() {
  47. $(this).rel += "tooltip";
  48. $(this).attr("title", "Zoom in");
  49. });
  50. $("div.jvectormap-zoomout").each(function() {
  51. $(this).rel += "tooltip";
  52. $(this).attr("title", "Zoom out");
  53. });
  54. });
  55. // resolve hash (link)
  56. function goToLocationHash(){
  57. if(!window.location.hash){
  58. window.location.hash = "/map";
  59. return;
  60. }
  61. // hide all containers
  62. $("#mainContent > div").hide();
  63. $("#errorOutput").show();
  64. // remove active from all menu entries
  65. $("#menutabs > li").removeClass('active');
  66. // determine which tab should be shown, default is "map"
  67. var tab = window.location.hash && window.location.hash.split("/")[1] || "map";
  68. //console.log("tab: " + tab);
  69. // show container
  70. var container = $("#"+tab);
  71. //console.log("container_pre: ", container);
  72. container.show();
  73. //console.log("container_post: ", container);
  74. // call menu.js
  75. updateMenu(tab);
  76. // toggle active in menu bar
  77. var nav_li = $("#tab_"+tab).parent();
  78. //console.log("nav_li", nav_li);
  79. nav_li && nav_li.addClass('active');
  80. }
  81. // toggle center of all elements with class "center" i.e. all divs (centers the divs unless leftWindow is shown)
  82. function toggleCenterDivs() {
  83. if ($("#leftWin").is(":visible")) {
  84. // delete center margin of all elements with class "center" i.e. all divs
  85. $(".center").css("margin-left", "");
  86. } else {
  87. // add center margin to all divs with class "center" if the config window is closed
  88. $(".center").css("margin-left", function() {return $(window).width()/20});
  89. }
  90. }
  91. // do on resize
  92. $(window).resize(function() {
  93. toggleCenterDivs();
  94. // prevent having lots of blue space above and below the map if the window is narrow
  95. // $("#map").width()/2+50: /2 because the map is 2:1 format, +50 because the zoom buttons shall not overlap the map
  96. $("#map").css("height", function() {return Math.min($("#map").width()/2+50, $(window).height()*0.8);});
  97. });
  98. /*
  99. * page alerts
  100. */
  101. // show a bootstrap alert that will expire over time
  102. // id is the unique name the alert will have (you can use the id to remove non-expiring alerts)
  103. // type may be: error, success, info or block (for warning)
  104. // head should contain the title
  105. // expire may be true (then the alert will expire over time) or false (then it will stay unless clicked on by the user)
  106. function showalert(id, head, message, type, expire) {
  107. var htmlcode = '<div id="' + id + '" class="alert alert-' + type + '"><button type="button" class="close" data-dismiss="alert" id="errorOutput">&times;</button><h4>' + head + '</h4>' + message + '</div>';
  108. $('#errorOutput').append(htmlcode);
  109. //alert will disappear after 10 seconds if not clicked
  110. if (expire) {
  111. setTimeout(function() {
  112. $("#" + id).remove();
  113. }, 10000);
  114. }
  115. }
  116. /*
  117. * database error handle
  118. */
  119. // output errors
  120. socket.on("error", function (data) {
  121. var message = "Errno: " + data.errno + "<br>Code: " + data.code;
  122. showalert("dbAlert", 'Database Error!', message, 'error', true);
  123. // reset "Get Incidents" button (stop showing "Loading..."), disable control buttons
  124. $('#getIncidents').text('Get Incidents');
  125. $('#getIncidents').removeClass("disabled");
  126. disableRequestControl();
  127. });
  128. /*
  129. * draw incidents on realtime attack/database request
  130. */
  131. // set markers of requested database incidents
  132. socket.on("incidents", function (data) {
  133. console.log(data);
  134. // mark incidents with delay between the markings
  135. world.delayedMarking(data, live);
  136. // if no db entries are found show an error
  137. if (data.length == 0)
  138. showalert("noDataAlert", "Request Error!", "No data found for this request.", "error", true);
  139. });
  140. socket.on("incidentsTable", function (result) {
  141. console.log(result);
  142. var data = result.data;
  143. var settings = $("#attackTable").dataTable().fnSettings();
  144. settings.oFeatures.bServerSide = true;
  145. console.log(settings);
  146. var tableData = [];
  147. for(var i=0; i<data.length; i++){
  148. tableData.push(world.generateTableEntry(data[i]));
  149. }
  150. var json = {
  151. recordsTotal: result.total,
  152. recordsFiltered: result.total,
  153. data: tableData
  154. };
  155. if(!tableCallback){
  156. $("#attackTable").dataTable()._fnAjaxUpdateDraw(json, settings);
  157. } else {
  158. tableCallback(json);
  159. tableCallback = null;
  160. }
  161. world.finishLoading(true);
  162. // if no db entries are found show an error
  163. if (data.length == 0)
  164. showalert("noDataAlert", "Request Error!", "No data found for this request.", "error", true);
  165. });
  166. // set marks on realtime attacks
  167. socket.on("markIncident", function(data) {
  168. if (live) {
  169. world.markIncident(data, live);
  170. var tableEntry = world.generateTableEntry(data);
  171. world.makeTableEntry(tableEntry);
  172. }
  173. });
  174. var fields;
  175. socket.on("setFields", function(data) {
  176. fields = data;
  177. /* set attack types for the filter */
  178. var html = "";
  179. for(var key in data["type"]){
  180. html += "<option value='" + key + "'>" + data["type"][key] + "</option>";
  181. }
  182. $("#filterAttackType").html(html);
  183. filter.initAttackTypes();
  184. });
  185. socket.on("setSensors", function(sensors) {
  186. /* set sensor types for the filter */
  187. var html = "";
  188. for(var i = 0; i < sensors.length; i++){
  189. var sensortype = sensors[i];
  190. html += "<option value='" + sensortype + "'>" + sensortype + "</option>";
  191. }
  192. $("#filterSensorType").html(html);
  193. filter.initSensorTypes();
  194. });
  195. function typeid2str(id){
  196. if(fields && fields["type"].hasOwnProperty(id))
  197. return fields["type"][id];
  198. else
  199. return fields && fields["type"][0] || "Unknown";
  200. }
  201. function typeid2descr(id){
  202. if(fields && fields["type_description"].hasOwnProperty(id))
  203. return fields["type_description"][id];
  204. else
  205. return fields && fields["type_description"][0] || "";
  206. }