Skip to content

Commit

Permalink
More leniency for queries plus a home page
Browse files Browse the repository at this point in the history
  • Loading branch information
atdixon committed Dec 16, 2022
1 parent d1828b5 commit f75c828
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
3 changes: 3 additions & 0 deletions conf/relay.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Change this to your actual deploy host.
hostname: "nostr-relay.untethr.me"

http:
port: 9090

Expand Down
29 changes: 23 additions & 6 deletions src/me/untethr/nostr/app.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
"<body>"
"<p>Hello!"
"<p>I am a fast <a target=\"_blank\" href=\"%s\">nostr</a> relay"
" of <a target=\"_blank\" href=\"%s\">this flavor</a>."
"<p>Add me to your nostr client using: <pre>wss://%s</pre>"
"<p>Or, get to know me better: <pre>curl -H 'Accept: application/nostr+json' https://%s</pre>"
"</body>")
nostr-url untethr-url maybe-server-hostname maybe-server-hostname)}))

(defn- handler-q [db req]
(let [rows (jdbc/execute! db
Expand Down Expand Up @@ -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)}]])))
Expand All @@ -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))
Expand All @@ -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)))
Expand All @@ -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)))
6 changes: 4 additions & 2 deletions src/me/untethr/nostr/validation.clj
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,17 @@
[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
;; in the query alongside bad, then we'll return matches if there are
;; 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]
Expand Down
15 changes: 8 additions & 7 deletions test/test/validation_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]})))))

0 comments on commit f75c828

Please sign in to comment.