|
@@ -21,28 +21,6 @@ from modules.IPTables import IPTables
|
|
|
from modules.NFTables import NFTables
|
|
|
|
|
|
|
|
|
-# connect to redis
|
|
|
-while True:
|
|
|
- try:
|
|
|
- redis_slaveof_ip = os.getenv('REDIS_SLAVEOF_IP', '')
|
|
|
- redis_slaveof_port = os.getenv('REDIS_SLAVEOF_PORT', '')
|
|
|
- if "".__eq__(redis_slaveof_ip):
|
|
|
- r = redis.StrictRedis(host=os.getenv('IPV4_NETWORK', '172.22.1') + '.249', decode_responses=True, port=6379, db=0)
|
|
|
- else:
|
|
|
- r = redis.StrictRedis(host=redis_slaveof_ip, decode_responses=True, port=redis_slaveof_port, db=0)
|
|
|
- r.ping()
|
|
|
- except Exception as ex:
|
|
|
- print('%s - trying again in 3 seconds' % (ex))
|
|
|
- time.sleep(3)
|
|
|
- else:
|
|
|
- break
|
|
|
-pubsub = r.pubsub()
|
|
|
-
|
|
|
-# rename fail2ban to netfilter
|
|
|
-if r.exists('F2B_LOG'):
|
|
|
- r.rename('F2B_LOG', 'NETFILTER_LOG')
|
|
|
-
|
|
|
-
|
|
|
# globals
|
|
|
WHITELIST = []
|
|
|
BLACKLIST= []
|
|
@@ -50,18 +28,10 @@ bans = {}
|
|
|
quit_now = False
|
|
|
exit_code = 0
|
|
|
lock = Lock()
|
|
|
-
|
|
|
-
|
|
|
-# init Logger
|
|
|
-logger = Logger(r)
|
|
|
-# init backend
|
|
|
-backend = sys.argv[1]
|
|
|
-if backend == "nftables":
|
|
|
- logger.logInfo('Using NFTables backend')
|
|
|
- tables = NFTables("MAILCOW", logger)
|
|
|
-else:
|
|
|
- logger.logInfo('Using IPTables backend')
|
|
|
- tables = IPTables("MAILCOW", logger)
|
|
|
+chain_name = "MAILCOW"
|
|
|
+r = None
|
|
|
+pubsub = None
|
|
|
+clear_before_quit = False
|
|
|
|
|
|
|
|
|
def refreshF2boptions():
|
|
@@ -250,17 +220,21 @@ def clear():
|
|
|
with lock:
|
|
|
tables.clearIPv4Table()
|
|
|
tables.clearIPv6Table()
|
|
|
- r.delete('F2B_ACTIVE_BANS')
|
|
|
- r.delete('F2B_PERM_BANS')
|
|
|
- pubsub.unsubscribe()
|
|
|
+ try:
|
|
|
+ if r is not None:
|
|
|
+ r.delete('F2B_ACTIVE_BANS')
|
|
|
+ r.delete('F2B_PERM_BANS')
|
|
|
+ except Exception as ex:
|
|
|
+ logger.logWarn('Error clearing redis keys F2B_ACTIVE_BANS and F2B_PERM_BANS: %s' % ex)
|
|
|
|
|
|
def watch():
|
|
|
- logger.logInfo('Watching Redis channel F2B_CHANNEL')
|
|
|
- pubsub.subscribe('F2B_CHANNEL')
|
|
|
-
|
|
|
+ global pubsub
|
|
|
global quit_now
|
|
|
global exit_code
|
|
|
|
|
|
+ logger.logInfo('Watching Redis channel F2B_CHANNEL')
|
|
|
+ pubsub.subscribe('F2B_CHANNEL')
|
|
|
+
|
|
|
while not quit_now:
|
|
|
try:
|
|
|
for item in pubsub.listen():
|
|
@@ -280,6 +254,7 @@ def watch():
|
|
|
ban(addr)
|
|
|
except Exception as ex:
|
|
|
logger.logWarn('Error reading log line from pubsub: %s' % ex)
|
|
|
+ pubsub = None
|
|
|
quit_now = True
|
|
|
exit_code = 2
|
|
|
|
|
@@ -403,21 +378,76 @@ def blacklistUpdate():
|
|
|
permBan(net=net, unban=True)
|
|
|
time.sleep(60.0 - ((time.time() - start_time) % 60.0))
|
|
|
|
|
|
-def quit(signum, frame):
|
|
|
- global quit_now
|
|
|
- quit_now = True
|
|
|
+def sigterm_quit(signum, frame):
|
|
|
+ global clear_before_quit
|
|
|
+ clear_before_quit = True
|
|
|
+ sys.exit(exit_code)
|
|
|
+
|
|
|
+def berfore_quit():
|
|
|
+ if clear_before_quit:
|
|
|
+ clear()
|
|
|
+ if pubsub is not None:
|
|
|
+ pubsub.unsubscribe()
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
- refreshF2boptions()
|
|
|
+ atexit.register(berfore_quit)
|
|
|
+ signal.signal(signal.SIGTERM, sigterm_quit)
|
|
|
+
|
|
|
+ # init Logger
|
|
|
+ logger = Logger(None)
|
|
|
+
|
|
|
+ # init backend
|
|
|
+ backend = sys.argv[1]
|
|
|
+ if backend == "nftables":
|
|
|
+ logger.logInfo('Using NFTables backend')
|
|
|
+ tables = NFTables(chain_name, logger)
|
|
|
+ else:
|
|
|
+ logger.logInfo('Using IPTables backend')
|
|
|
+ tables = IPTables(chain_name, logger)
|
|
|
+
|
|
|
# In case a previous session was killed without cleanup
|
|
|
clear()
|
|
|
+
|
|
|
# Reinit MAILCOW chain
|
|
|
# Is called before threads start, no locking
|
|
|
logger.logInfo("Initializing mailcow netfilter chain")
|
|
|
tables.initChainIPv4()
|
|
|
tables.initChainIPv6()
|
|
|
|
|
|
+ if os.getenv("DISABLE_NETFILTER_ISOLATION_RULE").lower() in ("y", "yes"):
|
|
|
+ logger.logInfo(f"Skipping {chain_name} isolation")
|
|
|
+ else:
|
|
|
+ logger.logInfo(f"Setting {chain_name} isolation")
|
|
|
+ tables.create_mailcow_isolation_rule("br-mailcow", [3306, 6379, 8983, 12345], os.getenv("MAILCOW_REPLICA_IP"))
|
|
|
+
|
|
|
+ # connect to redis
|
|
|
+ while True:
|
|
|
+ try:
|
|
|
+ redis_slaveof_ip = os.getenv('REDIS_SLAVEOF_IP', '')
|
|
|
+ redis_slaveof_port = os.getenv('REDIS_SLAVEOF_PORT', '')
|
|
|
+ if "".__eq__(redis_slaveof_ip):
|
|
|
+ r = redis.StrictRedis(host=os.getenv('IPV4_NETWORK', '172.22.1') + '.249', decode_responses=True, port=6379, db=0)
|
|
|
+ else:
|
|
|
+ r = redis.StrictRedis(host=redis_slaveof_ip, decode_responses=True, port=redis_slaveof_port, db=0)
|
|
|
+ r.ping()
|
|
|
+ pubsub = r.pubsub()
|
|
|
+ except Exception as ex:
|
|
|
+ print('%s - trying again in 3 seconds' % (ex))
|
|
|
+ time.sleep(3)
|
|
|
+ else:
|
|
|
+ break
|
|
|
+ Logger.r = r
|
|
|
+
|
|
|
+ # rename fail2ban to netfilter
|
|
|
+ if r.exists('F2B_LOG'):
|
|
|
+ r.rename('F2B_LOG', 'NETFILTER_LOG')
|
|
|
+ # clear bans in redis
|
|
|
+ r.delete('F2B_ACTIVE_BANS')
|
|
|
+ r.delete('F2B_PERM_BANS')
|
|
|
+
|
|
|
+ refreshF2boptions()
|
|
|
+
|
|
|
watch_thread = Thread(target=watch)
|
|
|
watch_thread.daemon = True
|
|
|
watch_thread.start()
|
|
@@ -460,9 +490,6 @@ if __name__ == '__main__':
|
|
|
whitelistupdate_thread.daemon = True
|
|
|
whitelistupdate_thread.start()
|
|
|
|
|
|
- signal.signal(signal.SIGTERM, quit)
|
|
|
- atexit.register(clear)
|
|
|
-
|
|
|
while not quit_now:
|
|
|
time.sleep(0.5)
|
|
|
|