16 Commits

Author SHA1 Message Date
Owen Schwartz
939fbd5d2c Merge pull request #109 from fosrl/dev
1.5.0
2026-08-18 17:22:55 -04:00
Owen
e1b47b69aa expose unwrap to allow for streaming 2026-08-06 17:14:35 -04:00
Owen
654a3c777a Basic routing working to targets 2026-08-06 15:48:40 -04:00
Owen
6f1851639a Process the scheme 2026-08-06 15:17:30 -04:00
Owen
74629a97f7 Add gp router endpoint for dowsteam http 2026-08-05 17:28:13 -04:00
Owen
5c697afcac Add exception to firewall to talk to traefik 2026-08-05 16:28:41 -04:00
Owen Schwartz
e25e5766df Merge pull request #102 from fosrl/dev
Change where heap is written to
2026-07-19 10:57:48 -04:00
Owen Schwartz
2264c1c7b4 Merge pull request #99 from fosrl/dependabot/docker/docker-dependencies-2adcbce36e
build(deps): bump alpine from 3.23 to 3.24 in the docker-dependencies group
2026-07-18 16:03:39 -04:00
Owen Schwartz
904199121d Merge pull request #100 from fosrl/dependabot/go_modules/go-dependencies-ae6daafbbe
build(deps): bump the go-dependencies group with 2 updates
2026-07-18 16:03:15 -04:00
Owen Schwartz
5b92bdeb1c Merge pull request #101 from fosrl/dependabot/github_actions/github-actions-dependencies-ad4479fa06
build(deps): bump the github-actions-dependencies group with 6 updates
2026-07-18 16:01:56 -04:00
Owen
daecacbd8e Change where heap is written to 2026-07-16 16:16:54 -04:00
dependabot[bot]
8efce78d94 build(deps): bump the github-actions-dependencies group with 6 updates
Bumps the github-actions-dependencies group with 6 updates:

