[ prog / sol / mona ]

prog


SPAM BOOTS ON SCHEMEBBS

8 2021-07-07 19:10 *
;;; This is basically the web server in gerbil/src/tutorial/httpd/
;;; (C) vyzo at hackzen.org
;;; insanely fast, you should use that for Scheme web applications

(define (run address)
  (let (httpd (start-http-server! address mux: (make-default-http-mux default-handler)))
    (http-register-handler httpd "/" root-handler)
    (http-register-handler httpd "/check" check-handler)
    (thread-join! httpd)))

;; /
(define (root-handler req res)
  (http-response-write res 200 '(("Content-Type" . "text/plain")) "Ok"))

;; /check
(define (check-handler req res)
  (let* ((params (params->alist (http-request-params req)))
         (ip (cdr (assoc "ip" params))))
    (if (or (is-banned? ip) (in-abuseipdb? ip))
      (http-response-write res 403 '(("Content-Type" . "text/plain")) "banned")
      (http-response-write res 200 '(("Content-Type" . "text/plain")) "not banned"))))

;; default
(define (default-handler req res)
  (http-response-write res 404 '(("Content-Type" . "text/plain"))
    "these aren't the droids you are looking for.\n"))

(def (main . args)
  (for-each (lambda (x) (hash-put! banlist x '() ))
            (map ip->uint (readlines BLACKLIST)))

  (define gopt
    (getopt (option 'address "-a" "--address"
                    help: "server address"
                    default: "127.0.0.1:<port>")))

  (try
   (let (opt (getopt-parse gopt args))
     (run (hash-get opt 'address)))
   (catch (getopt-error? exn)
     (getopt-display-help exn "hellod" (current-error-port))
     (exit 1))))
17


VIP:

do not edit these