Skip to content

Commit

Permalink
docs: add readme, author
Browse files Browse the repository at this point in the history
  • Loading branch information
LoneRifle committed Apr 16, 2023
1 parent db0aeb5 commit 2d1cf36
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
93 changes: 93 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# tow-hook

Route your Node.js application's TCP connectivity through WebSockets.
Built on [tcp-over-websockets](https://github.com/derhuerst/tcp-over-websockets).

Useful for scenarios where you are unable to make socket connections, eg:

- You are behind a corporate firewall that only allows outbound http(s) traffic

- If you are using [stackblitz](https://stackblitz.com/), which runs Node.js
completely within the browser, and hence does not support socket connections

## Quickstart

### Preparation

Host a tcp-over-websockets tunnel service. This will accept your
websocket connection and make a socket connection on your behalf:

```sh
$ DEBUG=tcp-over-websockets:* npx -p tcp-over-websockets tcp-over-websockets-server
listening on 8080
```

### As a module hook

```sh
# Set an optional env var to point to the websocket gateway
# Defaults to ws://localhost:8080
$ export TOW_TUNNEL=ws://localhost:8080

# You may wish to enable debug logging for the underlying
# tcp-over-websockets dependency by setting this env var
$ export DEBUG=tcp-over-websockets:*

# Use tow-hook without modifying your application
$ node -r tow-hook/register your-application.js

```

### Programmatic use

```js
const { hookToTunnel } = require('tow-hook')

hookToTunnel('ws://localhost:8080')

// Connections are now routed through websockets

```

## How it works

This package glues two dependencies together,
[mitm.js](https://github.com/moll/node-mitm) and
[tcp-over-websockets](https://github.com/derhuerst/tcp-over-websockets).

When invoked, mitm.js will intercept most calls to create connections,
in particular `net.connect()` and `tls.connect()`. tow-hook will then
to attach the sockets created by mitm.js to tcp-over-websockets, which will
route data from those sockets over a websocket connection.

mitm.js creates mock sockets that behave like raw socket connections,
notably for sockets created using `tls.connect()`. TLS support is hence
grafted on using [Forge](https://github.com/digitalbazaar/forge).

## Gotchas

### Sockets created manually are not intercepted

mitm.js does not intercept connections made with `net.Socket#connect()`
or `tls.Socket#connect()`, as it does not stub the Socket constructor
in the `net` module. If your application or dependency uses this for
connectivity, tow-hook will not route your connection.

ie, the following will not work:

```js
const net = require('net')
const socket = new net.Socket()

socket.connect(22, 'example.org')
```

but the following will:

```js
const net = require('net')
const socket = net.connect(22, 'example.org')
```

Track [moll/node-mitm#42](https://github.com/moll/node-mitm/issues/42)
for progress on this issue.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"author": "LoneRifle <[email protected]>",
"license": "MIT",
"dependencies": {
"mitm": "^1.7.2",
Expand Down

0 comments on commit 2d1cf36

Please sign in to comment.