Skip to content

Commit 2c25e04

Browse files
committed
Support HTTP header Config for client
1 parent e949dbe commit 2c25e04

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

cli/client.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ const argv = mri(process.argv.slice(2), {
1111
if (argv.help || argv.h) {
1212
process.stdout.write(`
1313
Usage:
14-
tcp-over-websockets <tunnel-url> <tunnelled-target> <port-to-listen-on>
14+
tcp-over-websockets <tunnel-url> <tunnelled-target> <port-to-listen-on> [<http-header-for-tunnel>]
1515
Arguments:
16-
tunnel-url The WebSocket address of the tunnel server.
17-
tunnelled-target The hostname & port to let the tunnel server connect to.
18-
port-to-listen-on The (local) port to expose the tunnel on.
16+
tunnel-url The WebSocket address of the tunnel server.
17+
tunnelled-target The hostname & port to let the tunnel server connect to.
18+
port-to-listen-on The (local) port to expose the tunnel on.
19+
http-header-for-tunnel The HTTP header for tunnerl websocket request, JSON format.
1920
Example:
2021
tcp-over-websockets wss://example.org localhost:22 8022
22+
tcp-over-websockets wss://example.org localhost:22 8022 '{ "Cookie" : "anything" }'
2123
\n`)
2224
process.exit(0)
2325
}
@@ -40,7 +42,13 @@ if (!target) showError('missing target argument')
4042
const port = argv._[2]
4143
if (!port) showError('missing port argument')
4244

43-
startClient(tunnel, target, port, (err) => {
45+
const header_str = argv._[3]
46+
let headers = undefined
47+
if (header_str) {
48+
headers = JSON.parse(header_str)
49+
}
50+
51+
startClient(tunnel, target, port, headers, (err) => {
4452
if (err) showError(err)
4553
else console.info(`tunneling ${target} via ${tunnel} & exposing it on port ${port}`)
4654
})

client.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
const {createServer} = require('net')
44
const {tunnelTo} = require('./tunnel')
55

6-
const startClient = (tunnel, target, port, cb) => {
7-
const tcpServer = createServer(tunnelTo(tunnel, target))
6+
const startClient = (tunnel, target, port, opt, cb) => {
7+
if (arguments.length === 4 && 'function' === typeof opt) {
8+
cb = opt
9+
opt = {}
10+
}
11+
const tcpServer = createServer(tunnelTo(tunnel, target, opt))
812

913
tcpServer.listen(port, cb)
1014
return tcpServer

tunnel.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ const ws = require('websocket-stream')
44
const pipe = require('pump')
55
const debug = require('debug')('tcp-over-websockets:client')
66

7-
const tunnelTo = (tunnel, target) => (local) => {
8-
const remote = ws(tunnel + (tunnel.slice(-1) === '/' ? '' : '/') + target)
7+
const tunnelTo = (tunnel, target, header) => (local) => {
8+
const remote = ws(tunnel + (tunnel.slice(-1) === '/' ? '' : '/') + target, {headers : header})
99

1010
const onError = (err) => {
1111
if (err) debug(err)

0 commit comments

Comments
 (0)