alpha version

This commit is contained in:
wuchangming 2016-03-16 13:27:40 +08:00
parent d7b1d0ede2
commit ae92b104a2
7 changed files with 92 additions and 41 deletions

4
lib/config/config.js Normal file
View File

@ -0,0 +1,4 @@
const SPY_WEINRE_DOMAIN = 'spyweinrefortest.com';
var config = exports;
config.SPY_WEINRE_DOMAIN = SPY_WEINRE_DOMAIN;

View File

@ -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,`<script src="http://${httpUtil.getIP()}:${_this.weinewPort}/target/target-script-min.js#anonymous"></script>`);
var newChunkString = htmlUtil.injectScriptIntoHtml(chunkString,`<script src="http://${config.SPY_WEINRE_DOMAIN}:${_this.weinewPort}/target/target-script-min.js#anonymous"></script>`);
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);
});
});

View File

@ -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(':');
};

View File

@ -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);
})
}

View File

@ -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"
}
}

16
test/testClientRequest.js Normal file
View File

@ -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();

19
test/testStream.js Normal file
View File

@ -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);