@cypress/debugging-proxy
Advanced tools
Comparing version 1.6.0 to 2.0.0
const net = require('net') | ||
const parse = require('./utils/parse') | ||
// https://stackoverflow.com/a/32104777/3474615 | ||
var regex_hostport = /^([^:]+)(:([0-9]+))?$/; | ||
var getHostPortFromString = function (hostString, defaultPort) { | ||
var host = hostString; | ||
var port = defaultPort; | ||
var result = regex_hostport.exec(hostString); | ||
if (result != null) { | ||
host = result[1]; | ||
if (result[2] != null) { | ||
port = result[3]; | ||
} | ||
module.exports = function (req, clientSocket, bodyhead, cb) { | ||
const [host, port] = parse.getHostPortFromString(req.url, 443); | ||
const shouldContinueProxying = cb({ | ||
req, | ||
host, | ||
port, | ||
socket: clientSocket, | ||
head: bodyhead, | ||
}) | ||
// stop the proxy if this returns false | ||
if (!shouldContinueProxying) { | ||
return | ||
} | ||
return ( [host, port] ); | ||
}; | ||
const proxySocket = new net.Socket(); | ||
module.exports = function (req, socket, bodyhead) { | ||
var hostPort = getHostPortFromString(req.url, 443); | ||
var hostDomain = hostPort[0]; | ||
var port = parseInt(hostPort[1]); | ||
this.proxySslConnectionToDomain(hostDomain, port) | ||
socket.setNoDelay(true) | ||
var proxySocket = new net.Socket(); | ||
proxySocket.connect(port, hostDomain, function () { | ||
proxySocket.setNoDelay(true) | ||
socket.write("HTTP/" + req.httpVersion + " 200 Connection established\r\n\r\n"); | ||
clientSocket.setNoDelay(true) | ||
proxySocket.setNoDelay(true) | ||
proxySocket.connect(port, host, function (err) { | ||
if (err) { | ||
clientSocket.write("HTTP/" + req.httpVersion + " 500 Connection error\r\n\r\n"); | ||
clientSocket.end(); | ||
return | ||
} | ||
clientSocket.write("HTTP/" + req.httpVersion + " 200 Connection established\r\n\r\n"); | ||
proxySocket.write(bodyhead); | ||
@@ -37,25 +37,24 @@ }); | ||
proxySocket.on('data', function (chunk) { | ||
socket.write(chunk); | ||
clientSocket.write(chunk); | ||
}); | ||
proxySocket.on('end', function () { | ||
socket.end(); | ||
clientSocket.end(); | ||
}); | ||
proxySocket.on('error', function () { | ||
socket.write("HTTP/" + req.httpVersion + " 500 Connection error\r\n\r\n"); | ||
socket.end(); | ||
proxySocket.on('error', function (err) { | ||
clientSocket.destroy(err) | ||
}); | ||
socket.on('data', function (chunk) { | ||
clientSocket.on('data', function (chunk) { | ||
proxySocket.write(chunk); | ||
}); | ||
socket.on('end', function () { | ||
clientSocket.on('end', function () { | ||
proxySocket.end(); | ||
}); | ||
socket.on('error', function () { | ||
proxySocket.end(); | ||
clientSocket.on('error', function (err) { | ||
proxySocket.destroy(err); | ||
}); | ||
} |
39
index.js
@@ -1,7 +0,8 @@ | ||
const httpProxy = require('http-proxy') | ||
const httpsProxy = require('./https-proxy') | ||
const url = require('url') | ||
const http = require('http') | ||
const https = require('https') | ||
const url = require('url') | ||
const debug = require('debug')('proxy') | ||
const httpProxy = require('http-proxy') | ||
const httpsProxy = require('./https-proxy') | ||
const { getHostPortFromString } = require('./utils/parse') | ||
@@ -14,16 +15,28 @@ function DebuggingProxy(options = {}) { | ||
const onConnection = (req, res) => { | ||
this.proxyRequestToUrl(req.url, req, res) | ||
const { onConnect, onRequest } = this.options | ||
// set onRequest callback function to options to default | ||
this.onRequest = onRequest || this._onRequest | ||
// set onConnect callback function to options to default | ||
this.onConnect = onConnect || this._onConnect | ||
const requestListener = (req, res) => { | ||
return this.onRequest(req.url, req, res) | ||
} | ||
const connectListener = (req, socket, head) => { | ||
return this._httpsProxy(req, socket, head, this.onConnect) | ||
} | ||
if (options.https) { | ||
this.server = https.createServer(options.https, onConnection) | ||
this.server = https.createServer(options.https, requestListener) | ||
} else { | ||
this.server = http.createServer(onConnection) | ||
this.server = http.createServer(requestListener) | ||
} | ||
this._httpsProxy = httpsProxy | ||
this.server.addListener('connect', this.httpsProxy.bind(this)) | ||
this.server.addListener('connect', connectListener) | ||
this.proxy = new httpProxy.createProxyServer() | ||
this.proxy = httpProxy.createProxyServer() | ||
this.server.on('upgrade', (req, socket, head) => { | ||
@@ -80,3 +93,5 @@ debug('ws request received for', req.url) | ||
DebuggingProxy.prototype.proxyRequestToUrl = function(reqUrl, req, res) { | ||
DebuggingProxy.prototype.getHostPortFromString = getHostPortFromString | ||
DebuggingProxy.prototype._onRequest = function(reqUrl, req, res) { | ||
// stub me | ||
@@ -119,7 +134,9 @@ if (!this.validateAuth(req)) { | ||
DebuggingProxy.prototype.proxySslConnectionToDomain = function(domain, port) { | ||
DebuggingProxy.prototype._onConnect = function(domain, port) { | ||
// stub me | ||
debug("Proxying HTTPS request for:", domain, port) | ||
return true | ||
} | ||
module.exports = DebuggingProxy |
@@ -8,3 +8,3 @@ { | ||
"homepage": "https://github.com/cypress-io/node-debugging-proxy", | ||
"version": "1.6.0", | ||
"version": "2.0.0", | ||
"main": "index.js", | ||
@@ -11,0 +11,0 @@ "license": "MIT", |
706070
8
181