Files
viktor-autoreg/tools/gh_tools_reward.py
T

132 lines
5.5 KiB
Python

# -*- coding: utf-8 -*-
"""
Viktor connected-tools reward looter (1000 credits / workspace, one-time).
PROVEN 2026-10-10:
GET /api/integration/coworker_github/connect?app_url=...&github_flow=install&jwt=***
-> 307 https://github.com/apps/viktor-com/installations/new?state=...
GitHub: login pooled account -> follow chain to /apps/viktor-com/installations/new/permissions
-> POST /apps/viktor-com/installations (hidden fields + install_target=all)
-> 200 HTML with meta-refresh url=https://api.viktor.com/api/integration/coworker_github/callback?code=..&installation_id=..&state=..
(meta-refresh! urllib/requests do NOT follow it - extract url= manually)
GET callback (no redirects) -> 307 app.viktor.com
GET /api/integration/github/installations?jwt=*** -> {"connected":true,"installation_count":N}
POST /api/coworker/grant-connected-tools-reward?jwt=*** -> {"granted_credits":1000,
"connected_tools_count":1,"reward_credits_balance":1000}
Notes:
- Reward is ONE-TIME 1000 credits per workspace (2nd+ tool -> granted_credits:0).
- Idempotent: re-POST returns 0, balance unchanged.
- Auth: query-param ?jwt=*** (Bearer/cookie currently 500 on some endpoints).
- reward_credits_balance counts into total remaining credits (total-remaining-credits.js:
credits_balance + previous + top_up + reward).
- In-house MCP apps (baremetrics/openrouter/... grant=api_key) -> 403
"In-house connect is not enabled for this workspace" (feature-gated).
- Other connect ids: coworker_bitbucket, coworker_msteams, coworker_discord,
coworker_github_cli, coworker_text2im (401 via query-jwt on this workspace).
Usage:
python3 tools/gh_tools_reward.py --jwt <JWT> --accounts gh_accounts.json [--n 1]
gh_accounts.json: [{"login","email","password","totp"}, ...]
Requires: requests, pyotp
"""
import argparse, json, re, time, urllib.parse
import requests, pyotp
UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/131.0 Safari/537.36"}
API = "https://api.viktor.com"
def qj(jwt):
return "?" + "jwt=" + urllib.parse.quote(jwt)
def hidden_fields(html):
fields = {}
for m in re.finditer(r'<input[^>]*type="hidden"[^>]*>', html):
nm = re.search(r'name="([^"]+)"', m.group(0))
vl = re.search(r'value="([^"]*)"', m.group(0))
if nm:
fields[nm.group(1)] = vl.group(1) if vl else ""
return fields
def gh_login(acc):
s = requests.Session()
s.headers.update(UA)
r = s.get("https://github.com/login", timeout=30)
f = hidden_fields(r.text)
f.update({"login": acc["email"], "password": acc["password"],
"webauthn-conditional": "undefined", "javascript-support": "enabled",
"webauthn-support": "supported", "webauthn-iuvpaa-support": "unknown",
"webauthn-iuv": "false", "commit": "Sign in",
"timestamp": str(int(time.time() * 1000))})
r = s.post("https://github.com/session", data=f, allow_redirects=False, timeout=30)
loc = r.headers.get("Location", "")
if "two-factor" in loc:
if not loc.startswith("http"):
loc = "https://github.com" + loc
r2 = s.get(loc, timeout=30)
f2 = hidden_fields(r2.text)
f2["app_otp"] = pyotp.TOTP(acc["totp"]).now()
s.post("https://github.com/sessions/two-factor", data=f2, allow_redirects=True, timeout=30)
return s
def install_viktor_app(s, jwt):
r = requests.get(API + "/api/integration/coworker_github/connect"
"?app_url=https%3A%2F%2Fapp.viktor.com&github_flow=install" + "&" + qj(jwt).lstrip("?"),
headers=UA, allow_redirects=False, timeout=30)
cur = r.headers.get("Location", "")
if "github.com" not in cur:
return None, "connect %s" % r.status_code
rr = None
for _ in range(4):
rr = s.get(cur, timeout=30, allow_redirects=False)
if rr.status_code in (301, 302, 307, 308):
nx = rr.headers.get("Location")
if not nx.startswith("http"):
nx = "https://github.com" + nx
cur = nx
continue
break
m = re.search(r'<form[^>]*action="/apps/viktor-com/installations"[^>]*>(.*?)</form>', rr.text, re.S)
if not m:
return None, "no install form (%s)" % rr.url[:80]
f = hidden_fields(m.group(0))
f["install_target"] = "all"
r2 = s.post("https://github.com/apps/viktor-com/installations", data=f, allow_redirects=False, timeout=30)
cb = re.search(r'url=(https://api\.viktor\.com[^"]+)', r2.text)
if not cb:
return None, "no callback (len=%d)" % len(r2.text)
cburl = cb.group(1).replace("&amp;", "&")
rc = requests.get(cburl, headers=UA, allow_redirects=False, timeout=30)
return cburl, "callback %s" % rc.status_code
def claim(jwt):
r = requests.post(API + "/api/coworker/grant-connected-tools-reward" + qj(jwt),
json={}, headers=UA, timeout=30)
return r.status_code, r.json()
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--jwt", required=True)
ap.add_argument("--accounts", required=True)
ap.add_argument("--n", type=int, default=1)
a = ap.parse_args()
accs = json.load(open(a.accounts, encoding="utf-8"))
for acc in accs[: a.n]:
s = gh_login(acc)
cb, msg = install_viktor_app(s, a.jwt)
print(acc["login"], "install:", msg)
if cb:
time.sleep(3)
print(acc["login"], "grant:", claim(a.jwt))
time.sleep(3)
if __name__ == "__main__":
main()