upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/components/wisp_relay/handlers.c
blob: 216472569db74db2c6ce4f7221a5aceeca6ac643 (plain)
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
#include "handlers.h"
#include "router.h"
#include "storage_engine.h"
#include "sub_manager.h"
#include "relay_validator.h"
#include "broadcaster.h"
#include "deletion.h"
#include "rate_limiter.h"
#include "relay_types.h"
#include "cJSON.h"
#include "esp_log.h"
#include <string.h>

static const char *TAG = "handlers";

int handle_event(relay_ctx_t *ctx, int conn_fd, const char *event_json, size_t event_len)
{
    if (!ctx || !event_json) return -1;

    if (ctx->rate_limiter) {
        if (!rate_limiter_check(ctx->rate_limiter, conn_fd, RATE_TYPE_EVENT)) {
            router_send_ok(ctx, conn_fd, "", false, "rate limited");
            return -1;
        }
    }

    cJSON *obj = cJSON_ParseWithLength(event_json, event_len);
    if (!obj) {
        router_send_ok(ctx, conn_fd, "", false, "invalid JSON");
        return -1;
    }

    cJSON *id_item = cJSON_GetObjectItem(obj, "id");
    cJSON *pubkey_item = cJSON_GetObjectItem(obj, "pubkey");
    cJSON *kind_item = cJSON_GetObjectItem(obj, "kind");
    cJSON *ca_item = cJSON_GetObjectItem(obj, "created_at");

    if (!id_item || !pubkey_item || !kind_item || !ca_item) {
        cJSON_Delete(obj);
        router_send_ok(ctx, conn_fd, "", false, "missing required fields");
        return -1;
    }

    const char *id_hex = id_item->valuestring;
    const char *pubkey_hex = pubkey_item->valuestring;
    int kind = kind_item->valueint;
    uint64_t created_at = (uint64_t)ca_item->valuedouble;

    if (ctx->config.max_future_sec > 0) {
        int64_t now = (int64_t)(xTaskGetTickCount() / configTICK_RATE_HZ);
        if ((int64_t)created_at > now + ctx->config.max_future_sec) {
            cJSON_Delete(obj);
            router_send_ok(ctx, conn_fd, id_hex, false, "created_at too far in future");
            return -1;
        }
    }

    uint8_t event_id[32];
    if (relay_hex_to_bytes(id_hex, 64, event_id, 32) != 0) {
        cJSON_Delete(obj);
        router_send_ok(ctx, conn_fd, "", false, "invalid event id");
        return -1;
    }

    if (storage_event_exists(ctx->storage, event_id)) {
        cJSON_Delete(obj);
        router_send_ok(ctx, conn_fd, id_hex, true, "duplicate");
        return 0;
    }

    if (!relay_validator_verify_event(event_json, event_len)) {
        cJSON_Delete(obj);
        router_send_ok(ctx, conn_fd, id_hex, false, "invalid signature");
        return -1;
    }

    cJSON_Delete(obj);

    storage_error_t err = storage_save_event_json(ctx->storage, event_json, event_len);
    if (err != STORAGE_OK) {
        const char *msg = (err == STORAGE_ERR_FULL) ? "relay full" :
                          (err == STORAGE_ERR_DUPLICATE) ? "duplicate" : "storage error";
        router_send_ok(ctx, conn_fd, id_hex, false, msg);
        return -1;
    }

    router_send_ok(ctx, conn_fd, id_hex, true, "");

    if (kind == NOSTR_KIND_DELETION) {
        deletion_process_json(ctx->storage, event_json, event_len);
    }

    broadcaster_fanout_json(ctx, event_json, event_len, kind, pubkey_hex, created_at);

    return 0;
}

static void parse_filter_json(const char *json, sub_filter_t *filter)
{
    memset(filter, 0, sizeof(sub_filter_t));
    cJSON *obj = cJSON_Parse(json);
    if (!obj) return;

    cJSON *arr;

    arr = cJSON_GetObjectItem(obj, "ids");
    if (arr && cJSON_IsArray(arr)) {
        filter->ids_count = cJSON_GetArraySize(arr);
        if (filter->ids_count > SUB_MAX_FILTER_IDS) filter->ids_count = SUB_MAX_FILTER_IDS;
        for (size_t i = 0; i < filter->ids_count; i++)
            filter->ids[i] = strdup(cJSON_GetArrayItem(arr, i)->valuestring);
    }

    arr = cJSON_GetObjectItem(obj, "authors");
    if (arr && cJSON_IsArray(arr)) {
        filter->authors_count = cJSON_GetArraySize(arr);
        if (filter->authors_count > SUB_MAX_FILTER_AUTHORS) filter->authors_count = SUB_MAX_FILTER_AUTHORS;
        for (size_t i = 0; i < filter->authors_count; i++)
            filter->authors[i] = strdup(cJSON_GetArrayItem(arr, i)->valuestring);
    }

    arr = cJSON_GetObjectItem(obj, "kinds");
    if (arr && cJSON_IsArray(arr)) {
        filter->kinds_count = cJSON_GetArraySize(arr);
        if (filter->kinds_count > SUB_MAX_FILTER_KINDS) filter->kinds_count = SUB_MAX_FILTER_KINDS;
        for (size_t i = 0; i < filter->kinds_count; i++)
            filter->kinds[i] = cJSON_GetArrayItem(arr, i)->valueint;
    }

    arr = cJSON_GetObjectItem(obj, "#e");
    if (arr && cJSON_IsArray(arr)) {
        filter->e_tags_count = cJSON_GetArraySize(arr);
        if (filter->e_tags_count > SUB_MAX_FILTER_ETAGS) filter->e_tags_count = SUB_MAX_FILTER_ETAGS;
        for (size_t i = 0; i < filter->e_tags_count; i++)
            filter->e_tags[i] = strdup(cJSON_GetArrayItem(arr, i)->valuestring);
    }

    arr = cJSON_GetObjectItem(obj, "#p");
    if (arr && cJSON_IsArray(arr)) {
        filter->p_tags_count = cJSON_GetArraySize(arr);
        if (filter->p_tags_count > SUB_MAX_FILTER_PTAGS) filter->p_tags_count = SUB_MAX_FILTER_PTAGS;
        for (size_t i = 0; i < filter->p_tags_count; i++)
            filter->p_tags[i] = strdup(cJSON_GetArrayItem(arr, i)->valuestring);
    }

    cJSON *since = cJSON_GetObjectItem(obj, "since");
    if (since) filter->since = (int64_t)since->valuedouble;
    cJSON *until = cJSON_GetObjectItem(obj, "until");
    if (until) filter->until = (int64_t)until->valuedouble;
    cJSON *limit = cJSON_GetObjectItem(obj, "limit");
    if (limit) filter->limit = limit->valueint;

    cJSON_Delete(obj);
}

void handle_req(relay_ctx_t *ctx, int conn_fd, const char *sub_id, const char *filters_json)
{
    if (!ctx || !sub_id) return;

    if (ctx->rate_limiter) {
        if (!rate_limiter_check(ctx->rate_limiter, conn_fd, RATE_TYPE_REQ)) {
            router_send_closed(ctx, conn_fd, sub_id, "rate limited");
            return;
        }
    }

    sub_filter_t filter;
    parse_filter_json(filters_json, &filter);

    int query_kind = -1;
    const char *query_author = NULL;
    int query_limit = filter.limit > 0 ? filter.limit : 100;

    if (filter.kinds_count > 0) query_kind = filter.kinds[0];
    if (filter.authors_count > 0) query_author = filter.authors[0];

    char **results = NULL;
    uint16_t count = 0;
    storage_query_events_json(ctx->storage, query_kind, query_author,
                              query_limit, &results, &count);

    for (uint16_t i = 0; i < count; i++) {
        router_send_event(ctx, conn_fd, sub_id, results[i], strlen(results[i]));
    }
    storage_free_query_results(results, count);

    router_send_eose(ctx, conn_fd, sub_id);

    sub_manager_add(ctx->sub_manager, conn_fd, sub_id, &filter, 1);

    sub_filter_t *f = &filter;
    for (size_t i = 0; i < f->ids_count; i++) free(f->ids[i]);
    for (size_t i = 0; i < f->authors_count; i++) free(f->authors[i]);
    for (size_t i = 0; i < f->e_tags_count; i++) free(f->e_tags[i]);
    for (size_t i = 0; i < f->p_tags_count; i++) free(f->p_tags[i]);
}

int handle_close(relay_ctx_t *ctx, int conn_fd, const char *sub_id)
{
    if (!ctx || !sub_id) return -1;
    sub_manager_remove(ctx->sub_manager, conn_fd, sub_id);
    return 0;
}