Skip to the content.

github-api-proxy

github-api-proxy

🔀🐙 A transparent reverse proxy for the GitHub REST and GraphQL APIs. Clients only change their base URL; every call is forwarded to api.github.com unchanged, and the proxy records who called what, when, how it went and how much rate limit is left.

Contents

Using it from clients

Replace https://api.github.com with the proxy URL. Both the github.com layout and the GitHub Enterprise layout (/api/v3, /api/graphql) are accepted, because some SDKs add those paths themselves when the host isn’t github.com.

Client Change
Pancake (PHP) GitHub::setDefaultEndpoint('https://github-proxy.example.com/') once at bootstrap, or set GITHUB_API_URL — every new GitHub(...) then uses the proxy (needs pancake#690)
curl / any HTTP client https://github-proxy.example.com/repos/owner/repo
Octokit.NET new GitHubClient(header, new Uri("https://github-proxy.example.com")) (it adds /api/v3, which is handled)
Octokit.js new Octokit({ baseUrl: "https://github-proxy.example.com" })
PyGithub Github(base_url="https://github-proxy.example.com", auth=...)
GraphQL POST https://github-proxy.example.com/graphql

Not covered. git clone/push go to github.com, not the API. Release asset uploads go to uploads.github.com, and some response bodies contain absolute api.github.com URLs (for example url fields). A client that follows those URLs leaves the proxy for that call.

Configuration (Grimoire)

In production every setting and secret lives in Grimoire. The host only has the three bootstrap values, written to /etc/github-api-proxy/env by the Deploy workflow:

Variable  
Grimoire__BaseUrl Grimoire API root (GRIMOIRE_BASE_URL repository secret)
Grimoire__ApiKey The application’s consumer API key (GRIMOIRE_API_KEY)
Grimoire__Environment Environment slug, default production (GRIMOIRE_ENVIRONMENT)

At startup the service reads every configuration of the environment (GET consumer/configurations) and these secrets by name (GET consumer/secrets/{name}; Grimoire cannot list secrets, so names are fixed, -- standing for :):

Secret in Grimoire Required  
MySql--ConnectionString ✅ Go MySQL DSN: user:pass@tcp(host:3306)/db?tls=preferred (UTC and parseTime are forced)
Admin--ApiToken ✅ Bearer token for the stats API (≥ 24 chars; openssl rand -hex 32)
LogStream--ApiToken   The LogStream client’s token (signs UDP packets)
Configuration in Grimoire Default  
Network:AllowedSources required IPs, CIDRs and host names allowed to call the proxy — see below
Network:RefreshInterval 5m How often the allow-list is re-read from Grimoire and host names re-resolved
Network:PublicHealth false true lets anyone reach GET /health on the admin listener (e.g. UptimeRobot)
Proxy:ListenAddr 127.0.0.1:8080 Proxy listener (behind NGINX)
Proxy:AdminListenAddr 127.0.0.1:8081 Stats API, /health, Swagger UI
Proxy:PublicBaseUrl derived per request Base used when rewriting Link/Location
Proxy:TrustedProxies 127.0.0.1,::1 Peers allowed to set X-Forwarded-*
Proxy:UpstreamUrl https://api.github.com  
Proxy:UpstreamTimeout 60s Max wait for GitHub’s response headers
Proxy:ResolveIdentities true Look up each new token’s account (one call on that token’s own quota)
Proxy:RateLimitWarnPercent 10 Warn when a caller’s remaining quota drops below this %
Proxy:SlowRequestThreshold 10s Warn on slower calls
Storage:RetentionDays 30 Hourly cleanup of old rows
Storage:QueueSize / Storage:BatchSize / Storage:FlushInterval 10000 / 200 / 1s MySQL writer
LogStream:Server – LogStream base URL (also the default UDP host); empty disables LogStream
LogStream:ApiKey – The LogStream client’s key (its app_key), e.g. github-api-proxy
LogStream:AppId the release semVer Override the app_id
LogStream:Transport Udp Udp or Http
LogStream:UdpHost / LogStream:UdpPort LogStream:Server’s host / 8082 Use 127.0.0.1 on the LogStream host (vinhedo3)
LogStream:UdpMaxPacketSize 8000 Larger entries go over HTTPS
LogStream:MinimumLevel info Lowest level sent (everything still goes to journald)
LogStream:Enabled true Switch LogStream off without deleting anything

Values are loaded once at startup — restart the service to apply changes — except Network:AllowedSources, which is re-read every Network:RefreshInterval. If Grimoire is unreachable, rejects the key or does not know the environment, the service refuses to start, so a deploy rolls back instead of running on partial settings. An environment variable with the key’s name (: written as __, e.g. Proxy__ListenAddr) still overrides a single Grimoire value; without Grimoire__BaseUrl, everything is read from the environment (local development).

The database tables (github_proxy_requests, github_proxy_tokens, github_proxy_migrations) are created on startup from internal/store/migrations.

Network allow-list

Only sources in Network:AllowedSources may use the proxy and the stats API; everyone else gets a GitHub-style 403 ({"message":"github-api-proxy: requests from this address are not allowed"}). Refused proxy calls are stored (status 403, error blocked by network allow-list, counted as blocked in the summary) and reported to LogStream as a warning at most once per address every 10 minutes. Loopback is always allowed.

Entries are separated by commas, semicolons, spaces or new lines; # starts a comment:

node1.example.com, node2.example.com, node3.example.com,
node4.example.com, node5.example.com, node6.example.com,
198.51.100.30           # cPanel host
10.6.0.0/24             # WireGuard VPN subnet
home.example.dyndns.org # local machines via dynamic DNS
203.0.113.25            # or the home IP itself

The client address is the TCP peer, or — when the peer is in Proxy:TrustedProxies (the local NGINX) — the right-most untrusted X-Forwarded-For entry, so it can’t be spoofed by a header.

Who is the caller?

Token Resolved as Cost
ghp_, github_pat_, gho_, ghu_ GitHub login, via GET /user 1 call per new token
ghs_ (App installation) installation:<account>, via GET /installation/repositories?per_page=1 1 call per new token (they rotate hourly)
App JWT app:<app id> from the iss claim free
none anonymous (see client_ip / user_agent) –
rejected by GitHub invalid:<fingerprint> –

Until resolution finishes (milliseconds, in the background), rows show token:<fingerprint>; rows are joined with github_proxy_tokens when queried, so earlier rows pick up the name automatically.

Stats API and Swagger

Served on Proxy:AdminListenAddr and limited to the allow-list. Swagger UI is at /swagger/ and the OpenAPI 3 document at /openapi.yaml. All endpoints except /health need Authorization: Bearer <Admin--ApiToken>; period accepts 90m, 24h, 7d… (max 90d).

Endpoint Returns
GET /health status (ok/degraded), version, uptime, DB state, writer queue/drops, allow-list size
GET /api/v1/summary?period=24h totals, 4xx/5xx, upstream failures, rate-limited, blocked, latency, bytes
GET /api/v1/callers?period=24h per caller: requests, errors, first/last seen, token kinds, IPs, user agents
GET /api/v1/routes?period=24h&caller= busiest normalised endpoints (/repos/{owner}/{repo}/pulls/{id})
GET /api/v1/route-usage?period=24h&caller= per token, method, route and status: requests, last time, avg duration, total size
GET /api/v1/timeline?period=24h&bucket=hour requests/errors/latency per minute, hour or day
GET /api/v1/requests?status=5xx&caller=&before_id= latest requests (keyset pagination)
GET /api/v1/rate-limits latest quota per caller and resource, most exhausted first

A ready-made Projects Monitor client is in integrations/projects-monitor (same pattern as WebhooksHandler.php, secrets in Src/secrets/githubApiProxy.secrets.php).

The admin host also serves the project icon at /favicon.ico, /favicon.svg and /apple-touch-icon.png (embedded in the binary, cached for a day). The proxy port serves none of them, so it stays a byte-for-byte pass-through to GitHub. Logo sources are in docs/images, including a social-preview.png for the repository’s social preview setting.

LogStream

Startup (version, settings origin, transport), shutdown, the allow-list loading, new callers identified, and warnings/errors — low or exhausted rate limits, GitHub 5xx, slow calls, refused addresses, MySQL or Grimoire failures — go to LogStream. Repeated events are throttled; every entry of one process shares a trace_id, so a run can be followed from start to stop.

By default each entry is one HMAC-signed UDP datagram to LogStream:UdpHost:UdpPort (8082): the LogStream--ApiToken signs the packet and never travels on the wire. The vinhedo nodes are allowed by LogStream’s UDP_ALLOWED_SOURCES; on the LogStream host itself use LogStream:UdpHost=127.0.0.1. Entries larger than LogStream:UdpMaxPacketSize are posted over HTTPS instead, which is why LogStream:Server stays required. UDP is fire-and-forget: rejected packets (e.g. a clock more than 30 s off) show up in LogStream’s GET /api/health as udp.rejected, so keep the node NTP-synced.

The app_id is the release version computed by GitVersion (e.g. 1.4.0), baked into the binary, so the LogStream UI can tell releases apart. LogStream:AppId overrides it.

Versioning and deployment

GitVersion (GitVersion.yml, GitHubFlow, continuous deployment) computes the version from the commits on main: add +semver: minor (or major) to a commit message to bump; the default is a patch. github-api-proxy --version prints it.

Workflow When What
build.yml pull requests gofmt, vet, tests (race), coverage, ShellCheck, amd64 + arm64 builds
deploy.yml push to main build & test → deploy to the vinhedo node → tag vX.Y.Z and GitHub release

The deploy detects the node’s CPU (x86_64 → amd64 for vinhedo1–4, aarch64 → arm64 for vinhedo5–6), uploads that build with a checksum and runs the root-owned installer, which swaps the release atomically, waits for systemd’s READY=1 (sent once both listeners are up) and rolls back to the previous release if the new one does not come up. The tag is created only after a successful deploy.

One-time host setup

# On the chosen vinhedo node, from a checkout or an extracted release:
sudo deploy/scripts/bootstrap.sh deploy github-proxy.example.com github-proxy-api.example.com
sudo certbot --nginx -d github-proxy.example.com -d github-proxy-api.example.com

bootstrap.sh [deploy-user] [proxy-domain] [admin-domain] creates the github-proxy service user, /opt/github-api-proxy, /etc/github-api-proxy, the systemd unit, the trusted installer with its sudoers rule and, when domains are given, the NGINX site. It prints the host key fingerprints for SSH_KNOWN_HOSTS.

Repository secrets (environment production)

Secret  
SSH_HOST, SSH_USER, SSH_PRIVATE_KEY, SSH_PORT (optional) The vinhedo node and the deploy user
SSH_KNOWN_HOSTS ssh-keyscan -p PORT HOST, checked against bootstrap.sh’s fingerprints
GRIMOIRE_BASE_URL, GRIMOIRE_API_KEY, GRIMOIRE_ENVIRONMENT (optional) Rendered into /etc/github-api-proxy/env

Moving the proxy to another node is a matter of running bootstrap.sh there and changing the SSH_* secrets.

Security notes

Development

go test -race ./...
go vet ./...
# run locally without Grimoire: every key from the environment
Network__AllowedSources='*' MySql__ConnectionString='u:p@tcp(127.0.0.1:3306)/proxy' \
Admin__ApiToken=$(openssl rand -hex 32) go run ./cmd/github-api-proxy