#!/usr/bin/env python3 """Pleiades admin CLI — manage users, plans, balances.""" import sys import time import db def create_user(username, web_pass, proxy_pass, country="", city="", region="", sticky=1, balance_gb=0, active=1): d = db.get_db() try: d.execute( "INSERT INTO users (username,password_hash,proxy_password_hash," "proxy_password_plain,active,region,country,city,sticky,balance_gb,created_at) " "VALUES (?,?,?,?,?,?,?,?,?,?,?)", (username, db.scrypt_hash(web_pass), db.scrypt_hash(proxy_pass), proxy_pass, int(active), region, country, city, int(sticky), float(balance_gb), int(time.time()))) d.commit() print(f"OK created user '{username}'") except Exception as e: print(f"ERROR: {e}") finally: d.close() def set_country(username, country): d = db.get_db() d.execute("UPDATE users SET country=? WHERE username=?", (country, username)) d.commit() d.close() print(f"OK {username} country={country}") ALLOWED_FIELDS = {"country", "city", "region", "sticky", "active", "balance_gb"} def set_field(username, field, value): if field not in ALLOWED_FIELDS: print(f"ERROR: field '{field}' not allowed (whitelist: {sorted(ALLOWED_FIELDS)})") return d = db.get_db() d.execute(f"UPDATE users SET {field}=? WHERE username=?", (value, username)) d.commit() d.close() print(f"OK {username} {field}={value}") def add_balance(username, gb): d = db.get_db() d.execute("UPDATE users SET balance_gb=balance_gb+? WHERE username=?", (float(gb), username)) d.commit() d.close() print(f"OK {username} +{gb} GB") def list_users(): d = db.get_db() for r in d.execute("SELECT id,username,active,country,city,sticky,balance_gb FROM users"): print(f"#{r['id']} {r['username']} active={r['active']} geo={r['country'] or '-'}/{r['city'] or '-'} " f"sticky={r['sticky']} bal={r['balance_gb']}GB") d.close() def main(): db.init_db() if len(sys.argv) < 2: print(__doc__) return cmd = sys.argv[1] a = sys.argv[2:] if cmd == "create-user" and len(a) >= 3: create_user(a[0], a[1], a[2], *a[3:]) elif cmd == "set-country" and len(a) == 2: set_country(a[0], a[1]) elif cmd == "set" and len(a) == 3: set_field(a[0], a[1], a[2]) elif cmd == "add-balance" and len(a) == 2: add_balance(a[0], a[1]) elif cmd == "list": list_users() else: print("commands: create-user [country city region sticky bal active] | " "set-country | set | add-balance | list") if __name__ == "__main__": main()