~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/stream

jetstream v2 in zig · mirror of https://tangled.org/zat.dev/stream

0
0
38 lines · 1.5 KB Raw History
#!/usr/bin/env python3
"""Probe whether bsky.network will still replay from a cursor.

Usage: wsprobe <cursor>
  101 + a binary commit frame => replay window still open
  a non-101 handshake or an #info frame => the cursor has aged out
"""
import socket, ssl, base64, os, struct, sys, json
host="bsky.network"; cursor=sys.argv[1]
path=f"/xrpc/com.atproto.sync.subscribeRepos?cursor={cursor}"
key=base64.b64encode(os.urandom(16)).decode()
raw=socket.create_connection((host,443),timeout=20)
s=ssl.create_default_context().wrap_socket(raw,server_hostname=host)
s.send(f"GET {path} HTTP/1.1\r\nHost: {host}\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n"
       f"Sec-WebSocket-Key: {key}\r\nSec-WebSocket-Version: 13\r\n\r\n".encode())
buf=b""
while b"\r\n\r\n" not in buf: buf+=s.recv(4096)
head,rest=buf.split(b"\r\n\r\n",1)
print("handshake:", head.split(b"\r\n")[0].decode())
if b"101" not in head.split(b"\r\n")[0]:
    print(rest[:300].decode(errors="replace")); sys.exit()
def frame():
    global rest
    while len(rest)<2: rest+=s.recv(4096)
    b1,b2=rest[0],rest[1]; ln=b2&127; i=2
    if ln==126:
        while len(rest)<4: rest+=s.recv(4096)
        ln=struct.unpack(">H",rest[2:4])[0]; i=4
    elif ln==127:
        while len(rest)<10: rest+=s.recv(4096)
        ln=struct.unpack(">Q",rest[2:10])[0]; i=10
    while len(rest)<i+ln: rest+=s.recv(4096)
    p=rest[i:i+ln]; rest=rest[i+ln:]
    return b1&15,p
op,p=frame()
print("first frame opcode:",op,"len:",len(p))
print("head bytes:",p[:120])
s.close()