Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. var url = require( 'url' ),
  2. http = require( 'http' ),
  3. https = require( 'https' );
  4.  
  5. function Proxy( api, port ) {
  6. var that = this;
  7. this.api = api;
  8. this.port = port;
  9. this.server = api.createServer( function ( clientRequest, clientResponse ) {
  10. that.request = new Request( clientRequest, clientResponse );
  11. } );
  12. this.server.listen( this.port );
  13. }
  14. Proxy.prototype = {
  15. stop: function ( ) {
  16. this.server.close( );
  17. }
  18. };
  19.  
  20. function Request( clientRequest, clientResponse ) {
  21. this.clientRequest = clientRequest;
  22. this.clientResponse = clientResponse;
  23. this.$_GET = url.parse( clientRequest.url, true ).query;
  24. this.url = this.$_GET.url;
  25. if ( this.url ) {
  26. var clientRequestUrl = this.clientRequestUrl = url.parse( this.url );
  27. this.ssl = clientRequestUrl.protocol !== 'http:';
  28. this.api = this.ssl ? https : http;
  29. this.requestOptions = {
  30. host: clientRequestUrl.hostname,
  31. method: clientRequest.method,
  32. port: clientRequestUrl.port || ( this.ssl ? 443 : 80 ),
  33. path: clientRequestUrl.path || ('/'),
  34. headers: clientRequest.headers
  35. };
  36. this.handle( );
  37. } else {
  38. clientResponse.end( 'Need url!' );
  39. }
  40. }
  41. Request.prototype = {
  42. handle: function ( ) {
  43. var that = this;
  44. var clientResponse = that.clientResponse;
  45. var clientRequest = that.clientRequest;console.log( this.requestOptions );
  46. var serverRequest = this.serverRequest = this.api.get( this.requestOptions, function ( serverResponse ) {
  47. that.serverResponse = serverResponse;
  48.  
  49. serverResponse.on( 'data', function ( chunk ) {
  50. clientResponse.write( chunk, 'binary' );
  51. } );
  52. serverResponse.on( 'end', function ( ) {
  53. clientResponse.end( );
  54. } );
  55.  
  56. var headers = serverResponse.headers;
  57. if ( headers.Location ) {console.log( headers.Location );
  58. //headers = Object.create( headers );
  59. headers.Location = url.parse( clientRequest.url ).pathname + '?url=' + headers.Location;
  60. console.log( headers.Location );
  61. }
  62.  
  63. clientResponse.writeHead( serverResponse.statusCode, serverResponse.headers );
  64.  
  65. clientRequest.on( 'data', function ( chunk ) {
  66. serverRequest.write( chunk, 'binary' );
  67. } );
  68. clientRequest.on( 'end', function ( ) {
  69. serverRequest.end( );
  70. } );
  71. } ).on( 'error', function ( e ) {
  72. if ( ++that.errors <= 5 ) {
  73. that.handle( );
  74. } else {
  75. clientResponse.end( 'Can\'t reach url: "' + that.url + '" (' + e + ')!' );
  76. }
  77. } );
  78. },
  79. errors: 0
  80. };
  81.  
  82. typeof proxy !== 'undefined' && proxy.stop( );
  83. proxy = new Proxy( require( 'http' ), 80 );
  84. //new Proxy( require( 'https' ), 8243 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement