Nodejs wrapper around NKN Tuna
Allows using NKN tuna directly from a nodejs script.
Example:
import { startTuna, waitConnected } from 'tuna-for-node'
//starts the tuna process
await startTuna('entry', new URL('./config/', import.meta.url))
//awaits tuna is connected
const ip = await waitConnected()
console.log('IP:', ip)
requires node 20 or greater
npm install tuna-for-node
pnpm add tuna-for-node
It downloads the correct tuna release binary (based on the platform) from Tuna releases. And uses a simple script to start/stop the process (using spawn
).
Starts the tuna client.
Param | Type | Description |
---|---|---|
command | 'entry' | 'exit' |
|
configDir | string | URL |
|
ipChangeCB | function |
|
validatePorts | boolean |
if it waits for the proxy port to be open |
restart | boolean |
if it should restart tuna if already running |
Awaits tuna client being connected.
Returns: Promise<string>
- the current ip
Param | Type | Description |
---|---|---|
validatePorts | boolean |
if it waits for the proxy port to be open |
start | boolean |
if tuna should be started if not running (throws error if false and tuna is already running) |
Stops the tuna client.
Starts a tuna proxy (http or socks5), this mode allows starting multiple proxies and there is no interference between each process. Port is assigned randomly by OS, and one can retrieve it from the return value listenPort
- This package relies on reading the log output stream from tuna to determine if connected and to what ip. Due to the limitation of the output, there is no way to determine which serivce disconnects and connects belong to. So in the configuration you must only have 1 service defined, it will throw error otherwise
- Tuna does not fail or report an error if the defined service binding port is already in use, so is up to you to make sure is not.
- Ability to start multiple (currently the process is global, so only one at the time can be started). One of the limitations is config directory, currently at best I could do a global map per config directory. However looking at more general solution options
- Handle all errors from the tuna output
- Better docs for startTunaProxy
- implement similar solution for startTuna