06-oct: [omniroute] re-auth provider Claude + helper OAuth y vigilante portapapeles
This commit is contained in:
24
omniroute/clipboard_watch.ps1
Normal file
24
omniroute/clipboard_watch.ps1
Normal file
@@ -0,0 +1,24 @@
|
||||
Add-Type -AssemblyName System.Windows.Forms
|
||||
$codeFile = "C:\Users\juanm\Documents\Github\INFRAESTRUCTURA\omniroute\oauth_code.txt"
|
||||
$exitFile = "C:\Users\juanm\Documents\Github\INFRAESTRUCTURA\omniroute\oauth_helper.exit"
|
||||
$last = ""
|
||||
try { $last = [System.Windows.Forms.Clipboard]::GetText() } catch {}
|
||||
$deadline = (Get-Date).AddMinutes(20)
|
||||
while ((Get-Date) -lt $deadline) {
|
||||
if (Test-Path $exitFile) { break }
|
||||
$c = ""
|
||||
try { $c = [System.Windows.Forms.Clipboard]::GetText() } catch {}
|
||||
if ($c -and $c -ne $last -and $c.Length -ge 20 -and ($c -match "#" -or $c -match "code=")) {
|
||||
# Limpiar espacios/saltos por si copia con formato
|
||||
$clean = $c.Trim()
|
||||
if ($clean -match "https?://") {
|
||||
$m = [regex]::Match($clean, "code=([^&#\s]+)")
|
||||
$s = [regex]::Match($clean, "state=([^&#\s]+)")
|
||||
if ($m.Success -and $s.Success) { $clean = $m.Groups[1].Value + "#" + $s.Groups[1].Value }
|
||||
}
|
||||
Set-Content -Path $codeFile -Value $clean -NoNewline
|
||||
break
|
||||
}
|
||||
$last = $c
|
||||
Start-Sleep -Milliseconds 700
|
||||
}
|
||||
90
omniroute/oauth_helper.py
Normal file
90
omniroute/oauth_helper.py
Normal file
@@ -0,0 +1,90 @@
|
||||
import subprocess, time, os, threading
|
||||
|
||||
base = r"C:\Users\juanm\Documents\Github\INFRAESTRUCTURA\omniroute"
|
||||
url_file = os.path.join(base, "oauth_url.txt")
|
||||
code_file = os.path.join(base, "oauth_code.txt")
|
||||
log_file = os.path.join(base, "oauth_helper.log")
|
||||
exit_file = os.path.join(base, "oauth_helper.exit")
|
||||
hb_file = os.path.join(base, "oauth_helper.hb")
|
||||
|
||||
for f in (url_file, code_file, log_file, exit_file, hb_file):
|
||||
try:
|
||||
os.remove(f)
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
|
||||
proc = subprocess.Popen(
|
||||
["omniroute.cmd", "oauth", "start", "--provider", "claude-code", "--no-browser"],
|
||||
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
|
||||
text=True, bufsize=1
|
||||
)
|
||||
|
||||
log = open(log_file, "w", encoding="utf-8", buffering=1)
|
||||
buf = ""
|
||||
|
||||
|
||||
def reader():
|
||||
global buf
|
||||
while True:
|
||||
ch = proc.stdout.read(1)
|
||||
if ch == "":
|
||||
if proc.poll() is not None:
|
||||
break
|
||||
time.sleep(0.05)
|
||||
continue
|
||||
log.write(ch)
|
||||
buf += ch
|
||||
if "https://claude.ai/oauth/authorize" in buf and not os.path.exists(url_file):
|
||||
start = buf.find("https://claude.ai/oauth/authorize")
|
||||
nl = buf.find("\n", start)
|
||||
if nl != -1:
|
||||
with open(url_file, "w", encoding="utf-8") as uf:
|
||||
uf.write(buf[start:nl].strip())
|
||||
if len(buf) > 200000:
|
||||
buf = buf[-50000:]
|
||||
|
||||
|
||||
threading.Thread(target=reader, daemon=True).start()
|
||||
|
||||
# Esperar la URL (max 2 min, es rapido)
|
||||
wait_url = time.time() + 120
|
||||
while time.time() < wait_url and not os.path.exists(url_file):
|
||||
time.sleep(0.5)
|
||||
|
||||
# Sondear el codigo SIN limite de tiempo; hasta que el proceso termine
|
||||
sent = False
|
||||
while proc.poll() is None:
|
||||
with open(hb_file, "w") as hb:
|
||||
hb.write(str(time.time()))
|
||||
if not sent and os.path.exists(code_file):
|
||||
try:
|
||||
with open(code_file, "r", encoding="utf-8") as cf:
|
||||
code = cf.read().strip()
|
||||
except Exception:
|
||||
code = ""
|
||||
if code:
|
||||
try:
|
||||
os.remove(code_file)
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
log.write("\n[helper] enviando codigo al proceso...\n")
|
||||
try:
|
||||
proc.stdin.write(code + "\n")
|
||||
proc.stdin.flush()
|
||||
except Exception as e:
|
||||
log.write("[helper] fallo al escribir stdin: %r\n" % e)
|
||||
sent = True
|
||||
time.sleep(1)
|
||||
|
||||
try:
|
||||
rest = proc.stdout.read()
|
||||
if rest:
|
||||
log.write(rest)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
log.write("\n=== PROCESO TERMINADO rc=%s ===\n" % proc.poll())
|
||||
log.close()
|
||||
|
||||
with open(exit_file, "w") as ef:
|
||||
ef.write("done")
|
||||
Reference in New Issue
Block a user