From a14ec889a49c1971eed1c038cb9c3a37a8064c46 Mon Sep 17 00:00:00 2001 From: dskvr Date: Tue, 23 Jul 2024 10:54:59 +0200 Subject: [PATCH] fix nip-11 for non-wss endpoints --- apps/nocapd/package.json | 2 +- apps/nocapd/src/classes/Worker.js | 2 +- .../default/DnsAdapterDefault/index.js | 7 ++-- .../default/GeoAdapterDefault/index.js | 1 - .../default/InfoAdapterDefault/index.js | 34 ++++++++++++++----- .../default/SslAdapterDefault/index.js | 16 ++++++--- package.json | 2 +- 7 files changed, 43 insertions(+), 21 deletions(-) diff --git a/apps/nocapd/package.json b/apps/nocapd/package.json index 59f88f90..f1a6f635 100644 --- a/apps/nocapd/package.json +++ b/apps/nocapd/package.json @@ -10,7 +10,7 @@ "@nostrwatch/controlflow": "0.2.1", "@nostrwatch/logger": "0.0.6", "@nostrwatch/nocap": "0.5.2", - "@nostrwatch/nocap-every-adapter-default": "1.3.2", + "@nostrwatch/nocap-every-adapter-default": "1.4.0", "@nostrwatch/nwcache": "0.1.2", "@nostrwatch/publisher": "0.4.3", "@nostrwatch/seed": "0.0.2", diff --git a/apps/nocapd/src/classes/Worker.js b/apps/nocapd/src/classes/Worker.js index 10490c8d..7607fdb2 100644 --- a/apps/nocapd/src/classes/Worker.js +++ b/apps/nocapd/src/classes/Worker.js @@ -100,7 +100,7 @@ export class NWWorker { const failure = (err) => { this.log.err(`Could not run ${this.pubkey} check for ${job.data.relay}: ${err.message}`) } try { const { relay:url } = job.data - const nocap = new Nocap(url, this.nocapOpts) + const nocap = new Nocap(url, {...this.nocapOpts, logLevel: 'debug'}) await nocap.useAdapters([...adaptersArray]) const result = await nocap.check(this.opts.checks.enabled).catch(failure) return { result } diff --git a/libraries/nocap/adapters/default/DnsAdapterDefault/index.js b/libraries/nocap/adapters/default/DnsAdapterDefault/index.js index 1cbe2597..7d3921d8 100644 --- a/libraries/nocap/adapters/default/DnsAdapterDefault/index.js +++ b/libraries/nocap/adapters/default/DnsAdapterDefault/index.js @@ -10,9 +10,10 @@ class DnsAdapterDefault { } async check_dns(){ let result = {}, data = {} - if(this.$.results.get('network') !== 'clearnet') - return this.$.logger.debug('DNS check skipped for url not accessible over clearnet') - let err = false + if(this.$.results.get('network') !== 'clearnet') { + this.$.logger.debug('DNS check skipped for url not accessible over clearnet') + return { status: "error", message: "Relay is not clearnet, cannot check DNS." } + } let Url = new URL(this.$.url) let url = `${Url.protocol}//${Url.host}`.replace('wss://', '').replace('ws://', '').replace(/\/+$/, ''); const query = `https://1.1.1.1/dns-query?name=${url}` diff --git a/libraries/nocap/adapters/default/GeoAdapterDefault/index.js b/libraries/nocap/adapters/default/GeoAdapterDefault/index.js index ee4855d6..02397a38 100644 --- a/libraries/nocap/adapters/default/GeoAdapterDefault/index.js +++ b/libraries/nocap/adapters/default/GeoAdapterDefault/index.js @@ -32,7 +32,6 @@ const IPV4 = /\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01 } else { const iparr = this.$.results.get('dns')?.data?.ipv4 - // console.log(iparr) ip = iparr[iparr.length-1] } const apiKey = this.getApiKey(); diff --git a/libraries/nocap/adapters/default/InfoAdapterDefault/index.js b/libraries/nocap/adapters/default/InfoAdapterDefault/index.js index 2323ac1f..afb60175 100644 --- a/libraries/nocap/adapters/default/InfoAdapterDefault/index.js +++ b/libraries/nocap/adapters/default/InfoAdapterDefault/index.js @@ -9,25 +9,41 @@ class InfoAdapterDefault { async check_info(){ let result, data = {} - const controller = new AbortController(); - const { signal } = controller; - const url = new URL(this.$.url), + const controller = new AbortController(), + { signal } = controller, + url = new URL(this.$.url), headers = {"Accept": "application/nostr+json"}, method = 'GET' - - url.protocol = 'https:' - // this.$.timeouts.create('info', this.$.config.timeout.info, () => controller.abort()) - try { + if( this.$.results.get('network') === 'tor' ) + { + url.protocol = 'onion:'; + } + else if(url.protocol === 'ws:') + { + url.protocol = 'http:'; + } + else if (url.protocol === 'wss:') + { + url.protocol = 'https:'; + } + + try + { const response = await fetch(url.toString(), { method, headers, signal }) data = await response.json() } - catch(e) { + + catch(e) + { result = { status: "error", message: e.message, data } } - if(!result) + if(!result) + { result = { status: "success", data } + } + // else { // // const validate = this.ajv.compile(data) // // const valid = validate(data) diff --git a/libraries/nocap/adapters/default/SslAdapterDefault/index.js b/libraries/nocap/adapters/default/SslAdapterDefault/index.js index 53d21c72..892557a5 100644 --- a/libraries/nocap/adapters/default/SslAdapterDefault/index.js +++ b/libraries/nocap/adapters/default/SslAdapterDefault/index.js @@ -4,10 +4,20 @@ class SslAdapterDefault { } async check_ssl(){ + if(typeof window !== 'undefined') { console.warn('Cannot check SSL from browser.'); return; } + + let result, data = {}; + const url = new URL(this.$.url); + const hostname = url.hostname; + const timeout = this.$.config?.timeout.ssl || 1000; + + if(url.protocol === 'ws:'){ + return this.$.finish('ssl', { status: "error", message: "Cannot check SSL for unsecured websocket.", data: {} }); + } let sslCertificate, sslChecker; try { @@ -22,11 +32,7 @@ class SslAdapterDefault { return; } - let result, data = {}; - const url = new URL(this.$.url); - const hostname = url.hostname; - const timeout = this.$.config?.timeout.ssl || 1000; - + let sslCheckerResponse, sslCertificateResponse; try { // Call Node.js specific functions only if available diff --git a/package.json b/package.json index 14273ca3..40d658de 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@nostrwatch/monorepo", "private": true, - "version": "0.0.1", + "version": "0.2.0", "scripts": { "packages:update": "node ./scripts/upkg.js", "deploy:nocapd@all": "ansible-playbook .ansible/nocapd/deploy.yaml -i .ansible/inventories/frankfurt -i .ansible/inventories/johannesburg -i .ansible/inventories/amsterdam -i .ansible/inventories/mumbai -i .ansible/inventories/newyork -i .ansible/inventories/saopaulo -i .ansible/inventories/seoul -i .ansible/inventories/siliconvalley -i .ansible/inventories/sydney",