From 98a4c81701c56c25a3f291f6545082f73282218a Mon Sep 17 00:00:00 2001 From: pablu Date: Fri, 10 Jul 2026 16:04:00 +0200 Subject: [PATCH] fix some shutdown bugs and so on --- .envrc | 1 + .gitignore | 2 +- config.go | 2 +- flake.lock | 61 ++++++++++++++++++++++++++++++++++++++++ flake.nix | 30 ++++++++++++++++++++ middleware/metrics.go | 1 + middleware/rate-limit.go | 1 + router.go | 4 +-- 8 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 .envrc create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore index a1f919d..53f0966 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,4 @@ bin/ data.json *.json config.yaml - +.direnv/ diff --git a/config.go b/config.go index 34b4c3c..385b7ac 100644 --- a/config.go +++ b/config.go @@ -41,7 +41,7 @@ type Config struct { Enabled bool `yaml:"enabled"` Path string `yaml:"path"` MaxAge int `yaml:"maxAge"` - MaxBackups int `yamls:"maxBackups"` + MaxBackups int `yaml:"maxBackups"` } `yaml:"file"` } `yaml:"logging"` Metrics struct { diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..5f60947 --- /dev/null +++ b/flake.lock @@ -0,0 +1,61 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1783522502, + "narHash": "sha256-iffAls3iaNTyJC2faYcUXSI+Gp02cDjYl+MygxKl2GI=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "0bb7ec54c8483066ec9d7720e780a5caa71f8612", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..86c7c01 --- /dev/null +++ b/flake.nix @@ -0,0 +1,30 @@ +{ + description = "Reverse proxy"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = + { + self, + nixpkgs, + flake-utils, + }: + flake-utils.lib.eachDefaultSystem ( + system: + let + pkgs = nixpkgs.legacyPackages.${system}; + in + { + devShells.default = pkgs.mkShell { + buildInputs = with pkgs; [ + go + gopls + go-tools + ]; + }; + } + ); +} diff --git a/middleware/metrics.go b/middleware/metrics.go index b53148e..dbc674d 100644 --- a/middleware/metrics.go +++ b/middleware/metrics.go @@ -43,6 +43,7 @@ func NewMetrics(bufferSize int, flushTimeout time.Duration, file string) *Metric c: make(chan RequestMetric, bufferSize), ticker: time.NewTicker(flushTimeout), file: file, + stop: make(chan struct{}), } } diff --git a/middleware/rate-limit.go b/middleware/rate-limit.go index 3ccf778..fa1de20 100644 --- a/middleware/rate-limit.go +++ b/middleware/rate-limit.go @@ -31,6 +31,7 @@ func NewLimiter(maxRequests int, refills int, refillInterval time.Duration, clea bucketRefill: refills, rwLock: &sync.RWMutex{}, rateChannel: make(chan string), + stop: make(chan struct{}), } } diff --git a/router.go b/router.go index 6add31c..2fbc200 100644 --- a/router.go +++ b/router.go @@ -37,7 +37,6 @@ func New(config *Config, client *http.Client) Router { for _, host := range config.Hosts { for _, domain := range host.Domains { curr := Host{host.Port, host.Remotes, host.Secure, &atomic.Uint32{}, make(map[string]*Host), host.AdditionalHeaders} - m[domain] = curr for subUrl, rewriteHost := range host.Rewrite { rewrite, ok := m[rewriteHost] @@ -46,6 +45,7 @@ func New(config *Config, client *http.Client) Router { } curr.Rewrites[subUrl] = &rewrite } + m[domain] = curr } } @@ -82,7 +82,7 @@ func rewriteRequestURL(r *http.Request, host *Host, remote string) error { r.RequestURI = "" r.URL.Scheme = remoteUrl.Scheme r.URL.Host = remoteUrl.Host - r.Header.Set("X-Forwarded-For", r.RemoteAddr) + r.Header.Set("X-Forwarded-For", strings.Split(r.RemoteAddr, ":")[0]) r.Header.Set("Cache-Control", "no-store, no-cache, max-age=0, must-revalidate, proxy-revalidate") return nil