Relaying the Hyperswarm DHT over framed streams to bring decentralized networking to everyone
npm install @hyperswarm/dht-relay> :test_tube: This project is still experimental. Do not use it in production.
Relaying the Hyperswarm DHT over framed streams to bring decentralized networking to everyone.
``sh`
npm install @hyperswarm/dht-relay
On the relaying side:
`js
import DHT from 'hyperdht'
import { relay } from '@hyperswarm/dht-relay'
relay(new DHT(), stream)
`
On the relayed side:
`js
import DHT from '@hyperswarm/dht-relay'
const dht = new DHT(stream)
`
From here, the API matches that of the Hyperswarm DHT:
As a convenience, we provide stream wrappers for common transport protocols. These may or may not be appropriate for your particular use case and so your mileage may vary.
TCP
The TCP wrapper is a re-export of
On the relaying side:
`js
import net from 'net'
import DHT from 'hyperdht'
import { relay } from '@hyperswarm/dht-relay'
import Stream from '@hyperswarm/dht-relay/tcp'
const dht = new DHT()
const server = net.createServer().listen(8080)
server.on('connection', (socket) => {
relay(dht, new Stream(false, socket))
})
`
On the relayed side:
`js
import net from 'net'
import DHT from '@hyperswarm/dht-relay'
import Stream from '@hyperswarm/dht-relay/tcp'
const socket = net.connect(8080)
const dht = new DHT(new Stream(true, socket))
`
WebSocket
The WebSocket wrapper is a simple Duplex stream that only adapts the interface of the WebSocket as the WebSocket API already provides its own framing and encryption.
On the relaying side:
`js
import { WebSocketServer } from 'ws'
import DHT from 'hyperdht'
import { relay } from '@hyperswarm/dht-relay'
import Stream from '@hyperswarm/dht-relay/ws'
const dht = new DHT()
const server = new WebSocketServer({ port: 8080 })
server.on('connection', (socket) => {
relay(dht, new Stream(false, socket))
})
`
On the relayed side:
`js
import DHT from '@hyperswarm/dht-relay'
import Stream from '@hyperswarm/dht-relay/ws'
const socket = new WebSocket('ws://localhost:8080')
const dht = new DHT(new Stream(true, socket))
`
You can start a DHT relay in the command line:
`sh`
npm install -g @hyperswarm/dht-relay
Run a DHT relay server:
`sh`
dht-relay # [--port 49443] [--host 0.0.0.0] [--cert
If running behind a proxy like NGINX then add --behind-proxy so logging info is correct.
A reference implementation of the relay protocol can be found in the lib/protocol.js module. The protocol is versioned and built on top of
All types are specified as their corresponding compact-encoding codec.
#### handshake (0)
1. uint8 Flagscustodial
- : 1fixed(32)
2. The public key of the peercustodial
3. (if is set) fixed(64) The secret key
#### ping (1)
_Empty_
#### pong (2)
_Empty_
#### connect (3)
1. uint8 Flagscustodial
- : 1uint32
2. The alias of the streamfixed(32)
3. The public key of the peercustodial
4. (if is set) fixed(64) The secret keyfixed(32)
5. The public key of the remote peer
#### connection (4)
1. uint8 Flagscustodial
- : 1uint32
2. The alias of the streamuint32
3. The alias of the serverfixed(32)
4. The public key of the remote peercustodial
5. (if is set) fixed(64) The Noise handshake hashcustodial
6. (if is not set) uint32 The ID of the Noise handshake session
#### connected (5)
1. uint32 The alias of the streamuint32
2. The remote alias of the stream
#### incoming (6)
1. uint32 The ID of the requestuint32
2. The alias of the serverfixed(32)
3. The public key of the remote peerbuffer
4. The Noise handshake payload
#### deny (7)
1. uint32 The ID of the request
#### accept (8)
1. uint32 The ID of the request
#### destroy (9)
1. uint8 Flagspaired
- : 1error
- : 2paired
2. (if is set) uint32 The alias of the streampaired
2. (if is not set) uint32 The remote alias of the streamerror
3. (if is set) string The reason the stream was destroyed
#### listen (10)
1. uint8 Flagscustodial
- : 1uint32
2. The alias of the serverfixed(32)
3. The public key of the servercustodial
4. (if is set) fixed(64) The secret key
#### listening (11)
1. uint32 The alias of the serveruint32
2. The remote alias of the serveripv4Address
3. [][ipv4address] The address of the server
#### close (12)
1. uint32 The alias of the server
#### closed (13)
1. uint32 The alias of the server
#### open (14)
1. uint8 Flagscustodial
- : 1uint32
2. The alias of the streamuint32
3. The alias of the servercustodial
4. (if is set) fixed(64) The Noise handshake hashcustodial
5. (if is not set) uint32 The ID of the Noise handshake session
#### end (15)
1. uint32 The alias of the stream
#### data (16)
1. uint32 The alias of the streamarray(buffer)
2. The data sent
#### result (17)
1. uint32 The query IDbuffer
2. The query specific data
#### finished (18)
1. uint32 The query ID
#### lookup (19)
1. uint32 The query IDfixed(32)
2. The topic to look up
#### announce (20)
1. uint8 Flagscustodial
- : 1uint32
2. The query IDfixed(32)
3. The topic to announcefixed(32)
4. The public key to announce oncustodial
5. (if is set) fixed(64) The secret key
#### unannounce (21)
1. uint8 Flagscustodial
- : 1uint32
2. The query IDfixed(32)
3. The topic to unannouncefixed(32)
4. The public key that was announced oncustodial
5. (if is set) fixed(64) The secret key
#### signAnnounce (22)
1. uint32 The ID of the requestuint32
2. The alias of the signeefixed(32)
3. The roundtrip token of the peerbuffer
4. The ID of the peerarray(
5. [ipv4Address][ipv4address]) The addresses that may relay messages
#### signUnannounce (23)
1. uint32 The ID of the requestuint32
2. The alias of the signeefixed(32)
3. The roundtrip token of the peerbuffer
4. The ID of the peerarray(
5. [ipv4Address][ipv4address]) The addresses that may relay messages
#### signature (24)
1. uint32 The ID of the requestbuffer
2. The signature
#### noiseSend (25)
1. uint8 FlagsisInitiator
- : 1uint32
2. The ID of the handshake sessionisInitiator
3. (if is set) The alias of the remote streambuffer
4. The Noise handshake payload
#### noiseReceive (26)
1. uint8 FlagsisInitiator
- : 1uint32
2. The ID of the handshake sessionisInitiator
3. (if is not set) The alias of the serverbuffer
4. The Noise handshake payload
#### noiseReply (27)
1. uint8 FlagsisInitiator
- : 1complete
- : 2uint32
2. The ID of the handshake sessionbuffer
3. The Noise handshake payloadisInitiator
4. (if and complete are not set) fixed(32) The public key of the remote peercomplete
5. (if is set) fixed(32) The ID of the remote streamcomplete
6. (if is set) fixed(32)` The holepunch secret
ISC
[ipv4address]: https://github.com/compact-encoding/compact-encoding-net#ipv4address