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

@@ -2,6 +2,7 @@ var net = require('net')
var Promise = require('bluebird')
var syrup = require('syrup')
var _ = require('lodash')
var wire = require('../../../../wire')
var logger = require('../../../../util/logger')
@@ -61,33 +62,37 @@ module.exports = syrup.serial()
})
}
plugin.createForward = function(port, to) {
plugin.createForward = function(id, forward) {
log.info(
'Creating reverse port forward from ":%d" to "%s:%d"'
, port
, to.host
, to.port
'Creating reverse port forward "%s" from ":%d" to "%s:%d"'
, id
, forward.devicePort
, forward.targetHost
, forward.targetPort
)
return connectService(1)
.then(function(out) {
var header = new Buffer(4)
header.writeUInt16LE(0, 0)
header.writeUInt16LE(port, 2)
header.writeUInt16LE(forward.devicePort, 2)
out.write(header)
return manager.add(port, out, to)
return manager.add(id, out, forward)
})
}
plugin.removeForward = function(port) {
log.info('Removing reverse port forward ":%d"', port)
manager.remove(port)
plugin.removeForward = function(id) {
log.info('Removing reverse port forward "%s"', id)
manager.remove(id)
return Promise.resolve()
}
plugin.connect = function(options) {
var resolver = Promise.defer()
var conn = net.connect(options)
var conn = net.connect({
host: options.targetHost
, port: options.targetPort
})
function connectListener() {
resolver.resolve(conn)
@@ -112,44 +117,29 @@ module.exports = syrup.serial()
group.on('leave', plugin.reset)
manager.on('add', function(port, to) {
push.send([
wireutil.global
, wireutil.envelope(new wire.DeviceForwardAddEvent(
options.serial
, port
, to.host
, to.port
))
])
})
var pushForwards = _.debounce(
function() {
push.send([
wireutil.global
, wireutil.envelope(new wire.ReverseForwardsEvent(
options.serial
, manager.listAll()
))
])
}
, 200
)
manager.on('remove', function(port) {
push.send([
wireutil.global
, wireutil.envelope(new wire.DeviceForwardRemoveEvent(
options.serial
, port
))
])
})
manager.on('add', pushForwards)
manager.on('remove', pushForwards)
return startService()
.then(awaitServer)
.then(function() {
plugin.createForward(3000, {
host: '127.0.0.1'
, port: 3000
})
router
.on(wire.ForwardTestMessage, function(channel, message) {
var reply = wireutil.reply(options.serial)
plugin.connect({
host: message.targetHost
, port: message.targetPort
})
plugin.connect(message)
.then(function(conn) {
conn.end()
push.send([
@@ -166,10 +156,7 @@ module.exports = syrup.serial()
})
.on(wire.ForwardCreateMessage, function(channel, message) {
var reply = wireutil.reply(options.serial)
plugin.createForward(message.devicePort, {
host: message.targetHost
, port: message.targetPort
})
plugin.createForward(message.id, message)
.then(function() {
push.send([
channel
@@ -186,7 +173,7 @@ module.exports = syrup.serial()
})
.on(wire.ForwardRemoveMessage, function(channel, message) {
var reply = wireutil.reply(options.serial)
plugin.removeForward(message.devicePort)
plugin.removeForward(message.id)
.then(function() {
push.send([
channel

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