fix members.add()

This commit is contained in:
Andre Staltz 2023-06-21 16:32:09 +03:00
parent 80f2a3afb0
commit 9998885daf
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
3 changed files with 16 additions and 18 deletions

View File

@ -1,9 +1,7 @@
const Crypto = require('node:crypto')
const Path = require('node:path') const Path = require('node:path')
const AtomicFileRW = require('atomic-file-rw') const AtomicFileRW = require('atomic-file-rw')
const Base58 = require('bs58')
class Tokens { class Members {
static #filePath static #filePath
/** /**
@ -34,38 +32,36 @@ class Tokens {
} }
const json = typeof buf === 'string' ? buf : buf.toString('utf-8') const json = typeof buf === 'string' ? buf : buf.toString('utf-8')
const arr = JSON.parse(json) const arr = JSON.parse(json)
for (const token of arr) { for (const pubkey of arr) {
this.#set.add(token) this.#set.add(pubkey)
} }
this.#loaded = true this.#loaded = true
}) })
} }
/** /**
* @param {string} token * @param {string} pubkey
* @returns {boolean} * @returns {boolean}
*/ */
static has(token) { static has(pubkey) {
if (!this.#loaded) { if (!this.#loaded) {
throw new Error('Members not loaded yet, cannot call has()') throw new Error('Members not loaded yet, cannot call has()')
} }
return this.#set.has(token) return this.#set.has(pubkey)
} }
static create() { /**
* @param {string} pubkey
*/
static add(pubkey) {
if (!this.#loaded) { if (!this.#loaded) {
throw new Error('Members not loaded yet, cannot call create()') throw new Error('Members not loaded yet, cannot call create()')
} }
let token this.#set.add(pubkey)
do {
token = Base58.encode(Crypto.randomBytes(32))
} while (this.#set.has(token))
this.#set.add(token)
this.#save((err, _) => { this.#save((err, _) => {
if (err) console.warn('Problem saving members file:', err) if (err) console.warn('Problem saving members file:', err)
}) })
return token
} }
} }
module.exports = Tokens module.exports = Members

View File

@ -45,13 +45,15 @@ module.exports = {
const [clientMetadata, cb] = args const [clientMetadata, cb] = args
const {pubkey, extra} = clientMetadata const {pubkey, extra} = clientMetadata
if (Members.has(pubkey)) { if (Members.has(pubkey)) {
debug('authorized member %s to connect', pubkey)
cb(null, true) cb(null, true)
} else if (extra && Tokens.has(extra)) { } else if (extra && Tokens.has(extra)) {
debug('authorized NEW member %s to connect', pubkey)
Tokens.delete(extra) Tokens.delete(extra)
Members.add(pubkey) Members.add(pubkey)
cb(null, true) cb(null, true)
} else { } else {
debug('prevented stranger %s from connecting to us', pubkey) debug('denied stranger %s from connecting', pubkey)
cb(new Error('client is a stranger')) cb(new Error('client is a stranger'))
} }
}) })

View File

@ -32,7 +32,7 @@ class Tokens {
} }
return return
} }
const json = typeof buf === 'string' ? buf : Buffer.toString(buf, 'utf-8') const json = typeof buf === 'string' ? buf : buf.toString('utf-8')
const arr = JSON.parse(json) const arr = JSON.parse(json)
for (const token of arr) { for (const token of arr) {
this.#set.add(token) this.#set.add(token)