Compare commits

..

2 Commits

Author SHA1 Message Date
wei ef160828c1 Some bug fixes. 2026-05-29 22:49:51 +08:00
wei 8cd904a34a Add maintenance url support. 2026-05-29 22:41:53 +08:00
2 changed files with 66 additions and 13 deletions
+49 -8
View File
@@ -30,11 +30,12 @@ cf_config = {
"cfSecret": None,
"allowedBroadcastServer": [
],
"ddnsBroadcastCode": "code-here"
"ddnsBroadcastCode": "code-here",
"maintenancePageURL": ""
}
class CloudflareDDNSHelper(Helper):
class CloudflareHelper(Helper):
def __init__(self, dc):
Helper.__init__(self)
self.dc = dc
@@ -53,10 +54,13 @@ class CloudflareDDNSHelper(Helper):
self.init = False
self.ip = None
# maintenance mode
self.is_maintenance = False
self.config = cf_config
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):
guild = getattr(message, "guild", None)
if guild is None:
@@ -69,7 +73,7 @@ class CloudflareDDNSHelper(Helper):
else:
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):
guild = getattr(message, "guild", None)
if guild is None:
@@ -88,6 +92,30 @@ class CloudflareDDNSHelper(Helper):
)
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:
def __init__(self, helper):
self.helper = helper
@@ -196,10 +224,20 @@ class CloudflareDDNSHelper(Helper):
self.logger.error("An error occurred while getting DNS records: {}".format(e))
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:
domain = item.get("name", None)
proxied = item.get("proxied", False)
domain_type = item.get("type", "A")
allow_maintenance = item.get("allowMaintenance", False)
if domain is None:
continue
@@ -208,9 +246,12 @@ class CloudflareDDNSHelper(Helper):
for record in dns_records:
name = record.get("name", None)
record_type = record.get("type", None)
# record_type = record.get("type", None)
if name == domain and record_type == domain_type:
# if name == domain and record_type == domain_type:
# record_id = record.get("id", None)
if name == domain:
record_id = record.get("id", None)
if record_id is None:
@@ -219,9 +260,9 @@ class CloudflareDDNSHelper(Helper):
continue
request_data = {
"type": domain_type,
"type": domain_type if not start_maintenance or not allow_maintenance else "CNAME",
"name": domain,
"content": self.ip,
"content": self.ip if not start_maintenance or not allow_maintenance else maintenance_url,
"ttl": 120,
"proxied": proxied
}
+17 -5
View File
@@ -6,7 +6,7 @@ import os
import discord
import dotenv
from discord.ext import commands
from helper import CloudflareDDNSHelper
from helper import CloudflareHelper
class DiscordServer(threading.Thread):
def __init__(self, daemon=False, **kwargs):
@@ -42,13 +42,25 @@ class DiscordServer(threading.Thread):
else:
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:
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):
self.commands[command_prefix] = func
self.commands[command_prefix] = {
"func": func,
"help": help
}
def inner(*args, **kwargs):
return func(*args, **kwargs)
@@ -77,7 +89,7 @@ class App:
self.logger.addHandler(sh)
self.dc_server = DiscordServer(daemon=True)
self.cf_helper = CloudflareDDNSHelper(self.dc_server)
self.cf_helper = CloudflareHelper(self.dc_server)
def main(self):
try: