Added blacklist support.
authorplugd <plugd@thelambdalab.xyz>
Thu, 10 Feb 2022 10:23:54 +0000 (11:23 +0100)
committerplugd <plugd@thelambdalab.xyz>
Thu, 10 Feb 2022 10:43:10 +0000 (11:43 +0100)
rags.scm

index 6eacfc4..f5da596 100644 (file)
--- a/rags.scm
+++ b/rags.scm
         (chicken process)
         (chicken process-context)
         (chicken process-context posix)
+        (chicken gc)
         matchable srfi-13 srfi-1
         uri-common tcp6 openssl)
 
 (define-record config
-  root-dir host port certfile keyfile uid gid
+  root-dir host port certfile keyfile uid gid blacklist blacklist-resp)
 
 (define file-types
   '(("gmi" "text/gemini" "charset=utf-8")
 (define (server-loop listener config)
   (let-values (((in-port out-port) (ssl-accept listener)))
     (let-values (((local-ip remote-ip) (tcp-addresses (ssl-port->tcp-port in-port))))
+      (print (conc "Memory statistics: " (memory-statistics)))
       (print "Accepted connection from " remote-ip
              " on " (seconds->string))
       (condition-case
-          (let ((request-line (read-line in-port)))
-            (print* "Serving request '" request-line "' ... ")
-            (with-output-to-port out-port
-              (lambda ()
-                (process-request config request-line)))
-            (print "done."))
+          (if (or (config-blacklist config)
+                  (not (member remote-ip
+                               (with-input-from-file
+                                   (config-blacklist config)))))
+              (let ((request-line (read-line in-port)))
+                (print* "Serving request '" request-line "' ... ")
+                (with-output-to-port out-port
+                  (lambda ()
+                    (process-request config request-line)))
+                (print "done."))
+              (begin
+                (print "Connection from blacklisted IP. Closing.")
+                (with-output-to-port out-port
+                  (lambda ()
+                    (print* "Refusing to serve to IP " remote-ip ".\n")
+                    (when (config-blacklist-resp config)
+                      (for-each print
+                                (with-input-from-file
+                                    (config-blacklist-resp config)
+                                  read-lines)))))))
         (o (exn)
            (print-error-message o))))
     (close-input-port in-port)
 
 (define (main)
   (let* ((progname (pathname-file (car (argv))))
-         (config (make-config #f #f 1965 #f #f #f #f)))
+         (config (make-config #f #f 1965 #f #f #f #f #f #f)))
     (if (null? (command-line-arguments))
         (print-usage progname)
         (let loop ((args (command-line-arguments)))
                       (equal? this-arg "--group"))
                   (config-gid-set! config (string->number (car rest-args)))
                   (loop (cdr rest-args)))
+                 ((or (equal? this-arg "-b")
+                      (equal? this-arg "--blacklist"))
+                  (config-blacklist-set! config (car rest-args))
+                  (loop (cdr rest-args)))
+                 ((or (equal? this-arg "-r")
+                      (equal? this-arg "--blacklist-resp"))
+                  (config-blacklist-resp-set! config (car rest-args))
+                  (loop (cdr rest-args)))
                  (else
                   (print-usage progname)))
                 (match args