create_sessions_browser.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/env python3
  2. """
  3. Requirements:
  4. pip install -r tools/requirements.txt
  5. Usage:
  6. python3 tools/create_sessions_browser.py <accounts_file> [--append sessions.jsonl] [--headless] [--delay]
  7. Examples:
  8. # Output to terminal
  9. python3 tools/create_sessions_browser.py <accounts_file>
  10. # Append to sessions.jsonl
  11. python3 tools/create_sessions_browser.py <accounts_file> --append sessions.jsonl
  12. # Add 5 second delay between sessions (default: 3)
  13. python3 tools/create_sessions_browser.py <accounts_file> --delay 5
  14. # Headless mode (may increase detection risk)
  15. python3 tools/create_sessions_browser.py <accounts_file> --headless
  16. Input (accounts_file):
  17. [{"username": "user", "password": "pass", "totp": "totp_secret"}, {...}, ...]
  18. Output:
  19. {"kind": "cookie", "username": "...", "id": "...", "auth_token": "...", "ct0": "..."}
  20. {"kind": "cookie", "username": "...", "id": "...", "auth_token": "...", "ct0": "..."}
  21. ...
  22. """
  23. import asyncio
  24. import json
  25. import sys
  26. from time import sleep
  27. from create_session_browser import login_and_get_session
  28. async def main():
  29. if len(sys.argv) < 2:
  30. print(
  31. "Usage: python3 create_sessions_browser.py <accounts_file>"
  32. " [--append sessions.jsonl] [--headless] [--delay N]"
  33. )
  34. sys.exit(1)
  35. input_file = sys.argv[1]
  36. append_file = None
  37. headless = False
  38. delay = 3
  39. # Parse optional arguments
  40. i = 2
  41. while i < len(sys.argv):
  42. arg = sys.argv[i]
  43. if arg == "--append":
  44. if i + 1 < len(sys.argv):
  45. append_file = sys.argv[i + 1]
  46. i += 2
  47. else:
  48. print("[!] Error: --append requires a filename", file=sys.stderr)
  49. sys.exit(1)
  50. elif arg == "--headless":
  51. headless = True
  52. i += 1
  53. elif arg == "--delay":
  54. delay = int(sys.argv[i + 1])
  55. i += 2
  56. else:
  57. print(f"[!] Warning: Unknown argument: {arg}", file=sys.stderr)
  58. i += 1
  59. with open(input_file) as f:
  60. accounts = json.load(f)
  61. if not accounts:
  62. print("No accounts in file")
  63. sys.exit(0)
  64. ok, fail = [], []
  65. for idx, acc in enumerate(accounts, 1):
  66. username = acc["username"]
  67. print(
  68. f"\n[{idx}/{len(accounts)}] {username}...",
  69. file=sys.stderr,
  70. flush=True,
  71. )
  72. try:
  73. session = await login_and_get_session(
  74. username, acc["password"], acc.get("totp"), headless
  75. )
  76. if append_file:
  77. with open(append_file, "a") as f:
  78. f.write(json.dumps(session) + "\n")
  79. else:
  80. print(json.dumps(session))
  81. ok.append(username)
  82. print(
  83. f" ✓ saved (id={session['id']})",
  84. file=sys.stderr,
  85. flush=True,
  86. )
  87. except Exception as error:
  88. fail.append(username)
  89. print(
  90. f" ✗ {error}",
  91. file=sys.stderr,
  92. flush=True,
  93. )
  94. if idx < len(accounts):
  95. sleep(delay)
  96. print(
  97. f"\nDone: {len(ok)} ok, {len(fail)} failed",
  98. file=sys.stderr,
  99. flush=True,
  100. )
  101. if fail:
  102. print(f" failed: {fail}", file=sys.stderr, flush=True)
  103. if __name__ == "__main__":
  104. asyncio.run(main())