Guest User

Untitled

a guest
Oct 17th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. # fork http traffic
  2. # e.g. proxy is your production system
  3. # and proxy2 your testing setup
  4.  
  5. http = require "http"
  6.  
  7. PRODUCTION_HOST = "localhost"
  8. PRODUCTION_PORT = 8081
  9. TESTING_HOST = "localhost"
  10. TESTING_PORT = 8082
  11.  
  12. SERVER_HOST = "localhost"
  13. SERVER_PORT = 8080
  14.  
  15. http.globalAgent.maxSockets = 500
  16.  
  17. server = http.createServer (req, res) ->
  18.  
  19. # stolen from node-http-proxy source
  20. if req.headers["x-forwarded-for"]
  21. req.headers["x-forwarded-for"] += "," + req.connection.remoteAddress || req.socket.remoteAddress
  22. else
  23. req.headers["x-forwarded-for"] = req.connection.remoteAddress || req.socket.remoteAddress
  24.  
  25. if req.headers["x-forwarded-port"]
  26. req.headers["x-forwarded-port"] += "," + req.connection.remotePort || req.socket.remoteAddress
  27. else
  28. req.headers["x-forwarded-port"] = req.connection.remotePort || req.socket.remoteAddress
  29.  
  30. options =
  31. host: PRODUCTION_HOST
  32. port: PRODUCTION_PORT
  33. path: req.url
  34. method: req.method
  35. headers: req.headers
  36.  
  37. proxyRequest = http.request options, (response) ->
  38. response.on "data", (data) -> res.write data #, "binary"
  39. response.once "end", -> res.end()
  40. response.headers.connection = "close" #TODO ???
  41. res.writeHead response.statusCode, response.headers
  42.  
  43. options.port = TESTING_PORT
  44. options.host = TESTING_HOST
  45. proxyRequest2 = http.request options
  46.  
  47. proxyRequest.once "error", (error) -> console.log error
  48. proxyRequest2.once "error", (error) -> console.log error
  49.  
  50. # handle data from client to proxy
  51. # forward data to upstream servers
  52. req.on "data", (data) ->
  53. try
  54. proxyRequest.write data #, "binary"
  55. catch e
  56. console.log e
  57. try
  58. proxyRequest2.write data
  59. catch e
  60. console.log e
  61.  
  62. # handle end event from client
  63. # end request of upstream servers
  64. req.once "end", ->
  65. try
  66. proxyRequest.end()
  67. catch e
  68. console.log e
  69. try
  70. proxyRequest2.end()
  71. catch e
  72. console.log e
  73.  
  74. server.listen SERVER_PORT, SERVER_HOST
  75.  
  76. (http.createServer (req, res) ->
  77. #console.log "Request on production system #{req.method} #{req.url}"
  78. #console.log ("#{key}: #{value}" for key, value of req.headers).join "\n"
  79. res.writeHead 200
  80. res.end()
  81. ).listen PRODUCTION_PORT, PRODUCTION_HOST
  82.  
  83. (http.createServer (req, res) ->
  84. #console.log "Request on testing system #{req.method} #{req.url}"
  85. #console.log ("#{key}: #{value}" for key, value of req.headers).join "\n"
  86. res.writeHead 200
  87. res.end()
  88. ).listen TESTING_PORT, TESTING_HOST
Add Comment
Please, Sign In to add comment