Add key server with username support
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ Runs on HuggingFace Spaces (Docker SDK)
|
|
| 6 |
from fastapi import FastAPI, HTTPException
|
| 7 |
from fastapi.responses import HTMLResponse
|
| 8 |
from pydantic import BaseModel
|
| 9 |
-
import json, os, secrets, string, time
|
| 10 |
from datetime import datetime, timedelta
|
| 11 |
from pathlib import Path
|
| 12 |
from threading import Lock
|
|
@@ -14,10 +14,17 @@ from threading import Lock
|
|
| 14 |
app = FastAPI(title="ArianKing Key Server")
|
| 15 |
|
| 16 |
# ── Storage ──────────────────────────────────────────────────────────────────
|
| 17 |
-
#
|
| 18 |
-
#
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
KEYS_FILE = DATA_DIR / "keys.json"
|
|
|
|
| 21 |
file_lock = Lock()
|
| 22 |
|
| 23 |
# Admin password — change this or set ADMIN_PASSWORD env var
|
|
@@ -29,6 +36,7 @@ class CreateKeyRequest(BaseModel):
|
|
| 29 |
admin_password: str
|
| 30 |
days: int = 30
|
| 31 |
label: str = ""
|
|
|
|
| 32 |
|
| 33 |
class ToggleKeyRequest(BaseModel):
|
| 34 |
admin_password: str
|
|
@@ -45,19 +53,39 @@ class ListKeysRequest(BaseModel):
|
|
| 45 |
class ValidateRequest(BaseModel):
|
| 46 |
key: str
|
| 47 |
hwid: str
|
|
|
|
| 48 |
|
| 49 |
|
| 50 |
# ── Helpers ──────────────────────────────────────────────────────────────────
|
| 51 |
def load_keys() -> dict:
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
return {}
|
| 56 |
|
| 57 |
def save_keys(keys: dict):
|
| 58 |
with file_lock:
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
def generate_key() -> str:
|
| 63 |
"""Generate key like ARIAN-XXXX-XXXX-XXXX"""
|
|
@@ -76,6 +104,9 @@ def check_admin(password: str):
|
|
| 76 |
def create_key(req: CreateKeyRequest):
|
| 77 |
check_admin(req.admin_password)
|
| 78 |
|
|
|
|
|
|
|
|
|
|
| 79 |
key = generate_key()
|
| 80 |
now = datetime.utcnow()
|
| 81 |
expires = now + timedelta(days=req.days)
|
|
@@ -86,14 +117,21 @@ def create_key(req: CreateKeyRequest):
|
|
| 86 |
"expires": expires.isoformat(),
|
| 87 |
"days": req.days,
|
| 88 |
"label": req.label,
|
|
|
|
| 89 |
"active": True,
|
| 90 |
-
"hwid": None,
|
| 91 |
"last_used": None,
|
| 92 |
"uses": 0
|
| 93 |
}
|
| 94 |
save_keys(keys)
|
| 95 |
|
| 96 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
|
| 99 |
@app.post("/api/validate")
|
|
@@ -114,6 +152,11 @@ def validate_key(req: ValidateRequest):
|
|
| 114 |
if datetime.utcnow() > expires:
|
| 115 |
return {"valid": False, "message": "Key expired"}
|
| 116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
# HWID binding: first use binds the key to this hardware
|
| 118 |
if entry["hwid"] is None:
|
| 119 |
entry["hwid"] = req.hwid
|
|
@@ -133,8 +176,9 @@ def validate_key(req: ValidateRequest):
|
|
| 133 |
|
| 134 |
return {
|
| 135 |
"valid": True,
|
| 136 |
-
"message": f"
|
| 137 |
"days_left": days_left,
|
|
|
|
| 138 |
"expires": entry["expires"]
|
| 139 |
}
|
| 140 |
|
|
@@ -195,6 +239,7 @@ def list_keys(req: ListKeysRequest):
|
|
| 195 |
result.append({
|
| 196 |
"key": key,
|
| 197 |
"label": data.get("label", ""),
|
|
|
|
| 198 |
"days": data["days"],
|
| 199 |
"days_left": days_left,
|
| 200 |
"expired": expired,
|
|
@@ -206,7 +251,6 @@ def list_keys(req: ListKeysRequest):
|
|
| 206 |
"uses": data.get("uses", 0)
|
| 207 |
})
|
| 208 |
|
| 209 |
-
# Sort: active first, then by creation date
|
| 210 |
result.sort(key=lambda x: (not x["active"], x["expired"], x["created"]), reverse=False)
|
| 211 |
return {"keys": result, "total": len(result)}
|
| 212 |
|
|
@@ -228,7 +272,6 @@ ADMIN_HTML = """<!DOCTYPE html>
|
|
| 228 |
min-height: 100vh;
|
| 229 |
}
|
| 230 |
|
| 231 |
-
/* Header */
|
| 232 |
.header {
|
| 233 |
background: linear-gradient(135deg, #7832ff 0%, #2878ff 100%);
|
| 234 |
padding: 20px 30px;
|
|
@@ -237,247 +280,113 @@ ADMIN_HTML = """<!DOCTYPE html>
|
|
| 237 |
justify-content: space-between;
|
| 238 |
box-shadow: 0 4px 20px rgba(120, 50, 255, 0.3);
|
| 239 |
}
|
| 240 |
-
.header h1 {
|
| 241 |
-
|
| 242 |
-
font-weight: 700;
|
| 243 |
-
color: white;
|
| 244 |
-
text-shadow: 0 2px 4px rgba(0,0,0,0.3);
|
| 245 |
-
}
|
| 246 |
-
.header span {
|
| 247 |
-
color: rgba(255,255,255,0.7);
|
| 248 |
-
font-size: 14px;
|
| 249 |
-
}
|
| 250 |
|
| 251 |
-
/* Login */
|
| 252 |
.login-container {
|
| 253 |
-
max-width: 400px;
|
| 254 |
-
|
| 255 |
-
padding: 30px;
|
| 256 |
-
background: #12121a;
|
| 257 |
-
border-radius: 12px;
|
| 258 |
-
border: 1px solid #1e1e30;
|
| 259 |
-
}
|
| 260 |
-
.login-container h2 {
|
| 261 |
-
color: #8b5cf6;
|
| 262 |
-
margin-bottom: 20px;
|
| 263 |
-
text-align: center;
|
| 264 |
}
|
|
|
|
| 265 |
|
| 266 |
-
|
| 267 |
-
.container {
|
| 268 |
-
max-width: 1100px;
|
| 269 |
-
margin: 20px auto;
|
| 270 |
-
padding: 0 20px;
|
| 271 |
-
}
|
| 272 |
|
| 273 |
-
/* Cards */
|
| 274 |
.card {
|
| 275 |
-
background: #12121a;
|
| 276 |
-
|
| 277 |
-
padding: 20px;
|
| 278 |
-
margin-bottom: 16px;
|
| 279 |
-
border: 1px solid #1e1e30;
|
| 280 |
-
}
|
| 281 |
-
.card h3 {
|
| 282 |
-
color: #8b5cf6;
|
| 283 |
-
margin-bottom: 12px;
|
| 284 |
-
font-size: 16px;
|
| 285 |
}
|
|
|
|
| 286 |
|
| 287 |
-
/* Inputs */
|
| 288 |
input, select {
|
| 289 |
-
background: #1a1a28;
|
| 290 |
-
border:
|
| 291 |
-
|
| 292 |
-
padding: 10px 14px;
|
| 293 |
-
border-radius: 6px;
|
| 294 |
-
font-size: 14px;
|
| 295 |
-
width: 100%;
|
| 296 |
-
outline: none;
|
| 297 |
-
transition: border-color 0.2s;
|
| 298 |
-
}
|
| 299 |
-
input:focus, select:focus {
|
| 300 |
-
border-color: #7832ff;
|
| 301 |
-
}
|
| 302 |
-
.input-group {
|
| 303 |
-
margin-bottom: 12px;
|
| 304 |
}
|
|
|
|
|
|
|
| 305 |
.input-group label {
|
| 306 |
-
display: block;
|
| 307 |
-
|
| 308 |
-
color: #888;
|
| 309 |
-
font-size: 12px;
|
| 310 |
-
text-transform: uppercase;
|
| 311 |
-
letter-spacing: 1px;
|
| 312 |
-
}
|
| 313 |
-
.input-row {
|
| 314 |
-
display: flex;
|
| 315 |
-
gap: 12px;
|
| 316 |
-
}
|
| 317 |
-
.input-row > * {
|
| 318 |
-
flex: 1;
|
| 319 |
}
|
|
|
|
|
|
|
| 320 |
|
| 321 |
-
/* Buttons */
|
| 322 |
.btn {
|
| 323 |
-
padding: 10px 20px;
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
}
|
| 332 |
-
.btn-
|
| 333 |
-
|
| 334 |
-
}
|
| 335 |
-
.btn-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
}
|
| 339 |
-
.btn-danger {
|
| 340 |
-
background: #dc2626;
|
| 341 |
-
}
|
| 342 |
-
.btn-danger:hover {
|
| 343 |
-
background: #ef4444;
|
| 344 |
-
}
|
| 345 |
-
.btn-success {
|
| 346 |
-
background: #16a34a;
|
| 347 |
-
}
|
| 348 |
-
.btn-success:hover {
|
| 349 |
-
background: #22c55e;
|
| 350 |
-
}
|
| 351 |
-
.btn-warning {
|
| 352 |
-
background: #d97706;
|
| 353 |
-
}
|
| 354 |
-
.btn-warning:hover {
|
| 355 |
-
background: #f59e0b;
|
| 356 |
-
}
|
| 357 |
-
.btn-sm {
|
| 358 |
-
padding: 6px 12px;
|
| 359 |
-
font-size: 12px;
|
| 360 |
-
}
|
| 361 |
-
|
| 362 |
-
/* Table */
|
| 363 |
-
.keys-table {
|
| 364 |
-
width: 100%;
|
| 365 |
-
border-collapse: collapse;
|
| 366 |
-
}
|
| 367 |
.keys-table th {
|
| 368 |
-
text-align: left;
|
| 369 |
-
|
| 370 |
-
color: #888;
|
| 371 |
-
font-size: 11px;
|
| 372 |
-
text-transform: uppercase;
|
| 373 |
-
letter-spacing: 1px;
|
| 374 |
border-bottom: 1px solid #1e1e30;
|
| 375 |
}
|
| 376 |
-
.keys-table td {
|
| 377 |
-
|
| 378 |
-
border-bottom: 1px solid #1a1a28;
|
| 379 |
-
font-size: 13px;
|
| 380 |
-
}
|
| 381 |
-
.keys-table tr:hover td {
|
| 382 |
-
background: #1a1a28;
|
| 383 |
-
}
|
| 384 |
|
| 385 |
-
|
| 386 |
-
.badge {
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
border-radius: 4px;
|
| 390 |
-
font-size: 11px;
|
| 391 |
-
font-weight: 600;
|
| 392 |
-
}
|
| 393 |
-
.badge-active {
|
| 394 |
-
background: rgba(22, 163, 74, 0.2);
|
| 395 |
-
color: #4ade80;
|
| 396 |
-
}
|
| 397 |
-
.badge-inactive {
|
| 398 |
-
background: rgba(220, 38, 38, 0.2);
|
| 399 |
-
color: #f87171;
|
| 400 |
-
}
|
| 401 |
-
.badge-expired {
|
| 402 |
-
background: rgba(217, 119, 6, 0.2);
|
| 403 |
-
color: #fbbf24;
|
| 404 |
-
}
|
| 405 |
|
| 406 |
-
.key-text {
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
cursor: pointer;
|
| 411 |
-
}
|
| 412 |
-
.key-text:hover {
|
| 413 |
-
color: #8b5cf6;
|
| 414 |
-
}
|
| 415 |
|
| 416 |
.hwid-text {
|
| 417 |
-
font-family: monospace;
|
| 418 |
-
|
| 419 |
-
color: #666;
|
| 420 |
-
max-width: 120px;
|
| 421 |
-
overflow: hidden;
|
| 422 |
-
text-overflow: ellipsis;
|
| 423 |
-
white-space: nowrap;
|
| 424 |
}
|
| 425 |
|
| 426 |
-
.stats {
|
| 427 |
-
display: flex;
|
| 428 |
-
gap: 16px;
|
| 429 |
-
margin-bottom: 16px;
|
| 430 |
-
}
|
| 431 |
.stat-box {
|
| 432 |
-
background: #12121a;
|
| 433 |
-
border:
|
| 434 |
-
border-radius: 8px;
|
| 435 |
-
padding: 16px 20px;
|
| 436 |
-
flex: 1;
|
| 437 |
-
text-align: center;
|
| 438 |
-
}
|
| 439 |
-
.stat-box .number {
|
| 440 |
-
font-size: 28px;
|
| 441 |
-
font-weight: 700;
|
| 442 |
-
color: #8b5cf6;
|
| 443 |
-
}
|
| 444 |
-
.stat-box .label {
|
| 445 |
-
font-size: 12px;
|
| 446 |
-
color: #666;
|
| 447 |
-
margin-top: 4px;
|
| 448 |
}
|
|
|
|
|
|
|
| 449 |
|
| 450 |
.toast {
|
| 451 |
-
position: fixed;
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
background: #16a34a;
|
| 455 |
-
color: white;
|
| 456 |
-
padding: 12px 20px;
|
| 457 |
-
border-radius: 8px;
|
| 458 |
-
font-size: 14px;
|
| 459 |
-
display: none;
|
| 460 |
-
z-index: 1000;
|
| 461 |
-
box-shadow: 0 4px 15px rgba(0,0,0,0.3);
|
| 462 |
-
}
|
| 463 |
-
.toast.error {
|
| 464 |
-
background: #dc2626;
|
| 465 |
-
}
|
| 466 |
-
|
| 467 |
-
.actions {
|
| 468 |
-
display: flex;
|
| 469 |
-
gap: 4px;
|
| 470 |
}
|
| 471 |
-
|
|
|
|
| 472 |
.hidden { display: none; }
|
| 473 |
|
| 474 |
-
/* Scrollbar */
|
| 475 |
::-webkit-scrollbar { width: 8px; }
|
| 476 |
::-webkit-scrollbar-track { background: #0a0a0f; }
|
| 477 |
::-webkit-scrollbar-thumb { background: #2a2a40; border-radius: 4px; }
|
| 478 |
::-webkit-scrollbar-thumb:hover { background: #3a3a55; }
|
| 479 |
-
|
| 480 |
.overflow-x { overflow-x: auto; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 481 |
</style>
|
| 482 |
</head>
|
| 483 |
<body>
|
|
@@ -503,7 +412,6 @@ ADMIN_HTML = """<!DOCTYPE html>
|
|
| 503 |
<!-- Main Dashboard -->
|
| 504 |
<div id="dashboard" class="container hidden">
|
| 505 |
|
| 506 |
-
<!-- Stats -->
|
| 507 |
<div class="stats">
|
| 508 |
<div class="stat-box">
|
| 509 |
<div class="number" id="statTotal">0</div>
|
|
@@ -527,22 +435,30 @@ ADMIN_HTML = """<!DOCTYPE html>
|
|
| 527 |
<div class="card">
|
| 528 |
<h3>Create New Key</h3>
|
| 529 |
<div class="input-row">
|
|
|
|
|
|
|
|
|
|
|
|
|
| 530 |
<div class="input-group">
|
| 531 |
<label>Duration (Days)</label>
|
| 532 |
<input type="number" id="keyDays" value="30" min="1" max="365">
|
| 533 |
</div>
|
| 534 |
<div class="input-group">
|
| 535 |
<label>Label (optional)</label>
|
| 536 |
-
<input type="text" id="keyLabel" placeholder="
|
| 537 |
</div>
|
| 538 |
<div style="display:flex;align-items:flex-end">
|
| 539 |
<button class="btn btn-primary" onclick="createKey()">Create Key</button>
|
| 540 |
</div>
|
| 541 |
</div>
|
| 542 |
-
<div id="newKeyResult" class="
|
| 543 |
-
<
|
| 544 |
-
<
|
| 545 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
| 546 |
</div>
|
| 547 |
</div>
|
| 548 |
|
|
@@ -557,6 +473,7 @@ ADMIN_HTML = """<!DOCTYPE html>
|
|
| 557 |
<thead>
|
| 558 |
<tr>
|
| 559 |
<th>Key</th>
|
|
|
|
| 560 |
<th>Label</th>
|
| 561 |
<th>Status</th>
|
| 562 |
<th>Days Left</th>
|
|
@@ -566,7 +483,7 @@ ADMIN_HTML = """<!DOCTYPE html>
|
|
| 566 |
</tr>
|
| 567 |
</thead>
|
| 568 |
<tbody id="keysBody">
|
| 569 |
-
<tr><td colspan="
|
| 570 |
</tbody>
|
| 571 |
</table>
|
| 572 |
</div>
|
|
@@ -575,6 +492,8 @@ ADMIN_HTML = """<!DOCTYPE html>
|
|
| 575 |
|
| 576 |
<script>
|
| 577 |
let adminPassword = '';
|
|
|
|
|
|
|
| 578 |
|
| 579 |
function showToast(msg, isError) {
|
| 580 |
const t = document.getElementById('toast');
|
|
@@ -615,16 +534,14 @@ async function loadKeys() {
|
|
| 615 |
const data = await api('list-keys', { admin_password: adminPassword });
|
| 616 |
const keys = data.keys;
|
| 617 |
|
| 618 |
-
// Stats
|
| 619 |
document.getElementById('statTotal').textContent = keys.length;
|
| 620 |
document.getElementById('statActive').textContent = keys.filter(k => k.active && !k.expired).length;
|
| 621 |
document.getElementById('statBound').textContent = keys.filter(k => k.hwid).length;
|
| 622 |
document.getElementById('statExpired').textContent = keys.filter(k => k.expired).length;
|
| 623 |
|
| 624 |
-
// Table
|
| 625 |
const tbody = document.getElementById('keysBody');
|
| 626 |
if (keys.length === 0) {
|
| 627 |
-
tbody.innerHTML = '<tr><td colspan="
|
| 628 |
return;
|
| 629 |
}
|
| 630 |
|
|
@@ -635,30 +552,28 @@ async function loadKeys() {
|
|
| 635 |
else statusBadge = '<span class="badge badge-active">Active</span>';
|
| 636 |
|
| 637 |
const hwid = k.hwid
|
| 638 |
-
?
|
| 639 |
: '<span style="color:#444">Unbound</span>';
|
| 640 |
|
| 641 |
const toggleBtn = k.active
|
| 642 |
-
?
|
| 643 |
-
:
|
| 644 |
|
| 645 |
const resetBtn = k.hwid
|
| 646 |
-
?
|
| 647 |
: '';
|
| 648 |
|
| 649 |
-
return
|
| 650 |
-
<td><span class="key-text" onclick="navigator.clipboard.writeText('
|
| 651 |
-
<td>
|
| 652 |
-
<td>
|
| 653 |
-
<td>
|
| 654 |
-
<td>
|
| 655 |
-
<td>
|
| 656 |
-
<td
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
</td>
|
| 661 |
-
</tr>`;
|
| 662 |
}).join('');
|
| 663 |
} catch(e) {
|
| 664 |
showToast('Failed to load keys: ' + e.message, true);
|
|
@@ -666,17 +581,31 @@ async function loadKeys() {
|
|
| 666 |
}
|
| 667 |
|
| 668 |
async function createKey() {
|
|
|
|
| 669 |
const days = parseInt(document.getElementById('keyDays').value) || 30;
|
| 670 |
const label = document.getElementById('keyLabel').value;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 671 |
try {
|
| 672 |
const data = await api('create-key', {
|
| 673 |
admin_password: adminPassword,
|
| 674 |
days: days,
|
| 675 |
-
label: label
|
|
|
|
| 676 |
});
|
|
|
|
|
|
|
| 677 |
document.getElementById('newKeyText').textContent = data.key;
|
|
|
|
| 678 |
document.getElementById('newKeyResult').classList.remove('hidden');
|
| 679 |
-
showToast('Key created
|
|
|
|
|
|
|
| 680 |
loadKeys();
|
| 681 |
} catch(e) {
|
| 682 |
showToast('Failed: ' + e.message, true);
|
|
@@ -684,9 +613,13 @@ async function createKey() {
|
|
| 684 |
}
|
| 685 |
|
| 686 |
function copyKey() {
|
| 687 |
-
|
| 688 |
-
|
| 689 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 690 |
}
|
| 691 |
|
| 692 |
async function toggleKey(key, active) {
|
|
@@ -700,7 +633,7 @@ async function toggleKey(key, active) {
|
|
| 700 |
}
|
| 701 |
|
| 702 |
async function resetHwid(key) {
|
| 703 |
-
if (!confirm('Reset HWID for ' + key + '?
|
| 704 |
try {
|
| 705 |
await api('reset-hwid', { admin_password: adminPassword, key: key });
|
| 706 |
showToast('HWID reset');
|
|
|
|
| 6 |
from fastapi import FastAPI, HTTPException
|
| 7 |
from fastapi.responses import HTMLResponse
|
| 8 |
from pydantic import BaseModel
|
| 9 |
+
import json, os, secrets, string, time, shutil
|
| 10 |
from datetime import datetime, timedelta
|
| 11 |
from pathlib import Path
|
| 12 |
from threading import Lock
|
|
|
|
| 14 |
app = FastAPI(title="ArianKing Key Server")
|
| 15 |
|
| 16 |
# ── Storage ──────────────────────────────────────────────────────────────────
|
| 17 |
+
# Try multiple persistent locations (HuggingFace /data, then app dir)
|
| 18 |
+
# Auto-backup to prevent data loss
|
| 19 |
+
if Path("/data").exists():
|
| 20 |
+
DATA_DIR = Path("/data")
|
| 21 |
+
elif Path("/home/user/app").exists():
|
| 22 |
+
DATA_DIR = Path("/home/user/app")
|
| 23 |
+
else:
|
| 24 |
+
DATA_DIR = Path(".")
|
| 25 |
+
|
| 26 |
KEYS_FILE = DATA_DIR / "keys.json"
|
| 27 |
+
BACKUP_FILE = DATA_DIR / "keys_backup.json"
|
| 28 |
file_lock = Lock()
|
| 29 |
|
| 30 |
# Admin password — change this or set ADMIN_PASSWORD env var
|
|
|
|
| 36 |
admin_password: str
|
| 37 |
days: int = 30
|
| 38 |
label: str = ""
|
| 39 |
+
username: str = ""
|
| 40 |
|
| 41 |
class ToggleKeyRequest(BaseModel):
|
| 42 |
admin_password: str
|
|
|
|
| 53 |
class ValidateRequest(BaseModel):
|
| 54 |
key: str
|
| 55 |
hwid: str
|
| 56 |
+
username: str = ""
|
| 57 |
|
| 58 |
|
| 59 |
# ── Helpers ──────────────────────────────────────────────────────────────────
|
| 60 |
def load_keys() -> dict:
|
| 61 |
+
# Try main file first, then backup
|
| 62 |
+
for filepath in [KEYS_FILE, BACKUP_FILE]:
|
| 63 |
+
if filepath.exists():
|
| 64 |
+
try:
|
| 65 |
+
with open(filepath, "r") as f:
|
| 66 |
+
data = json.load(f)
|
| 67 |
+
if isinstance(data, dict):
|
| 68 |
+
return data
|
| 69 |
+
except (json.JSONDecodeError, IOError):
|
| 70 |
+
continue
|
| 71 |
return {}
|
| 72 |
|
| 73 |
def save_keys(keys: dict):
|
| 74 |
with file_lock:
|
| 75 |
+
# Write to temp file first, then rename (atomic write)
|
| 76 |
+
tmp_file = DATA_DIR / "keys_tmp.json"
|
| 77 |
+
try:
|
| 78 |
+
with open(tmp_file, "w") as f:
|
| 79 |
+
json.dump(keys, f, indent=2)
|
| 80 |
+
# Replace main file
|
| 81 |
+
shutil.move(str(tmp_file), str(KEYS_FILE))
|
| 82 |
+
# Also write backup
|
| 83 |
+
with open(BACKUP_FILE, "w") as f:
|
| 84 |
+
json.dump(keys, f, indent=2)
|
| 85 |
+
except Exception as e:
|
| 86 |
+
# Fallback: direct write
|
| 87 |
+
with open(KEYS_FILE, "w") as f:
|
| 88 |
+
json.dump(keys, f, indent=2)
|
| 89 |
|
| 90 |
def generate_key() -> str:
|
| 91 |
"""Generate key like ARIAN-XXXX-XXXX-XXXX"""
|
|
|
|
| 104 |
def create_key(req: CreateKeyRequest):
|
| 105 |
check_admin(req.admin_password)
|
| 106 |
|
| 107 |
+
if not req.username.strip():
|
| 108 |
+
raise HTTPException(status_code=400, detail="Username is required")
|
| 109 |
+
|
| 110 |
key = generate_key()
|
| 111 |
now = datetime.utcnow()
|
| 112 |
expires = now + timedelta(days=req.days)
|
|
|
|
| 117 |
"expires": expires.isoformat(),
|
| 118 |
"days": req.days,
|
| 119 |
"label": req.label,
|
| 120 |
+
"username": req.username.strip(),
|
| 121 |
"active": True,
|
| 122 |
+
"hwid": None,
|
| 123 |
"last_used": None,
|
| 124 |
"uses": 0
|
| 125 |
}
|
| 126 |
save_keys(keys)
|
| 127 |
|
| 128 |
+
return {
|
| 129 |
+
"success": True,
|
| 130 |
+
"key": key,
|
| 131 |
+
"username": req.username.strip(),
|
| 132 |
+
"expires": expires.isoformat(),
|
| 133 |
+
"days": req.days
|
| 134 |
+
}
|
| 135 |
|
| 136 |
|
| 137 |
@app.post("/api/validate")
|
|
|
|
| 152 |
if datetime.utcnow() > expires:
|
| 153 |
return {"valid": False, "message": "Key expired"}
|
| 154 |
|
| 155 |
+
# Check username
|
| 156 |
+
expected_user = entry.get("username", "")
|
| 157 |
+
if expected_user and req.username.strip().lower() != expected_user.strip().lower():
|
| 158 |
+
return {"valid": False, "message": "Invalid username for this key"}
|
| 159 |
+
|
| 160 |
# HWID binding: first use binds the key to this hardware
|
| 161 |
if entry["hwid"] is None:
|
| 162 |
entry["hwid"] = req.hwid
|
|
|
|
| 176 |
|
| 177 |
return {
|
| 178 |
"valid": True,
|
| 179 |
+
"message": f"Welcome {expected_user}! {days_left}d {hours_left}h remaining",
|
| 180 |
"days_left": days_left,
|
| 181 |
+
"username": expected_user,
|
| 182 |
"expires": entry["expires"]
|
| 183 |
}
|
| 184 |
|
|
|
|
| 239 |
result.append({
|
| 240 |
"key": key,
|
| 241 |
"label": data.get("label", ""),
|
| 242 |
+
"username": data.get("username", ""),
|
| 243 |
"days": data["days"],
|
| 244 |
"days_left": days_left,
|
| 245 |
"expired": expired,
|
|
|
|
| 251 |
"uses": data.get("uses", 0)
|
| 252 |
})
|
| 253 |
|
|
|
|
| 254 |
result.sort(key=lambda x: (not x["active"], x["expired"], x["created"]), reverse=False)
|
| 255 |
return {"keys": result, "total": len(result)}
|
| 256 |
|
|
|
|
| 272 |
min-height: 100vh;
|
| 273 |
}
|
| 274 |
|
|
|
|
| 275 |
.header {
|
| 276 |
background: linear-gradient(135deg, #7832ff 0%, #2878ff 100%);
|
| 277 |
padding: 20px 30px;
|
|
|
|
| 280 |
justify-content: space-between;
|
| 281 |
box-shadow: 0 4px 20px rgba(120, 50, 255, 0.3);
|
| 282 |
}
|
| 283 |
+
.header h1 { font-size: 24px; font-weight: 700; color: white; text-shadow: 0 2px 4px rgba(0,0,0,0.3); }
|
| 284 |
+
.header span { color: rgba(255,255,255,0.7); font-size: 14px; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
|
|
|
|
| 286 |
.login-container {
|
| 287 |
+
max-width: 400px; margin: 80px auto; padding: 30px;
|
| 288 |
+
background: #12121a; border-radius: 12px; border: 1px solid #1e1e30;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
}
|
| 290 |
+
.login-container h2 { color: #8b5cf6; margin-bottom: 20px; text-align: center; }
|
| 291 |
|
| 292 |
+
.container { max-width: 1200px; margin: 20px auto; padding: 0 20px; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
|
|
|
|
| 294 |
.card {
|
| 295 |
+
background: #12121a; border-radius: 10px; padding: 20px;
|
| 296 |
+
margin-bottom: 16px; border: 1px solid #1e1e30;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
}
|
| 298 |
+
.card h3 { color: #8b5cf6; margin-bottom: 12px; font-size: 16px; }
|
| 299 |
|
|
|
|
| 300 |
input, select {
|
| 301 |
+
background: #1a1a28; border: 1px solid #2a2a40; color: #e0e0e8;
|
| 302 |
+
padding: 10px 14px; border-radius: 6px; font-size: 14px;
|
| 303 |
+
width: 100%; outline: none; transition: border-color 0.2s;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
}
|
| 305 |
+
input:focus, select:focus { border-color: #7832ff; }
|
| 306 |
+
.input-group { margin-bottom: 12px; }
|
| 307 |
.input-group label {
|
| 308 |
+
display: block; margin-bottom: 4px; color: #888;
|
| 309 |
+
font-size: 12px; text-transform: uppercase; letter-spacing: 1px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 310 |
}
|
| 311 |
+
.input-row { display: flex; gap: 12px; }
|
| 312 |
+
.input-row > * { flex: 1; }
|
| 313 |
|
|
|
|
| 314 |
.btn {
|
| 315 |
+
padding: 10px 20px; border: none; border-radius: 6px;
|
| 316 |
+
font-size: 14px; font-weight: 600; cursor: pointer;
|
| 317 |
+
transition: all 0.2s; color: white;
|
| 318 |
+
}
|
| 319 |
+
.btn-primary { background: linear-gradient(135deg, #7832ff, #5a20d0); }
|
| 320 |
+
.btn-primary:hover { background: linear-gradient(135deg, #8b45ff, #6b30e0); box-shadow: 0 4px 15px rgba(120, 50, 255, 0.4); }
|
| 321 |
+
.btn-danger { background: #dc2626; }
|
| 322 |
+
.btn-danger:hover { background: #ef4444; }
|
| 323 |
+
.btn-success { background: #16a34a; }
|
| 324 |
+
.btn-success:hover { background: #22c55e; }
|
| 325 |
+
.btn-warning { background: #d97706; }
|
| 326 |
+
.btn-warning:hover { background: #f59e0b; }
|
| 327 |
+
.btn-sm { padding: 6px 12px; font-size: 12px; }
|
| 328 |
+
|
| 329 |
+
.keys-table { width: 100%; border-collapse: collapse; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 330 |
.keys-table th {
|
| 331 |
+
text-align: left; padding: 10px; color: #888;
|
| 332 |
+
font-size: 11px; text-transform: uppercase; letter-spacing: 1px;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
border-bottom: 1px solid #1e1e30;
|
| 334 |
}
|
| 335 |
+
.keys-table td { padding: 10px; border-bottom: 1px solid #1a1a28; font-size: 13px; }
|
| 336 |
+
.keys-table tr:hover td { background: #1a1a28; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
|
| 338 |
+
.badge { display: inline-block; padding: 3px 8px; border-radius: 4px; font-size: 11px; font-weight: 600; }
|
| 339 |
+
.badge-active { background: rgba(22, 163, 74, 0.2); color: #4ade80; }
|
| 340 |
+
.badge-inactive { background: rgba(220, 38, 38, 0.2); color: #f87171; }
|
| 341 |
+
.badge-expired { background: rgba(217, 119, 6, 0.2); color: #fbbf24; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
|
| 343 |
+
.key-text { font-family: 'Consolas', 'Courier New', monospace; color: #c4b5fd; font-size: 13px; cursor: pointer; }
|
| 344 |
+
.key-text:hover { color: #8b5cf6; }
|
| 345 |
+
|
| 346 |
+
.username-text { color: #60a5fa; font-weight: 600; font-size: 13px; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 347 |
|
| 348 |
.hwid-text {
|
| 349 |
+
font-family: monospace; font-size: 11px; color: #666;
|
| 350 |
+
max-width: 120px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 351 |
}
|
| 352 |
|
| 353 |
+
.stats { display: flex; gap: 16px; margin-bottom: 16px; }
|
|
|
|
|
|
|
|
|
|
|
|
|
| 354 |
.stat-box {
|
| 355 |
+
background: #12121a; border: 1px solid #1e1e30;
|
| 356 |
+
border-radius: 8px; padding: 16px 20px; flex: 1; text-align: center;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 357 |
}
|
| 358 |
+
.stat-box .number { font-size: 28px; font-weight: 700; color: #8b5cf6; }
|
| 359 |
+
.stat-box .label { font-size: 12px; color: #666; margin-top: 4px; }
|
| 360 |
|
| 361 |
.toast {
|
| 362 |
+
position: fixed; top: 20px; right: 20px; background: #16a34a;
|
| 363 |
+
color: white; padding: 12px 20px; border-radius: 8px; font-size: 14px;
|
| 364 |
+
display: none; z-index: 1000; box-shadow: 0 4px 15px rgba(0,0,0,0.3);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 365 |
}
|
| 366 |
+
.toast.error { background: #dc2626; }
|
| 367 |
+
.actions { display: flex; gap: 4px; flex-wrap: wrap; }
|
| 368 |
.hidden { display: none; }
|
| 369 |
|
|
|
|
| 370 |
::-webkit-scrollbar { width: 8px; }
|
| 371 |
::-webkit-scrollbar-track { background: #0a0a0f; }
|
| 372 |
::-webkit-scrollbar-thumb { background: #2a2a40; border-radius: 4px; }
|
| 373 |
::-webkit-scrollbar-thumb:hover { background: #3a3a55; }
|
|
|
|
| 374 |
.overflow-x { overflow-x: auto; }
|
| 375 |
+
|
| 376 |
+
.new-key-box {
|
| 377 |
+
margin-top: 12px; padding: 16px; background: #1a1a28;
|
| 378 |
+
border-radius: 8px; border: 1px solid #2a2a40;
|
| 379 |
+
}
|
| 380 |
+
.new-key-box .key-display {
|
| 381 |
+
font-family: 'Consolas', monospace; font-size: 20px;
|
| 382 |
+
color: #c4b5fd; letter-spacing: 1px;
|
| 383 |
+
}
|
| 384 |
+
.new-key-box .user-display {
|
| 385 |
+
font-size: 14px; color: #60a5fa; margin-top: 4px;
|
| 386 |
+
}
|
| 387 |
+
.copy-all-btn {
|
| 388 |
+
margin-top: 8px; display: flex; gap: 8px;
|
| 389 |
+
}
|
| 390 |
</style>
|
| 391 |
</head>
|
| 392 |
<body>
|
|
|
|
| 412 |
<!-- Main Dashboard -->
|
| 413 |
<div id="dashboard" class="container hidden">
|
| 414 |
|
|
|
|
| 415 |
<div class="stats">
|
| 416 |
<div class="stat-box">
|
| 417 |
<div class="number" id="statTotal">0</div>
|
|
|
|
| 435 |
<div class="card">
|
| 436 |
<h3>Create New Key</h3>
|
| 437 |
<div class="input-row">
|
| 438 |
+
<div class="input-group">
|
| 439 |
+
<label>Username *</label>
|
| 440 |
+
<input type="text" id="keyUsername" placeholder="Username for this key...">
|
| 441 |
+
</div>
|
| 442 |
<div class="input-group">
|
| 443 |
<label>Duration (Days)</label>
|
| 444 |
<input type="number" id="keyDays" value="30" min="1" max="365">
|
| 445 |
</div>
|
| 446 |
<div class="input-group">
|
| 447 |
<label>Label (optional)</label>
|
| 448 |
+
<input type="text" id="keyLabel" placeholder="Notes...">
|
| 449 |
</div>
|
| 450 |
<div style="display:flex;align-items:flex-end">
|
| 451 |
<button class="btn btn-primary" onclick="createKey()">Create Key</button>
|
| 452 |
</div>
|
| 453 |
</div>
|
| 454 |
+
<div id="newKeyResult" class="new-key-box hidden">
|
| 455 |
+
<div style="color:#888;font-size:12px;text-transform:uppercase;letter-spacing:1px">New Key Created</div>
|
| 456 |
+
<div class="key-display" id="newKeyText"></div>
|
| 457 |
+
<div class="user-display">Username: <span id="newKeyUser"></span></div>
|
| 458 |
+
<div class="copy-all-btn">
|
| 459 |
+
<button class="btn btn-sm btn-primary" onclick="copyKey()">Copy Key</button>
|
| 460 |
+
<button class="btn btn-sm btn-primary" onclick="copyAll()">Copy Key + Username</button>
|
| 461 |
+
</div>
|
| 462 |
</div>
|
| 463 |
</div>
|
| 464 |
|
|
|
|
| 473 |
<thead>
|
| 474 |
<tr>
|
| 475 |
<th>Key</th>
|
| 476 |
+
<th>Username</th>
|
| 477 |
<th>Label</th>
|
| 478 |
<th>Status</th>
|
| 479 |
<th>Days Left</th>
|
|
|
|
| 483 |
</tr>
|
| 484 |
</thead>
|
| 485 |
<tbody id="keysBody">
|
| 486 |
+
<tr><td colspan="8" style="text-align:center;color:#666">Loading...</td></tr>
|
| 487 |
</tbody>
|
| 488 |
</table>
|
| 489 |
</div>
|
|
|
|
| 492 |
|
| 493 |
<script>
|
| 494 |
let adminPassword = '';
|
| 495 |
+
let lastCreatedKey = '';
|
| 496 |
+
let lastCreatedUser = '';
|
| 497 |
|
| 498 |
function showToast(msg, isError) {
|
| 499 |
const t = document.getElementById('toast');
|
|
|
|
| 534 |
const data = await api('list-keys', { admin_password: adminPassword });
|
| 535 |
const keys = data.keys;
|
| 536 |
|
|
|
|
| 537 |
document.getElementById('statTotal').textContent = keys.length;
|
| 538 |
document.getElementById('statActive').textContent = keys.filter(k => k.active && !k.expired).length;
|
| 539 |
document.getElementById('statBound').textContent = keys.filter(k => k.hwid).length;
|
| 540 |
document.getElementById('statExpired').textContent = keys.filter(k => k.expired).length;
|
| 541 |
|
|
|
|
| 542 |
const tbody = document.getElementById('keysBody');
|
| 543 |
if (keys.length === 0) {
|
| 544 |
+
tbody.innerHTML = '<tr><td colspan="8" style="text-align:center;color:#666">No keys yet. Create one above.</td></tr>';
|
| 545 |
return;
|
| 546 |
}
|
| 547 |
|
|
|
|
| 552 |
else statusBadge = '<span class="badge badge-active">Active</span>';
|
| 553 |
|
| 554 |
const hwid = k.hwid
|
| 555 |
+
? '<span class="hwid-text" title="' + k.hwid + '">' + k.hwid.substring(0,16) + '...</span>'
|
| 556 |
: '<span style="color:#444">Unbound</span>';
|
| 557 |
|
| 558 |
const toggleBtn = k.active
|
| 559 |
+
? '<button class="btn btn-sm btn-warning" onclick="toggleKey(\\''+k.key+'\\',false)">Disable</button>'
|
| 560 |
+
: '<button class="btn btn-sm btn-success" onclick="toggleKey(\\''+k.key+'\\',true)">Enable</button>';
|
| 561 |
|
| 562 |
const resetBtn = k.hwid
|
| 563 |
+
? '<button class="btn btn-sm btn-primary" onclick="resetHwid(\\''+k.key+'\\')">Reset HWID</button>'
|
| 564 |
: '';
|
| 565 |
|
| 566 |
+
return '<tr>' +
|
| 567 |
+
'<td><span class="key-text" onclick="navigator.clipboard.writeText(\\''+k.key+'\\');showToast(\\'Copied!\\')" title="Click to copy">' + k.key + '</span></td>' +
|
| 568 |
+
'<td><span class="username-text">' + (k.username || '-') + '</span></td>' +
|
| 569 |
+
'<td>' + (k.label || '-') + '</td>' +
|
| 570 |
+
'<td>' + statusBadge + '</td>' +
|
| 571 |
+
'<td>' + (k.expired ? '0' : k.days_left + 'd') + '</td>' +
|
| 572 |
+
'<td>' + hwid + '</td>' +
|
| 573 |
+
'<td>' + k.uses + '</td>' +
|
| 574 |
+
'<td class="actions">' + toggleBtn + resetBtn +
|
| 575 |
+
'<button class="btn btn-sm btn-danger" onclick="deleteKey(\\''+k.key+'\\')">Delete</button>' +
|
| 576 |
+
'</td></tr>';
|
|
|
|
|
|
|
| 577 |
}).join('');
|
| 578 |
} catch(e) {
|
| 579 |
showToast('Failed to load keys: ' + e.message, true);
|
|
|
|
| 581 |
}
|
| 582 |
|
| 583 |
async function createKey() {
|
| 584 |
+
const username = document.getElementById('keyUsername').value.trim();
|
| 585 |
const days = parseInt(document.getElementById('keyDays').value) || 30;
|
| 586 |
const label = document.getElementById('keyLabel').value;
|
| 587 |
+
|
| 588 |
+
if (!username) {
|
| 589 |
+
showToast('Username is required!', true);
|
| 590 |
+
document.getElementById('keyUsername').focus();
|
| 591 |
+
return;
|
| 592 |
+
}
|
| 593 |
+
|
| 594 |
try {
|
| 595 |
const data = await api('create-key', {
|
| 596 |
admin_password: adminPassword,
|
| 597 |
days: days,
|
| 598 |
+
label: label,
|
| 599 |
+
username: username
|
| 600 |
});
|
| 601 |
+
lastCreatedKey = data.key;
|
| 602 |
+
lastCreatedUser = username;
|
| 603 |
document.getElementById('newKeyText').textContent = data.key;
|
| 604 |
+
document.getElementById('newKeyUser').textContent = username;
|
| 605 |
document.getElementById('newKeyResult').classList.remove('hidden');
|
| 606 |
+
showToast('Key created for ' + username);
|
| 607 |
+
document.getElementById('keyUsername').value = '';
|
| 608 |
+
document.getElementById('keyLabel').value = '';
|
| 609 |
loadKeys();
|
| 610 |
} catch(e) {
|
| 611 |
showToast('Failed: ' + e.message, true);
|
|
|
|
| 613 |
}
|
| 614 |
|
| 615 |
function copyKey() {
|
| 616 |
+
navigator.clipboard.writeText(lastCreatedKey);
|
| 617 |
+
showToast('Key copied!');
|
| 618 |
+
}
|
| 619 |
+
|
| 620 |
+
function copyAll() {
|
| 621 |
+
navigator.clipboard.writeText('Key: ' + lastCreatedKey + '\\nUsername: ' + lastCreatedUser);
|
| 622 |
+
showToast('Key + Username copied!');
|
| 623 |
}
|
| 624 |
|
| 625 |
async function toggleKey(key, active) {
|
|
|
|
| 633 |
}
|
| 634 |
|
| 635 |
async function resetHwid(key) {
|
| 636 |
+
if (!confirm('Reset HWID for ' + key + '?')) return;
|
| 637 |
try {
|
| 638 |
await api('reset-hwid', { admin_password: adminPassword, key: key });
|
| 639 |
showToast('HWID reset');
|