-
Notifications
You must be signed in to change notification settings - Fork 3
/
nostr.ps1
201 lines (184 loc) · 6.02 KB
/
nostr.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
function ConvertTo-UnixTime {
param(
[Parameter(ValueFromPipeline)]
[datetime]$Date = [datetime]::Now
)
process{
[int]((New-TimeSpan -Start (Get-Date -Date '1970-01-01') -End (($Date).ToUniversalTime())).TotalSeconds)
}
}
function New-NostrFilterString {
[CmdletBinding()]
param(
[string[]]$ids,
[string[]]$authors,
[int[]]$kinds,
[string]$etag,
[string]$ptag,
[int]$limit,
[datetime]$since,
[datetime]$until,
$relay
)
$filters = [ordered]@{}
switch($PSBoundParameters.Keys){
'ids' {$filters.add('ids',$ids)}
'authors' {$filters.add('authors',$authors)}
'kinds' {$filters.add('kinds',$kinds)}
'etag' {$filters.add('#e',$etag)}
'ptag' {$filters.add('#p',$ptag)}
'limit' {$filters.add('limit',$limit)}
'since' {$filters.add('since',($since | ConvertTo-UnixTime))}
'until' {$filters.add('until',($until | ConvertTo-UnixTime))}
}
$nosQ = @(
"REQ",
"nostrps",
$filters
)
ConvertTo-Json -InputObject $nosQ -Compress
}
function New-NostrRelayConnection {
[CmdletBinding()]
param(
$URL
)
$WS = New-Object System.Net.WebSockets.ClientWebSocket
$CT = New-Object System.Threading.CancellationToken
$WS.Options.UseDefaultCredentials = $true
#Get connected
$Conn = $WS.ConnectAsync($URL, $CT)
While (!$Conn.IsCompleted) {
Start-Sleep -Milliseconds 100
}
Write-Verbose "Connected to $($URL)"
return $ws
}
function Send-NostrRequest {
[CmdletBinding()]
param(
$WebSocket,
$QueryString
)
$Command = [System.Text.Encoding]::UTF8.GetBytes($QueryString)
$Send = New-Object System.ArraySegment[byte] -ArgumentList @(,$Command)
$CT = New-Object System.Threading.CancellationToken
$Conn = $WebSocket.SendAsync($Send, [System.Net.WebSockets.WebSocketMessageType]::Text, $true, $CT)
While (!$Conn.IsCompleted) {
Start-Sleep -Milliseconds 100
}
Write-Verbose "Sent Request $QueryString"
return $conn
}
function Get-NostrEvent {
[CmdletBinding()]
param (
[Parameter()]
#[ValidateSet('wss://eden.nostr.land','wss://relay.snort.social','wss://relay.damus.io','wss://relay.nostr.info')]
[string[]]$Relay = 'wss://eden.nostr.land',
$Ids,
$Kinds,
$Authors,
$Limit,
$Since,
$Until
)
begin {
$filter = New-NostrFilterString @PSBoundParameters
}
process {
foreach($rUri in $Relay){
$ws = New-NostrRelayConnection -URL $rUri
$send = Send-NostrRequest -QueryString $filter -WebSocket $ws
While ($WS.State -eq 'Open') {
$Array = [byte[]] @(,0) * 1024
$Recv = New-Object System.ArraySegment[byte] -ArgumentList @(,$Array)
$CT = New-Object System.Threading.CancellationToken
$Conn = $WS.ReceiveAsync($Recv, $CT)
While (!$Conn.IsCompleted) {
Start-Sleep -Milliseconds 100
}
$out = [System.Text.Encoding]::utf8.GetString($Recv.array)
if($out -match '^\["EOSE"' ){
Write-Verbose "Received EOSE, closing connection."
$send = Send-NostrRequest -QueryString '["CLOSE", "nostrps"]' -WebSocket $ws
$ws.dispose()
$send.dispose()
$conn.dispose()
continue
}
try {
$outobj = $out | ConvertFrom-Json -ErrorAction stop -AsHashtable | Where-Object {$_ -is [hashtable]}
$outobj.created_at = Get-Date -UnixTimeSeconds $outobj.created_at
$outobj.add('Relay',$rUri)
new-object -TypeName psobject -Property $outobj
} catch {
# "could not convert from json"
}
}
}
}
}
function Test-NostrWebSocket {
param(
[Parameter(ValueFromPipeline)]
$Relay
)
process{
$WS = New-Object System.Net.WebSockets.ClientWebSocket
$CT = New-Object System.Threading.CancellationToken
$WS.Options.UseDefaultCredentials = $true
Write-Host "Testing $Relay " -NoNewline
$sw = [System.Diagnostics.Stopwatch]::StartNew()
$Conn = $WS.ConnectAsync($Relay, $CT)
While (!$Conn.IsCompleted) {
Write-Host "." -NoNewline
Start-Sleep -Milliseconds 10
}
$sw.Stop(); $Conn.Dispose(); $ws.Dispose()
Write-Host " connected after $($sw.ElapsedMilliseconds) ms"
return $sw.ElapsedMilliseconds
}
}
$relays = @(
'wss://nostr.milou.lol',
'wss://bitcoiner.social',
'wss://relay.nostr.com.au',
'wss://relay.nostrati.com',
'wss://nostr.inosta.cc',
'wss://nostr.plebchain.org',
'wss://atlas.nostr.land',
'wss://relay.nostrich.land',
'wss://relay.nostriches.org',
'wss://private.red.gb.net',
'wss://nostr.decentony.com',
'wss://relay.orangepill.dev',
'wss://puravida.nostr.land',
'wss://nostr.wine',
'wss://eden.nostr.land',
'wss://nostr.gives.africa',
'wss://relay.snort.social',
'wss://relay.damus.io'
)
function Get-NostrRelayInfo {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline)]
$Uri
)
process {
write-verbose "testing $uri"
$httpsUri = $uri -replace '^ws','http'
$out = [ordered]@{
relay = $uri
}
$out.timeHttps = (Measure-Command {$r=Invoke-RestMethod -TimeoutSec 2 $httpsUri -Headers @{accept='application/nostr+json'} -ErrorAction SilentlyContinue }).TotalMilliseconds
$out.timeWss = Test-NostrWebSocket -Relay $uri
$out.nips = $r.supported_nips -join ', '
$out.software = $r.software
$out.version = $r.version
$out.pubkey = $r.pubkey
[pscustomobject]$out
}
}
#$relays | Get-NostrRelayInfo