From f75c828a1ab58c5d76444aad0e07f3c7ccf1e795 Mon Sep 17 00:00:00 2001 From: Aaron Dixon Date: Fri, 16 Dec 2022 09:32:10 -0600 Subject: [PATCH] More leniency for queries plus a home page --- conf/relay.yaml | 3 +++ src/me/untethr/nostr/app.clj | 29 +++++++++++++++++++++++------ src/me/untethr/nostr/validation.clj | 6 ++++-- test/test/validation_test.clj | 15 ++++++++------- 4 files changed, 38 insertions(+), 15 deletions(-) diff --git a/conf/relay.yaml b/conf/relay.yaml index 2e2d19a..bc25247 100644 --- a/conf/relay.yaml +++ b/conf/relay.yaml @@ -1,3 +1,6 @@ +# Change this to your actual deploy host. +hostname: "nostr-relay.untethr.me" + http: port: 9090 diff --git a/src/me/untethr/nostr/app.clj b/src/me/untethr/nostr/app.clj index 5d18022..3969b65 100644 --- a/src/me/untethr/nostr/app.clj +++ b/src/me/untethr/nostr/app.clj @@ -261,7 +261,10 @@ :headers {"Content-Type" "application/nostr+json"} :body nip11-json}) -(defn- handler [nip11-json metrics db subs-atom fulfill-atom req] +(def ^:private nostr-url "https://github.com/nostr-protocol/nostr") +(def ^:private untethr-url "https://github.com/atdixon/me.untethr.nostr-relay") + +(defn- handler [maybe-server-hostname nip11-json metrics db subs-atom fulfill-atom req] ;; req contains :remote-addr, :headers {}, ... (cond (:websocket? req) @@ -271,7 +274,20 @@ :on-receive (partial ws-receive metrics db subs-atom fulfill-atom websocket-state) :on-close (partial ws-close metrics db subs-atom fulfill-atom websocket-state)})) (and (nip11-request? req) nip11-json) - (nip11-response nip11-json))) + (nip11-response nip11-json) + (not (str/blank? maybe-server-hostname)) + {:status 200 + :headers {"Content-Type" "text/html"} + :body (format + (str + "" + "

Hello!" + "

I am a fast nostr relay" + " of this flavor." + "

Add me to your nostr client using:

wss://%s
" + "

Or, get to know me better:

curl -H 'Accept: application/nostr+json' https://%s
" + "") + nostr-url untethr-url maybe-server-hostname maybe-server-hostname)})) (defn- handler-q [db req] (let [rows (jdbc/execute! db @@ -308,11 +324,11 @@ ;; -- -(defn- ring-handler [nip05-json nip11-json metrics db subs-atom fulfill-atom] +(defn- ring-handler [maybe-server-hostname nip05-json nip11-json metrics db subs-atom fulfill-atom] (ring/ring-handler (ring/router [["/" {:get - (partial handler nip11-json metrics db subs-atom fulfill-atom)}] + (partial handler maybe-server-hostname nip11-json metrics db subs-atom fulfill-atom)}] ["/.well-known/nostr.json" {:get (partial handler-nip05 nip05-json)}] ["/metrics" {:get (partial handler-metrics metrics)}] ["/q" {:get (partial handler-q db)}]]))) @@ -327,7 +343,7 @@ ;; ?: simple auth for metrics? (defn go! - [db-path server-port nip05-json nip11-json] + [db-path maybe-server-hostname server-port nip05-json nip11-json] (let [db (store/init! db-path) subs-atom (atom (subscribe/create-empty-subs)) fulfill-atom (atom (fulfill/create-empty-registry)) @@ -336,7 +352,7 @@ #(subscribe/num-subscriptions subs-atom) #(subscribe/num-firehose-filters subs-atom))] (hk/run-server - (ring-handler nip05-json nip11-json metrics db subs-atom fulfill-atom) + (ring-handler maybe-server-hostname nip05-json nip11-json metrics db subs-atom fulfill-atom) {:port server-port :max-ws 4194304}) (log/infof "server started on port %d; sleeping forever" server-port) (Thread/sleep Integer/MAX_VALUE))) @@ -361,6 +377,7 @@ nip11-json (slurp-json* "conf/nip11.json")] (go! (get-in conf [:sqlite :file]) + (get-in conf [:hostname]) (get-in conf [:http :port]) nip05-json nip11-json))) diff --git a/src/me/untethr/nostr/validation.clj b/src/me/untethr/nostr/validation.clj index ac0667e..32c1f7a 100644 --- a/src/me/untethr/nostr/validation.clj +++ b/src/me/untethr/nostr/validation.clj @@ -90,7 +90,7 @@ [the-filter] ;; based on what we see from clients in the wild, we'll forgive some mistakes ;; in requests that we can argue won't change the result semantics: - ;; (1) we've seen non-hex authors in queries; here we replace these with a + ;; (1) we've seen non-hex authors and ids in queries; here we replace these with a ;; valid hex str that should have no real reference in our db. in this ;; way we'll do the right thing if there is one bad author ref in the ;; query (i.e., we'll return no results). if there are good author refs @@ -98,7 +98,9 @@ ;; any for the good author refs (cond-> the-filter (not (empty? (:authors the-filter))) - (update :authors #(mapv (fn [x] (if (hex-str? x) x zero-hex-str)) %)))) + (update :authors #(mapv (fn [x] (if (hex-str? x) x zero-hex-str)) %)) + (not (empty? (:ids the-filter))) + (update :ids #(mapv (fn [x] (if (hex-str? x) x zero-hex-str)) %)))) (defn close-err [req-id] diff --git a/test/test/validation_test.clj b/test/test/validation_test.clj index 3167a8e..7036eef 100644 --- a/test/test/validation_test.clj +++ b/test/test/validation_test.clj @@ -15,10 +15,11 @@ (is (= {k []} (validation/conform-filter-lenient {k []})))) (doseq [k [:since :until :limit]] (is (= {k 0} (validation/conform-filter-lenient {k 0})))) - (is (= {:authors [validation/zero-hex-str]} - (validation/conform-filter-lenient {:authors ["bad"]}))) - (is (= {:authors [validation/zero-hex-str validation/zero-hex-str]} - (validation/conform-filter-lenient {:authors ["bad" "bad"]}))) - (is (= {:authors [validation/zero-hex-str fake-hex-str]} - (validation/conform-filter-lenient - {:authors ["bad" fake-hex-str]})))) + (doseq [k [:ids :authors]] + (is (= {k [validation/zero-hex-str]} + (validation/conform-filter-lenient {k ["bad"]}))) + (is (= {k [validation/zero-hex-str validation/zero-hex-str]} + (validation/conform-filter-lenient {k ["bad" "bad"]}))) + (is (= {k [validation/zero-hex-str fake-hex-str]} + (validation/conform-filter-lenient + {k ["bad" fake-hex-str]})))))