Add maintenance url support.

This commit is contained in:
2026-05-29 22:41:53 +08:00
parent cec96f7014
commit 8cd904a34a
2 changed files with 61 additions and 11 deletions
+44 -6
View File
@@ -30,11 +30,12 @@ cf_config = {
"cfSecret": None, "cfSecret": None,
"allowedBroadcastServer": [ "allowedBroadcastServer": [
], ],
"ddnsBroadcastCode": "code-here" "ddnsBroadcastCode": "code-here",
"maintenancePageURL": ""
} }
class CloudflareDDNSHelper(Helper): class CloudflareHelper(Helper):
def __init__(self, dc): def __init__(self, dc):
Helper.__init__(self) Helper.__init__(self)
self.dc = dc self.dc = dc
@@ -53,10 +54,13 @@ class CloudflareDDNSHelper(Helper):
self.init = False self.init = False
self.ip = None self.ip = None
# maintenance mode
self.is_maintenance = False
self.config = cf_config self.config = cf_config
self.allowed_broadcast_channels = {} self.allowed_broadcast_channels = {}
@self.dc.handle_command("/cf:update_ddns") @self.dc.handle_command("/cf:update_ddns", help="Update cloudflare DNS")
async def renew(client, message): async def renew(client, message):
guild = getattr(message, "guild", None) guild = getattr(message, "guild", None)
if guild is None: if guild is None:
@@ -69,7 +73,7 @@ class CloudflareDDNSHelper(Helper):
else: else:
await message.channel.send(f"Unsupported server: {guild.name}") await message.channel.send(f"Unsupported server: {guild.name}")
@self.dc.handle_command("/cf:add_channel") @self.dc.handle_command("/cf:add_channel", help="Add channel to the cf operator list")
async def add_channel(client, message): async def add_channel(client, message):
guild = getattr(message, "guild", None) guild = getattr(message, "guild", None)
if guild is None: if guild is None:
@@ -88,6 +92,30 @@ class CloudflareDDNSHelper(Helper):
) )
await message.channel.send(result) await message.channel.send(result)
@self.dc.handle_command("/cf:enable-maintenance", help="Enable maintenance mode")
async def enable_maintenance(client, message):
guild = getattr(message, "guild", None)
if guild is None:
await message.channel.send("This command can only be used in server channels.")
return
if self.is_allowed_broadcast_server(guild.name):
await message.channel.send(f"Updating DDNS...")
else:
await message.channel.send(f"Unsupported server: {guild.name}")
@self.dc.handle_command("/cf:status-maintenance", help="Check status of maintenance mode")
async def check_maintenance(client, message):
guild = getattr(message, "guild", None)
if guild is None:
await message.channel.send("This command can only be used in server channels.")
return
if self.is_allowed_broadcast_server(guild.name):
await message.channel.send("Maintenance mode is on" if self.is_maintenance else "Maintenance mode is off")
else:
await message.channel.send(f"Unsupported server: {guild.name}")
class DCStream: class DCStream:
def __init__(self, helper): def __init__(self, helper):
self.helper = helper self.helper = helper
@@ -196,10 +224,20 @@ class CloudflareDDNSHelper(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')
start_maintenance = False
if self.is_maintenance and maintenance_url is not None:
self.logger.info("Maintenance mode is on. Redirecting support dns records to maintenance page...")
start_maintenance = True
elif self.is_maintenance and not maintenance_url:
self.logger.warning("Maintenance mode is on but maintenancePageURL is not set yet. Ignoring...")
for item in domains: for item in domains:
domain = item.get("name", None) domain = item.get("name", None)
proxied = item.get("proxied", False) proxied = item.get("proxied", False)
domain_type = item.get("type", "A") domain_type = item.get("type", "A")
allow_maintenance = item.get("allowMaintenance", False)
if domain is None: if domain is None:
continue continue
@@ -219,9 +257,9 @@ class CloudflareDDNSHelper(Helper):
continue continue
request_data = { request_data = {
"type": domain_type, "type": domain_type if not start_maintenance or not allow_maintenance else "CNAME",
"name": domain, "name": domain,
"content": self.ip, "content": self.ip if not start_maintenance or not allow_maintenance else maintenance_url,
"ttl": 120, "ttl": 120,
"proxied": proxied "proxied": proxied
} }
+17 -5
View File
@@ -6,7 +6,7 @@ import os
import discord import discord
import dotenv import dotenv
from discord.ext import commands from discord.ext import commands
from helper import CloudflareDDNSHelper from helper import CloudflareHelper
class DiscordServer(threading.Thread): class DiscordServer(threading.Thread):
def __init__(self, daemon=False, **kwargs): def __init__(self, daemon=False, **kwargs):
@@ -42,13 +42,25 @@ class DiscordServer(threading.Thread):
else: else:
await message.channel.send(f'You are in private chat.') await message.channel.send(f'You are in private chat.')
if message.content.startswith('/help'):
msg = "Commands:\n"
for command in self.commands:
msg += f'- {command}: {self.commands[command].get('help') if self.commands[command].get('help') else "A command"}\n'
await message.channel.send(
msg
)
for command in self.commands: for command in self.commands:
if message.content.startswith(command): if message.content.startswith(command):
await self.commands[command](self.client, message) await self.commands[command].get("func")(self.client, message)
def handle_command(self, command_prefix): def handle_command(self, command_prefix, help=None):
def decorator(func): def decorator(func):
self.commands[command_prefix] = func self.commands[command_prefix] = {
"func": func,
"help": help
}
def inner(*args, **kwargs): def inner(*args, **kwargs):
return func(*args, **kwargs) return func(*args, **kwargs)
@@ -77,7 +89,7 @@ class App:
self.logger.addHandler(sh) self.logger.addHandler(sh)
self.dc_server = DiscordServer(daemon=True) self.dc_server = DiscordServer(daemon=True)
self.cf_helper = CloudflareDDNSHelper(self.dc_server) self.cf_helper = CloudflareHelper(self.dc_server)
def main(self): def main(self):
try: try: