Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 0 additions & 46 deletions CodexBuddyMac/CodexBuddyMac/Bridge/codex_usage_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import json
import os
import secrets
import socket
import ssl
import subprocess
import sys
Expand All @@ -23,8 +22,6 @@


CODEX_USAGE_URL = "https://chatgpt.com/backend-api/wham/usage"
DISCOVERY_REQUEST = b"CODEXBUDDY_DISCOVER_V1"
DISCOVERY_REPLY_PREFIX = "CODEXBUDDY_USAGE_V1 "
CLAUDE_USAGE_FILE = Path(__file__).with_name("claude_usage.json")

# Live Claude usage (Pro/Max OAuth). Same metric Claude Code shows in /usage.
Expand Down Expand Up @@ -169,41 +166,6 @@ def persist_claude_retry_deadline(path: Path, deadline: float) -> None:
pass


class DiscoveryResponder(threading.Thread):
def __init__(self, host: str, http_port: int, discovery_port: int, advertise_host: str | None) -> None:
super().__init__(daemon=True)
self.host = host
self.http_port = http_port
self.discovery_port = discovery_port
self.advertise_host = advertise_host

def run(self) -> None:
bind_host = self.host if self.host not in ("", "0.0.0.0", "::") else "0.0.0.0"
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((bind_host, self.discovery_port))
while True:
data, addr = sock.recvfrom(1024)
if data.strip() != DISCOVERY_REQUEST:
continue

host = self.advertise_host or local_address_for(addr[0])
url = f"http://{host}:{self.http_port}/usage.json"
reply = (DISCOVERY_REPLY_PREFIX + url).encode("utf-8")
for _ in range(3):
sock.sendto(reply, addr)
time.sleep(0.03)


def local_address_for(remote_host: str) -> str:
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
try:
sock.connect((remote_host, 9))
return sock.getsockname()[0]
except OSError:
return socket.gethostbyname(socket.gethostname())


def trusted_ssl_context() -> ssl.SSLContext:
try:
import certifi
Expand Down Expand Up @@ -1121,9 +1083,6 @@ def main() -> None:
parser.add_argument("--manual", action="store_true", help="Only serve --file; do not call Codex live usage.")
parser.add_argument("--no-claude", action="store_true", help="Do not attach Claude usage from --claude-file.")
parser.add_argument("--no-live-claude", action="store_true", help="Do not call Claude OAuth usage; only attach --claude-file.")
parser.add_argument("--discovery-port", type=int, default=8788)
parser.add_argument("--advertise-host", help="Host/IP to return to discovery clients. Defaults to the LAN IP used to reach each client.")
parser.add_argument("--no-discovery", action="store_true", help="Disable UDP bridge discovery.")
args = parser.parse_args()

UsageHandler.usage_file = Path(args.file)
Expand All @@ -1147,13 +1106,8 @@ def main() -> None:
bridge_lock.close()
raise
write_runtime_pid(BRIDGE_PID_FILE)
if not args.no_discovery:
DiscoveryResponder(args.host, args.port, args.discovery_port, args.advertise_host).start()
mode = "manual JSON" if UsageHandler.live is False else f"live Codex OAuth via {UsageHandler.auth_file}"
print(f"Serving {mode} at http://{args.host}:{args.port}/usage.json")
if not args.no_discovery:
advertised = args.advertise_host or "auto LAN IP"
print(f"Discovery on UDP {args.discovery_port}, advertising {advertised}:{args.port}")
if UsageHandler.live:
print(f"Falls back to {UsageHandler.usage_file} if live usage is unavailable")
if UsageHandler.claude:
Expand Down
3 changes: 1 addition & 2 deletions CodexBuddyMac/CodexBuddyMac/BridgeService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ enum BridgeService {
"--host", "127.0.0.1",
"--port", "8789",
"--file", fallbackUsageURL.path,
"--claude-file", claudeUsageURL.path,
"--no-discovery"
"--claude-file", claudeUsageURL.path
]
stdoutHandle = try FileHandle(forWritingTo: stdoutURL)
stderrHandle = try FileHandle(forWritingTo: stderrURL)
Expand Down
Loading