OverviewFilesCommitsIssuesDocsReleasesPeers

atproto-bridge / resolver / atproto-resolver.py

33 lines · python ref:
#!/usr/bin/env python3
# atproto handle resolver — serves /.well-known/atproto-did for *.7rnx.net,
# returning the DID for the requesting Host from a central JSON registry.
# Proves multi-user wildcard-subdomain handle resolution with NO per-user DNS:
# adding a user = one line in the registry, not a DNS record.
import http.server, json

REGISTRY = "/home/fleeky/atproto-handles.json"   # {"<handle>.7rnx.net": "did:plc:..."}
BIND, PORT = "127.0.0.1", 8099

class Handler(http.server.BaseHTTPRequestHandler):
    def do_GET(self):
        if self.path != "/.well-known/atproto-did":
            self.send_error(404, "not found"); return
        host = (self.headers.get("Host") or "").split(":")[0].lower()
        try:
            reg = json.load(open(REGISTRY))
        except Exception:
            reg = {}
        did = reg.get(host)
        if not did:
            self.send_error(404, "no such handle"); return
        body = did.encode()
        self.send_response(200)
        self.send_header("Content-Type", "text/plain")
        self.send_header("Content-Length", str(len(body)))
        self.end_headers()
        self.wfile.write(body)
    def log_message(self, *a): pass

if __name__ == "__main__":
    http.server.HTTPServer((BIND, PORT), Handler).serve_forever()