|
| 1 | +import { UnidentifiedEvent } from '../@types/event' |
| 2 | +import { Tag } from '../@types/base' |
| 3 | +import { StoredProbeResult } from '../@types/relay-probe-snapshot' |
| 4 | +import { Settings } from '../@types/settings' |
| 5 | +import { EventKinds, EventTags } from '../constants/base' |
| 6 | + |
| 7 | +const DEFAULT_PROBE_INTERVAL_SECONDS = 3600 |
| 8 | +const MIN_PROBE_INTERVAL_SECONDS = 60 |
| 9 | + |
| 10 | +export const getEffectiveProbeIntervalSeconds = (settings: Settings): number => { |
| 11 | + const configured = settings.nip66?.probeIntervalSeconds ?? DEFAULT_PROBE_INTERVAL_SECONDS |
| 12 | + |
| 13 | + return Math.max(configured, MIN_PROBE_INTERVAL_SECONDS) |
| 14 | +} |
| 15 | + |
| 16 | +const appendDnsProbeTags = (tags: Tag[], dns: StoredProbeResult['dns']): void => { |
| 17 | + if (dns.status === 'skipped') { |
| 18 | + tags.push(['dns', 'skipped']) |
| 19 | + return |
| 20 | + } |
| 21 | + |
| 22 | + if (dns.status === 'error') { |
| 23 | + tags.push(['dns', '!resolved']) |
| 24 | + return |
| 25 | + } |
| 26 | + |
| 27 | + tags.push(['dns', 'resolved']) |
| 28 | +} |
| 29 | + |
| 30 | +const appendTlsProbeTags = (tags: Tag[], tls: StoredProbeResult['tls']): void => { |
| 31 | + if (tls.status === 'skipped') { |
| 32 | + tags.push(['ssl', 'skipped']) |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + if (tls.status === 'error') { |
| 37 | + tags.push(['ssl', '!valid']) |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + const valid = tls.data?.valid === true |
| 42 | + tags.push(['ssl', valid ? 'valid' : '!valid']) |
| 43 | + |
| 44 | + if (tls.data?.expiresAt) { |
| 45 | + const expiresAtSeconds = Math.floor(new Date(tls.data.expiresAt).getTime() / 1000) |
| 46 | + tags.push(['ssl-expires', String(expiresAtSeconds)]) |
| 47 | + } |
| 48 | + |
| 49 | + if (tls.data?.issuer) { |
| 50 | + tags.push(['ssl-issuer', tls.data.issuer]) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +export const normalizeRelayUrlForDTag = (relayUrl: string): string => { |
| 55 | + const parsed = new URL(relayUrl) |
| 56 | + parsed.protocol = parsed.protocol.toLowerCase() |
| 57 | + parsed.hostname = parsed.hostname.toLowerCase() |
| 58 | + |
| 59 | + if ( |
| 60 | + (parsed.protocol === 'wss:' && parsed.port === '443') || |
| 61 | + (parsed.protocol === 'ws:' && parsed.port === '80') |
| 62 | + ) { |
| 63 | + parsed.port = '' |
| 64 | + } |
| 65 | + |
| 66 | + let normalized = parsed.toString() |
| 67 | + |
| 68 | + if ((parsed.pathname === '/' || parsed.pathname === '') && !normalized.endsWith('/')) { |
| 69 | + normalized = `${normalized}/` |
| 70 | + } |
| 71 | + |
| 72 | + return normalized |
| 73 | +} |
| 74 | + |
| 75 | +export const buildRelayDiscoveryEvent = ( |
| 76 | + result: StoredProbeResult, |
| 77 | + monitorPubkey: string, |
| 78 | + createdAt: number, |
| 79 | +): UnidentifiedEvent => { |
| 80 | + const tags: Tag[] = [ |
| 81 | + [EventTags.Deduplication, normalizeRelayUrlForDTag(result.target.relayUrl)], |
| 82 | + ['n', result.target.networkType], |
| 83 | + ] |
| 84 | + |
| 85 | + if (result.wsRtt.status === 'ok' && typeof result.wsRtt.data?.rttOpenMs === 'number') { |
| 86 | + tags.push(['rtt-open', String(result.wsRtt.data.rttOpenMs)]) |
| 87 | + } |
| 88 | + |
| 89 | + appendDnsProbeTags(tags, result.dns) |
| 90 | + appendTlsProbeTags(tags, result.tls) |
| 91 | + |
| 92 | + return { |
| 93 | + kind: EventKinds.RELAY_DISCOVERY, |
| 94 | + pubkey: monitorPubkey, |
| 95 | + created_at: createdAt, |
| 96 | + content: '', |
| 97 | + tags, |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +export const buildMonitorAnnouncementEvent = ( |
| 102 | + settings: Settings, |
| 103 | + monitorPubkey: string, |
| 104 | + createdAt: number, |
| 105 | +): UnidentifiedEvent => { |
| 106 | + const nip66 = settings.nip66 |
| 107 | + const timeouts = nip66?.timeouts |
| 108 | + |
| 109 | + const tags: Tag[] = [ |
| 110 | + ['frequency', String(getEffectiveProbeIntervalSeconds(settings))], |
| 111 | + ['c', 'ws'], |
| 112 | + ['c', 'nip11'], |
| 113 | + ['c', 'ssl'], |
| 114 | + ['c', 'dns'], |
| 115 | + ] |
| 116 | + |
| 117 | + if (timeouts) { |
| 118 | + tags.push(['timeout', 'open', String(timeouts.wsRttMs)]) |
| 119 | + tags.push(['timeout', 'nip11', String(timeouts.nip11Ms)]) |
| 120 | + tags.push(['timeout', 'dns', String(timeouts.dnsMs)]) |
| 121 | + tags.push(['timeout', 'ssl', String(timeouts.tlsMs)]) |
| 122 | + } |
| 123 | + |
| 124 | + return { |
| 125 | + kind: EventKinds.RELAY_MONITOR_ANNOUNCEMENT, |
| 126 | + pubkey: monitorPubkey, |
| 127 | + created_at: createdAt, |
| 128 | + content: '', |
| 129 | + tags, |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +export const buildMonitorProfileEvent = (monitorPubkey: string, createdAt: number): UnidentifiedEvent => { |
| 134 | + return { |
| 135 | + kind: EventKinds.SET_METADATA, |
| 136 | + pubkey: monitorPubkey, |
| 137 | + created_at: createdAt, |
| 138 | + content: JSON.stringify({ |
| 139 | + name: 'Nostream Relay Monitor', |
| 140 | + about: 'Automated NIP-66 relay health monitor for this Nostream instance.', |
| 141 | + }), |
| 142 | + tags: [], |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +export const buildMonitorRelayListEvent = ( |
| 147 | + relayUrl: string, |
| 148 | + monitorPubkey: string, |
| 149 | + createdAt: number, |
| 150 | +): UnidentifiedEvent => { |
| 151 | + return { |
| 152 | + kind: EventKinds.RELAY_LIST, |
| 153 | + pubkey: monitorPubkey, |
| 154 | + created_at: createdAt, |
| 155 | + content: '', |
| 156 | + tags: [ |
| 157 | + [EventTags.Relay, relayUrl, 'read'], |
| 158 | + [EventTags.Relay, relayUrl, 'write'], |
| 159 | + ], |
| 160 | + } |
| 161 | +} |
0 commit comments