#!/usr/bin/env python3
import json
import subprocess
from pathlib import Path
sites = {}
source = Path('/etc/userdatadomains')
for raw in source.read_text(errors='replace').splitlines() if source.exists() else []:
parts = raw.split('==')
if len(parts) < 5 or ':' not in parts[0]:
continue
domain = parts[0].split(':', 1)[0].strip().lower().rstrip('.')
root = Path(parts[4].strip())
if root.is_dir() and ((root / 'wp-config.php').is_file() or ((root / 'wp-admin').is_dir() and (root / 'wp-includes').is_dir())):
sites.setdefault(str(root), domain)
managed = []
setup_rule_present = False
for root, domain in sites.items():
path = Path(root) / '.htaccess'
try:
text = path.read_text(errors='replace')
except Exception:
continue
if '# BEGIN wp-MM-System request protection' in text and 'SERankingBacklinksBot' in text:
managed.append((root, domain))
start = text.find('# BEGIN wp-MM-System request protection')
end = text.find('# END wp-MM-System request protection', start)
block = text[start:end] if end >= 0 else text[start:]
setup_rule_present = setup_rule_present or 'setup-config' in block
test_domain = managed[0][1] if managed else ''
codes = {}
if test_domain:
for name, path, ua in (
('xmlrpc', '/xmlrpc.php', ''),
('setup', '/wp-admin/setup-config.php', ''),
('bot', '/', 'SERankingBacklinksBot/1.0'),
):
command = ['curl', '-sS', '-H', 'Host: ' + test_domain, '-o', '/dev/null', '-w', '%{http_code}', '--max-time', '8']
if ua:
command += ['-A', ua]
command += ['http://127.0.0.1' + path]
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
codes[name] = result.stdout.strip() or ('error:' + result.stderr.strip()[-120:])
backups = sorted(Path('/root/wpmm-backups').glob('request-protection-*'), key=lambda p: p.stat().st_mtime, reverse=True)
ok = bool(test_domain) and codes.get('xmlrpc') == '403' and codes.get('bot') == '403' and not setup_rule_present
print(json.dumps({'sites': len(sites), 'managed': len(managed), 'test_domain': test_domain, 'codes': codes, 'setup_rule_present': setup_rule_present, 'ok': ok, 'latest_backup': str(backups[0]) if backups else ''}, ensure_ascii=False))