Skip to content

Commit cde034b

Browse files
committed
closes #83
1 parent ec7b5e6 commit cde034b

2 files changed

Lines changed: 49 additions & 8 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
"last-eventemitter": "^1.1.1",
8686
"lodash": "^4.17.21",
8787
"lokijs": "1.5.6",
88+
"mdns": "^2.7.2",
8889
"mkdirp": "^0.5.1",
8990
"moment": "^2.29.4",
9091
"morgan": "^1.10.0",

src/service/service-host.js

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
const CORS = require('cors')
22
const {URL} = require('url')
3+
const mdns = require('mdns')
34
const http = require('http')
45
const https = require('https')
56
const morgan = require('morgan')
67
const express = require('express')
78
const bodyParser = require('body-parser')
89
const expressListRoutes = require('express-list-routes')
910
const debug = require('debug')('dataparty.service.host')
11+
const objectHasher = require('node-object-hash').hasher()
1012

1113
const reach = require('../utils/reach')
1214

@@ -24,17 +26,18 @@ class ServiceHost {
2426
* @class module:Service.ServiceHost
2527
* @link module:Service
2628
* @param {Object} options.cors Cors to be passed to express via the `cors` package
27-
* @param {boolean} options.trust_proxy When true, the server will parse forwarding headers. This should be set when running behind a load-balancer for accurate error messages and logging
28-
* @param {string} options.listenUri The uri of the host interface to tell express to listen on. Defaults to `http://0.0.0.0:4001
29-
* @param {boolean} options.i2pEnabled When true, this server will be available over i2p
30-
* @param {string} options.i2pSamHost The hostname of the i2p SAM control API. Defaults to `127.0.0.1`
31-
* @param {Integer} options.i2pSamPort The port of the i2p SAM control API. Defaults to `7656`
32-
* @param {string} options.i2pForwardHost Override i2p forward host. This defaults to `localhost` and typically doesn't need to be changed
29+
* @param {boolean} [options.trust_proxy=false] When true, the server will parse forwarding headers. This should be set when running behind a load-balancer for accurate error messages and logging
30+
* @param {string} [options.listenUri=http://0.0.0.0:4000] The uri of the host interface to tell express to listen on. Defaults to `http://0.0.0.0:4001
31+
* @param {boolean} [options.i2pEnabled=false] When true, this server will be available over i2p
32+
* @param {string} [options.i2pSamHost=127.0.0.1] The hostname of the i2p SAM control API. Defaults to `127.0.0.1`
33+
* @param {Integer} [options.i2pSamPort=7656] The port of the i2p SAM control API. Defaults to `7656`
34+
* @param {string} [options.i2pForwardHost=localhost] Override i2p forward host. This defaults to `localhost` and typically doesn't need to be changed
3335
* @param {Integer} options.i2pForwardPort Override i2p forward post. This defaults to the port supplied in `options.listenUri`.
3436
* @param {string} options.i2pKey When set this i2p key will be used to host the service. If not set a new i2p key will be generated. Defaults to `null`
35-
* @param {boolean} options.wsEnabled When true the server will host a dataparty websocket service. Defaults to `true`
37+
* @param {boolean} [options.wsEnabled=true] When true the server will host a dataparty websocket service. Defaults to `true`
3638
* @param {Integer} options.wsPort Port for the websocket service to listen on. Defaults to `null`, using the same port as the http server.
37-
* @param {string} options.wsUpgradePath The path within the http server to host an upgradeable websocket. Defaults to `/ws`
39+
* @param {string} [options.wsUpgradePath=/ws] The path within the http server to host an upgradeable websocket. Defaults to `/ws`
40+
* @param {boolean} [options.mdnsEnabled=true] When true, the server will publish mDNS records advertising the service and party identity
3841
* @param {module:Service.ServiceRunner} options.runner A pre-configured runner
3942
*/
4043

@@ -51,6 +54,7 @@ class ServiceHost {
5154
wsEnabled = true,
5255
wsPort = null,
5356
wsUpgradePath = '/ws',
57+
mdnsEnabled = true,
5458
runner
5559
}={}){
5660

@@ -121,6 +125,8 @@ class ServiceHost {
121125
}
122126
}
123127

128+
this.mdnsEnabled = mdnsEnabled
129+
124130
this.started = false
125131
}
126132

@@ -221,6 +227,40 @@ class ServiceHost {
221227
debug('\t', 'address', this.i2pUri)
222228
debug('\t', 'key', this.i2p.getPublicKey())
223229
}
230+
231+
if(this.mdnsEnabled && this.apiServer && this.apiServerUri.protocol != 'file:'){
232+
233+
let servicePkg = null
234+
let partyIdentity = null
235+
const routerClass = this.runner.constructor.name
236+
237+
switch(routerClass){
238+
case 'ServiceRunner':
239+
case 'ServiceRunnerNode':
240+
partyIdentity = this.runner.party.identity
241+
servicePkg = this.runner.service.compiled.package
242+
break
243+
case 'RunnerRouter':
244+
partyIdentity = this.runner.defaultRunner.party.identity
245+
servicePkg = this.runner.defaultRunner.service.compiled.package
246+
break
247+
}
248+
249+
250+
const idHash = objectHasher.hash(
251+
partyIdentity.toJSON()
252+
)
253+
254+
255+
const txt_record = {
256+
partyhash: idHash,
257+
pkgname: servicePkg.name
258+
}
259+
260+
console.log('mdns', servicePkg.name, idHash)
261+
this.mdnsAd = mdns.createAdvertisement(mdns.tcp('party'), parseInt(listenPort), {txtRecord: txt_record})
262+
}
263+
224264
}
225265

226266
/**

0 commit comments

Comments
 (0)