|
| 1 | +# hsync |
| 2 | + |
| 3 | +> Reverse-proxy and TCP tunnel client for Node.js AND browsers. Share local servers as public URLs, tunnel arbitrary TCP between clients via WebSocket. Works in-browser too. |
| 4 | + |
| 5 | +## What It Does |
| 6 | + |
| 7 | +1. **Public URLs for local servers** - Like ngrok but self-hosted |
| 8 | +2. **TCP tunneling** - Forward any TCP port between two hsync clients |
| 9 | +3. **Browser support** - Works in browsers via WebSocket, not just Node.js |
| 10 | +4. **MQTT integration** - Pub/sub messaging between connected clients |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +```bash |
| 15 | +npm install -g hsync |
| 16 | +``` |
| 17 | + |
| 18 | +Or use in browser: |
| 19 | +```html |
| 20 | +<script src="https://cdn.jsdelivr.net/npm/hsync/dist/hsync.min.js"></script> |
| 21 | +``` |
| 22 | + |
| 23 | +## Quick Start |
| 24 | + |
| 25 | +### Share a local webserver |
| 26 | + |
| 27 | +```bash |
| 28 | +# Share localhost:3000 as a public URL |
| 29 | +hsync -p 3000 |
| 30 | +``` |
| 31 | + |
| 32 | +### Programmatic usage (Node.js) |
| 33 | + |
| 34 | +```javascript |
| 35 | +import { hsync } from 'hsync'; |
| 36 | + |
| 37 | +const connection = await hsync({ |
| 38 | + server: 'wss://your-hsync-server.com/_hs', |
| 39 | + secret: 'your-secret', |
| 40 | + name: 'myapp', |
| 41 | + local: 'http://localhost:3000' |
| 42 | +}); |
| 43 | + |
| 44 | +console.log('Public URL:', connection.publicUrl); |
| 45 | +``` |
| 46 | + |
| 47 | +### Browser usage |
| 48 | + |
| 49 | +```javascript |
| 50 | +const connection = await hsync.dynamicConnect(); |
| 51 | +// Get a temporary public URL without config |
| 52 | +``` |
| 53 | + |
| 54 | +## Configuration |
| 55 | + |
| 56 | +| Flag | Env Variable | Description | |
| 57 | +|------|-------------|-------------| |
| 58 | +| `-p, --port` | `PORT` | Local webserver port | |
| 59 | +| `-s, --hsync-server` | `HSYNC_SERVER` | Server URL (wss://...) | |
| 60 | +| `-hs, --hsync-secret` | `HSYNC_SECRET` | Authentication secret | |
| 61 | +| `-d, --dynamic-host` | `HSYNC_DYNAMIC_HOST` | Get temp URL without secret | |
| 62 | + |
| 63 | +### TCP Tunneling |
| 64 | + |
| 65 | +```bash |
| 66 | +# Listen locally on port 2222, forward to remote host:22 |
| 67 | +hsync --listener-local-port 2222 --listener-target-host remote.server.com --listener-target-port 22 |
| 68 | +``` |
| 69 | + |
| 70 | +### Relay mode (remote → local) |
| 71 | + |
| 72 | +```bash |
| 73 | +# Accept connections on server port 8080, forward to local port 3000 |
| 74 | +hsync --relay-inbound-port 8080 --relay-target-host localhost --relay-target-port 3000 |
| 75 | +``` |
| 76 | + |
| 77 | +## Key Exports |
| 78 | + |
| 79 | +```javascript |
| 80 | +import { hsync } from 'hsync'; // Main connection function |
| 81 | +import { hsync } from 'hsync/web'; // Browser-specific (smaller) |
| 82 | +import { hsync } from 'hsync/node'; // Node.js-specific |
| 83 | +import { getConfig } from 'hsync/config'; // Configuration utilities |
| 84 | +``` |
| 85 | + |
| 86 | +## Architecture |
| 87 | + |
| 88 | +``` |
| 89 | + ┌─────────────────┐ |
| 90 | + │ hsync-server │ |
| 91 | + │ (public URL) │ |
| 92 | + └────────┬────────┘ |
| 93 | + │ WebSocket |
| 94 | + ┌─────────────────┼─────────────────┐ |
| 95 | + │ │ │ |
| 96 | + ┌──────▼──────┐ ┌──────▼──────┐ ┌──────▼──────┐ |
| 97 | + │ hsync (Node)│ │hsync (Browser)│ │ hsync (Node)│ |
| 98 | + │ localhost:X │ │ Web App │ │ localhost:Y │ |
| 99 | + └─────────────┘ └───────────────┘ └─────────────┘ |
| 100 | +``` |
| 101 | + |
| 102 | +## Use Cases |
| 103 | + |
| 104 | +1. **Development** - Share local dev server with teammates |
| 105 | +2. **IoT** - Connect devices behind NAT without port forwarding |
| 106 | +3. **Browser P2P** - Connect browsers via WebSocket relay |
| 107 | +4. **Remote access** - SSH through firewall via TCP tunnel |
| 108 | +5. **Webhooks** - Receive webhooks on local machine |
| 109 | + |
| 110 | +## File Structure |
| 111 | + |
| 112 | +| File | Purpose | |
| 113 | +|------|---------| |
| 114 | +| `hsync.js` | Node.js client implementation | |
| 115 | +| `hsync-web.js` | Browser client (WebSocket only) | |
| 116 | +| `cli.js` | Command-line interface | |
| 117 | +| `connection.js` | Connection management | |
| 118 | +| `config.js` | Configuration handling | |
| 119 | +| `lib/` | Internal utilities | |
| 120 | + |
| 121 | +## Related Projects |
| 122 | + |
| 123 | +- [hsync-server](https://github.com/monteslu/hsync-server) - The server component |
| 124 | +- [rawr](https://github.com/iceddev/rawr) - JSON-RPC over any transport (used internally) |
| 125 | + |
| 126 | +## License |
| 127 | + |
| 128 | +ISC |
0 commit comments