Skip to content

Commit

Permalink
Move client conn handler to separate file
Browse files Browse the repository at this point in the history
Users of this package may wish to build their own clients that connect
to a tcp-over-websockets server. This change makes the TCP tunnelling
handler available to package users, while still keeping the
module.exports conventions used here.

- Create `tunnel`, a new module
- Move handler that tunnels an incoming TCP connection from `client`
  to `tunnel`, naming the function `tunnelTo`
- Add `tunnel.js`to manifest of files to be exported by package.json
  • Loading branch information
LoneRifle authored and derhuerst committed Apr 10, 2023
1 parent b5d9e74 commit 2d96ee7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
14 changes: 2 additions & 12 deletions client.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 10,7 @@
"files": [
"client.js",
"server.js",
"tunnel.js",
"cli"
],
"keywords": [
Expand Down
17 changes: 17 additions & 0 deletions tunnel.js
Original file line number Diff line number Diff line change
@@ -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}

0 comments on commit 2d96ee7

Please sign in to comment.