Skip to content

Commit

Permalink
Support HTTP header Config for client
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyun94 committed Feb 20, 2024
1 parent e949dbe commit 2c25e04
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
18 changes: 13 additions & 5 deletions cli/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ const argv = mri(process.argv.slice(2), {
if (argv.help || argv.h) {
process.stdout.write(`
Usage:
tcp-over-websockets <tunnel-url> <tunnelled-target> <port-to-listen-on>
tcp-over-websockets <tunnel-url> <tunnelled-target> <port-to-listen-on> [<http-header-for-tunnel>]
Arguments:
tunnel-url The WebSocket address of the tunnel server.
tunnelled-target The hostname & port to let the tunnel server connect to.
port-to-listen-on The (local) port to expose the tunnel on.
tunnel-url The WebSocket address of the tunnel server.
tunnelled-target The hostname & port to let the tunnel server connect to.
port-to-listen-on The (local) port to expose the tunnel on.
http-header-for-tunnel The HTTP header for tunnerl websocket request, JSON format.
Example:
tcp-over-websockets wss://example.org localhost:22 8022
tcp-over-websockets wss://example.org localhost:22 8022 '{ "Cookie" : "anything" }'
\n`)
process.exit(0)
}
Expand All @@ -40,7 +42,13 @@ if (!target) showError('missing target argument')
const port = argv._[2]
if (!port) showError('missing port argument')

startClient(tunnel, target, port, (err) => {
const header_str = argv._[3]
let headers = undefined
if (header_str) {
headers = JSON.parse(header_str)
}

startClient(tunnel, target, port, headers, (err) => {
if (err) showError(err)
else console.info(`tunneling ${target} via ${tunnel} & exposing it on port ${port}`)
})
8 changes: 6 additions & 2 deletions client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
const {createServer} = require('net')
const {tunnelTo} = require('./tunnel')

const startClient = (tunnel, target, port, cb) => {
const tcpServer = createServer(tunnelTo(tunnel, target))
const startClient = (tunnel, target, port, opt, cb) => {
if (arguments.length === 4 && 'function' === typeof opt) {
cb = opt
opt = {}
}
const tcpServer = createServer(tunnelTo(tunnel, target, opt))

tcpServer.listen(port, cb)
return tcpServer
Expand Down
4 changes: 2 additions & 2 deletions tunnel.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ 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 tunnelTo = (tunnel, target, header) => (local) => {
const remote = ws(tunnel + (tunnel.slice(-1) === '/' ? '' : '/') + target, {headers : header})

const onError = (err) => {
if (err) debug(err)
Expand Down

0 comments on commit 2c25e04

Please sign in to comment.