Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const http = require("http");
- const URL = require("url");
- /*Function createRoundRobinProvider(port,proxies)
- Creates a Round Robin Distributing Server that Devides the Requests to the Proxies
- Parameters:
- port - The port the server uses
- proxies - The proxies in an array of host-port objects [{host:"host",port:0},{host:"host",port:1}]...
- Returns:
- trus
- Events:
- request - fired on incomming request, returns the request and the relay Proxy
- response - fired on outgoing proxy response, returns the response and the relay Proxy
- */
- function proxyDistributior(port,proxies){
- this.proxy_index = 0;
- self = this;
- http.createServer((request,response)=>{
- if(self.proxy_index >= proxies.length){self.proxy_index = 0;}
- var proxy = proxies[self.proxy_index++];
- self.emit("request",request,proxy);
- console.log(request.url)
- var proxy_request = http.request({
- host:proxy.host,
- port:proxy.port,
- path:URL.parse(request.url).path,
- method:request.method,
- headers:request.headers
- },(proxy_response)=>{
- self.emit("response",proxy_response,proxy);
- proxy_response.on('data',(chunk)=>{
- response.write(chunk);
- });
- proxy_response.on('end',_=>{
- response.end();
- });
- response.writeHead(proxy_response.statusCode,proxy_response.headers);
- });
- request.on('data',(chunk)=>{
- proxy_request.write(chunk);
- });
- request.on('end',_=>{
- proxy_request.end();
- });
- }).listen(port);
- return this;
- }
- util.inherits(proxyDistributior,require("events").EventEmitter);
- this.proxyDistributor= proxyDistributior;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement