Compare commits

...

2 Commits

Author SHA1 Message Date
wei ce21869440 Some fixes for maintenance mode. 2026-05-29 23:17:02 +08:00
wei 7ae8d4330a Update README.md and some fixes. 2026-05-29 23:01:47 +08:00
2 changed files with 46 additions and 8 deletions
+14 -1
View File
@@ -43,6 +43,19 @@ doUpdate: true # set it to true to enable updater
domains: [] # subdomains that you want auto-update (Example: { "name": "www.example.com", "proxied": true }) domains: [] # subdomains that you want auto-update (Example: { "name": "www.example.com", "proxied": true })
zone: null # zone of your domain name (such as example.com) zone: null # zone of your domain name (such as example.com)
ddnsBroadcastCode: code-here # IMPORTANT: Replace it that only you know ddnsBroadcastCode: code-here # IMPORTANT: Replace it that only you know
maintenancePageDNS: "" # Maintenance page DNS name, such as maintenance.example.com
maintenancePageURL: "" # Deprecated fallback. Use maintenancePageDNS instead.
``` ```
Remember to restart the tool after you save the config file. For dns item, use this template:
```
{
"name": "www.example.com", # DNS name
"proxied": false, # Enable proxy (Enable this if you want to hide your server's real IP)
"type": "A", # or "CNAME", "AAA", "TEXT", "MX"
"allowMaintenance": true # Redirect to maintenancePageDNS if maintenance mode is enabled.
}
```
Remember to restart the tool after you save the config file.
+32 -7
View File
@@ -4,6 +4,7 @@ import os
import sys import sys
import threading import threading
import time import time
from urllib.parse import urlparse
import requests import requests
from utils.yaml import yaml_parser, yaml_writer from utils.yaml import yaml_parser, yaml_writer
from settings import CONFIG_DIR from settings import CONFIG_DIR
@@ -31,6 +32,7 @@ cf_config = {
"allowedBroadcastServer": [ "allowedBroadcastServer": [
], ],
"ddnsBroadcastCode": "code-here", "ddnsBroadcastCode": "code-here",
"maintenancePageDNS": "",
"maintenancePageURL": "" "maintenancePageURL": ""
} }
@@ -92,7 +94,7 @@ class CloudflareHelper(Helper):
) )
await message.channel.send(result) await message.channel.send(result)
@self.dc.handle_command("/cf:enable-maintenance", help="Enable maintenance mode") @self.dc.handle_command("/cf:maintenance", help="Toggle maintenance mode")
async def enable_maintenance(client, message): async def enable_maintenance(client, message):
guild = getattr(message, "guild", None) guild = getattr(message, "guild", None)
if guild is None: if guild is None:
@@ -100,7 +102,14 @@ class CloudflareHelper(Helper):
return return
if self.is_allowed_broadcast_server(guild.name): if self.is_allowed_broadcast_server(guild.name):
await message.channel.send(f"Updating DDNS...") if not self.is_maintenance:
self.is_maintenance = True
self.update_ddns()
await message.channel.send(f"Maintenance mode is enabled. Updating DDNS...")
else:
self.is_maintenance = False
self.update_ddns()
await message.channel.send(f"Maintenance mode is disabled. Updating DDNS...")
else: else:
await message.channel.send(f"Unsupported server: {guild.name}") await message.channel.send(f"Unsupported server: {guild.name}")
@@ -224,14 +233,14 @@ class CloudflareHelper(Helper):
self.logger.error("An error occurred while getting DNS records: {}".format(e)) self.logger.error("An error occurred while getting DNS records: {}".format(e))
return return
maintenance_url = self.config.get('maintenancePageURL') maintenance_dns = self.get_maintenance_page_dns()
start_maintenance = False start_maintenance = False
if self.is_maintenance and maintenance_url is not None: if self.is_maintenance and maintenance_dns:
self.logger.info("Maintenance mode is on. Redirecting support dns records to maintenance page...") self.logger.info("Maintenance mode is on. Redirecting support dns records to maintenance page...")
start_maintenance = True start_maintenance = True
elif self.is_maintenance and not maintenance_url: elif self.is_maintenance:
self.logger.warning("Maintenance mode is on but maintenancePageURL is not set yet. Ignoring...") self.logger.warning("Maintenance mode is on but maintenancePageDNS is not set yet. Ignoring...")
for item in domains: for item in domains:
domain = item.get("name", None) domain = item.get("name", None)
@@ -262,7 +271,7 @@ class CloudflareHelper(Helper):
request_data = { request_data = {
"type": domain_type if not start_maintenance or not allow_maintenance else "CNAME", "type": domain_type if not start_maintenance or not allow_maintenance else "CNAME",
"name": domain, "name": domain,
"content": self.ip if not start_maintenance or not allow_maintenance else maintenance_url, "content": self.ip if not start_maintenance or not allow_maintenance else maintenance_dns,
"ttl": 120, "ttl": 120,
"proxied": proxied "proxied": proxied
} }
@@ -286,6 +295,22 @@ class CloudflareHelper(Helper):
self.logger.error(f"Unable to update dns record for domain name {domain}: {e}") self.logger.error(f"Unable to update dns record for domain name {domain}: {e}")
continue continue
def get_maintenance_page_dns(self):
value = self.config.get("maintenancePageDNS") or self.config.get("maintenancePageURL")
if value is None:
return None
value = str(value).strip()
if value == "":
return None
parsed = urlparse(value)
if parsed.hostname:
return parsed.hostname
return value.rstrip("/")
def parse_allowed_broadcast_channels(self): def parse_allowed_broadcast_channels(self):
raw = self.config.get("allowedBroadcastServer", []) raw = self.config.get("allowedBroadcastServer", [])
allowed = {} allowed = {}