X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?p=rags.git;a=blobdiff_plain;f=rags.scm;h=febfd823f986ac4a36478c9c3904c16ad7a921db;hp=6eacfc42970f2e1a691ecbb64a71efd590139a0d;hb=HEAD;hpb=316325e01f929f70c7a31b7b65cd5082ff638957 diff --git a/rags.scm b/rags.scm index 6eacfc4..febfd82 100644 --- a/rags.scm +++ b/rags.scm @@ -17,11 +17,12 @@ (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") @@ -193,7 +194,14 @@ "Port: '" (config-port config) "'\n" "Root directory: '" (config-root-dir config) "'\n" "Cert file: '" (config-certfile config) "'\n" - "Key file: '" (config-keyfile config) "'\n") + "Key file: '" (config-keyfile config) "'") + + (if (config-blacklist config) + (print "Blacklist file: '" (config-blacklist config) "'")) + (if (config-blacklist-resp config) + (print "Blacklist responce file: '" (config-blacklist-resp config) "'")) + + (print) (print* "Dropping privilages ... ") (drop-privs config) @@ -230,15 +238,33 @@ (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 (and (config-blacklist config) + (member remote-ip + (with-input-from-file + (config-blacklist config) + read))) + (begin + (print "Connection from blacklisted IP. Closing.") + (with-output-to-port out-port + (lambda () + (serve-document-header (ext->mime "txt")) + (print "Refusing to serve to IP " remote-ip ".\n") + (when (config-blacklist-resp config) + (print) + (for-each print + (with-input-from-file + (config-blacklist-resp config) + read-lines)))))) + (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."))) (o (exn) (print-error-message o)))) (close-input-port in-port) @@ -251,11 +277,16 @@ (print "Usage:\n" progname " [-h/--help]\n" progname " [-p/--port PORT] [-u/--user UID] [-g/--group GID]\n" - indent-str " server-root-dir hostname certfile keyfile"))) + indent-str " [-b/--blacklist FILE] [-r/--blacklist-resp RESPFILE]\n" + indent-str " server-root-dir hostname certfile keyfile\n" + "\n" + "The -b option can be used to specify a FILE containing a list of IP addresses\n" + "to block from the server. If a connection from a blocked address is served,\n" + "the response file RESPFILE is served instead, if this is provided."))) (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))) @@ -278,6 +309,14 @@ (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