From fee393ae83d884a65cb3bf77d5a07a06860ccf77 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Wed, 15 Jul 2020 12:01:38 -0400 Subject: [PATCH 1/9] avoid duplicate alias sign up --- services/gunDB/Mediator/index.js | 39 ++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/services/gunDB/Mediator/index.js b/services/gunDB/Mediator/index.js index f8fc73ec..533b57cb 100644 --- a/services/gunDB/Mediator/index.js +++ b/services/gunDB/Mediator/index.js @@ -1275,6 +1275,38 @@ const register = async (alias, pass) => { ) } + // this import is done here to avoid circular dependency hell + const { timeout5 } = require('../contact-api/utils') + + let userData = await timeout5( + new Promise(res => { + gun.get(`~@${alias}`).once(ud => res(ud)) + }) + ) + + if (userData) { + throw new Error( + 'The given alias has been used before, use an unique alias instead.' + ) + } + + await new Promise(res => setTimeout(res, 300)) + + userData = await timeout5( + new Promise(res => { + gun.get(`~@${alias}`).once(ud => res(ud), { + // https://github.com/amark/gun/pull/971#issue-438630761 + wait: 1500 + }) + }) + ) + + if (userData) { + throw new Error( + 'The given alias has been used before, use an unique alias instead. (Caught at 2nd try)' + ) + } + _isRegistering = true /** @type {import('../contact-api/SimpleGUN').CreateAck} */ @@ -1282,6 +1314,13 @@ const register = async (alias, pass) => { user.create(alias, pass, ack => res(ack)) ) + // An empty ack object seems to be caused by a duplicate alias sign up + if ('{}' === JSON.stringify(ack)) { + throw new Error( + 'The given alias has been used before, use an unique alias instead. (Empty ack)' + ) + } + _isRegistering = false if (typeof ack.err === 'string') { From 6708d1622f2d84ed82f51c230be71e7069785fe1 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Thu, 16 Jul 2020 14:03:44 -0400 Subject: [PATCH 2/9] export super peer as constant --- config/defaults.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/config/defaults.js b/config/defaults.js index d30c9ad4..4443c21f 100644 --- a/config/defaults.js +++ b/config/defaults.js @@ -25,6 +25,8 @@ const parsePath = (filePath = "") => { const lndDirectory = getLndDirectory(); +const SHOCK_SUPER_PEER = "http://gun.shock.network:8765/gun" + module.exports = (mainnet = false) => { const network = mainnet ? "mainnet" : "testnet"; @@ -48,8 +50,10 @@ module.exports = (mainnet = false) => { logfile: "shockapi.log", lndLogFile: parsePath(`${lndDirectory}/logs/bitcoin/${network}/lnd.log`), lndDirPath: lndDirectory, - peers: ["http://gun.shock.network:8765/gun"], + peers: [SHOCK_SUPER_PEER], useTLS: false, tokenExpirationMS: 4500000 }; }; + +module.exports.SHOCK_SUPER_PEER = SHOCK_SUPER_PEER \ No newline at end of file From 58d0fd71e912c485e61eef55b805349c443b89ea Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Thu, 16 Jul 2020 14:05:30 -0400 Subject: [PATCH 3/9] check super peer conn for dup alias check --- services/gunDB/Mediator/index.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/services/gunDB/Mediator/index.js b/services/gunDB/Mediator/index.js index 533b57cb..89f7810b 100644 --- a/services/gunDB/Mediator/index.js +++ b/services/gunDB/Mediator/index.js @@ -12,8 +12,10 @@ require('gun/lib/open') // @ts-ignore require('gun/lib/load') const debounce = require('lodash/debounce') -const Encryption = require('../../../utils/encryptionStore') +const Encryption = require('../../../utils/encryptionStore') +const { SHOCK_SUPER_PEER } = require('../../../config/defaults') +const { PEERS } = require('../config') const Key = require('../contact-api/key') /** @type {import('../contact-api/SimpleGUN').ISEA} */ @@ -1275,6 +1277,25 @@ const register = async (alias, pass) => { ) } + const shocknetPeerInUse = PEERS.includes(SHOCK_SUPER_PEER) + + if (shocknetPeerInUse) { + /** + * @type {Record} + */ + // @ts-ignore + const currPeers = gun._.opt.peers + // This is a very basic test, only checks that the websocket is there but + // doesn't necessarily mean it is connected or that super peer is UP + const shocknetPeerConnected = !!currPeers[SHOCK_SUPER_PEER].wire + + if (!shocknetPeerConnected) { + throw new Error( + `API Must be connected to super peer to check for duplicate aliases` + ) + } + } + // this import is done here to avoid circular dependency hell const { timeout5 } = require('../contact-api/utils') From 8123042741510732db7843680968b148930d0f80 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Fri, 17 Jul 2020 13:38:37 -0400 Subject: [PATCH 4/9] typings --- services/gunDB/config.js | 4 +++- services/gunDB/contact-api/SimpleGUN.ts | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/services/gunDB/config.js b/services/gunDB/config.js index 355581a1..44dab137 100644 --- a/services/gunDB/config.js +++ b/services/gunDB/config.js @@ -11,7 +11,9 @@ dotenv.config() // @ts-ignore Let it crash if undefined exports.DATA_FILE_NAME = process.env.DATA_FILE_NAME || defaults.dataFileName -// @ts-ignore Let it crash if undefined +/** + * @type {string[]} + */ exports.PEERS = process.env.PEERS ? JSON.parse(process.env.PEERS) : defaults.peers diff --git a/services/gunDB/contact-api/SimpleGUN.ts b/services/gunDB/contact-api/SimpleGUN.ts index d7e54313..1cef7225 100644 --- a/services/gunDB/contact-api/SimpleGUN.ts +++ b/services/gunDB/contact-api/SimpleGUN.ts @@ -30,9 +30,18 @@ interface OpenListenerDataObj { export type Listener = (data: ListenerData, key: string) => void export type Callback = (ack: Ack) => void +export interface Peer { + url: string + id: string + wire: Record +} + export interface Soul { get: string put: Primitive | null | object | undefined + opt: { + peers: Record + } } export type OpenListenerData = Primitive | null | OpenListenerDataObj From e56942123677ca485df708c41c5e89caf7266533 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Fri, 17 Jul 2020 13:44:26 -0400 Subject: [PATCH 5/9] envvar for disabling dup alias check via connected peers --- .env.example | 3 ++- services/gunDB/config.js | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.env.example b/.env.example index 4ea11d88..ad9e0eb7 100644 --- a/.env.example +++ b/.env.example @@ -3,4 +3,5 @@ PEERS=["http://gun.shock.network:8765/gun"] MS_TO_TOKEN_EXPIRATION=4500000 DISABLE_SHOCK_ENCRYPTION=false CACHE_HEADERS_MANDATORY=true -SHOCK_CACHE=true \ No newline at end of file +SHOCK_CACHE=true +DISABLE_PEER_ALIAS_CHECK = false diff --git a/services/gunDB/config.js b/services/gunDB/config.js index 44dab137..424cc53c 100644 --- a/services/gunDB/config.js +++ b/services/gunDB/config.js @@ -23,3 +23,6 @@ exports.MS_TO_TOKEN_EXPIRATION = Number( ) exports.SHOW_LOG = process.env.SHOW_GUN_DB_LOG === 'true' + +exports.DISABLE_PEER_ALIAS_CHECK = + process.env.DISABLE_PEER_ALIAS_CHECK === 'true' From 35553a93b6125e040c1a914b5f9a5930b7f4505f Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Fri, 17 Jul 2020 14:00:52 -0400 Subject: [PATCH 6/9] better dup alias check --- config/defaults.js | 2 -- services/gunDB/Mediator/index.js | 34 +++++++++++++++++++++----------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/config/defaults.js b/config/defaults.js index 4443c21f..37cae17a 100644 --- a/config/defaults.js +++ b/config/defaults.js @@ -55,5 +55,3 @@ module.exports = (mainnet = false) => { tokenExpirationMS: 4500000 }; }; - -module.exports.SHOCK_SUPER_PEER = SHOCK_SUPER_PEER \ No newline at end of file diff --git a/services/gunDB/Mediator/index.js b/services/gunDB/Mediator/index.js index 89f7810b..92f48045 100644 --- a/services/gunDB/Mediator/index.js +++ b/services/gunDB/Mediator/index.js @@ -12,10 +12,10 @@ require('gun/lib/open') // @ts-ignore require('gun/lib/load') const debounce = require('lodash/debounce') +const size = require('lodash/size') const Encryption = require('../../../utils/encryptionStore') -const { SHOCK_SUPER_PEER } = require('../../../config/defaults') -const { PEERS } = require('../config') +const { DISABLE_PEER_ALIAS_CHECK } = require('../config') const Key = require('../contact-api/key') /** @type {import('../contact-api/SimpleGUN').ISEA} */ @@ -1277,23 +1277,33 @@ const register = async (alias, pass) => { ) } - const shocknetPeerInUse = PEERS.includes(SHOCK_SUPER_PEER) - - if (shocknetPeerInUse) { + if (DISABLE_PEER_ALIAS_CHECK) { + logger.warn(`DISABLE_PEER_ALIAS_CHECK true, use only for testing purposes`) + } else { /** - * @type {Record} + * Peers provided to gun. */ - // @ts-ignore const currPeers = gun._.opt.peers - // This is a very basic test, only checks that the websocket is there but - // doesn't necessarily mean it is connected or that super peer is UP - const shocknetPeerConnected = !!currPeers[SHOCK_SUPER_PEER].wire - if (!shocknetPeerConnected) { + if (size(currPeers) === 0) { + logger.info( + `Unexpected: Duplicate alias check enabled but Gun has no peers set. If you're testing gun without peers set DISABLE_PEER_ALIAS_CHECK to true.` + ) throw new Error( - `API Must be connected to super peer to check for duplicate aliases` + `Unexpected: Duplicate alias check enabled but Gun has no peers set.` ) } + + /** + * Of those, how many are actually connected + */ + const connectedPeers = Object.values(currPeers).filter(p => !!p.wire).length + + if (connectedPeers === 0) { + throw new Error( + `No connected peers, therefore cannot check for duplicate aliases.` + ) + } // else it's connected to at least one } // this import is done here to avoid circular dependency hell From ef213ee2dc7d3de0867810864b972a670223cede Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Fri, 17 Jul 2020 14:18:55 -0400 Subject: [PATCH 7/9] better error catch --- src/routes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes.js b/src/routes.js index 73222778..032cf5c3 100644 --- a/src/routes.js +++ b/src/routes.js @@ -713,7 +713,7 @@ module.exports = async ( logger.error(err); return res.status(500).json({ field: "unknown", - errorMessage: err + errorMessage: err.message || err }) } }); From bc03211ecc89231e401dfc2ce808d2fa9b5f3cca Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Fri, 17 Jul 2020 14:39:28 -0400 Subject: [PATCH 8/9] throw if connected to peers and peer dup alias check is disabled --- services/gunDB/Mediator/index.js | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/services/gunDB/Mediator/index.js b/services/gunDB/Mediator/index.js index 92f48045..5a2c06ff 100644 --- a/services/gunDB/Mediator/index.js +++ b/services/gunDB/Mediator/index.js @@ -1277,14 +1277,25 @@ const register = async (alias, pass) => { ) } - if (DISABLE_PEER_ALIAS_CHECK) { - logger.warn(`DISABLE_PEER_ALIAS_CHECK true, use only for testing purposes`) - } else { - /** - * Peers provided to gun. - */ - const currPeers = gun._.opt.peers + /** + * Peers provided to gun. + */ + const currPeers = gun._.opt.peers + if (DISABLE_PEER_ALIAS_CHECK) { + if (size(currPeers)) { + logger.info( + `Unexpected: disabled peer alias check while peers were specified.` + ) + throw new Error( + `Unexpected: disabled peer alias check while peers were specified.` + ) + } + + logger.warn( + `DISABLE_PEER_ALIAS_CHECK true, use only for testing purposes with no peers connected` + ) + } else { if (size(currPeers) === 0) { logger.info( `Unexpected: Duplicate alias check enabled but Gun has no peers set. If you're testing gun without peers set DISABLE_PEER_ALIAS_CHECK to true.` From b44fd99e56cb9debb955c3584c27cf96bfe4a289 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Fri, 17 Jul 2020 15:36:55 -0400 Subject: [PATCH 9/9] simpler dup alias check --- .env.example | 1 - services/gunDB/Mediator/index.js | 94 +++++++++---------------- services/gunDB/config.js | 3 - services/gunDB/contact-api/SimpleGUN.ts | 4 +- 4 files changed, 38 insertions(+), 64 deletions(-) diff --git a/.env.example b/.env.example index ad9e0eb7..65c31d1c 100644 --- a/.env.example +++ b/.env.example @@ -4,4 +4,3 @@ MS_TO_TOKEN_EXPIRATION=4500000 DISABLE_SHOCK_ENCRYPTION=false CACHE_HEADERS_MANDATORY=true SHOCK_CACHE=true -DISABLE_PEER_ALIAS_CHECK = false diff --git a/services/gunDB/Mediator/index.js b/services/gunDB/Mediator/index.js index 5a2c06ff..bfd8764b 100644 --- a/services/gunDB/Mediator/index.js +++ b/services/gunDB/Mediator/index.js @@ -12,10 +12,8 @@ require('gun/lib/open') // @ts-ignore require('gun/lib/load') const debounce = require('lodash/debounce') -const size = require('lodash/size') const Encryption = require('../../../utils/encryptionStore') -const { DISABLE_PEER_ALIAS_CHECK } = require('../config') const Key = require('../contact-api/key') /** @type {import('../contact-api/SimpleGUN').ISEA} */ @@ -1280,73 +1278,51 @@ const register = async (alias, pass) => { /** * Peers provided to gun. */ - const currPeers = gun._.opt.peers + const peers = Object.values(gun._.opt.peers) - if (DISABLE_PEER_ALIAS_CHECK) { - if (size(currPeers)) { - logger.info( - `Unexpected: disabled peer alias check while peers were specified.` - ) - throw new Error( - `Unexpected: disabled peer alias check while peers were specified.` - ) - } - - logger.warn( - `DISABLE_PEER_ALIAS_CHECK true, use only for testing purposes with no peers connected` - ) - } else { - if (size(currPeers) === 0) { - logger.info( - `Unexpected: Duplicate alias check enabled but Gun has no peers set. If you're testing gun without peers set DISABLE_PEER_ALIAS_CHECK to true.` - ) - throw new Error( - `Unexpected: Duplicate alias check enabled but Gun has no peers set.` - ) - } - - /** - * Of those, how many are actually connected - */ - const connectedPeers = Object.values(currPeers).filter(p => !!p.wire).length - - if (connectedPeers === 0) { - throw new Error( - `No connected peers, therefore cannot check for duplicate aliases.` - ) - } // else it's connected to at least one - } - - // this import is done here to avoid circular dependency hell - const { timeout5 } = require('../contact-api/utils') - - let userData = await timeout5( - new Promise(res => { - gun.get(`~@${alias}`).once(ud => res(ud)) - }) + const theresPeers = peers.length > 0 + const atLeastOneIsConnected = peers.some( + p => p.wire && p.wire.readyState === 1 ) - if (userData) { + if (theresPeers && !atLeastOneIsConnected) { throw new Error( - 'The given alias has been used before, use an unique alias instead.' + 'No connected to any peers for checking of duplicate aliases' ) } - await new Promise(res => setTimeout(res, 300)) + if (theresPeers && atLeastOneIsConnected) { + // this import is done here to avoid circular dependency hell + const { timeout5 } = require('../contact-api/utils') - userData = await timeout5( - new Promise(res => { - gun.get(`~@${alias}`).once(ud => res(ud), { - // https://github.com/amark/gun/pull/971#issue-438630761 - wait: 1500 + let userData = await timeout5( + new Promise(res => { + gun.get(`~@${alias}`).once(ud => res(ud)) }) - }) - ) - - if (userData) { - throw new Error( - 'The given alias has been used before, use an unique alias instead. (Caught at 2nd try)' ) + + if (userData) { + throw new Error( + 'The given alias has been used before, use an unique alias instead.' + ) + } + + await new Promise(res => setTimeout(res, 300)) + + userData = await timeout5( + new Promise(res => { + gun.get(`~@${alias}`).once(ud => res(ud), { + // https://github.com/amark/gun/pull/971#issue-438630761 + wait: 1500 + }) + }) + ) + + if (userData) { + throw new Error( + 'The given alias has been used before, use an unique alias instead. (Caught at 2nd try)' + ) + } } _isRegistering = true diff --git a/services/gunDB/config.js b/services/gunDB/config.js index 424cc53c..44dab137 100644 --- a/services/gunDB/config.js +++ b/services/gunDB/config.js @@ -23,6 +23,3 @@ exports.MS_TO_TOKEN_EXPIRATION = Number( ) exports.SHOW_LOG = process.env.SHOW_GUN_DB_LOG === 'true' - -exports.DISABLE_PEER_ALIAS_CHECK = - process.env.DISABLE_PEER_ALIAS_CHECK === 'true' diff --git a/services/gunDB/contact-api/SimpleGUN.ts b/services/gunDB/contact-api/SimpleGUN.ts index 1cef7225..3a9cd77b 100644 --- a/services/gunDB/contact-api/SimpleGUN.ts +++ b/services/gunDB/contact-api/SimpleGUN.ts @@ -33,7 +33,9 @@ export type Callback = (ack: Ack) => void export interface Peer { url: string id: string - wire: Record + wire?: { + readyState: number + } } export interface Soul {