batman dev

This commit is contained in:
pika 2025-03-22 10:35:17 +01:00
parent a4ce8a291d
commit de000c7ac6
10 changed files with 389 additions and 34 deletions

View file

@ -5,10 +5,30 @@ command_exists() {
command -v "$@" >/dev/null 2>&1
}
scriptPath=""
# Default values
scriptPath="/app/src/update.py"
apiKey="${CADDYDB_API_KEY:-}"
dashboardUrl="${CADDYDB_URL:-http://10.0.0.25:5000/api/update}"
# Check if API key is set
if [ -z "$apiKey" ]; then
echo "Warning: CADDYDB_API_KEY environment variable is not set."
echo "Please set it before running this script."
exit 1
fi
if command_exists python3; then
echo "*/10 * * * * /usr/bin/python3 ${scriptPath}" | crontab -
# Create a wrapper script that includes the API key
wrapperScript="/tmp/caddydb_update_wrapper.sh"
echo "#!/bin/bash" > "$wrapperScript"
echo "export CADDYDB_API_KEY=\"$apiKey\"" >> "$wrapperScript"
echo "python3 $scriptPath --url $dashboardUrl" >> "$wrapperScript"
chmod +x "$wrapperScript"
# Add to crontab
echo "*/10 * * * * $wrapperScript" | crontab -
echo "Added cron job to update CaddyDB every 10 minutes"
else
echo "No python was found.."
exit 1
fi

90
src/nginx_update.py Normal file
View file

@ -0,0 +1,90 @@
#!/usr/bin/env python3
import requests
import re
import socket
import os
import argparse
import sys
import glob
# Default configuration
NGINX_CONF_DIR = "/etc/nginx/sites-enabled"
DASHBOARD_URL = "http://10.0.0.25:5000/api/update"
SERVER_NAME = socket.gethostname()
API_KEY = os.environ.get('CADDYDB_API_KEY', '')
def parse_nginx_configs(conf_dir):
entries = {}
# Get all config files
config_files = glob.glob(f"{conf_dir}/*")
for config_file in config_files:
try:
with open(config_file, "r") as file:
content = file.read()
# Extract server_name and proxy_pass
server_blocks = re.findall(r'server\s*{[^}]*}', content, re.DOTALL)
for block in server_blocks:
server_names = re.search(r'server_name\s+([^;]+);', block)
proxy_pass = re.search(r'proxy_pass\s+([^;]+);', block)
if server_names and proxy_pass:
domains = server_names.group(1).strip().split()
target = proxy_pass.group(1).strip()
for domain in domains:
if domain != '_': # Skip default server
entries[domain] = target
except Exception as e:
print(f"Error reading Nginx config {config_file}: {e}")
return entries
def send_update(url, api_key, server_name, entries):
data = {
"server": server_name,
"entries": entries,
"source_type": "nginx"
}
headers = {"X-API-Key": api_key}
try:
response = requests.post(url, json=data, headers=headers)
if response.status_code == 200:
print(f"Successfully updated {server_name} data")
return True
else:
print(f"Error: {response.status_code} - {response.text}")
return False
except Exception as e:
print(f"Error sending update: {e}")
return False
def main():
parser = argparse.ArgumentParser(description='Update CaddyDB with Nginx reverse proxy entries')
parser.add_argument('--url', default=DASHBOARD_URL, help='CaddyDB API URL')
parser.add_argument('--dir', default=NGINX_CONF_DIR, help='Path to Nginx config directory')
parser.add_argument('--server', default=SERVER_NAME, help='Server name')
parser.add_argument('--key', default=API_KEY, help='API key')
args = parser.parse_args()
if not args.key:
print("Error: API key is required. Set CADDYDB_API_KEY environment variable or use --key")
sys.exit(1)
entries = parse_nginx_configs(args.dir)
if not entries:
print("No entries found in Nginx configurations")
sys.exit(1)
success = send_update(args.url, args.key, args.server, entries)
sys.exit(0 if success else 1)
if __name__ == "__main__":
main()

115
src/traefik_update.py Normal file
View file

