ous.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Slitheen - a decoy routing system for censorship resistance
  3. * Copyright (C) 2017 Cecylia Bocovich (cbocovic@uwaterloo.ca)
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, version 3.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * Additional permission under GNU GPL version 3 section 7
  18. *
  19. * If you modify this Program, or any covered work, by linking or combining
  20. * it with the OpenSSL library (or a modified version of that library),
  21. * containing parts covered by the terms of the OpenSSL Licence and the
  22. * SSLeay license, the licensors of this Program grant you additional
  23. * permission to convey the resulting work. Corresponding Source for a
  24. * non-source form of such a combination shall include the source code
  25. * for the parts of the OpenSSL library used as well as that of the covered
  26. * work.
  27. */
  28. var fs = require('fs');
  29. var page = require('webpage').create();
  30. var initial_done = false;
  31. var initial_bytes = '';
  32. var totalbytes = 0;
  33. var downstreamdata = {};
  34. var upstream_data = {};
  35. var ous_in_data = '';
  36. var slitheenID = '';
  37. var server = require('webserver').create();
  38. var ous_in = server.listen('127.0.0.1:8888', function(request, response) {
  39. console.log("Read in " + request.post);
  40. if(slitheenID == ''){
  41. slitheenID = request.post + ' ';
  42. } else {
  43. ous_in_data += request.post;
  44. }
  45. response.close();
  46. });
  47. if(!ous_in){
  48. console.log('Failed to listen on port 8888');
  49. phantom.exit();
  50. } else {
  51. console.log('Listening :)');
  52. }
  53. var output = fs.open("OUS_out", {mode: 'wb'});
  54. page.captureContent = ['.*'];
  55. page.onResourceRequested = function(request, network) {
  56. //console.log('Request ' + JSON.stringify(request, undefined, 4));
  57. if( ous_in_data != ''){
  58. var bytes = ous_in_data;
  59. ous_in_data = '';
  60. bytes.replace(/\r?\n|\r/g, "");
  61. network.setHeader('X-Slitheen', slitheenID + bytes);
  62. console.log('Sent X-Slitheen: ' + slitheenID + bytes);
  63. upstream_data[request.id] = bytes;
  64. } else {
  65. network.setHeader('X-Slitheen', slitheenID);
  66. console.log('Sent X-Slitheen: ' + slitheenID);
  67. }
  68. };
  69. page.onResourceReceived = function(response) {
  70. //console.log('Receive ' + JSON.stringify(response, undefined, 4));
  71. var id = response.id;
  72. if (response.stage == "start"){
  73. downstreamdata[response.id] = response.bodySize;
  74. }
  75. if (response.stage == "end"){
  76. totalbytes += downstreamdata[response.id];
  77. //console.log("totalbytes is now " + totalbytes);
  78. }
  79. //check to see if request successfully carried data
  80. if(upstream_data.hasOwnProperty(id)){
  81. if(response.status != 0){
  82. delete upstream_data[id];
  83. }
  84. }
  85. if(response.stage == "end" && response.contentType == "slitheen"){
  86. output.write(response.bodySize + '\n' + response.body);
  87. output.flush();
  88. }
  89. };
  90. var count = 1;
  91. function loadpage(){
  92. page.clearMemoryCache();
  93. totalbytes = 0;
  94. var t = Date.now();
  95. page.open('https://gmail.com', function(status) {
  96. console.log("Status for page load "+ count + " : " + status);
  97. if(status === "success") {
  98. t = Date.now() - t;
  99. count += 1;
  100. //if(count > 102){
  101. // phantom.exit();
  102. //}
  103. fs.write("timing1.out", t + ',', 'a');
  104. fs.write("size1.out", totalbytes + ',', 'a');
  105. } else {
  106. fs.write("timing1.out", '-1,', 'a');
  107. fs.write("size1.out", '-1,', 'a');
  108. }
  109. for( var id in upstream_data){
  110. ous_in_data += upstream_data[id];
  111. }
  112. loadpage();
  113. });
  114. }
  115. loadpage();