-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbouncer.go
251 lines (197 loc) · 4.98 KB
/
bouncer.go
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package main
import (
"context"
"fmt"
"sync"
//"time"
"encoding/json"
"github.com/nbd-wtf/go-nostr"
"github.com/coder/websocket"
"github.com/coder/websocket/wsjson"
"codeberg.org/Yonle/bostr2/relayHandler"
)
type SessionEvents map[string]map[string]struct{}
type SessionEOSEs map[string]int
type SessionSubs map[string]nostr.Filters
type MessageChan chan []json.RawMessage
type ClientEvents []nostr.Event
type Session struct {
ClientIP string
ClientREQ MessageChan
ClientCLOSE MessageChan
ClientEVENT MessageChan
clientEvents ClientEvents
events SessionEvents
pendingEOSE SessionEOSEs
subscriptions SessionSubs
relay *relayHandler.RelaySession
destroyed chan struct{}
once sync.Once
conn *websocket.Conn
ctx context.Context
}
func (s *Session) Start() {
s.relay.Init(config.Relays)
}
func (s *Session) StartListening() {
// deal with destroy request.
go func() {
<-s.ctx.Done()
/*go func() {
time.Sleep(7 * time.Second)
select {
case <-s.destroyed:
default:
panic("something hangs.")
}
}()*/
s.relay.Wait()
close(s.destroyed)
}()
// deal with what upstream says
go func() {
listener:
for {
select {
case d, open := <-s.relay.UpEVENT:
if !open {
continue listener
}
s.handleUpstreamEVENT(d)
case d, open := <-s.relay.UpEOSE:
if !open {
continue listener
}
s.handleUpstreamEOSE(d)
case conn, open := <-s.relay.UpConnected:
if !open {
continue listener
}
s.resendEvents(conn)
s.reopenSubscriptions(conn)
case d, open := <-s.ClientREQ:
if !open {
continue listener
}
s.handleClientREQ(d)
case d, open := <-s.ClientCLOSE:
if !open {
continue listener
}
s.handleClientCLOSE(d)
case d, open := <-s.ClientEVENT:
if !open {
continue listener
}
s.handleClientEVENT(d)
case <-s.destroyed:
break listener
}
}
}()
}
func (s *Session) reopenSubscriptions(conn *websocket.Conn) {
for subID, filters := range s.subscriptions {
data := []interface{}{"REQ", subID}
for _, filter := range filters {
data = append(data, filter)
}
wsjson.Write(s.ctx, conn, data)
}
}
func (s *Session) resendEvents(conn *websocket.Conn) {
for _, event := range s.clientEvents {
data := []interface{}{"EVENT", event}
wsjson.Write(s.ctx, conn, data)
}
}
func (s *Session) handleClientREQ(d []json.RawMessage) {
var subID string
if err := json.Unmarshal(d[1], &subID); err != nil {
wsjson.Write(s.ctx, s.conn, [2]string{"NOTICE", "error: received subID is not a string"})
return
}
var filters nostr.Filters
for index, rawFilter := range d[2:] {
var filter nostr.Filter
if err := filter.UnmarshalJSON(rawFilter); err != nil {
// if it doesn't looks finje, then just stop.
wsjson.Write(s.ctx, s.conn, [3]string{"CLOSED", subID, fmt.Sprintf("error: one of your filter on index %d does not looks right.", index)})
return
}
filters = append(filters, filter)
}
s.subscriptions[subID] = filters
s.events[subID] = make(map[string]struct{})
s.pendingEOSE[subID] = 0
s.once.Do(s.Start)
s.relay.Broadcast(d)
}
func (s *Session) handleClientCLOSE(d []json.RawMessage) {
var subID string
if err := json.Unmarshal(d[1], &subID); err != nil {
wsjson.Write(s.ctx, s.conn, [2]string{"NOTICE", "error: received subID is not a string"})
return
}
delete(s.subscriptions, subID)
delete(s.events, subID)
delete(s.pendingEOSE, subID)
s.relay.Broadcast(d)
wsjson.Write(s.ctx, s.conn, [3]string{"CLOSED", subID, ""})
}
func (s *Session) handleClientEVENT(d []json.RawMessage) {
var event nostr.Event
if err := event.UnmarshalJSON(d[1]); err != nil {
wsjson.Write(s.ctx, s.conn, [2]string{"NOTICE", "error: invalid EVENT"})
return
}
id := event.GetID()
s.clientEvents = append(s.clientEvents, event)
s.once.Do(s.Start)
s.relay.Broadcast(d)
wsjson.Write(s.ctx, s.conn, [4]interface{}{"OK", id, true, ""})
}
func (s *Session) handleUpstreamEVENT(d []json.RawMessage) {
var subID string
if err := json.Unmarshal(d[1], &subID); err != nil {
return
}
if _, ok := s.events[subID]; !ok {
return
}
var event nostr.Event
if err := event.UnmarshalJSON(d[2]); err != nil {
return
}
eventID := event.GetID()
if _, ok := s.events[subID][eventID]; ok {
return
}
// get the filters and validate
if filters := s.subscriptions[subID]; !filters.Match(&event) {
// "shut"
return
}
s.events[subID][eventID] = struct{}{}
wsjson.Write(s.ctx, s.conn, d)
if _, ok := s.pendingEOSE[subID]; ok {
if len(s.events[subID]) >= 500 {
delete(s.pendingEOSE, subID)
wsjson.Write(s.ctx, s.conn, [2]string{"EOSE", subID})
}
}
}
func (s *Session) handleUpstreamEOSE(d []json.RawMessage) {
var subID string
if err := json.Unmarshal(d[1], &subID); err != nil {
return
}
if _, ok := s.pendingEOSE[subID]; !ok {
return
}
s.pendingEOSE[subID]++
if s.pendingEOSE[subID] >= s.relay.HowManyRelaysAreConnected {
delete(s.pendingEOSE, subID)
wsjson.Write(s.ctx, s.conn, [2]string{"EOSE", subID})
}
}