0

Fail2ban is used to block users on certain patterns via reject command. Sometimes, especially when users are behind a NAT with a lot of users they get blocked unintendely. The user then writes an E-Mail and then I ask for his IP-Address to unblock (often E-Mail is forwarded to me, so I don't have the Mail header with his IP address).

To make this step easier, I'd like to redirect him to a special dynamic webpage that displays its IP-address and contact E-Mail. An example is here:

enter image description here

I'd like to have a similar webpage. An easy solution would be php, but that requires php to be installed and some webserver configuration. So simplest solution would be a small webserver that just serves a small text an IP address.

I've considered using python, because Python is already installed. I've found this article https://www.anycodings.com/1questions/2464569/how-do-i-get-the-client-ip-address-on-a-python-server but that prints the IP-Address only on stdout and serves files.

To use python as simple as with php it would require Flask or Django as far as I found out. However these are big frameworks and much oversized for this simple usecase. Also they probably can mean some security issue. This info page should also be secure by design so not creating any possible security issues compared to reject method. I know DOS is more likely because of more data transmitted than using reject, but size of this page is very small so this would be OK.

So what is an easy solution for this task? Python? If so could you hint some keywords I can search for to achieve this task? Are there other solutions? Maybe these fit even better than the python idea.

Hannes
  • 157
  • 8

1 Answers1

0

Found the solution here https://pythonbasics.org/webserver/

So basically you only need to add one line self.wfile.write(bytes("<p>IP-Address: " + self.client_address[0] + "</p>", "utf-8")) The client_address field is documented here https://docs.python.org/3/library/http.server.html#http.server.BaseHTTPRequestHandler.client_address

So the full code for displaying the IP address of visitor is:

from http.server import BaseHTTPRequestHandler, HTTPServer

listenAddress = "0.0.0.0"
serverPort = 8080

class MyServer(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()
        self.wfile.write(bytes("<html><head><title>https://pythonbasics.org</title></head>", "utf-8"))
        self.wfile.write(bytes("<p>Request: %s</p>" % self.path, "utf-8"))
        self.wfile.write(bytes("<body>", "utf-8"))
        self.wfile.write(bytes("<p>This is an example web server.</p>", "utf-8"))
        self.wfile.write(bytes("<p>IP-Address: " + self.client_address[0] + "</p>", "utf-8"))
        self.wfile.write(bytes("</body></html>", "utf-8"))

if __name__ == "__main__":        
    webServer = HTTPServer((listenAddress, serverPort), MyServer)
    print("Server started http://%s:%s" % (listenAddress, serverPort))

    try:
        webServer.serve_forever()
    except KeyboardInterrupt:
        pass

    webServer.server_close()
    print("Server stopped.")
Hannes
  • 157
  • 8