require("./m3m4") var http = require('http'); var server = http.createServer(); server.listen(100, function () { var port = server.address().port; console.log('HTTP(s) proxy server listening on port %d', port); }); var fs=require("fs") var request = require('request'); function eachHeader (obj, fn) { if (Array.isArray(obj.rawHeaders)) { // ideal scenario... >= node v0.11.x // every even entry is a "key", every odd entry is a "value" var key = null; obj.rawHeaders.forEach(function (v) { if (key === null) { key = v; } else { fn(key, v); key = null; } }); } else { // otherwise we can *only* proxy the header names as lowercase'd var headers = obj.headers; if (!headers) return; Object.keys(headers).forEach(function (key) { var value = headers[key]; if (Array.isArray(value)) { // set-cookie value.forEach(function (val) { fn(key, val); }); } else { fn(key, value); } }); } } var hopByHopHeaders = [ 'Connection', 'Keep-Alive', 'Proxy-Authenticate', 'Proxy-Authorization', 'TE', 'Trailers', 'Transfer-Encoding', 'Upgrade' ]; // create a case-insensitive RegExp to match "hop by hop" headers var isHopByHop = new RegExp('^(' + hopByHopHeaders.join('|') + ')$', 'i'); //伪造http function parse (req, server) { var parsed = require("url").parse(req.url); parsed.uri=parsed.href; // proxy the request HTTP method parsed.method = req.method; // setup outbound proxy request HTTP headers var headers = {}; parsed.headers = headers; eachHeader(req, function (key, value) { var keyLower = key.toLowerCase(); if (isHopByHop.test(key)) { } else { var v = headers[key]; if (Array.isArray(v)) { v.push(value); } else if (null != v) { headers[key] = [ v, value ]; } else { headers[key] = value; } } }); if (null == parsed.port) { // default the port number if not specified, for >= node v0.11.6... // https://github.com/joyent/node/issues/6199 parsed.port = 80; } return parsed } server.on('request', function (req, res) { var url=req.url var parsed=parse(req) // console.log(parsed) if(url.Test("reduce_bd-logo1_oper.png")){ request(parsed,function(err,resp,body){ res.writeHeader(200) fs.createReadStream("a.png").pipe(res) }).pipe(fs.createWriteStream("a.png")) }else{ request(parsed,function(err,resp,body){ }).pipe(res) } })