/
plugin.py
74 lines (56 loc) · 2.62 KB
/
plugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from supybot import callbacks, httpserver, schedule, world
from supybot.commands import wrap
# Minimize the amount of valid endpoints
if httpserver.http_servers and ".well-known" in httpserver.http_servers[0].callbacks:
del httpserver.http_servers[0].callbacks[".well-known"]
# Overwrite the original do_X. It does all kinds of weird stuff, like an
# overview page which plugins are loaded, and CSS, and more of that ..
# minimize what is open to the Internet, only respond to URLs we expect.
def patched_do_X(self, callbackMethod, *args, **kwargs):
if self.path == "/healthz":
self.send_response(200)
self.send_header("Content-Type", "text/plain")
self.end_headers()
self.wfile.write(b"200: OK")
return
if self.path == "/robots.txt":
self.send_response(200)
self.send_header("Content-Type", "text/plain")
self.end_headers()
self.wfile.write(b"User-agent: *\nDisallow: /")
return
if self.path.count("/") <= 1:
self.send_response(404)
self.send_header("Content-Type", "text/plain")
self.end_headers()
self.wfile.write(b"404: page not found")
return
httpserver.original_do_X(self, callbackMethod, *args, **kwargs)
# Only log requests that hit an error code
def patched_log_message(self, format, *args):
if not (200 <= int(args[1]) < 400):
httpserver.original_log_message(self, format, *args)
# Ensure we only capture the original function once; this plugin can be
# reloaded many times.
if not hasattr(httpserver, "original_do_X"):
httpserver.original_do_X = httpserver.SupyHTTPRequestHandler.do_X
httpserver.original_log_message = httpserver.SupyHTTPRequestHandler.log_message
httpserver.SupyHTTPRequestHandler.do_X = patched_do_X
httpserver.SupyHTTPRequestHandler.log_message = patched_log_message
class OpenTTD(callbacks.Plugin):
"""Some commands specific for OpenTTD channels."""
def __init__(self, irc):
super().__init__(irc)
# We have the flush disabled in the configuration, but this also stops
# flushing of database-files. So every 2 minutes we still flush, which
# ironically doesn't flush everything, but only the database-files.
schedule.addPeriodicEvent(world.flush, 120, name="flush", now=False)
def ports(self, irc, msg, args):
"""takes no arguments"""
irc.reply(
"OpenTTD uses TCP port 3979 for server <-> client communication, "
"UDP port 3979 for LAN discovery, "
"and TCP port 3978 for content service, a.k.a. BaNaNaS (outbound)"
)
ports = wrap(ports)
Class = OpenTTD