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 24, 2024
1 parent e949dbe commit 8c528ae
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
17 changes: 14 additions & 3 deletions cli/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@ const mri = require('mri')
const pkg = require('../package.json')

const argv = mri(process.argv.slice(2), {
boolean: ['help', 'h', 'version', 'v']
boolean: ['help', 'h', 'version', 'v'],
alias : {H: 'header'}
})

if (argv.help || argv.h) {
process.stdout.write(`
Usage:
tcp-over-websockets <tunnel-url> <tunnelled-target> <port-to-listen-on>
tcp-over-websockets [options] <tunnel-url> <tunnelled-target> <port-to-listen-on>
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.
Options:
-H, --header <header> Pass custom header(s) to tunnel server
Example:
tcp-over-websockets wss://example.org localhost:22 8022
tcp-over-websockets --header "Cookie:FOOL" wss://example.org localhost:22 8022
\n`)
process.exit(0)
}
Expand All @@ -40,7 +44,14 @@ if (!target) showError('missing target argument')
const port = argv._[2]
if (!port) showError('missing port argument')

startClient(tunnel, target, port, (err) => {
let headers = {}
const header_items = Array.isArray(argv.header) ? argv.header : [argv.header]
for (const item of header_items) {
const [key, ...data] = item.split(':');
headers[key.trim()] = data.join(':').trim()
}

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, opt) => (local) => {
const remote = ws(tunnel + (tunnel.slice(-1) === '/' ? '' : '/') + target, opt)

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

0 comments on commit 8c528ae

Please sign in to comment.