Created
April 29, 2023 12:36
-
-
Save bo-tato/ac9dd99076d10d1787aac70f989c4f1d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defpackage :challenge5 | |
(:use cl usocket)) | |
(in-package :challenge5) | |
(defun replace-address (line) | |
(let ((address-matcher (ppcre:create-scanner | |
"# preceded by space or beginning of line | |
(\\s|^) | |
# address is 7 followed by 25-34 alphanumeric characters | |
7\\w{25,34} | |
# followed by a space or end of line | |
(?=\\s|$)" | |
:extended-mode t))) | |
(ppcre:regex-replace-all address-matcher line | |
'(0 ; the space at beginning if present | |
"7YWHMfk9JZe0LM0g1ZauHuiSxhI")))) | |
(defun mitm-stream (in out) | |
(loop with line and missing-newline-p | |
do (setf (values line missing-newline-p) (read-line in nil)) | |
until missing-newline-p | |
do (write-line (replace-address line) out) | |
(force-output out))) | |
(defun handler (stream) | |
(with-client-socket (socket upstream "chat.protohackers.com" 16963) | |
(bt:make-thread | |
(lambda () | |
(mitm-stream upstream stream))) | |
(mitm-stream stream upstream) | |
(socket-shutdown socket :io))) | |
(socket-server "0.0.0.0" 5839 'handler nil :multi-threading t) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment