AP manageur V1.2 (Bêta)

This commit is contained in:
yoannmultigaming committed 2026-11-23 01:24:08 +01:00
1 parent e2eaea4b46
commit 165a06be22
6 files changed
+958 -54

No files matched your search

+102 -42
View File
@@ -10755,7 +10755,7 @@ def download_remote_file(remote_filename, dest_path, custom_cfg=None):
def list_remote_files_detailed(custom_cfg=None):
cfg = custom_cfg or get_backup_remote_config()
if not cfg.get('enabled'):
if not cfg.get('enabled') and not custom_cfg:
return None, "Stockage distant désactivé"
provider = str(cfg.get('provider', 'ftp')).strip().lower()
p_cfg = cfg.get(provider, {}) if isinstance(cfg.get(provider), dict) else {}
@@ -11493,7 +11493,7 @@ class Handler(BaseHTTPRequestHandler):
except Exception as e:
self.send_json({'error': f'Erreur génération archive ZIP: {e}'}, 500); return
if self.path.startswith('/api/backups/list'):
if not valid_session(self): self.send_json({'error':'Authentification requise'},401); return
if not valid_session(self) and auth_state()['configured']: self.send_json({'error':'Authentification requise'},401); return
parsed = urllib.parse.urlparse(self.path)
qs = urllib.parse.parse_qs(parsed.query)
if qs.get('sync_remote', ['0'])[0] in ('1', 'true'):
@@ -11513,10 +11513,10 @@ class Handler(BaseHTTPRequestHandler):
'total_size_str': format_bytes(total_size)
}); return
if self.path == '/api/backups/remote':
if not valid_session(self): self.send_json({'error':'Authentification requise'},401); return
if not valid_session(self) and auth_state()['configured']: self.send_json({'error':'Authentification requise'},401); return
self.send_json({'ok': True, 'remote': get_backup_remote_config()}); return
if self.path.startswith('/api/backups/remote/files'):
if not valid_session(self): self.send_json({'error':'Authentification requise'},401); return
if not valid_session(self) and auth_state()['configured']: self.send_json({'error':'Authentification requise'},401); return
rcfg = get_backup_remote_config()
if not rcfg.get('enabled'):
self.send_json({'ok': False, 'error': "Le stockage distant n'est pas activé. Activez-le dans les réglages de sauvegarde.", 'enabled': False}, 400); return
@@ -12249,15 +12249,27 @@ class Handler(BaseHTTPRequestHandler):
raw_json = data.get('backup')
if raw_json and isinstance(raw_json, dict):
self.send_json({'ok': True, 'filename': fname, 'backup': raw_json}); return
if not b64_content:
self.send_json({'error': 'Contenu du fichier manquant'}, 400); return
try:
raw_bytes = base64.b64decode(b64_content)
backup = inspect_backup_bytes(raw_bytes, fname)
self.send_json({'ok': True, 'filename': fname, 'backup': backup})
except Exception as e:
self.send_json({'error': f'Impossible d’analyser le fichier : {e}'}, 400)
return
if b64_content:
try:
raw_bytes = base64.b64decode(b64_content)
backup = inspect_backup_bytes(raw_bytes, fname)
self.send_json({'ok': True, 'filename': fname, 'backup': backup})
except Exception as e:
self.send_json({'error': f'Impossible d’analyser le fichier : {e}'}, 400)
return
if fname:
full_path, clean_name = safe_backup_filepath(fname)
if os.path.isfile(full_path):
try:
with open(full_path, 'rb') as f_read:
raw_bytes = f_read.read()
backup = inspect_backup_bytes(raw_bytes, clean_name)
sz = os.path.getsize(full_path)
self.send_json({'ok': True, 'filename': clean_name, 'size': sz, 'backup': backup})
return
except Exception as e:
self.send_json({'error': f'Impossible d’analyser le fichier local : {e}'}, 400); return
self.send_json({'error': 'Contenu du fichier manquant ou fichier local introuvable'}, 400); return
if self.path == '/api/backup/import':
if not valid_session(self) and auth_state()['configured']: self.send_json({'error':'Authentification requise'},401); return
try:
@@ -12268,6 +12280,14 @@ class Handler(BaseHTTPRequestHandler):
raw_bytes = base64.b64decode(data['data_base64'])
backup = inspect_backup_bytes(raw_bytes, data.get('filename', ''))
stats = import_backup_data(backup, data.get('options', {}))
elif 'filename' in data and data['filename'] and not data.get('backup'):
full_path, clean_name = safe_backup_filepath(data['filename'])
if not os.path.isfile(full_path):
self.send_json({'error': 'Fichier de sauvegarde introuvable'}, 404); return
with open(full_path, 'rb') as f_read:
raw_bytes = f_read.read()
backup = inspect_backup_bytes(raw_bytes, clean_name)
stats = import_backup_data(backup, data.get('options', {}))
else:
stats = import_backup_data(data)
invalidate_status_cache()
@@ -12282,6 +12302,75 @@ class Handler(BaseHTTPRequestHandler):
except Exception as e:
self.send_json({'error': f'Erreur lors de la restauration: {e}'}, 400)
return
if self.path == '/api/backups/remote/test':
if not valid_session(self) and auth_state()['configured']: self.send_json({'error':'Authentification requise'},401); return
custom_cfg = data.get('config') if isinstance(data, dict) and 'config' in data else None
try:
ok, msg = test_backup_remote(custom_cfg)
self.send_json({'ok': ok, 'message': msg}); return
except Exception as e:
self.send_json({'ok': False, 'error': str(e)}, 500); return
if self.path == '/api/backups/remote/files':
if not valid_session(self) and auth_state()['configured']: self.send_json({'error':'Authentification requise'},401); return
custom_cfg = data.get('config') if (isinstance(data, dict) and data.get('config')) else None
rcfg = custom_cfg or get_backup_remote_config()
if not rcfg.get('enabled') and not custom_cfg:
self.send_json({'ok': False, 'error': "Le stockage distant n'est pas activé.", 'enabled': False}, 400); return
provider = str(rcfg.get('provider', 'ftp')).strip().lower()
try:
items, err = list_remote_files_detailed(rcfg)
if items is None:
self.send_json({'ok': False, 'error': err or f"Impossible de contacter le stockage distant ({provider.upper()})", 'provider': provider}, 500); return
prov_names = {'gdrive': 'Google Drive', 'dropbox': 'Dropbox', 'sftp': 'SFTP', 'ftp': 'FTP', 'webdav': 'WebDAV', 's3': 'S3', 'webhook': 'Webhook'}
self.send_json({
'ok': True,
'enabled': True,
'provider': provider,
'provider_name': prov_names.get(provider, provider.upper()),
'files': items,
'count': len(items)
}); return
except Exception as e:
self.send_json({'ok': False, 'error': f"Erreur lors de la lecture des fichiers distants : {e}"}, 500); return
if self.path == '/api/backups/remote/file/pull':
if not valid_session(self) and auth_state()['configured']: self.send_json({'error':'Authentification requise'},401); return
fname = str(data.get('filename', '')).strip()
if not fname or not fname.lower().endswith(BACKUP_ALLOWED_EXTS):
self.send_json({'error': 'Nom de fichier invalide'}, 400); return
clean_name = os.path.basename(fname)
custom_cfg = data.get('config') if isinstance(data, dict) and 'config' in data else None
ensure_backup_dir()
full_path = os.path.join(BACKUP_DIR, clean_name)
tmp_dest = full_path + '.download.tmp'
try:
download_remote_file(clean_name, tmp_dest, custom_cfg=custom_cfg)
os.replace(tmp_dest, full_path)
try:
sync_remote_backups_presence(force=True)
except Exception:
pass
log('recuperation_sauvegarde_distante', f"{username(self) or 'Administrateur'} ({clean_name})", request_ip(self), 'BACKUP')
backup_data = None
try:
with open(full_path, 'rb') as f_read:
backup_data = inspect_backup_bytes(f_read.read(), clean_name)
except Exception as e_insp:
print(f"[ap-backup] Avertissement inspection pull : {e_insp}", flush=True)
file_size = os.path.getsize(full_path) if os.path.exists(full_path) else 0
items, total_size = list_server_backups()
self.send_json({
'ok': True,
'message': f"Sauvegarde « {clean_name} » importée avec succès !",
'filename': clean_name,
'size': file_size,
'backup': backup_data,
'backups': items
}); return
except Exception as e:
if os.path.exists(tmp_dest):
try: os.unlink(tmp_dest)
except OSError: pass
self.send_json({'error': f"Échec récupération sauvegarde : {e}"}, 500); return
if self.path == '/api/vpn/profiles':
if not valid_session(self) and auth_state()['configured']: self.send_json({'error':'Authentification requise'},401); return
pid = create_or_update_vpn_profile(data)
@@ -12729,10 +12818,6 @@ class Handler(BaseHTTPRequestHandler):
rcfg = save_backup_remote_config(data)
log('configuration_stockage_distant', f"{username(self) or 'Administrateur'} ({rcfg.get('provider', '')})", request_ip(self), 'BACKUP')
self.send_json({'ok': True, 'message': 'Configuration du stockage distant enregistrée avec succès', 'remote': rcfg}); return
if self.path == '/api/backups/remote/test':
custom_cfg = data.get('config') if isinstance(data, dict) and 'config' in data else None
ok, msg = test_backup_remote(custom_cfg)
self.send_json({'ok': True, 'message': msg}); return
if self.path == '/api/backups/remote/upload':
fname = str(data.get('filename', '')).strip()
full_path, clean_name = safe_backup_filepath(fname)
@@ -12747,31 +12832,6 @@ class Handler(BaseHTTPRequestHandler):
log('televersement_distant_manuel', f"{username(self) or 'Administrateur'} ({clean_name})", request_ip(self), 'BACKUP')
items, total_size = list_server_backups()
self.send_json({'ok': True, 'message': msg, 'remote': get_backup_remote_config(), 'backups': items}); return
if self.path == '/api/backups/remote/file/pull':
if not valid_session(self): self.send_json({'error':'Authentification requise'},401); return
fname = str(data.get('filename', '')).strip()
if not fname or not fname.lower().endswith(BACKUP_ALLOWED_EXTS):
self.send_json({'error': 'Nom de fichier invalide'}, 400); return
clean_name = os.path.basename(fname)
ensure_backup_dir()
full_path = os.path.join(BACKUP_DIR, clean_name)
tmp_dest = full_path + '.download.tmp'
try:
download_remote_file(clean_name, tmp_dest)
os.replace(tmp_dest, full_path)
sync_remote_backups_presence(force=True)
log('recuperation_sauvegarde_distante', f"{username(self) or 'Administrateur'} ({clean_name})", request_ip(self), 'BACKUP')
items, total_size = list_server_backups()
self.send_json({
'ok': True,
'message': f"Sauvegarde « {clean_name} » importée localement avec succès !",
'backups': items
}); return
except Exception as e:
if os.path.exists(tmp_dest):
try: os.unlink(tmp_dest)
except OSError: pass
self.send_json({'error': f"Échec récupération sauvegarde : {e}"}, 500); return
if self.path == '/api/backups/remote/file/delete':
if not valid_session(self): self.send_json({'error':'Authentification requise'},401); return
fname = str(data.get('filename', '')).strip()