From 59a7ae5c916dbe9ac5eac391f21158c7bdce944a Mon Sep 17 00:00:00 2001 From: Owen Date: Wed, 23 Sep 2026 16:53:03 -0400 Subject: [PATCH] Support custom bandwidth limits when creating sites --- main.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index a0e980a..2a6ac10 100644 --- a/main.go +++ b/main.go @@ -64,6 +64,9 @@ type WgConfig struct { type Peer struct { PublicKey string `json:"publicKey"` AllowedIPs []string `json:"allowedIps"` + // BandwidthLimit optionally overrides the global --bandwidth-limit for this peer (e.g. "50mbit", + // "1gbit"). Only used when traffic shaping is enabled; empty means use the global default. + BandwidthLimit string `json:"bandwidthLimit,omitempty"` } type PeerBandwidth struct { @@ -1259,6 +1262,12 @@ func handleAddPeer(w http.ResponseWriter, r *http.Request) { return } + if peer.BandwidthLimit != "" && !tcRateRegex.MatchString(peer.BandwidthLimit) { + http.Error(w, "Invalid bandwidthLimit: expected a tc rate such as 50mbit or 1gbit", http.StatusBadRequest) + metrics.RecordPeerOperation("add", "error") + return + } + err := addPeer(peer) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) @@ -1321,11 +1330,16 @@ func addPeerInternal(peer Peer) error { return fmt.Errorf("failed to add peer: %v", err) } - // Setup bandwidth limiting for each peer IP + // Setup bandwidth limiting for each peer IP, using the peer-specific limit if one was + // supplied and falling back to the global default otherwise. if doTrafficShaping { - logger.Debug("doTrafficShaping is true, setting up bandwidth limits for %d IPs", len(wgIPs)) + limit := bandwidthLimit + if peer.BandwidthLimit != "" { + limit = peer.BandwidthLimit + } + logger.Debug("doTrafficShaping is true, setting up bandwidth limits (%s) for %d IPs", limit, len(wgIPs)) for _, wgIP := range wgIPs { - if err := setupPeerBandwidthLimit(wgIP, bandwidthLimit); err != nil { + if err := setupPeerBandwidthLimit(wgIP, limit); err != nil { logger.Warn("Failed to setup bandwidth limit for peer IP %s: %v", wgIP, err) } }