wip
This commit is contained in:
parent
e2618b2d35
commit
ad913c0492
2 changed files with 30 additions and 14 deletions
15
agent.py
15
agent.py
|
@ -73,18 +73,27 @@ def parse_caddyfile():
|
|||
with open(CADDYFILE_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)
|
||||
# Improved regex pattern to better handle different Caddyfile formats
|
||||
pattern = re.compile(r"(?P<domains>[^\s{]+(?:,\s*[^\s{]+)*)\s*{[^}]*?(?:reverse_proxy|handle|respond)\s+(?P<target>https?:\/\/[\d\.]+:\d+|[\d\.]+:\d+)[^}]*?}", re.DOTALL)
|
||||
matches = pattern.findall(content)
|
||||
|
||||
for domains, target in matches:
|
||||
for domain in domains.split(", "):
|
||||
for domain in re.split(r',\s*', domains):
|
||||
domain = domain.strip()
|
||||
if domain: # Only add non-empty domains
|
||||
if domain and not domain.lower() == "host": # Skip entries actually named "host"
|
||||
entries[domain] = target.strip()
|
||||
|
||||
logger.info(f"Found {len(entries)} domain entries in Caddyfile")
|
||||
|
||||
# Debug output to help diagnose parsing issues
|
||||
for domain, target in entries.items():
|
||||
logger.debug(f"Parsed domain: {domain} -> {target}")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error parsing Caddyfile: {e}")
|
||||
# Log the error details for debugging
|
||||
import traceback
|
||||
logger.error(traceback.format_exc())
|
||||
|
||||
return entries
|
||||
|
||||
|
|
29
app.py
29
app.py
|
@ -7,6 +7,7 @@ import logging
|
|||
import json
|
||||
from datetime import datetime
|
||||
from dotenv import load_dotenv
|
||||
import re
|
||||
|
||||
# Load environment variables
|
||||
load_dotenv()
|
||||
|
@ -56,31 +57,37 @@ def verify_token(token):
|
|||
return None
|
||||
|
||||
def parse_local_caddyfile():
|
||||
"""Parse a local Caddyfile if LOCAL_MODE is enabled"""
|
||||
if not CADDYFILE_PATH or not os.path.exists(CADDYFILE_PATH):
|
||||
logger.error(f"Local Caddyfile not found at {CADDYFILE_PATH}")
|
||||
return {}
|
||||
|
||||
# Import here to avoid circular imports
|
||||
import re
|
||||
|
||||
"""Parse a local Caddyfile with improved logic matching the agent"""
|
||||
entries = {}
|
||||
try:
|
||||
if not os.path.exists(CADDYFILE_PATH):
|
||||
logger.error(f"Local Caddyfile not found at {CADDYFILE_PATH}")
|
||||
return entries
|
||||
|
||||
with open(CADDYFILE_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)
|
||||
# Improved regex pattern to better handle different Caddyfile formats
|
||||
pattern = re.compile(r"(?P<domains>[^\s{]+(?:,\s*[^\s{]+)*)\s*{[^}]*?(?:reverse_proxy|handle|respond)\s+(?P<target>https?:\/\/[\d\.]+:\d+|[\d\.]+:\d+)[^}]*?}", re.DOTALL)
|
||||
matches = pattern.findall(content)
|
||||
|
||||
for domains, target in matches:
|
||||
for domain in domains.split(", "):
|
||||
for domain in re.split(r',\s*', domains):
|
||||
domain = domain.strip()
|
||||
if domain:
|
||||
if domain and not domain.lower() == "host": # Skip entries actually named "host"
|
||||
entries[domain] = target.strip()
|
||||
|
||||
logger.info(f"Found {len(entries)} domain entries in local Caddyfile")
|
||||
|
||||
# Debug output to help diagnose parsing issues
|
||||
for domain, target in entries.items():
|
||||
logger.debug(f"Parsed domain: {domain} -> {target}")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error parsing local Caddyfile: {e}")
|
||||
# Log the error details for debugging
|
||||
import traceback
|
||||
logger.error(traceback.format_exc())
|
||||
|
||||
return entries
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue