commit df516dc683ab27f1254c46e12e5d373c9a5e7d1b
parent 7d746c875420f30216a29744172230ce4524e11a
Author: Oliver Lowe <o@olowe.co>
Date: Sun, 22 Nov 2026 20:39:33 +1100
import authproxy
Diffstat:
3 files changed, 115 insertions(+), 0 deletions(-)
diff --git a/README b/README
@@ -4,6 +4,7 @@ To install, run install.sh.
The following commands are provided:
+- authproxy - authenticating HTTP reverse proxy
- backlight - set backlight brightness
- bbpr - create a Bitbucket pull request
- bbsnip - create a snippet on BitBucket
diff --git a/man/authproxy.1 b/man/authproxy.1
@@ -0,0 +1,56 @@
+.Dd
+.Dt AUTHPROXY 1
+.Sh NAME
+.Nm authproxy
+.Nd authenticating HTTP reverse proxy
+.Sh SYNOPSIS
+.Nm
+.Op Fl l Ar address
+.Ar file
+.Sh DESCRIPTION
+.Nm
+authenticates requests from trusted clients over plain HTTP
+to a target HTTPS host.
+Responses are returned unmodified from the target host.
+The proxy listens on
+.Ar address ,
+or a random address if not specified,
+then reads the value to be set in the
+Authorization field of client requests from
+.Ar file .
+The base name of
+.Ar file
+becomes the target host of the upstream HTTPS service.
+.Pp
+The intended use case is granting ephemeral clients from a trusted network
+access to some API served over HTTPS mandating authentication
+without needing to configure each client with credentials.
+Clients which would normally make a request directly to some protected resource
+such as https://ai.example.com/v1/openai/completions
+can instead make the same request to the proxy,
+over plain HTTP and a substituted hostname,
+such as http://proxy.example.net/v1/openai/completions.
+
+The following flags are understood:
+.Bl -tag -width Ds
+.It Fl l Ar address
+An address in host:port format to listen for incoming requests.
+The default is
+to listen on all addresses on a random port.
+.El
+
+.Sh EXAMPLES
+Relay requests received on the loopback address port 6969
+to ai.example.net:
+.Pp
+.Dl authproxy -l localhost:6969 $HOME/.config/authproxy/ai.example.net
+.Pp
+In this case the entire contents of the file
+.Pa $HOME/.config/authproxy/ai.example.net
+could contain a Bearer token:
+.Pp
+.Dl Bearer api_key_12345
+.Sh EXIT STATUS
+.Ex
+.Sh SEE ALSO
+.Lk https://blog.exe.dev/http-proxy-secrets Some Secret Management Belongs in Your HTTP Proxy
diff --git a/src/authproxy/authproxy.go b/src/authproxy/authproxy.go
@@ -0,0 +1,58 @@
+package main
+
+import (
+ "bytes"
+ "flag"
+ "fmt"
+ "log"
+ "net"
+ "net/http"
+ "net/http/httputil"
+ "os"
+ "path"
+)
+
+func injectAuth(host, auth string) func(*httputil.ProxyRequest) {
+ return func(preq *httputil.ProxyRequest) {
+ log.Println(preq.In.Method, preq.In.URL)
+ preq.Out.Host = host
+ preq.Out.URL.Scheme = "https"
+ preq.Out.URL.Host = preq.Out.Host
+ preq.Out.Header = preq.In.Header.Clone()
+ preq.Out.Header.Set("Authorization", auth)
+ }
+}
+
+const usage string = "usage: authproxy [-l address] file"
+
+var lflag = flag.String("l", ":0", "listen address")
+
+func main() {
+ flag.Parse()
+
+ if len(flag.Args()) != 1 {
+ fmt.Fprintln(os.Stderr, usage)
+ os.Exit(2)
+ }
+ keyfile := flag.Args()[0]
+
+ key, err := os.ReadFile(keyfile)
+ if err != nil {
+ log.Fatal("load proxy auth:", err)
+ }
+ key = bytes.TrimSpace(key)
+ proxy := &httputil.ReverseProxy{
+ Rewrite: injectAuth(path.Base(keyfile), string(key)),
+ }
+
+ ln, err := net.Listen("tcp", *lflag)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, err)
+ os.Exit(1)
+ }
+ log.Println("listening on", ln.Addr())
+ srv := &http.Server{
+ Handler: proxy,
+ }
+ log.Fatal(srv.Serve(ln))
+}