Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support HTTP header Config for client #9

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(':');
xiaoyun94 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading