diff --git a/client.js b/client.js index 496da8c..a295874 100644 --- a/client.js +++ b/client.js @@ -1,20 +1,10 @@ 'use strict' -const ws = require('websocket-stream') const {createServer} = require('net') -const pipe = require('pump') -const debug = require('debug')('tcp-over-websockets:client') +const {tunnelTo} = require('./tunnel') const startClient = (tunnel, target, port, cb) => { - const tcpServer = createServer((local) => { - const remote = ws(tunnel + (tunnel.slice(-1) === '/' ? '' : '/') + target) - - const onError = (err) => { - if (err) debug(err) - } - pipe(remote, local, onError) - pipe(local, remote, onError) - }) + const tcpServer = createServer(tunnelTo(tunnel, target)) tcpServer.listen(port, cb) return tcpServer diff --git a/package.json b/package.json index 4d29e6e..912c17e 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "files": [ "client.js", "server.js", + "tunnel.js", "cli" ], "keywords": [ diff --git a/tunnel.js b/tunnel.js new file mode 100644 index 0000000..dd4d50c --- /dev/null +++ b/tunnel.js @@ -0,0 +1,17 @@ +'use strict' + +const ws = require('websocket-stream') +const pipe = require('pump') +const debug = require('debug')('tcp-over-websockets:client') + +const tunnelTo = (tunnel, target) => (local) => { + const remote = ws(tunnel + (tunnel.slice(-1) === '/' ? '' : '/') + target) + + const onError = (err) => { + if (err) debug(err) + } + pipe(remote, local, onError) + pipe(local, remote, onError) +} + +module.exports = {tunnelTo}