| Package | From | To |
| --- | --- | --- |
| [actions/checkout](https://github.com/actions/checkout) | `6.0.2` | `7.0.0` |
| [docker/setup-qemu-action](https://github.com/docker/setup-qemu-action) | `4.0.0` | `4.2.0` |
| [docker/setup-buildx-action](https://github.com/docker/setup-buildx-action) | `4.0.0` | `4.2.0` |
| [docker/login-action](https://github.com/docker/login-action) | `4.1.0` | `4.4.0` |
| [actions/setup-go](https://github.com/actions/setup-go) | `6.4.0` | `6.5.0` |
| [actions/upload-artifact](https://github.com/actions/upload-artifact) | `7.0.0` | `7.0.1` |


Updates `actions/checkout` from 6.0.2 to 7.0.0
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](de0fac2e45...9c091bb21b)

Updates `docker/setup-qemu-action` from 4.0.0 to 4.2.0
- [Release notes](https://github.com/docker/setup-qemu-action/releases)
- [Commits](ce360397dd...96fe6ef7f3)

Updates `docker/setup-buildx-action` from 4.0.0 to 4.2.0
- [Release notes](https://github.com/docker/setup-buildx-action/releases)
- [Commits](4d04d5d948...bb05f3f551)

Updates `docker/login-action` from 4.1.0 to 4.4.0
- [Release notes](https://github.com/docker/login-action/releases)
- [Commits](4907a6ddec...af1e73f918)

Updates `actions/setup-go` from 6.4.0 to 6.5.0
- [Release notes](https://github.com/actions/setup-go/releases)
- [Commits](4a3601121d...924ae3a1cd)

Updates `actions/upload-artifact` from 7.0.0 to 7.0.1
- [Release notes](https://github.com/actions/upload-artifact/releases)
- [Commits](bbbca2ddaa...043fb46d1a)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-version: 7.0.0
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: github-actions-dependencies
- dependency-name: docker/setup-qemu-action
  dependency-version: 4.2.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: github-actions-dependencies
- dependency-name: docker/setup-buildx-action
  dependency-version: 4.2.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: github-actions-dependencies
- dependency-name: docker/login-action
  dependency-version: 4.4.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: github-actions-dependencies
- dependency-name: actions/setup-go
  dependency-version: 6.5.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: github-actions-dependencies
- dependency-name: actions/upload-artifact
  dependency-version: 7.0.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: github-actions-dependencies
...

Signed-off-by: dependabot[bot] <support@github.com>
2026-07-07 19:01:31 +00:00
dependabot[bot]
c29e6ec535 build(deps): bump the go-dependencies group with 2 updates
Bumps the go-dependencies group with 2 updates: [golang.org/x/crypto](https://github.com/golang/crypto) and [golang.org/x/sync](https://github.com/golang/sync).


Updates `golang.org/x/crypto` from 0.52.0 to 0.53.0
- [Commits](https://github.com/golang/crypto/compare/v0.52.0...v0.53.0)

Updates `golang.org/x/sync` from 0.20.0 to 0.21.0
- [Commits](https://github.com/golang/sync/compare/v0.20.0...v0.21.0)

---
updated-dependencies:
- dependency-name: golang.org/x/crypto
  dependency-version: 0.53.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: go-dependencies
- dependency-name: golang.org/x/sync
  dependency-version: 0.21.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: go-dependencies
...

Signed-off-by: dependabot[bot] <support@github.com>
2026-07-07 19:01:29 +00:00
dependabot[bot]
0a1496e8de build(deps): bump alpine in the docker-dependencies group
Bumps the docker-dependencies group with 1 update: alpine.


Updates `alpine` from 3.23 to 3.24

---
updated-dependencies:
- dependency-name: alpine
  dependency-version: '3.24'
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: docker-dependencies
...

Signed-off-by: dependabot[bot] <support@github.com>
2026-07-07 19:01:17 +00:00
Owen Schwartz
4f325dd92f Merge pull request #92 from fosrl/chore/dependabot-single-pr-groups
chore(dependabot): limit grouped dependency updates to one PR per ecosystem
2026-07-07 15:00:19 -04:00
Marc Schäfer
d611eb2022 chore(dependabot): limit grouped dependency updates to one PR per ecosystem 2026-06-07 11:02:22 +02:00
7 changed files with 229 additions and 52 deletions

View File

@@ -1,40 +1,32 @@
version: 2
updates:
- package-ecosystem: "gomod"
directory: "/"
schedule:
interval: "daily"
open-pull-requests-limit: 1
groups:
dev-patch-updates:
dependency-type: "development"
update-types:
- "patch"
dev-minor-updates:
dependency-type: "development"
update-types:
- "minor"
prod-patch-updates:
dependency-type: "production"
update-types:
- "patch"
prod-minor-updates:
dependency-type: "production"
update-types:
- "minor"
go-dependencies:
patterns:
- "*"
- package-ecosystem: "docker"
directory: "/"
schedule:
interval: "daily"
open-pull-requests-limit: 1
groups:
patch-updates:
update-types:
- "patch"
minor-updates:
update-types:
- "minor"
docker-dependencies:
patterns:
- "*"
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
open-pull-requests-limit: 1
groups:
github-actions-dependencies:
patterns:
- "*"

View File

@@ -36,16 +36,16 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
- name: Set up QEMU
uses: docker/setup-qemu-action@ce360397dd3f832beb865e1373c09c0e9f86d70a # v4.0.0
uses: docker/setup-qemu-action@96fe6ef7f33517b61c61be40b68a1882f3264fb8 # v4.2.0
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v4.0.0
uses: docker/setup-buildx-action@bb05f3f5519dd87d3ba754cc423b652a5edd6d2c # v4.2.0
- name: Log in to Docker Hub
uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4.1.0
uses: docker/login-action@af1e73f918a031802d376d3c8bbc3fe56130a9b0 # v4.4.0
with:
registry: docker.io
username: ${{ secrets.DOCKER_HUB_USERNAME }}
@@ -57,7 +57,7 @@ jobs:
shell: bash
- name: Install Go
uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
uses: actions/setup-go@924ae3a1cded613372ab5595356fb5720e22ba16 # v6.5.0
with:
go-version: 1.26
@@ -80,7 +80,7 @@ jobs:
shell: bash
- name: Login in to GHCR
uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4.1.0
uses: docker/login-action@af1e73f918a031802d376d3c8bbc3fe56130a9b0 # v4.4.0
with:
registry: ghcr.io
username: ${{ github.actor }}
@@ -156,7 +156,7 @@ jobs:
shell: bash
- name: Upload artifacts from /bin
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: binaries
path: bin/

View File

@@ -14,14 +14,14 @@ jobs:
runs-on: amd64-runner
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
- name: Read go version
id: goversion
run: echo "version=$(cat .go-version)" >> $GITHUB_OUTPUT
- name: Set up Go
uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
uses: actions/setup-go@924ae3a1cded613372ab5595356fb5720e22ba16 # v6.5.0
with:
go-version: ${{ steps.goversion.outputs.version }}

View File

@@ -17,7 +17,7 @@ COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o /gerbil
# Start a new stage from scratch
FROM alpine:3.23 AS runner
FROM alpine:3.24 AS runner
RUN apk add --no-cache iptables iproute2

8
go.mod
View File

@@ -12,8 +12,8 @@ require (
go.opentelemetry.io/otel/metric v1.44.0
go.opentelemetry.io/otel/sdk v1.44.0
go.opentelemetry.io/otel/sdk/metric v1.44.0
golang.org/x/crypto v0.52.0
golang.org/x/sync v0.20.0
golang.org/x/crypto v0.53.0
golang.org/x/sync v0.21.0
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20230429144221-925a1e7659e6
)
@@ -40,8 +40,8 @@ require (
go.opentelemetry.io/proto/otlp v1.10.0 // indirect
go.yaml.in/yaml/v2 v2.4.2 // indirect
golang.org/x/net v0.55.0 // indirect
golang.org/x/sys v0.45.0 // indirect
golang.org/x/text v0.37.0 // indirect
golang.org/x/sys v0.46.0 // indirect
golang.org/x/text v0.38.0 // indirect
golang.zx2c4.com/wireguard v0.0.0-20230325221338-052af4a8072b // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20260526163538-3dc84a4a5aaa // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20260526163538-3dc84a4a5aaa // indirect

16
go.sum
View File

@@ -83,18 +83,18 @@ go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI=
go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU=
golang.org/x/crypto v0.52.0 h1:RMs7fP2rXdep0CftQlK8Uf+kibLm7qkCcradZWYz988=
golang.org/x/crypto v0.52.0/go.mod h1:1QgfPxDqh0T2M/elOJtp9RvuR95kVjir0e6/BvEmGbc=
golang.org/x/crypto v0.53.0 h1:QZ4Muo8THX6CizN2vPPd5fBGHyogrdK9fG4wLPFUsto=
golang.org/x/crypto v0.53.0/go.mod h1:DNLU434OwVakk9PzuwV8w62mAJpRJL3vsgcfp4Qnsio=
golang.org/x/net v0.55.0 h1:bcvxaJn3e1U6InsFWt1JUq1aSjnRxLzT2rtD2KfkDF8=
golang.org/x/net v0.55.0/go.mod h1:L5U2KuzuOe1lY7Z+aWVIKK6qEeJXnXV9yzGA+WCHJww=
golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4=
golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
golang.org/x/sync v0.21.0 h1:HLII4xRRTtCRkxYp4HNFF0Js/Og6q2i++KXbg0gHCwM=
golang.org/x/sync v0.21.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.45.0 h1:dO4czNzziLiiXplLQgBCEpCvXQ3dnkn0SdaZSYdQ+FY=
golang.org/x/sys v0.45.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/text v0.37.0 h1:Cqjiwd9eSg8e0QAkyCaQTNHFIIzWtidPahFWR83rTrc=
golang.org/x/text v0.37.0/go.mod h1:a5sjxXGs9hsn/AJVwuElvCAo9v8QYLzvavO5z2PiM38=
golang.org/x/sys v0.46.0 h1:noSf2Fq6F8DBgS+LysIkx7rIExoNHJsxOAtPp4rthXw=
golang.org/x/sys v0.46.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/text v0.38.0 h1:sXmwo9DwP3OK9EZ7PqAdaooSGozfl/3a6/xJcbzPRhE=
golang.org/x/text v0.38.0/go.mod h1:YXZt3QhHUKYT53r2lLKFIVi6Ao1jdzrTR/KQ09qyxF4=
golang.zx2c4.com/wireguard v0.0.0-20230325221338-052af4a8072b h1:J1CaxgLerRR5lgx3wnr6L04cJFbWoceSK9JWBdglINo=
golang.zx2c4.com/wireguard v0.0.0-20230325221338-052af4a8072b/go.mod h1:tqur9LnfstdR9ep2LaJT4lFUl0EjlHtge+gAjmsHUG4=
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20230429144221-925a1e7659e6 h1:CawjfCvYQH2OU3/TnxLx97WDSUDRABfT18pCOYwc2GE=

201
main.go
View File

@@ -3,6 +3,7 @@ package main
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"errors"
"flag"
@@ -11,10 +12,12 @@ import (
"log"
"net"
"net/http"
"net/http/httputil"
_ "net/http/pprof"
"os"
"os/exec"
"os/signal"
"path/filepath"
"runtime"
"runtime/pprof"
"strconv"
@@ -103,6 +106,144 @@ type UpdateDestinationsRequest struct {
Destinations []relay.PeerDestination `json:"destinations"`
}
// pangolinDestHeader carries the downstream host:port (reachable over the
// WireGuard interface) that a /router request should be rewritten to,
// optionally prefixed with a scheme (e.g. "https://100.96.128.1:443"). It is
// stripped before the request is forwarded.
const pangolinDestHeader = "p-dest-header"
// pangolinHostHeader optionally carries the Host header value that should be
// sent to the destination, when it differs from pangolinDestHeader (e.g. the
// target's configured IP/hostname rather than the WireGuard routing
// address). It is stripped before the request is forwarded. When absent,
// the destination from pangolinDestHeader is used as the Host header, same
// as before.
const pangolinHostHeader = "p-dest-host-header"
// splitDestHeader separates an optional "scheme://" prefix from a
// pangolinDestHeader value, defaulting to "http" when none is present.
func splitDestHeader(dest string) (scheme, host string) {
if s, rest, ok := strings.Cut(dest, "://"); ok {
return s, rest
}
return "http", dest
}
// hostname strips an optional ":port" suffix from a host header value.
func hostname(hostport string) string {
if h, _, err := net.SplitHostPort(hostport); err == nil {
return h
}
return hostport
}
// routerSNIContextKey carries the TLS ServerName (SNI) that
// routerTransport's DialTLSContext should present, since we dial the
// WireGuard destination IP but the remote end typically terminates TLS
// based on the original hostname (pangolinHostHeader), not that IP.
type routerSNIContextKey struct{}
// routerTransport is routerProxy's RoundTripper. It mirrors
// http.DefaultTransport except for TLS dials, where it sets the SNI from
// routerSNIContextKey instead of letting it default to the dial address
// (the WireGuard IP), which the destination's TLS termination won't have a
// matching certificate/route for.
var routerTransport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialTLSContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
serverName := hostname(addr)
if sni, ok := ctx.Value(routerSNIContextKey{}).(string); ok && sni != "" {
serverName = sni
}
dialer := &tls.Dialer{Config: &tls.Config{ServerName: serverName}}
return dialer.DialContext(ctx, network, addr)
},
}
// logDebugRequest dumps a request's destination, headers, and body at debug
// level for troubleshooting (e.g. verifying an auth header made it through
// the proxy chain intact). It reads and restores req.Body so the request can
// still be sent afterward. Header values (including secrets like API keys)
// are logged as-is - only intended to be enabled for local troubleshooting.
func logDebugRequest(label string, req *http.Request) {
var headerLines strings.Builder
for name, values := range req.Header {
for _, v := range values {
fmt.Fprintf(&headerLines, "\n %s: %s", name, v)
}
}
body := []byte("<empty>")
if req.Body != nil {
data, err := io.ReadAll(req.Body)
req.Body.Close()
if err != nil {
logger.Error("%s: failed to read body for logging: %v", label, err)
} else {
body = data
}
req.Body = io.NopCloser(bytes.NewReader(data))
}
logger.Debug("%s: %s %s://%s%s headers:%s\nbody: %s", label, req.Method, req.URL.Scheme, req.Host, req.URL.RequestURI(), headerLines.String(), body)
}
// routerProxy forwards /router/* requests from the Pangolin AI gateway to a
// destination on the WireGuard network, as named by pangolinDestHeader.
// Used for proxying AI chat completion requests (incl. streaming) to
// providers reachable only from a site.
var routerProxy = &httputil.ReverseProxy{
Rewrite: func(pr *httputil.ProxyRequest) {
scheme, dest := splitDestHeader(pr.In.Header.Get(pangolinDestHeader))
pr.Out.URL.Scheme = scheme
pr.Out.URL.Host = dest
pr.Out.URL.Path = strings.TrimPrefix(pr.In.URL.Path, "/router")
if !strings.HasPrefix(pr.Out.URL.Path, "/") {
pr.Out.URL.Path = "/" + pr.Out.URL.Path
}
pr.Out.URL.RawPath = ""
pr.Out.Host = dest
if hostOverride := pr.In.Header.Get(pangolinHostHeader); hostOverride != "" {
pr.Out.Host = hostOverride
ctx := context.WithValue(pr.Out.Context(), routerSNIContextKey{}, hostname(hostOverride))
pr.Out = pr.Out.WithContext(ctx)
}
pr.Out.Header.Del(pangolinDestHeader)
pr.Out.Header.Del(pangolinHostHeader)
logger.Debug("Router proxy: %s %s -> %s (Host: %s)", pr.In.Method, pr.In.URL.Path, pr.Out.URL.String(), pr.Out.Host)
logDebugRequest("Router outbound request", pr.Out)
},
Transport: routerTransport,
// Flush written bytes to the client immediately rather than buffering,
// which is required for SSE-based streaming chat completions.
FlushInterval: -1,
ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
logger.Error("Router proxy error for %s: %v", r.URL.Path, err)
http.Error(w, "Bad gateway", http.StatusBadGateway)
},
}
func handleRouter(w http.ResponseWriter, r *http.Request) {
dest := r.Header.Get(pangolinDestHeader)
hostOverride := r.Header.Get(pangolinHostHeader)
logger.Debug("Router request received: %s %s dest=%s host=%s remote=%s", r.Method, r.URL.Path, dest, hostOverride, r.RemoteAddr)
if dest == "" {
http.Error(w, fmt.Sprintf("Missing %s header", pangolinDestHeader), http.StatusBadRequest)
return
}
_, host := splitDestHeader(dest)
if _, _, err := net.SplitHostPort(host); err != nil {
http.Error(w, "Invalid destination", http.StatusBadRequest)
return
}
routerProxy.ServeHTTP(w, r)
}
// httpMetricsMiddleware wraps HTTP handlers with metrics tracking
func httpMetricsMiddleware(endpoint string, handler http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
@@ -132,6 +273,16 @@ func (w *responseWriterWrapper) WriteHeader(statusCode int) {
w.ResponseWriter.WriteHeader(statusCode)
}
// Unwrap exposes the underlying ResponseWriter so http.ResponseController
// (used by httputil.ReverseProxy's streaming Flush, and by Hijack/Push
// callers) can see through this wrapper to the real http.Flusher etc.
// Without this, ReverseProxy's flushes on /router/* silently no-op and
// streamed responses (e.g. SSE) get buffered until the response completes
// instead of being forwarded incrementally.
func (w *responseWriterWrapper) Unwrap() http.ResponseWriter {
return w.ResponseWriter
}
func parseLogLevel(level string) logger.LogLevel {
switch strings.ToUpper(level) {
case "DEBUG":
@@ -150,8 +301,6 @@ func parseLogLevel(level string) logger.LogLevel {
}
func main() {
go monitorMemory(1024 * 1024 * 512) // trigger if memory usage exceeds 512MB
var (
err error
wgconfig WgConfig
@@ -344,6 +493,15 @@ func main() {
flag.Parse()
// Heap profiles are dumped into the configured config directory (the same
// directory as the config file, or /var/config as a fallback for remote-config
// setups) so they land on the volume the operator actually has mounted.
heapDir := "/var/config/heap"
if configFile != "" {
heapDir = filepath.Join(filepath.Dir(configFile), "heap")
}
go monitorMemory(1024*1024*512, heapDir) // trigger if memory usage exceeds 512MB
// Derive IFB device name from the WireGuard interface name (Linux limit: 15 chars)
ifbName = "ifb_" + interfaceName
if len(ifbName) > 15 {
@@ -560,6 +718,7 @@ func main() {
http.HandleFunc("/update-destinations", httpMetricsMiddleware("update_destinations", handleUpdateDestinations))
http.HandleFunc("/update-local-snis", httpMetricsMiddleware("update_local_snis", handleUpdateLocalSNIs))
http.HandleFunc("/healthz", httpMetricsMiddleware("healthz", handleHealthz))
http.HandleFunc("/router/", httpMetricsMiddleware("router", handleRouter))
// Register metrics endpoint only for Prometheus backend.
// OTel backend pushes to a collector; no /metrics endpoint needed.
@@ -743,7 +902,7 @@ func ensureWireguardInterface(wgconfig WgConfig) error {
if disableFirewall {
logger.Warn("Firewall disabled: all inbound traffic on %s will be allowed", interfaceName)
} else if err := ensureWireguardFirewall(); err != nil {
} else if err := ensureWireguardFirewall(wgconfig.IpAddress); err != nil {
logger.Warn("Failed to ensure WireGuard firewall rules: %v", err)
}
@@ -919,11 +1078,18 @@ func ensureMSSClamping() error {
return nil
}
func ensureWireguardFirewall() error {
func ensureWireguardFirewall(localIpAddress string) error {
// Rules to enforce:
// 1. Allow established/related connections (responses to our outbound traffic)
// 2. Allow ICMP ping packets
// 3. Drop all other inbound traffic from peers
// 3. Allow inbound traffic to ports 80/443 on the local IP only (for Traefik)
// 4. Drop all other inbound traffic from peers
// Strip any CIDR suffix so we're left with just the host IP
localIp := localIpAddress
if ip, _, err := net.ParseCIDR(localIpAddress); err == nil {
localIp = ip.String()
}
// Define the rules we want to ensure exist
rules := [][]string{
@@ -943,6 +1109,24 @@ func ensureWireguardFirewall() error {
"--icmp-type", "8",
"-j", "ACCEPT",
},
// Allow inbound HTTP to the local IP only (for Traefik)
{
"-A", "INPUT",
"-i", interfaceName,
"-p", "tcp",
"--dport", "80",
"-d", localIp,
"-j", "ACCEPT",
},
// Allow inbound HTTPS to the local IP only (for Traefik)
{
"-A", "INPUT",
"-i", interfaceName,
"-p", "tcp",
"--dport", "443",
"-d", localIp,
"-j", "ACCEPT",
},
// Drop all other inbound traffic from WireGuard interface
{
"-A", "INPUT",
@@ -1599,7 +1783,7 @@ func notifyPeerChange(action, publicKey string) {
}
}
func monitorMemory(limit uint64) {
func monitorMemory(limit uint64, heapDir string) {
var m runtime.MemStats
for {
runtime.ReadMemStats(&m)
@@ -1615,8 +1799,9 @@ func monitorMemory(limit uint64) {
// Record memory spike metric
metrics.RecordMemorySpike(severity)
f, err := os.Create(fmt.Sprintf("/var/config/heap/heap-spike-%d.pprof", time.Now().Unix()))
if err != nil {
if err := os.MkdirAll(heapDir, 0o755); err != nil {
log.Println("could not create heap profile directory:", err)
} else if f, err := os.Create(filepath.Join(heapDir, fmt.Sprintf("heap-spike-%d.pprof", time.Now().Unix()))); err != nil {
log.Println("could not create profile:", err)
} else {
pprof.WriteHeapProfile(f)