|
@@ -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,8 @@ 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
|
|
|
|
|
|
|
|
|
def refreshF2boptions():
|
|
@@ -250,9 +218,10 @@ def clear():
|
|
|
with lock:
|
|
|
tables.clearIPv4Table()
|
|
|
tables.clearIPv6Table()
|
|
|
- r.delete('F2B_ACTIVE_BANS')
|
|
|
- r.delete('F2B_PERM_BANS')
|
|
|
- pubsub.unsubscribe()
|
|
|
+ if r:
|
|
|
+ r.delete('F2B_ACTIVE_BANS')
|
|
|
+ r.delete('F2B_PERM_BANS')
|
|
|
+ pubsub.unsubscribe()
|
|
|
|
|
|
def watch():
|
|
|
logger.logInfo('Watching Redis channel F2B_CHANNEL')
|
|
@@ -409,15 +378,60 @@ def quit(signum, frame):
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
- refreshF2boptions()
|
|
|
+ # 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()
|
|
|
+ except Exception as ex:
|
|
|
+ print('%s - trying again in 3 seconds' % (ex))
|
|
|
+ time.sleep(3)
|
|
|
+ else:
|
|
|
+ break
|
|
|
+ pubsub = r.pubsub()
|
|
|
+ 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()
|