ous.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 [name of library's license],
  22. * the licensors of this Program grant you additional permission to convey
  23. * the resulting work. {Corresponding Source for a non-source form of such
  24. * a combination shall include the source code for the parts of the OpenSSL
  25. * library used as well as that of the covered work.}
  26. */
  27. var fs = require('fs');
  28. var page = require('webpage').create();
  29. var initial_done = false;
  30. var initial_bytes = '';
  31. var totalbytes = 0;
  32. var downstreamdata = {};
  33. var upstream_data = {};
  34. var ous_in_data = '';
  35. var slitheenID = '';
  36. var server = require('webserver').create();
  37. var ous_in = server.listen('127.0.0.1:8888', function(request, response) {
  38. console.log("Read in " + request.post);
  39. if(slitheenID == ''){
  40. slitheenID = request.post + ' ';
  41. } else {
  42. ous_in_data += request.post;
  43. }
  44. response.close();
  45. });
  46. if(!ous_in){
  47. console.log('Failed to listen on port 8888');
  48. phantom.exit();
  49. } else {
  50. console.log('Listening :)');
  51. }
  52. var output = fs.open("OUS_out", {mode: 'wb'});
  53. page.captureContent = ['.*'];
  54. page.onResourceRequested = function(request, network) {
  55. //console.log('Request ' + JSON.stringify(request, undefined, 4));
  56. if( ous_in_data != ''){
  57. var bytes = ous_in_data;
  58. ous_in_data = '';
  59. bytes.replace(/\r?\n|\r/g, "");
  60. network.setHeader('X-Slitheen', slitheenID + bytes);
  61. console.log('Sent X-Slitheen: ' + slitheenID + bytes);
  62. upstream_data[request.id] = bytes;
  63. } else {
  64. network.setHeader('X-Slitheen', slitheenID);
  65. console.log('Sent X-Slitheen: ' + slitheenID);
  66. }
  67. };
  68. page.onResourceReceived = function(response) {
  69. //console.log('Receive ' + JSON.stringify(response, undefined, 4));
  70. var id = response.id;
  71. if (response.stage == "start"){
  72. downstreamdata[response.id] = response.bodySize;
  73. }
  74. if (response.stage == "end"){
  75. totalbytes += downstreamdata[response.id];
  76. //console.log("totalbytes is now " + totalbytes);
  77. }
  78. //check to see if request successfully carried data
  79. if(upstream_data.hasOwnProperty(id)){
  80. if(response.status != 0){
  81. delete upstream_data[id];
  82. }
  83. }
  84. if(response.stage == "end" && response.contentType == "slitheen"){
  85. output.write(response.bodySize + '\n' + response.body);
  86. output.flush();
  87. }
  88. };
  89. var count = 1;
  90. function loadpage(){
  91. page.clearMemoryCache();
  92. totalbytes = 0;
  93. var t = Date.now();
  94. page.open('https://gmail.com', function(status) {
  95. console.log("Status for page load "+ count + " : " + status);
  96. if(status === "success") {
  97. t = Date.now() - t;
  98. count += 1;
  99. //if(count > 102){
  100. // phantom.exit();
  101. //}
  102. fs.write("timing1.out", t + ',', 'a');
  103. fs.write("size1.out", totalbytes + ',', 'a');
  104. } else {
  105. fs.write("timing1.out", '-1,', 'a');
  106. fs.write("size1.out", '-1,', 'a');
  107. }
  108. for( var id in upstream_data){
  109. ous_in_data += upstream_data[id];
  110. }
  111. loadpage();
  112. });
  113. }
  114. loadpage();