-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose a register-alias service (#3)
* Partial implementation * More experiments * Use dht-prom-client scraper * (WIP) put rpc-client in dht-prom-client * Rm unused comments * Use published dht-prom-client * rm log
- Loading branch information
Showing
5 changed files
with
150 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
const ReadyResource = require('ready-resource') | ||
const RPC = require('protomux-rpc') | ||
const crypto = require('crypto') | ||
const b4a = require('b4a') | ||
|
||
// TODO: cleaner dep management (don't want to store the encodings | ||
// twice, but there's a ciruclar dep of sorts) | ||
const { AliasReqEnc, AliasRespEnc } = require('dht-prom-client/lib/encodings') | ||
|
||
const PROTOCOL_NAME = 'register-alias' | ||
|
||
class AliasRpcServer extends ReadyResource { | ||
constructor (swarm, secret, putAliasCb) { | ||
super() | ||
|
||
this.swarm = swarm | ||
this._putAlias = putAliasCb | ||
this.secret = secret | ||
|
||
this.swarm.on('connection', this._onconnection.bind(this)) | ||
} | ||
|
||
get publicKey () { | ||
return this.swarm.keyPair.pubicKey | ||
} | ||
|
||
async _open () { | ||
await this.swarm.listen() | ||
} | ||
|
||
async _close () { | ||
await this.swarm.destroy() | ||
} | ||
|
||
_onconnection (socket) { | ||
const uid = crypto.randomUUID() | ||
const remotePublicKey = socket.remotePublicKey | ||
|
||
socket.on('error', (error) => { | ||
this.emit('socket-error', { error, uid, remotePublicKey }) | ||
}) | ||
|
||
const rpc = new RPC(socket, { protocol: PROTOCOL_NAME }) | ||
rpc.respond( | ||
'alias', | ||
{ responseEncoding: AliasRespEnc, requestEncoding: AliasReqEnc }, | ||
async (req) => { | ||
if (!b4a.equals(req.secret, this.secret)) { | ||
return { success: false, errorMessage: 'unauthorised' } | ||
} | ||
|
||
const targetPublicKey = req.targetPublicKey | ||
const alias = req.alias | ||
|
||
this.emit('alias-request', { uid, remotePublicKey, targetPublicKey, alias }) | ||
try { | ||
const updated = await this._putAlias(alias, targetPublicKey) | ||
this.emit('register-success', { uid, alias, targetPublicKey, updated }) | ||
return { | ||
success: true, | ||
updated | ||
} | ||
} catch (error) { | ||
this.emit('register-error', { error, uid }) | ||
return { | ||
success: false, | ||
errorMessage: `Failed to register alias (uid ${uid})` | ||
} | ||
} | ||
} | ||
) | ||
} | ||
} | ||
|
||
module.exports = AliasRpcServer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters