2024-07-23 17:57:21 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"grain/relay/db"
|
2024-07-24 20:56:04 +00:00
|
|
|
"grain/relay/handlers/kinds"
|
2024-07-25 14:19:42 +00:00
|
|
|
"grain/relay/handlers/response"
|
2024-07-23 17:57:21 +00:00
|
|
|
"grain/relay/utils"
|
|
|
|
|
|
|
|
relay "grain/relay/types"
|
|
|
|
|
|
|
|
"golang.org/x/net/websocket"
|
|
|
|
)
|
|
|
|
|
2024-07-25 13:57:24 +00:00
|
|
|
func HandleEvent(ws *websocket.Conn, message []interface{}) {
|
2024-07-23 17:57:21 +00:00
|
|
|
if len(message) != 2 {
|
|
|
|
fmt.Println("Invalid EVENT message format")
|
2024-07-27 14:06:34 +00:00
|
|
|
response.SendNotice(ws, "", "Invalid EVENT message format")
|
2024-07-23 17:57:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
eventData, ok := message[1].(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
fmt.Println("Invalid event data format")
|
2024-07-27 14:06:34 +00:00
|
|
|
response.SendNotice(ws, "", "Invalid event data format")
|
2024-07-23 17:57:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
eventBytes, err := json.Marshal(eventData)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error marshaling event data:", err)
|
2024-07-27 14:06:34 +00:00
|
|
|
response.SendNotice(ws, "", "Error marshaling event data")
|
2024-07-23 17:57:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var evt relay.Event
|
|
|
|
err = json.Unmarshal(eventBytes, &evt)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error unmarshaling event data:", err)
|
2024-07-27 14:06:34 +00:00
|
|
|
response.SendNotice(ws, "", "Error unmarshaling event data")
|
2024-07-23 17:57:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-27 14:06:34 +00:00
|
|
|
eventSize := len(eventBytes) // Calculate event size
|
|
|
|
HandleKind(context.TODO(), evt, ws, eventSize)
|
2024-07-23 17:57:21 +00:00
|
|
|
|
|
|
|
fmt.Println("Event processed:", evt.ID)
|
|
|
|
}
|
|
|
|
|
2024-07-27 14:06:34 +00:00
|
|
|
func HandleKind(ctx context.Context, evt relay.Event, ws *websocket.Conn, eventSize int) {
|
2024-07-23 17:57:21 +00:00
|
|
|
if !utils.CheckSignature(evt) {
|
2024-07-25 14:19:42 +00:00
|
|
|
response.SendOK(ws, evt.ID, false, "invalid: signature verification failed")
|
2024-07-23 17:57:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
collection := db.GetCollection(evt.Kind)
|
2024-07-25 13:57:24 +00:00
|
|
|
rateLimiter := utils.GetRateLimiter()
|
2024-07-26 20:46:01 +00:00
|
|
|
sizeLimiter := utils.GetSizeLimiter()
|
2024-07-27 14:06:34 +00:00
|
|
|
|
|
|
|
category := determineCategory(evt.Kind)
|
2024-07-25 13:57:24 +00:00
|
|
|
|
2024-07-26 14:02:34 +00:00
|
|
|
if allowed, msg := rateLimiter.AllowEvent(evt.Kind, category); !allowed {
|
|
|
|
response.SendOK(ws, evt.ID, false, msg)
|
2024-07-25 13:57:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-26 20:46:01 +00:00
|
|
|
if allowed, msg := sizeLimiter.AllowSize(evt.Kind, eventSize); !allowed {
|
|
|
|
response.SendOK(ws, evt.ID, false, msg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-23 17:57:21 +00:00
|
|
|
var err error
|
2024-07-24 20:37:55 +00:00
|
|
|
switch {
|
|
|
|
case evt.Kind == 0:
|
2024-07-24 14:04:45 +00:00
|
|
|
err = kinds.HandleKind0(ctx, evt, collection, ws)
|
2024-07-24 20:37:55 +00:00
|
|
|
case evt.Kind == 1:
|
2024-07-27 14:06:34 +00:00
|
|
|
err = kinds.HandleKind1(ctx, evt, collection, ws)
|
2024-07-24 20:54:11 +00:00
|
|
|
case evt.Kind == 2:
|
2024-07-27 14:06:34 +00:00
|
|
|
err = kinds.HandleKind2(ctx, evt, ws)
|
2024-07-24 20:37:55 +00:00
|
|
|
case evt.Kind == 3:
|
|
|
|
err = kinds.HandleReplaceableKind(ctx, evt, collection, ws)
|
2024-07-27 19:47:37 +00:00
|
|
|
case evt.Kind == 5:
|
|
|
|
err = kinds.HandleKind5(ctx, evt, db.GetClient(), ws)
|
2024-07-24 20:54:11 +00:00
|
|
|
case evt.Kind >= 4 && evt.Kind < 45:
|
2024-07-27 14:06:34 +00:00
|
|
|
err = kinds.HandleRegularKind(ctx, evt, collection, ws)
|
2024-07-24 20:54:11 +00:00
|
|
|
case evt.Kind >= 1000 && evt.Kind < 10000:
|
2024-07-27 14:06:34 +00:00
|
|
|
err = kinds.HandleRegularKind(ctx, evt, collection, ws)
|
2024-07-24 20:37:55 +00:00
|
|
|
case evt.Kind >= 10000 && evt.Kind < 20000:
|
|
|
|
err = kinds.HandleReplaceableKind(ctx, evt, collection, ws)
|
|
|
|
case evt.Kind >= 20000 && evt.Kind < 30000:
|
|
|
|
fmt.Println("Ephemeral event received and ignored:", evt.ID)
|
|
|
|
case evt.Kind >= 30000 && evt.Kind < 40000:
|
|
|
|
err = kinds.HandleParameterizedReplaceableKind(ctx, evt, collection, ws)
|
2024-07-23 17:57:21 +00:00
|
|
|
default:
|
2024-07-25 14:05:33 +00:00
|
|
|
err = kinds.HandleUnknownKind(ctx, evt, collection, ws)
|
2024-07-23 17:57:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2024-07-25 14:19:42 +00:00
|
|
|
response.SendOK(ws, evt.ID, false, fmt.Sprintf("error: %v", err))
|
2024-07-23 17:57:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-25 14:19:42 +00:00
|
|
|
response.SendOK(ws, evt.ID, true, "")
|
2024-07-25 13:57:24 +00:00
|
|
|
}
|
2024-07-27 14:06:34 +00:00
|
|
|
|
|
|
|
func determineCategory(kind int) string {
|
|
|
|
switch {
|
|
|
|
case kind == 0, kind == 3, kind >= 10000 && kind < 20000:
|
|
|
|
return "replaceable"
|
|
|
|
case kind == 1, kind >= 4 && kind < 45, kind >= 1000 && kind < 10000:
|
|
|
|
return "regular"
|
|
|
|
case kind == 2:
|
|
|
|
return "deprecated"
|
|
|
|
case kind >= 20000 && kind < 30000:
|
|
|
|
return "ephemeral"
|
|
|
|
case kind >= 30000 && kind < 40000:
|
|
|
|
return "parameterized_replaceable"
|
|
|
|
default:
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
}
|