insert.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 orm = require("orm");
  19. var City = require("geoip").City; // requires 'npm install geoip'
  20. var city = new City("../../GeoLiteCity.dat");
  21. var Models = require("./models.js");
  22. var config = require("../../config.json");
  23. var fields = require("../fields.js")["fields"];
  24. var types = fields["type"];
  25. var typeids = [];
  26. for(var key in types){
  27. typeids.push(key);
  28. }
  29. function rand(min, max){
  30. return Math.floor((Math.random()*(max+1-min))+min);
  31. }
  32. function randIP(){
  33. return rand(0,255) + "." + rand(0,255) + "." + rand(0,255) + "." + rand(0,255);
  34. }
  35. function randType(){
  36. var index = rand(0,typeids.length-1);
  37. return typeids[index];
  38. }
  39. orm.connect(config.db, function (err, db) {
  40. if(err){
  41. console.log(err);
  42. return;
  43. }
  44. Models.initialize(db);
  45. var Incident = Models.Incident;
  46. var items = [];
  47. var max = new Date();
  48. var date, day = 100;
  49. do {
  50. for(var i = 0; i < 5; i++){
  51. date = new Date(2013, 0, day, rand(0, 23), rand(0, 59), rand(0, 59));
  52. var sip = randIP();
  53. var dip = randIP();
  54. var sgeo = city.lookupSync(sip);
  55. var dgeo = city.lookupSync(dip);
  56. if(sgeo && dgeo){
  57. items.push({
  58. date: date,
  59. sensortype: "test" + rand(1,9),
  60. sensorname: "test" + rand(1,9),
  61. source_ip: sip,
  62. source_port: rand(1024, 65535),
  63. destination_ip: dip,
  64. destination_port: 80,
  65. type: randType(),
  66. source_country: sgeo.country_name,
  67. source_cc: sgeo.country_code,
  68. source_city: sgeo.city,
  69. source_latitude: sgeo.latitude,
  70. source_longitude: sgeo.longitude,
  71. destination_country: dgeo.country_name,
  72. destination_cc: dgeo.country_code,
  73. destination_city: dgeo.city,
  74. destination_latitude: dgeo.latitude,
  75. destination_longitude: dgeo.longitude,
  76. authorized: rand(0, 1),
  77. });
  78. }
  79. }
  80. day++;
  81. } while(date < max);
  82. // store new items in the database
  83. Incident.create(items, function (err, items) {
  84. // err - description of the error or null
  85. if(err){
  86. console.log(err);
  87. return;
  88. }
  89. // items - array of inserted items
  90. console.log("inserted %d items", items.length);
  91. }
  92. );
  93. });