GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — VortexPanel/panel/cli.py at main · BrowserlessAPI/VortexPanel Expand file tree/ Copy pathcli.pyMore file actions 607 lines (524 loc) · 21.9 KB / Copy pathcli.pyFile metadata and controls607 lines (524 loc) · 21.9 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 """`vortexpanel` - manage the panel from the server's shell (as root). Installed as /usr/local/bin/vortexpanel by install.sh. Every command works while the panel is stopped or broken, so it is also the recovery tool: forgotten password, lost 2FA phone, locked-out IP, unknown login path. """ import argparse import json import os import re import secrets import shutil import socket import string import subprocess import sys import time INSTALL_DIR = '/opt/vortexpanel' CONFIG_FILE = os.path.join(INSTALL_DIR, 'config.json') UNIT = '/etc/systemd/system/vortexpanel.service' SSL_DIR = os.path.join(INSTALL_DIR, 'ssl') os.environ.setdefault('VP_NO_INIT', '1') sys.path.insert(0, INSTALL_DIR) def sh(cmd, t=60): r = subprocess.run(cmd, shell=True, capture_output=True, text=True, timeout=t) return r.stdout.strip(), r.stderr.strip(), r.returncode def die(msg, code=1): sys.stdout.flush() print(f'Error: {msg}', file=sys.stderr) sys.exit(code) def need_root(): if os.geteuid() != 0: die('run this command as root (sudo vortexpanel ...)') def load_cfg(): try: return json.load(open(CONFIG_FILE)) except Exception: return {} def save_cfg(cfg): tmp = CONFIG_FILE + '.tmp.cli' fd = os.open(tmp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600) with os.fdopen(fd, 'w') as f: json.dump(cfg, f, indent=2) os.replace(tmp, CONFIG_FILE) def unit_text(): try: return open(UNIT).read() except Exception: return '' def bind(): m = re.search(r'(?:--bind|-b)\s+(\S+):(\d+)', unit_text()) return (m.group(1), int(m.group(2))) if m else ('0.0.0.0', 8888) def scheme(): return 'https' if '--certfile' in unit_text() else 'http' def public_ip(): out, _, _ = sh("ip -4 route get 1.1.1.1 2>/dev/null | awk '{for(i=1;i<=NF;i++) if($i==\"src\") print $(i+1)}'") if out: return out.split()[0] out, _, _ = sh("hostname -I 2>/dev/null | awk '{print $1}'") return out or 'SERVER-IP' def health(tries=30): import ssl import urllib.request _, port = bind() ctx = ssl.create_default_context() ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE op = urllib.request.build_opener(urllib.request.ProxyHandler({}), urllib.request.HTTPSHandler(context=ctx)) for i in range(tries): for host in ('127.0.0.1', '[::1]'): try: if op.open(f'{scheme()}://{host}:{port}/api/health', timeout=3).status == 200: return True except Exception: pass time.sleep(1) return False def restart(wait=True): sh('systemctl daemon-reload; systemctl restart vortexpanel', t=60) if wait: ok = health() print('Panel is running.' if ok else 'The panel did not answer yet - see: vortexpanel logs') return ok return True def creds_mod(): from panel.routes import auth return auth def rand(n, alphabet=string.ascii_lowercase + string.digits): return ''.join(secrets.choice(alphabet) for _ in range(n)) # --- commands -------------------------------------------------------------------- def cmd_info(a): cfg = load_cfg() _, port = bind() path = cfg.get('security_path') or '' host = (cfg.get('panel_domain') if cfg.get('panel_domain_enforced') else '') or public_ip() try: user = json.load(open(os.path.join(INSTALL_DIR, 'credentials.json'))).get('username', 'admin') except Exception: user = '?' active = sh('systemctl is-active vortexpanel')[0] ver = '' try: from panel import updater ver = updater.current_version() except Exception: pass print(f'VortexPanel {ver} ({active})') print(f' Address: {scheme()}://{host}:{port}/{path}') print(f' Username: {user}') pw_file = os.path.join(INSTALL_DIR, 'admin_password.txt') if os.path.exists(pw_file): print(f' Password: {open(pw_file).read().strip()} (initial password, removed after the first sign-in)') else: print(' Password: (set by you) - forgot it? vortexpanel reset-password') if scheme() == 'https' and 'panel.crt' in unit_text(): print(' HTTPS: self-signed certificate - your browser shows a warning once; ' 'add a domain and Let\'s Encrypt in Settings > Panel SSL to remove it') if cfg.get('allowed_ips'): print(f' Allowed IPs: {", ".join(cfg["allowed_ips"])} (vortexpanel allow-ip clear)') print('Commands: vortexpanel --help') def cmd_reset_password(a): need_root() auth = creds_mod() pw = a.password or rand(16, string.ascii_letters + string.digits) if len(pw) < 8: die('the password must be at least 8 characters') target = a.user if target: from panel import users as U u = U.find_user_by_name(target) if not u: creds = auth.get_credentials() if creds.get('username') != target: die(f'no account named {target}') target = None else: U.update_user(u['id'], {'password_hash': auth._hash_password(pw), 'session_version': secrets.token_hex(8)}) if not target: creds = auth.get_credentials() creds['password_hash'] = auth._hash_password(pw) creds['session_version'] = secrets.token_hex(8) auth.save_credentials(creds) target = creds.get('username', 'admin') auth._clear_attempts('acct:admin') print(f'New password for {target}: {pw}') print('All existing sessions of this account were signed out.') def cmd_username(a): need_root() auth = creds_mod() if not re.fullmatch(r'[A-Za-z0-9][A-Za-z0-9._@-]{2,63}', a.name): die('3-64 characters: letters, digits, dot, dash, underscore or @') creds = auth.get_credentials() creds['username'] = a.name creds['session_version'] = secrets.token_hex(8) auth.save_credentials(creds) print(f'Administrator username is now: {a.name}') def cmd_disable_2fa(a): need_root() auth = creds_mod() if a.user: from panel import users as U u = U.find_user_by_name(a.user) if u: U.update_user(u['id'], {'totp_secret': None, 'totp_enabled': None, 'totp_last_step': None, 'backup_codes': None, 'session_version': secrets.token_hex(8)}) print(f'Two-factor authentication turned off for {a.user}.') return creds = auth.get_credentials() for k in ('totp_secret', 'totp_enabled', 'totp_last_step', 'backup_codes'): creds.pop(k, None) creds['session_version'] = secrets.token_hex(8) auth.save_credentials(creds) print(f'Two-factor authentication turned off for {creds.get("username", "admin")}. ' 'Turn it on again in Settings > Security.') def port_free(port): for fam, addr in ((socket.AF_INET, '0.0.0.0'), (socket.AF_INET6, '::')): s = socket.socket(fam, socket.SOCK_STREAM) try: s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind((addr, port)) except OSError as e: if fam == socket.AF_INET6 and e.errno in (97, 99): # no IPv6 continue return False finally: s.close() return True def set_unit(host=None, port=None, tls=None): """Rewrite the unit's --bind / --certfile / --keyfile.""" text = unit_text() if not text: die('the vortexpanel service is not installed') h, p = bind() host = host or h port = port or p text = re.sub(r'(--bind|-b)\s+\S+:\d+', f'--bind {host}:{port}', text, count=1) if tls is not None: text = re.sub(r'\s*--certfile\s+\S+', '', text) text = re.sub(r'\s*--keyfile\s+\S+', '', text) if tls: crt, key = tls text = re.sub(r'(--bind\s+\S+:\d+)', rf'\1 --certfile {crt} --keyfile {key}', text, count=1) with open(UNIT + '.tmp', 'w') as f: f.write(text) os.replace(UNIT + '.tmp', UNIT) def firewall_open(port): sh(f'command -v ufw >/dev/null && ufw allow {port}/tcp') sh(f'command -v firewall-cmd >/dev/null && firewall-cmd --state >/dev/null 2>&1 && ' f'firewall-cmd --permanent --add-port={port}/tcp && firewall-cmd --add-port={port}/tcp') def cmd_port(a): need_root() port = a.port if not 1024 <= port <= 65535: die('the port must be between 1024 and 65535') _, old = bind() if port == old: print(f'The panel already uses port {port}.') return if not port_free(port): die(f'port {port} is already in use') old_unit = unit_text() set_unit(port=port) firewall_open(port) if not restart(): with open(UNIT, 'w') as f: f.write(old_unit) restart() die(f'the panel did not start on port {port}; port {old} was restored') cfg = load_cfg() cfg['port'] = port save_cfg(cfg) print(f'The panel now listens on port {port}. Remove the old rule when you no longer need it: ' f'ufw delete allow {old}/tcp') cmd_info(a) def cmd_path(a): need_root() cfg = load_cfg() v = (a.value or '').strip().strip('/') if v in ('off', 'none', 'disable'): v = '' elif v in ('', 'random'): v = rand(8) if v and not re.fullmatch(r'[A-Za-z0-9_-]{4,64}', v): die('4-64 characters: letters, digits, dash or underscore') cfg['security_path'] = v save_cfg(cfg) print('Login path turned off.' if not v else f'Login path is now /{v}') cmd_info(a) def cmd_domain(a): need_root() cfg = load_cfg() if a.value in ('off', 'none', ''): cfg['panel_domain'] = '' cfg['panel_domain_enforced'] = False save_cfg(cfg) print('The panel answers on any address again.') else: die('set the panel domain in Settings > Panel; here it can only be turned off: vortexpanel domain off') def gen_selfsigned(): os.makedirs(SSL_DIR, mode=0o700, exist_ok=True) ip = public_ip() san = f'IP:{ip}' if re.fullmatch(r'[0-9.]+', ip) else 'DNS:localhost' out, err, rc = sh(f'openssl req -x509 -nodes -days 3650 -newkey rsa:2048 ' f'-keyout {SSL_DIR}/panel.key.new -out {SSL_DIR}/panel.crt.new ' f'-subj "/CN={ip}/O=VortexPanel/OU=Panel" -addext "subjectAltName={san}" 2>&1') if rc != 0: die('openssl failed: ' + (out or err)[-300:]) os.chmod(f'{SSL_DIR}/panel.key.new', 0o600) os.replace(f'{SSL_DIR}/panel.key.new', f'{SSL_DIR}/panel.key') os.replace(f'{SSL_DIR}/panel.crt.new', f'{SSL_DIR}/panel.crt') return f'{SSL_DIR}/panel.crt', f'{SSL_DIR}/panel.key' def cmd_ssl(a): need_root() cfg = load_cfg() old_unit = unit_text() if a.mode == 'off': set_unit(tls=False) cfg['ssl_enabled'] = False msg = 'HTTPS turned off. The panel now uses plain HTTP - only do this behind a VPN or SSH tunnel.' else: crt, key = gen_selfsigned() set_unit(tls=(crt, key)) cfg['ssl_enabled'] = True msg = 'HTTPS turned on with a new self-signed certificate.' if not restart(): with open(UNIT, 'w') as f: f.write(old_unit) restart() die('the panel did not start; the previous setting was restored') save_cfg(cfg) print(msg) cmd_info(a) def cmd_allow_ip(a): need_root() cfg = load_cfg() if a.action == 'clear': cfg['allowed_ips'] = [] print('IP allowlist cleared: the panel accepts every address again.') elif a.action == 'add' and a.ip: import ipaddress try: ipaddress.ip_network(a.ip, strict=False) except ValueError: die(f'not an IP address or CIDR: {a.ip}') cfg.setdefault('allowed_ips', []) if a.ip not in cfg['allowed_ips']: cfg['allowed_ips'].append(a.ip) print(f'Added {a.ip}. Allowed: {", ".join(cfg["allowed_ips"])}') else: print('Allowed IPs: ' + (', '.join(cfg.get('allowed_ips') or []) or 'any')) return save_cfg(cfg) def cmd_unlock(a): need_root() lf = os.path.join(INSTALL_DIR, 'lockout.json') try: os.remove(lf) except FileNotFoundError: pass print('All sign-in lockouts were cleared.') if sh('systemctl is-active vortexpanel')[0] == 'active': restart() # clears the in-memory counters too def cmd_service(a): need_root() if a.cmd == 'restart': restart() elif a.cmd == 'start': sh('systemctl start vortexpanel') print('Panel is running.' if health() else 'The panel did not answer - see: vortexpanel logs') elif a.cmd == 'stop': sh('systemctl stop vortexpanel') print('Panel stopped. Websites and services keep running.') elif a.cmd == 'status': os.system('systemctl status vortexpanel --no-pager -n 0') print('Health check:', 'OK' if health(3) else 'not answering') def cmd_logs(a): os.system(f'journalctl -u vortexpanel -n {int(a.lines)} --no-pager; ' f'tail -n {int(a.lines)} /var/log/vortexpanel/error.log 2>/dev/null') def cmd_update(a): need_root() from panel import updater if a.check: c = updater.check() print(f'Installed: {c["current"]} Latest release: {c["latest"]}') print('An update is available: vortexpanel update' if c.get('has_update') else 'Up to date.') return try: v = updater.run_update(a.version or '', log=lambda m: print(m, flush=True), wait_restart=True, force_unsigned=a.allow_unsigned) print(f'VortexPanel {v} is installed and running.') except Exception as e: die(str(e)) def cmd_rollback(a): need_root() from panel import updater try: v = updater.rollback(log=lambda m: print(m, flush=True)) print(f'Rolled back to {v or "the previous version"}.') except Exception as e: die(str(e)) def cmd_backup(a): need_root() from panel import panelbackup path = panelbackup.create(note='cli') print(f'Panel settings backup: {path}') def cmd_users(a): from panel import users as U auth = creds_mod() c = auth.get_credentials() print(f'{c.get("username", "admin"):<24} admin (primary)') for u in U.list_users(): print(f'{u["username"]:<24} {u["role"]:<6} {"disabled" if u.get("disabled") else ""} ' f'{",".join(u.get("sites") or [])}') def cmd_plugins(a): from panel import plugins as P act = a.action if act == 'list': off = P.disabled_ids() found = P.discover() if P.safe_mode(): print('Safe mode is ON: the panel loads no plugins (vortexpanel plugins safe-mode off).') if not found: print(f'No plugins in {P.PLUGINS_DIR}') cv = P.core_version() for pid, d, man, err in found: man = man or {} st = 'error: ' + err if err else ('disabled' if pid in off else ('incompatible: needs ' + man.get('requires_core', '') if not P.version_matches(cv, man.get('requires_core', '')) else 'enabled')) print(f'{pid:<16} {man.get("version", ""):<10} {st}') return if act in ('enable', 'disable'): need_root() if not a.id: die('Usage: vortexpanel plugins ' + act + ' <id>') if a.id not in [x[0] for x in P.discover()]: die(f'No plugin "{a.id}" in {P.PLUGINS_DIR}') P.set_enabled(a.id, act == 'enable') print(f'Plugin {a.id} {act}d. Restarting the panel...', flush=True) os.system('systemctl restart vortexpanel') return if act == 'safe-mode': need_root() on = (a.id or 'on') == 'on' P.set_safe_mode(on) print(f'Safe mode {"on: plugins are not loaded" if on else "off"}. Restarting the panel...', flush=True) os.system('systemctl restart vortexpanel') def cmd_app_versions(a): from panel import appversions st = appversions.refresh(force=True) if a.action == 'refresh' else appversions.status() if st.get('in_use'): print(f"Tested versions list #{st['serial']} ({st.get('issued', '')[:10]}) from {st.get('server')}") else: print('Using the built-in App Store versions (no tested list yet).') if st.get('error'): print('Last check: ' + st['error']) def cmd_app_test(a): from panel import apptest sys.exit(apptest.main(a.rest or ['status'])) def cmd_offline(a): from panel import softs if a.action == 'import': if not a.file: sys.exit('usage: vortexpanel offline import <bundle.tar | package.zip>') try: import tarfile if tarfile.is_tarfile(a.file): r = softs.import_bundle(a.file, log=print) else: r = softs.import_package(a.file, log=print) print('Imported:', r) except softs.SoftError as e: sys.exit(f'import failed: {e}') elif a.action == 'clear': softs.offline_clear() print('Offline store cleared.') else: st = softs.offline_status() print(f"Offline store: {st['files']} file(s); catalog serial {st['catalog_serial']}, " f"App index serial {st['apps_index_serial']}; offline mode {'on' if st['offline'] else 'off'}") def cmd_mirror(a): from panel import softs s = softs.settings() which = 'apps_mirrors' if a.apps else 'mirrors' cur = list(s[which]) if a.action == 'add' and a.url: if a.url.rstrip('/') not in cur: cur.append(a.url.rstrip('/')) softs.save_settings({which: cur}) elif a.action == 'remove' and a.url: cur = [u for u in cur if u != a.url.rstrip('/')] softs.save_settings({which: cur}) s = softs.settings() print('Software mirrors:', ', '.join(s['mirrors']) or '(none)') print('App mirrors: ', ', '.join(s['apps_mirrors']) or '(none)') def main(argv=None): p = argparse.ArgumentParser(prog='vortexpanel', description='Manage VortexPanel from the shell.') sp = p.add_subparsers(dest='command') sp.add_parser('info', help='show the address, username and status (default)').set_defaults(fn=cmd_info) x = sp.add_parser('reset-password', help='set a new password (random if none is given)') x.add_argument('password', nargs='?') x.add_argument('--user', help='another panel account (default: the administrator)') x.set_defaults(fn=cmd_reset_password) x = sp.add_parser('username', help='change the administrator username') x.add_argument('name') x.set_defaults(fn=cmd_username) x = sp.add_parser('disable-2fa', help='turn off two-factor authentication (lost phone)') x.add_argument('--user') x.set_defaults(fn=cmd_disable_2fa) x = sp.add_parser('port', help='move the panel to another port') x.add_argument('port', type=int) x.set_defaults(fn=cmd_port) x = sp.add_parser('path', help='set the login path: a value, "random" or "off"') x.add_argument('value', nargs='?', default='random') x.set_defaults(fn=cmd_path) x = sp.add_parser('domain', help='"off" removes the panel domain restriction') x.add_argument('value') x.set_defaults(fn=cmd_domain) x = sp.add_parser('ssl', help='"self" = new self-signed certificate, "off" = plain HTTP') x.add_argument('mode', choices=['self', 'off']) x.set_defaults(fn=cmd_ssl) x = sp.add_parser('allow-ip', help='show, add to or clear the IP allowlist') x.add_argument('action', nargs='?', choices=['list', 'add', 'clear'], default='list') x.add_argument('ip', nargs='?') x.set_defaults(fn=cmd_allow_ip) sp.add_parser('unlock', help='clear sign-in lockouts').set_defaults(fn=cmd_unlock) sp.add_parser('users', help='list panel accounts').set_defaults(fn=cmd_users) for c in ('restart', 'start', 'stop', 'status'): x = sp.add_parser(c, help=f'{c} the panel service') x.set_defaults(fn=cmd_service, cmd=c) x = sp.add_parser('logs', help='show recent panel logs') x.add_argument('-n', '--lines', default=50) x.set_defaults(fn=cmd_logs) x = sp.add_parser('update', help='install the latest signed release') x.add_argument('version', nargs='?') x.add_argument('--check', action='store_true', help='only check') x.add_argument('--allow-unsigned', action='store_true', help='install even if the signature cannot be verified') x.set_defaults(fn=cmd_update) sp.add_parser('rollback', help='go back to the version before the last update').set_defaults(fn=cmd_rollback) sp.add_parser('backup', help='back up the panel settings and accounts').set_defaults(fn=cmd_backup) x = sp.add_parser('plugins', help='list, enable or disable plugins; "safe-mode on|off" loads none') x.add_argument('action', nargs='?', choices=['list', 'enable', 'disable', 'safe-mode'], default='list') x.add_argument('id', nargs='?') x.set_defaults(fn=cmd_plugins) x = sp.add_parser('app-versions', help='tested App Store versions from Cloudflare R2: status or refresh') x.add_argument('action', nargs='?', choices=['status', 'refresh'], default='status') x.set_defaults(fn=cmd_app_versions) x = sp.add_parser('app-test', help='App Store test runner (test servers only): setup | run | status') x.add_argument('rest', nargs=argparse.REMAINDER) x.set_defaults(fn=cmd_app_test) x = sp.add_parser('offline', help='offline bundles: status | import <file> | clear') x.add_argument('action', nargs='?', choices=['status', 'import', 'clear'], default='status') x.add_argument('file', nargs='?', help='bundle .tar or package .zip to import') x.set_defaults(fn=cmd_offline) x = sp.add_parser('mirror', help='self-hosted download mirrors: list | add <url> | remove <url> (--apps for App mirrors)') x.add_argument('action', nargs='?', choices=['list', 'add', 'remove'], default='list') x.add_argument('url', nargs='?') x.add_argument('--apps', action='store_true', help='operate on App mirrors instead of software mirrors') x.set_defaults(fn=cmd_mirror) a = p.parse_args(argv) if not getattr(a, 'fn', None): a = p.parse_args(['info']) a.fn(a) if __name__ == '__main__': main() |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |