Skip to content

Commit

Permalink
added precheck, fixed many edge cases and updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dskvr committed Nov 28, 2023
1 parent 8f120d0 commit 5b43c33
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 80 deletions.
42 changes: 21 additions & 21 deletions packages/logger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,43 @@ export default class Logger {

constructor(name, log_level="INFO", split_logs=false) {
this.logger = createLogger?.default? createLogger.default(name): createLogger(name)
this.log_level = config.log_level? config.log_level : log_level;
this.log_level = new String(config.log_level? config.log_level : log_level).toUpperCase();
this.split_logs = split_logs || false
}

fatal(message) {
if (['FATAL', 'ERROR', 'WARN', 'INFO', 'DEBUG'].includes(this.log_level)) {
this.logger.error(`FATAL: ${message}`);
this.write(message)
}
if (!['FATAL', 'ERROR', 'WARN', 'INFO', 'DEBUG'].includes(this.log_level))
return
this.logger.error(`FATAL: ${message}`);
this.write(message)
}

err(message) {
if (['ERROR', 'WARN', 'INFO', 'DEBUG'].includes(this.log_level)) {
this.logger.error(message);
this.write(message)
}
if (!['ERROR', 'WARN', 'INFO', 'DEBUG'].includes(this.log_level))
return
this.logger.error(message);
this.write(message)
}

warn(message) {
if (['WARN', 'INFO', 'DEBUG'].includes(this.log_level)) {
this.logger.warn(message);
this.write(message)
}
if (!['WARN', 'INFO', 'DEBUG'].includes(this.log_level))
return
this.logger.warn(message);
this.write(message)
}

info(message) {
if (['INFO', 'DEBUG'].includes(this.log_level)) {
this.logger.info(message);
this.write(message)
}
if (!['INFO', 'DEBUG'].includes(this.log_level))
return
this.logger.info(message);
this.write(message)
}

debug(message) {
if (['DEBUG'].includes(this.log_level)) {
this.logger.debug(message);
this.write(message)
}
if (!['DEBUG'].includes(this.log_level))
return
this.logger.debug(message);
this.write(message)
}

async write(message){
Expand Down
4 changes: 2 additions & 2 deletions packages/nocap/adapters/default/DnsAdapterDefault/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class DnsAdapterDefault {
constructor(parent){
this.$ = parent
}
async check_dns(resolve){
async check_dns(){
if(this.$.results.get('network') !== 'clearnet')
return this.$.logger.warn('DNS check skipped for url not accessible over clearnet')
let err = false
Expand All @@ -20,7 +20,7 @@ class DnsAdapterDefault {
const ipv4 = dns?.Answer?.length ? this.filterIPv4FromDoh(dns) : []
const ipv6 = dns?.Answer?.length ? this.filterIPv6FromDoh(dns) : []
const result = { dns, ipv4, ipv6 }
resolve('dns', result)
this.$.finish('dns', result)
}

filterIPv4FromDoh(jsonData) {
Expand Down
6 changes: 3 additions & 3 deletions packages/nocap/adapters/default/GeoAdapterDefault/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import { fetch } from 'cross-fetch'
this.$ = parent
}

async check_geo(resolve){
async check_geo(){
let err
let endpoint
const ipArr = this.$.results.get('ipv4')
const ip = ipArr[ipArr?.length-1]
if(!ip)
resolve('geo', { geo: { error: 'No IP address found' } })
this.$.finish('geo', { geo: { error: 'No IP address found' }})
if(this.config?.auth?.ip_api_key)
endpoint = `https://pro.ip-api.com/json/${ip}?key=${this.config.auth.ip_api_key}`
else
Expand All @@ -21,7 +21,7 @@ import { fetch } from 'cross-fetch'
if(err) return this.throw(err)
const json = await response.json()
const result = { geo: json }
resolve('geo', result)
this.$.finish('geo', result)
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/nocap/adapters/default/InfoAdapterDefault/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class InfoAdapterDefault {
this.$ = parent
}

async check_info(resolve){
async check_info(){
const controller = new AbortController();
const { signal } = controller;
const url = new URL(this.$.url),
Expand All @@ -20,7 +20,7 @@ class InfoAdapterDefault {
const response = await fetch(url.toString(), { method, headers, signal })
const json = await response.json()
const result = { info: json }
resolve('info', result)
this.$.finish('info', result)
}
catch(e) { return this.$.throw(e) }
}
Expand Down
8 changes: 2 additions & 6 deletions packages/nocap/adapters/default/RelayAdapterDefault/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ class RelayAdapterDefault {
* @private
* @returns promise<result?>
*/
async check_connect(){
if(this.$.isConnected())
this.$.logger.warn('Cannot check connect, already connected')
async check_connect(deferred){
this.$.set('ws', new WebSocket(this.$.url))
this.bind_events()
return this.$.addPromise('connect')
return deferred
}

/**
Expand All @@ -29,7 +27,6 @@ class RelayAdapterDefault {
async check_read(){
let event = JSON.stringify(['REQ', this.$.subid('read'), { limit: 1, kinds: [1] }])
this.$.ws.send(event)
return this.$.addPromise('read')
}

/**
Expand All @@ -41,7 +38,6 @@ class RelayAdapterDefault {
async check_write(){
const ev = JSON.stringify(['EVENT', this.config?.event_sample || this.$.SAMPLE_EVENT])
this.$.ws.send(ev)
return this.$.addPromise('write')
}

/**
Expand Down
5 changes: 2 additions & 3 deletions packages/nocap/adapters/default/SslAdapterDefault/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SslAdapterDefault {
this.$ = parent
}

async check_ssl(resolve){
async check_ssl(){
const url = new URL(this.$.url)
const hostname = url.hostname
const timeout = this.$.config?.ssl_timeout? this.$.config.ssl_timeout: 1000
Expand All @@ -20,8 +20,7 @@ class SslAdapterDefault {
response.validFor.push(await sslValidator.validateSSL(response.cert.pemEncoded, { domain: url.hostname }))
response.validFor = [...new Set(response.validFor)].filter( domain => domain instanceof String && domain !== "" )
const result = { ssl: response }
resolve('ssl', result)

this.$.finish('ssl', result)
}

sslCheckerOptions(port){
Expand Down
Loading

0 comments on commit 5b43c33

Please sign in to comment.