[ prog / sol / mona ]

prog


SPAM BOOTS ON SCHEMEBBS

7 2021-07-07 19:01 *

The authentication server, below. Gerbil and Gambit were a nightmare to port on FreeBSD and once again patches weren't shared upstream because having to register anywhere to post something is such a pain. Gerbil's included batteries allows to write server programs roughly as terses as their goland counterparts.

(import :std/net/httpd
        :std/net/address
        :std/text/json
        :std/sugar
        :std/iter
        :std/getopt
        :std/misc/alist
        :std/net/request
        :gerbil/gambit/threads
        :std/misc/bytes)
(export main)

(define BLACKLIST "listed_ip_180.txt") ; fetch https://www.stopforumspam.com/downloads/listed_ip_180.zip
(define APIKEY "<get_your_own>")
(define banlist (make-hash-table-eq))

(define (params->alist p)
   (map (lambda (x)
          (let (l (string-split x #\=))
            (cons (car l) (cadr l))))
        (string-split p #\&)))

(define (ip->uint ip)
  (u8vector->uint
   (uint-list->u8vector
    (map string->number (string-split ip #\.))
    'little
    1)))

(define (uint->ip n)
  (string-join
   (map number->string (u8vector->list (uint->u8vector n)))
   #\.))

(define (readlines file)
 (let ((infile (open-input-file file)))
   (let loop ((lines '())
              (next-line (read-line infile)))
    (if (eof-object? next-line)
        (begin (close-input-port infile)
               (reverse lines))
        (loop (cons next-line  lines)
              (read-line infile))))))

(define (is-banned? ip)
  (hash-key? banlist (ip->uint ip)))

(define (in-abuseipdb? ip)
  (let* ((json (http-get "http://api.abuseipdb.com/api/v2/check"
                        headers: `(("Key" . ,APIKEY)
                                   ("Accept" . "application/json"))
                        params: `(("ipAddress" . ,ip))))
         (score (hash-get (hash-get (request-json json) 'data) 'abuseConfidenceScore)))
    (if (> score 1)
      (begin (hash-put! banlist (ip->uint ip) '()) #t)
      #f)))
17


VIP:

do not edit these