From dac324a63bc0e1645fb01ab56dc2dc0d1c98c9de Mon Sep 17 00:00:00 2001 From: Jakob L. Kreuze Date: Sat, 12 Aug 2023 16:40:11 -0400 Subject: [api] Identify comments based on clearnet/darknet --- README.md | 6 ++++ haunt/api.scm | 10 +++---- haunt/jakob/dynamic/capabilities/comments.scm | 27 +++++++++++------ haunt/jakob/dynamic/capabilities/common.scm | 4 ++- haunt/jakob/dynamic/schema-comments.sql | 3 +- haunt/jakob/dynamic/util.scm | 23 ++++++++++++++- haunt/jakob/utils/comments.scm | 15 ++++++++-- haunt/static/image/i2p.svg | 1 + haunt/static/image/tor.svg | 42 +++++++++++++++++++++++++++ 9 files changed, 110 insertions(+), 21 deletions(-) create mode 100644 haunt/static/image/i2p.svg create mode 100644 haunt/static/image/tor.svg diff --git a/README.md b/README.md index e9cb26f..634923f 100644 --- a/README.md +++ b/README.md @@ -223,3 +223,9 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +## Static Assets + +| Tor Logo | haunt/static/tor.svg | The Tor Project, Inc. | [CC BY 3.0 US](https://creativecommons.org/licenses/by/3.0/us/deed.en) | +| I2P Logo | haunt/static/i2p.svg | I2P Project | [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/) | + diff --git a/haunt/api.scm b/haunt/api.scm index 354e1b6..1e71064 100644 --- a/haunt/api.scm +++ b/haunt/api.scm @@ -25,6 +25,7 @@ (jakob dynamic errors) (jakob dynamic logging) (jakob dynamic rate-limiter) + (jakob dynamic util) (json) (rnrs conditions) (rnrs exceptions) @@ -54,12 +55,9 @@ (define (clearnet-only handler) (lambda (request body) - (let ((originating-ip (assoc-ref (request-headers request) 'x-forwarded-for))) - (if (and (or (string-prefix? "127." originating-ip) - (string-suffix? ":1" originating-ip)) - (not (%debug-enabled))) - (panic "This API is only available on the clearnet." #:code 403) - (handler request body))))) + (if (and (from-darknet? request) (not (%debug-enabled))) + (panic "This API is only available on the clearnet." #:code 403) + (handler request body)))) (define (handle-api-request request body endpoint) "Route handler for the API server." diff --git a/haunt/jakob/dynamic/capabilities/comments.scm b/haunt/jakob/dynamic/capabilities/comments.scm index 33f2429..ee2a52d 100644 --- a/haunt/jakob/dynamic/capabilities/comments.scm +++ b/haunt/jakob/dynamic/capabilities/comments.scm @@ -42,17 +42,21 @@ This interface exists for dynamically generating the comment view from Haunt." (define (make-internal-comment~ . args) - (let* ((args-needing-processing (take-right args 3)) + (let* ((args-needing-processing (take-right args 4)) (approved (list-ref args-needing-processing 0)) (approved (string->date approved "~Y~m~d ~H~M~S.~N")) (reactions (list-ref args-needing-processing 1)) (reactions (if reactions (with-input-from-string reactions read) '())) - (replies (list-ref args-needing-processing 2))) - + (originating-network (list-ref args-needing-processing 2)) + (replies (list-ref args-needing-processing 3))) (apply make-internal-comment - (append (drop-right args 3) (list approved reactions replies))))) + `(,@(drop-right args 4) + ,approved + ,reactions + ,replies + ,originating-network)))) (define (order-comments comments) (define seen (make-hash-table)) (define (id comment) (first comment)) @@ -78,7 +82,7 @@ This interface exists for dynamically generating the comment view from Haunt." (hash-append! seen 'terminal parsed)) (pass (cdr cur) initial-comments remaining))))) (pass comments comments '())) - (let* ((query "SELECT id, name, subject, email, comment, url, approved, reactions, reply_to + (let* ((query "SELECT id, name, subject, email, comment, url, approved, reactions, originating_network, reply_to FROM comments WHERE slug = $1 and approved IS NOT NULL") (result (exec-query conn query (list slug)))) (if (positive? (length result)) @@ -106,6 +110,10 @@ This is a wrapper around `get-comments-by-slug'." (define (put-comment request body) "API endpoint handler for submitting a comment" + (define (request-originating-network request) + (cond ((from-tor? request) "tor") + ((from-i2p? request) "i2p") + (else "clearnet"))) (define (valid-comment? form-data) (and (assoc "slug" form-data) (assoc "name" form-data) @@ -124,9 +132,9 @@ This is a wrapper around `get-comments-by-slug'." (string->number (assoc-value form-data "captcha-id")))))) (define (insert-comment form-data) (exec-query conn - "INSERT INTO comments (submitted, slug, name, subject, - email, url, comment, reply_to) - VALUES (now(), $1, $2, $3, $4, $5, $6, $7);" + "INSERT INTO comments (submitted, slug, name, subject, email, + url, comment, reply_to, originating_network) + VALUES (now(), $1, $2, $3, $4, $5, $6, $7, $8);" (list (assoc-value form-data "slug") (assoc-value form-data "name") (assoc-value form-data "subject") @@ -136,7 +144,8 @@ This is a wrapper around `get-comments-by-slug'." (if (and (assoc-value form-data "reply-to") (positive? (string-length (assoc-value form-data "reply-to")))) (assoc-value form-data "reply-to") - #f))) + #f) + (request-originating-network request))) (values (build-response #:code 307 #:headers '((Location . "https://jakob.space"))) diff --git a/haunt/jakob/dynamic/capabilities/common.scm b/haunt/jakob/dynamic/capabilities/common.scm index 0e7d7b8..2bca1a2 100644 --- a/haunt/jakob/dynamic/capabilities/common.scm +++ b/haunt/jakob/dynamic/capabilities/common.scm @@ -31,6 +31,7 @@ internal-comment-publish-time internal-comment-reactions internal-comment-replies + internal-comment-originating-network sort-comments)) (define-json-mapping @@ -57,7 +58,8 @@ (vector->list x))) (lambda (x) (list->vector (map (lambda (y) (json-string->scm (internal-comment->json y))) - x))))) + x)))) + (originating-network internal-comment-originating-network)) (define (sort-comments comments) "Sort COMMENTS, a list of `' chronologically" diff --git a/haunt/jakob/dynamic/schema-comments.sql b/haunt/jakob/dynamic/schema-comments.sql index 1f2edc7..8519f4b 100644 --- a/haunt/jakob/dynamic/schema-comments.sql +++ b/haunt/jakob/dynamic/schema-comments.sql @@ -9,7 +9,8 @@ CREATE TABLE comments( url VARCHAR(100), comment VARCHAR(1024) NOT NULL, reactions VARCHAR(1024), - reply_to INT + reply_to INT, + originating_network VARCHAR(100) ); -- Use `now' for `submitted'. diff --git a/haunt/jakob/dynamic/util.scm b/haunt/jakob/dynamic/util.scm index f6f0382..e34ae91 100644 --- a/haunt/jakob/dynamic/util.scm +++ b/haunt/jakob/dynamic/util.scm @@ -20,6 +20,7 @@ #:use-module (srfi srfi-1) #:use-module (srfi srfi-19) #:use-module (srfi srfi-26) + #:use-module (web request) #:use-module (web uri) #:export (assoc-value acons-normalize @@ -27,7 +28,10 @@ decode-form date \ No newline at end of file diff --git a/haunt/static/image/tor.svg b/haunt/static/image/tor.svg new file mode 100644 index 0000000..ff0f894 --- /dev/null +++ b/haunt/static/image/tor.svg @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- cgit v1.3