fix(RelayInfo): fixed handling relays with outdated certificates

This commit is contained in:
Yury 2024-05-27 17:46:06 +03:00
parent eba2472f2f
commit 7322f7c7f5

View File

@ -2,6 +2,7 @@ import { NDKEvent } from '@nostr-dev-kit/ndk'
import { log } from '../main.js'
import axios from 'axios'
import { RelayInfo } from '../types/index.js'
import https from 'https'
export const relayInfoJob = async (event: NDKEvent): Promise<string> => {
log('New relay-info job', event.rawEvent())
@ -22,8 +23,15 @@ export const relayInfoJob = async (event: NDKEvent): Promise<string> => {
}
const prefixes = { wss: 'wss://', https: 'https://', http: 'http://' }
const headers = {
headers: { Accept: 'application/nostr+json' }
// Relay may have outdated certificate 'rejectUnauthorized: false' will prevent
// throwing ann error on request
const agent = new https.Agent({
rejectUnauthorized: false
})
const config = {
headers: { Accept: 'application/nostr+json' },
httpsAgent: agent
}
const requests = relays.map((relay) => {
@ -32,12 +40,12 @@ export const relayInfoJob = async (event: NDKEvent): Promise<string> => {
}
return axios
.get<RelayInfo>((relay = `${prefixes.https}${relay}`), headers)
.catch(() => {
return axios
.get<RelayInfo>(`${prefixes.http}${relay}`, headers)
.get<RelayInfo>((relay = `${prefixes.https}${relay}`), config)
.catch(() =>
axios
.get<RelayInfo>(`${prefixes.http}${relay}`, config)
.catch(() => undefined)
})
)
})
let responses = await Promise.all(requests)