diff --git a/lib/config/config.js b/lib/config/config.js new file mode 100644 index 0000000..3295716 --- /dev/null +++ b/lib/config/config.js @@ -0,0 +1,4 @@ +const SPY_WEINRE_DOMAIN = 'spyweinrefortest.com'; + +var config = exports; +config.SPY_WEINRE_DOMAIN = SPY_WEINRE_DOMAIN; diff --git a/lib/proxy/SpyProxy.js b/lib/proxy/SpyProxy.js index 02fddea..a35c713 100644 --- a/lib/proxy/SpyProxy.js +++ b/lib/proxy/SpyProxy.js @@ -7,6 +7,8 @@ const httpUtil = require('../util/httpUtil'); const zlib = require('zlib'); const htmlUtil = require('../util/htmlUtil'); const os = require('os'); +const debug = require('debug')('spy-debugger'); +const config = require('../config/config'); module.exports = class SpyProxy { constructor(options) { @@ -19,22 +21,28 @@ module.exports = class SpyProxy { var server = new http.Server(); server.listen(port, () => { server.on('error', (e) => { - // console.error(e); + console.error(e); }); server.on('request', (req, res) => { - console.log('re'); var urlObject = url.parse(req.url); + var orginHost = req.headers['host']; + + debug('request urlObject ---> ', urlObject); + + var host = orginHost.split(':')[0]; + if(host === config.SPY_WEINRE_DOMAIN) { + host = '127.0.0.1'; + } + var rOptions = { protocol: urlObject.protocol, - hostname: urlObject.host, + host: host, method: req.method, port: urlObject.port || 80, path: urlObject.path - // auth } rOptions.headers = req.headers; var proxyReq = new http.ClientRequest(rOptions, (proxyRes) => { - var isGzip = httpUtil.isGzip(proxyRes); var isHtml = httpUtil.isHtml(proxyRes); @@ -52,22 +60,24 @@ module.exports = class SpyProxy { } }); + res.writeHead(proxyRes.statusCode); + if (isHtml) { if (isGzip) { var _this = this; - proxyRes.pipe(new zlib.Gunzip()).pipe(through(function (chunk, enc, callback) { + proxyRes.pipe(new zlib.Gunzip()) + .pipe(through(function (chunk, enc, callback) { var chunkString = chunk.toString(); console.log(_this.weinewPort); - var newChunkString = htmlUtil.injectScriptIntoHtml(chunkString,``); + var newChunkString = htmlUtil.injectScriptIntoHtml(chunkString,``); + console.log(newChunkString); this.push(new Buffer(newChunkString)); - // console.log(chunk.toString('utf-8')); callback(); })).pipe(new zlib.Gzip()).pipe(res); } else { proxyRes.pipe(through(function (chunk, enc, callback) { this.push(chunk); - // console.log(chunk.toString('utf-8')); callback(); })).pipe(res); } @@ -76,18 +86,14 @@ module.exports = class SpyProxy { proxyRes.pipe(res); } - }); - proxyReq.on('error', (e) => { - // console.error(e); - }) - req.pipe(through(function (chunk, enc, callback) { - this.push(chunk); - // TODO: - // console.log(chunk.toString('utf-8')); - callback(); - })).pipe(proxyReq); + req.on('aborted', function () { + console.log('aborted'); + proxyReq.abort(); + }); + + req.pipe(proxyReq); }); }); diff --git a/lib/util/httpUtil.js b/lib/util/httpUtil.js index 06f5bdc..8d8f97c 100644 --- a/lib/util/httpUtil.js +++ b/lib/util/httpUtil.js @@ -12,22 +12,6 @@ httpUtil.isHtml = function (res) { var contentType = res.headers['content-type']; return (typeof contentType != 'undefined') && /text\/html|application\/xhtml\+xml/.test(contentType); } -httpUtil.getIP = function () { - var ifaces = os.networkInterfaces(); - - var ip; - - Object.keys(ifaces).forEach(function (ifname) { - - ifaces[ifname].forEach(function (iface) { - if ('IPv4' !== iface.family || iface.internal !== false) { - // skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses - return; - } - ip = iface.address; - }); - }); - - return ip; - -} +httpUtil.hasPort = (host) => { + return !!~host.indexOf(':'); +}; diff --git a/lib/weinre/weinreDelegate.js b/lib/weinre/weinreDelegate.js index b425151..69569b7 100644 --- a/lib/weinre/weinreDelegate.js +++ b/lib/weinre/weinreDelegate.js @@ -6,8 +6,25 @@ const weinre = require('weinre'); const child_process = require('child_process'); const SpyProxy = require('../proxy/SpyProxy'); +// console log 颜色 +const FgRed = "\x1b[31m"; +const FgGreen = "\x1b[32m"; +const Reset = "\x1b[0m"; + var weinreDelegate = module.exports; +// fixed weinre bugs: https://github.com/apache/cordova-weinre/pull/13 +var originPrepareStackTrace = Error.prepareStackTrace; +Error.prepareStackTrace = (error, structuredStackTrace) => { + try { + originPrepareStackTrace(error, structuredStackTrace); + } catch (e) { + console.info('cause by weinre utils.js: ', e); + } finally { + return 'cause by weinre utils.js'; + } +} + weinreDelegate.run = function run() { let unBoundedPort; // get an unbounded port @@ -31,10 +48,13 @@ function startWeinreServer (port) { }); weinreServer.on('listening', () => { // auto open debugger page - child_process.exec(`open http://127.0.0.1:${port}/client/#anonymous`); + child_process.exec(`open http://127.0.0.1:${port}/client`); new SpyProxy({ weinewPort: port }); - console.log(`open ---> http://127.0.0.1:${port}/client/#anonymous`); + console.log(`${FgGreen}%s${Reset}`,`浏览器打开 ---> http://127.0.0.1:${port}/client`); }); + weinreServer.on('error', (e) => { + console.error(e); + }) } diff --git a/package.json b/package.json index d01b14a..92f7598 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "web page one-stop remote network debugger", "main": "index.js", "scripts": { - "test": "test" + "test": "DEBUG=spy-debugger node-debug index.js" }, "repository": { "type": "git", @@ -20,12 +20,14 @@ }, "homepage": "https://github.com/wuchangming/spy-debugger#readme", "dependencies": { + "debug": "^2.2.0", "harmon": "^1.3.1", "http-proxy": "^1.13.1", "mitm": "^1.2.0", "redbird": "^0.4.14", "requires-port": "^1.0.0", "through2": "^2.0.1", + "tunnel": "0.0.4", "weinre": "2.0.0-pre-I0Z7U9OV" } } diff --git a/test/testClientRequest.js b/test/testClientRequest.js new file mode 100644 index 0000000..2cfd9eb --- /dev/null +++ b/test/testClientRequest.js @@ -0,0 +1,16 @@ +const http = require('http'); + +console.log(11); +var client = new http.ClientRequest({ + protocol: 'http:', + hostname: '192.168.1.103', + method: 'GET', + port: '56047', + path: '/target/target-script-min.js' + +}, (res) => { + console.log(res); + +}); + +client.end(); diff --git a/test/testStream.js b/test/testStream.js new file mode 100644 index 0000000..c266546 --- /dev/null +++ b/test/testStream.js @@ -0,0 +1,19 @@ +var Readable = require('stream').Readable; +var rs = Readable(); + +var c = 97 - 1; + +rs._read = function () { + if (c >= 'z'.charCodeAt(0)) return rs.push(null); + + setTimeout(function () { + rs.push(String.fromCharCode(++c)); + }, 100); +}; + +rs.pipe(process.stdout); + +process.on('exit', function () { + console.error('\n_read() called ' + (c - 97) + ' times'); +}); +process.stdout.on('error', process.exit);