1
0
Fork 0
mirror of https://github.com/openstf/stf synced 2025-10-05 02:29:26 +02:00

Reverse port forwarding UI actually works now.

This commit is contained in:
Simo Kinnunen 2014-10-14 22:57:44 +09:00
parent 99864fb223
commit cecf08a244
10 changed files with 158 additions and 134 deletions

View file

@ -7,45 +7,51 @@ var ForwardWriter = require('./writer')
// Handles multiple ports
function ForwardManager() {
var handlersByPort = Object.create(null)
var handlersById = Object.create(null)
this.has = function(port) {
return !!handlersByPort[port]
this.has = function(id) {
return !!handlersById[id]
}
this.add = function(port, conn, to) {
this.add = function(id, conn, options) {
function endListener() {
delete handlersByPort[port]
this.emit('remove', port, to)
delete handlersById[id]
this.emit('remove', id, options)
}
var handler = new ForwardHandler(conn, to)
if (this.has(id)) {
this.remove(id)
}
var handler = new ForwardHandler(conn, options)
handler.on('end', endListener.bind(this))
handlersByPort[port] = handler
handlersById[id] = handler
this.emit('add', port, to)
this.emit('add', id, options)
}
this.remove = function(port) {
var handler = handlersByPort[port]
this.remove = function(id) {
var handler = handlersById[id]
if (handler) {
handler.end()
}
}
this.removeAll = function() {
Object.keys(handlersByPort).forEach(function(port) {
handlersByPort[port].end()
Object.keys(handlersById).forEach(function(id) {
handlersById[id].end()
})
}
this.listAll = function() {
return Object.keys(handlersByPort).map(function(port) {
var handler = handlersByPort[port]
return Object.keys(handlersById).map(function(id) {
var handler = handlersById[id]
return {
port: port
, to: handler.to
id: id
, devicePort: handler.options.devicePort
, targetHost: handler.options.targetHost
, targetPort: handler.options.targetPort
}
})
}
@ -56,7 +62,7 @@ function ForwardManager() {
util.inherits(ForwardManager, events.EventEmitter)
// Handles a single port
function ForwardHandler(conn, to) {
function ForwardHandler(conn, options) {
var destHandlersById = Object.create(null)
function endListener() {
@ -73,7 +79,7 @@ function ForwardHandler(conn, to) {
if (packet) {
if (!dest) {
// Let's create a new connection
dest = destHandlersById[id] = new DestHandler(id, conn, to)
dest = destHandlersById[id] = new DestHandler(id, conn, options)
dest.on('end', packetEndListener.bind(null, id))
}
@ -87,11 +93,16 @@ function ForwardHandler(conn, to) {
}
}
function readableListener() {
// No-op but must exist so that we get the 'end' event.
}
conn.pipe(new ForwardReader())
.on('end', endListener.bind(this))
.on('packet', packetListener)
.on('readable', readableListener)
this.to = to
this.options = options
this.end = function() {
conn.end()
@ -103,7 +114,7 @@ function ForwardHandler(conn, to) {
util.inherits(ForwardHandler, events.EventEmitter)
// Handles a single connection
function DestHandler(id, conn, to) {
function DestHandler(id, conn, options) {
function endListener() {
conn.removeListener('drain', drainListener)
this.emit('end')
@ -133,7 +144,10 @@ function DestHandler(id, conn, to) {
}
}
var dest = net.connect(to)
var dest = net.connect({
host: options.targetHost
, port: options.targetPort
})
.on('error', errorListener)
var writer = dest.pipe(new ForwardWriter(id))