@ -0,0 +1,115 @@
#!/usr/bin/env python3
import requests
import socket
import os
import argparse
import sys
import yaml
import toml
import json
# Default configuration
TRAEFIK_CONF_PATH = "/etc/traefik/traefik.yml"
DASHBOARD_URL = "http://10.0.0.25:5000/api/update"
SERVER_NAME = socket.gethostname()
API_KEY = os.environ.get('CADDYDB_API_KEY', '')
def parse_traefik_config(conf_path):
entries = {}
# Determine file type
if conf_path.endswith(('.yml', '.yaml')):
try:
with open(conf_path, 'r') as file:
config = yaml.safe_load(file)
except Exception as e:
print(f"Error reading YAML config: {e}")
return entries
elif conf_path.endswith('.toml'):
try:
with open(conf_path, 'r') as file:
config = toml.load(file)
except Exception as e:
print(f"Error reading TOML config: {e}")
return entries
elif conf_path.endswith('.json'):
try:
with open(conf_path, 'r') as file:
config = json.load(file)
except Exception as e:
print(f"Error reading JSON config: {e}")
return entries
else:
print(f"Unsupported file format: {conf_path}")
return entries
# Try to extract router configurations
try:
# For static configuration
if 'http' in config and 'routers' in config['http']:
for router_name, router in config['http']['routers'].items():
if 'rule' in router and 'service' in router:
# Extract host from rule (assuming Host rule)
host_match = router['rule']
if 'Host(' in host_match:
domain = host_match.split('Host(')[1].split(')')[0].replace('`', '').strip()
service = router['service']
entries[domain] = f"traefik service: {service}"
# For dynamic file providers
if 'providers' in config and 'file' in config['providers']:
file_path = config['providers']['file'].get('filename')
if file_path:
# Recursively parse the dynamic config
dynamic_entries = parse_traefik_config(file_path)
entries.update(dynamic_entries)
except Exception as e:
print(f"Error parsing Traefik config: {e}")
return entries
def send_update(url, api_key, server_name, entries):
data = {
"server": server_name,
"entries": entries,
"source_type": "traefik"
}
headers = {"X-API-Key": api_key}
try:
response = requests.post(url, json=data, headers=headers)
if response.status_code == 200:
print(f"Successfully updated {server_name} data")
return True
else:
print(f"Error: {response.status_code} - {response.text}")
return False
except Exception as e:
print(f"Error sending update: {e}")
return False
def main():
parser = argparse.ArgumentParser(description='Update CaddyDB with Traefik reverse proxy entries')
parser.add_argument('--url', default=DASHBOARD_URL, help='CaddyDB API URL')
parser.add_argument('--config', default=TRAEFIK_CONF_PATH, help='Path to Traefik config file')
parser.add_argument('--server', default=SERVER_NAME, help='Server name')
parser.add_argument('--key', default=API_KEY, help='API key')
args = parser.parse_args()
if not args.key:
print("Error: API key is required. Set CADDYDB_API_KEY environment variable or use --key")
sys.exit(1)
entries = parse_traefik_config(args.config)
if not entries:
print("No entries found in Traefik configuration")
sys.exit(1)
success = send_update(args.url, args.key, args.server, entries)
sys.exit(0 if success else 1)
if __name__ == "__main__":
main()

View file

@ -2,17 +2,21 @@
import requests
import re
# import os
import socket
import os
import argparse
import sys
CADDYFILE_PATH = "/opt/docker/caddy/conf/Caddyfile" # Pfad zur Caddyfile
DASHBOARD_URL = "http://10.0.0.25:5000/update" # Anpassen!
# Default configuration
CADDYFILE_PATH = "/opt/docker/caddy/conf/Caddyfile"
DASHBOARD_URL = "http://10.0.0.25:5000/api/update"
SERVER_NAME = socket.gethostname()
API_KEY = os.environ.get('CADDYDB_API_KEY', '')
def parse_caddyfile():
def parse_caddyfile(file_path):
entries = {}
try:
with open(CADDYFILE_PATH, "r") as file:
with open(file_path, "r") as file:
content = file.read()
pattern = re.compile(r"(?P<domains>[^\s{]+(?:,\s*[^\s{]+)*)\s*{.*?reverse_proxy\s+(?P<target>https?:\/\/[\d\.]+:\d+|[\d\.]+:\d+).*?}", re.DOTALL)
@ -22,16 +26,51 @@ def parse_caddyfile():
for domain in domains.split(", "):
entries[domain] = target.strip()
except Exception as e:
print(f"Fehler beim Lesen der Caddyfile: {e}")
print(f"Error reading Caddyfile: {e}")
return entries
def send_update():
data = {"server": SERVER_NAME, "entries": parse_caddyfile()}
def send_update(url, api_key, server_name, entries, source_type="caddy"):
data = {
"server": server_name,
"entries": entries,
"source_type": source_type
}
headers = {"X-API-Key": api_key}
try:
response = requests.post(DASHBOARD_URL, json=data)
print(response.json())
response = requests.post(url, json=data, headers=headers)
if response.status_code == 200:
print(f"Successfully updated {server_name} data")
return True
else:
print(f"Error: {response.status_code} - {response.text}")
return False
except Exception as e:
print(f"Fehler beim Senden: {e}")
print(f"Error sending update: {e}")
return False
def main():
parser = argparse.ArgumentParser(description='Update CaddyDB with reverse proxy entries')
parser.add_argument('--url', default=DASHBOARD_URL, help='CaddyDB API URL')
parser.add_argument('--file', default=CADDYFILE_PATH, help='Path to Caddyfile')
parser.add_argument('--server', default=SERVER_NAME, help='Server name')
parser.add_argument('--key', default=API_KEY, help='API key')
parser.add_argument('--source', default="caddy", help='Source type (caddy, nginx, traefik)')
args = parser.parse_args()
if not args.key:
print("Error: API key is required. Set CADDYDB_API_KEY environment variable or use --key")
sys.exit(1)
entries = parse_caddyfile(args.file)
if not entries:
print("No entries found in Caddyfile")
sys.exit(1)
success = send_update(args.url, args.key, args.server, entries, args.source)
sys.exit(0 if success else 1)
if __name__ == "__main__":
send_update()
main()