From 03cad9f3150bf13af034f190e0d631db888c442e Mon Sep 17 00:00:00 2001 From: yusing Date: Sun, 6 Oct 2024 09:23:41 +0800 Subject: [PATCH] added package version api, dependencies upgrade --- .github/workflows/docker-image.yml | 2 + .gitlab-ci.yml | 4 +- Dockerfile | 15 +++- Makefile | 20 ++--- cmd/main.go | 2 + go.mod | 16 ++-- go.sum | 38 ++++---- internal/api/handler.go | 1 + internal/api/v1/version.go | 11 +++ internal/net/http/header_utils.go | 29 ++---- internal/net/http/middleware/forward_auth.go | 17 ++-- internal/net/http/middleware/trace.go | 38 +++----- mtrace.json | 95 -------------------- pkg/version.go | 7 ++ scripts/build.sh | 5 ++ 15 files changed, 107 insertions(+), 193 deletions(-) create mode 100644 internal/api/v1/version.go delete mode 100644 mtrace.json create mode 100644 pkg/version.go create mode 100755 scripts/build.sh diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index 69437f4..aeb395f 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -61,6 +61,8 @@ jobs: outputs: type=image,name=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true cache-from: type=gha cache-to: type=gha,mode=max + build-args: | + VERSION=${{ github.ref_name }} - name: Generate artifact attestation uses: actions/attest-build-provenance@v1 diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index cf34ab6..fd49577 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -11,5 +11,5 @@ build-image: - echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER $CI_REGISTRY --password-stdin script: - echo building $CI_REGISTRY_IMAGE - - docker build --pull -t $CI_REGISTRY_IMAGE . - - docker push $CI_REGISTRY_IMAGE \ No newline at end of file + - docker build --no-cache --build-arg VERSION=$CI_COMMIT_REF_NAME -t $CI_REGISTRY_IMAGE . + - docker push $CI_REGISTRY_IMAGE diff --git a/Dockerfile b/Dockerfile index 02e6eb6..c74aa6c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ # Stage 1: Builder FROM golang:1.23.1-alpine AS builder -RUN apk add --no-cache tzdata +RUN apk add --no-cache tzdata make WORKDIR /src @@ -13,13 +13,20 @@ RUN --mount=type=cache,target="/go/pkg/mod" \ ENV GOCACHE=/root/.cache/go-build -# Build the application with better caching +ARG VERSION +ENV VERSION=${VERSION} + +COPY scripts /src/scripts +COPY Makefile /src/ + RUN --mount=type=cache,target="/go/pkg/mod" \ --mount=type=cache,target="/root/.cache/go-build" \ --mount=type=bind,src=cmd,dst=/src/cmd \ --mount=type=bind,src=internal,dst=/src/internal \ - CGO_ENABLED=0 GOOS=linux go build -ldflags '-w -s' -pgo=auto -o /app/go-proxy ./cmd && \ - mkdir /app/error_pages /app/certs + --mount=type=bind,src=pkg,dst=/src/pkg \ + make build && \ + mkdir -p /app/error_pages /app/certs && \ + cp bin/go-proxy /app/go-proxy # Stage 2: Final image FROM scratch diff --git a/Makefile b/Makefile index 2b33b8f..0581220 100755 --- a/Makefile +++ b/Makefile @@ -1,18 +1,16 @@ -BUILD_FLAG ?= -s -w +VERSION ?= $(shell git describe --tags --abbrev=0) +BUILD_FLAGS ?= -s -w -X github.com/yusing/go-proxy/pkg.version=${VERSION} +export VERSION +export BUILD_FLAGS +export CGO_ENABLED = 0 +export GOOS = linux .PHONY: all setup build test up restart logs get debug run archive repush rapid-crash debug-list-containers all: debug -setup: - mkdir -p config certs - [ -f config/config.yml ] || cp config.example.yml config/config.yml - [ -f config/providers.yml ] || touch config/providers.yml - build: - mkdir -p bin - CGO_ENABLED=0 GOOS=linux \ - go build -ldflags '${BUILD_FLAG}' -pgo=auto -o bin/go-proxy ./cmd + scripts/build.sh test: GOPROXY_TEST=1 go test ./internal/... @@ -30,10 +28,10 @@ get: go get -u ./cmd && go mod tidy debug: - make BUILD_FLAG="" build && sudo GOPROXY_DEBUG=1 bin/go-proxy + make build && sudo GOPROXY_DEBUG=1 bin/go-proxy run-test: - make BUILD_FLAG="" build && sudo GOPROXY_TEST=1 bin/go-proxy + make build && sudo GOPROXY_TEST=1 bin/go-proxy run: make build && sudo bin/go-proxy diff --git a/cmd/main.go b/cmd/main.go index 4be929f..66927ec 100755 --- a/cmd/main.go +++ b/cmd/main.go @@ -28,6 +28,7 @@ import ( R "github.com/yusing/go-proxy/internal/route" "github.com/yusing/go-proxy/internal/server" F "github.com/yusing/go-proxy/internal/utils/functional" + "github.com/yusing/go-proxy/pkg" ) func main() { @@ -54,6 +55,7 @@ func main() { ForceColors: true, TimestampFormat: "01-02 15:04:05", }) + logrus.Infof("go-proxy version %s", pkg.GetVersion()) } if args.Command == common.CommandReload { diff --git a/go.mod b/go.mod index 5174ba7..1d9b072 100644 --- a/go.mod +++ b/go.mod @@ -3,15 +3,16 @@ module github.com/yusing/go-proxy go 1.23.1 require ( + github.com/coder/websocket v1.8.12 github.com/docker/cli v27.3.1+incompatible github.com/docker/docker v27.3.1+incompatible github.com/fsnotify/fsnotify v1.7.0 - github.com/go-acme/lego/v4 v4.18.0 + github.com/go-acme/lego/v4 v4.19.0 github.com/puzpuzpuz/xsync/v3 v3.4.0 github.com/santhosh-tekuri/jsonschema v1.2.4 github.com/sirupsen/logrus v1.9.3 - golang.org/x/net v0.29.0 - golang.org/x/text v0.18.0 + golang.org/x/net v0.30.0 + golang.org/x/text v0.19.0 gopkg.in/yaml.v3 v3.0.1 ) @@ -19,7 +20,6 @@ require ( github.com/Microsoft/go-winio v0.6.2 // indirect github.com/cenkalti/backoff/v4 v4.3.0 // indirect github.com/cloudflare/cloudflare-go v0.106.0 // indirect - github.com/coder/websocket v1.8.12 // indirect github.com/containerd/log v0.1.0 // indirect github.com/distribution/reference v0.6.0 // indirect github.com/docker/go-connections v0.5.0 // indirect @@ -47,13 +47,13 @@ require ( go.opentelemetry.io/otel/metric v1.30.0 // indirect go.opentelemetry.io/otel/sdk v1.30.0 // indirect go.opentelemetry.io/otel/trace v1.30.0 // indirect - golang.org/x/crypto v0.27.0 // indirect + golang.org/x/crypto v0.28.0 // indirect golang.org/x/mod v0.21.0 // indirect golang.org/x/oauth2 v0.23.0 // indirect golang.org/x/sync v0.8.0 // indirect - golang.org/x/sys v0.25.0 // indirect - golang.org/x/time v0.6.0 // indirect - golang.org/x/tools v0.25.0 // indirect + golang.org/x/sys v0.26.0 // indirect + golang.org/x/time v0.7.0 // indirect + golang.org/x/tools v0.26.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gotest.tools/v3 v3.5.1 // indirect ) diff --git a/go.sum b/go.sum index 37022c9..2044e34 100644 --- a/go.sum +++ b/go.sum @@ -12,8 +12,9 @@ github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I= github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk= github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E= github.com/docker/cli v27.3.1+incompatible h1:qEGdFBF3Xu6SCvCYhc7CzaQTlBmqDuzxPDpigSyeKQQ= @@ -28,8 +29,8 @@ github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2 github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= -github.com/go-acme/lego/v4 v4.18.0 h1:2hH8KcdRBSb+p5o9VZIm61GAOXYALgILUCSs1Q+OYsk= -github.com/go-acme/lego/v4 v4.18.0/go.mod h1:Blkg3izvXpl3zxk7WKngIuwR2I/hvYVP3vRnvgBp7m8= +github.com/go-acme/lego/v4 v4.19.0 h1:c7YabBOwoa2URsPiCNGQsdzQnbd8Y23B4/66gxh4H7c= +github.com/go-acme/lego/v4 v4.19.0/go.mod h1:wtDe3dDkmV4/oI2nydpNXSJpvV10J9RCyZ6MbYxNtlQ= github.com/go-jose/go-jose/v4 v4.0.4 h1:VsjPI33J0SB9vQM6PLmNjoHqMQNGPiZ0rHL7Ni7Q6/E= github.com/go-jose/go-jose/v4 v4.0.4/go.mod h1:NKb5HO1EZccyMpiZNbdUw/14tiXNyUJh188dfnMCAfc= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= @@ -48,8 +49,6 @@ github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= -github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 h1:asbCHRVmodnJTuQ3qamDwqVOIjwqUPTYmYuemVOx+Ys= github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0/go.mod h1:ggCgvZ2r7uOoQjOyu2Y1NhHmEPPzzuhWgcza5M1Ji1I= github.com/jarcoal/httpmock v1.3.0 h1:2RJ8GP0IIaWwcC9Fp2BmVi8Kog3v2Hn7VXM3fTd+nuc= @@ -79,8 +78,9 @@ github.com/ovh/go-ovh v1.6.0/go.mod h1:cTVDnl94z4tl8pP1uZ/8jlVxntjSIf09bNcQ5TJSC github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/puzpuzpuz/xsync/v3 v3.4.0 h1:DuVBAdXuGFHv8adVXjWWZ63pJq+NRXOWVXlKDBZ+mJ4= github.com/puzpuzpuz/xsync/v3 v3.4.0/go.mod h1:VjzYrABPabuM4KyBh1Ftq6u8nhwY5tBPKP9jpmh0nnA= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= @@ -115,8 +115,8 @@ go.opentelemetry.io/proto/otlp v1.3.1/go.mod h1:0X1WI4de4ZsLrrJNLAQbFeLCm3T7yBkR golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A= -golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70= +golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw= +golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0= @@ -125,8 +125,8 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo= -golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0= +golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4= +golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU= golang.org/x/oauth2 v0.23.0 h1:PbgcYx2W7i4LvjJWEbf0ngHV6qJYr86PkAV3bXdLEbs= golang.org/x/oauth2 v0.23.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -138,25 +138,25 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= -golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= +golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224= -golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= -golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U= -golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= +golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM= +golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= +golang.org/x/time v0.7.0 h1:ntUhktv3OPE6TgYxXWv9vKvUSJyIFJlyohwbkEwPrKQ= +golang.org/x/time v0.7.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= -golang.org/x/tools v0.25.0 h1:oFU9pkj/iJgs+0DT+VMHrx+oBKs/LJMV+Uvg78sl+fE= -golang.org/x/tools v0.25.0/go.mod h1:/vtpO8WL1N9cQC3FN5zPqb//fRXskFHbLKk4OW1Q7rg= +golang.org/x/tools v0.26.0 h1:v/60pFQmzmT9ExmjDv2gGIfi3OqfKoEP6I5+umXlbnQ= +golang.org/x/tools v0.26.0/go.mod h1:TPVVj70c7JJ3WCazhD8OdXcZg/og+b9+tH/KxylGwH0= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/genproto v0.0.0-20240227224415-6ceb2ff114de h1:F6qOa9AZTYJXOUEr4jDysRDLrm4PHePlge4v4TGAlxY= +google.golang.org/genproto v0.0.0-20240903143218-8af14fe29dc1 h1:BulPr26Jqjnd4eYDVe+YvyR7Yc2vJGkO5/0UxD0/jZU= google.golang.org/genproto/googleapis/api v0.0.0-20240903143218-8af14fe29dc1 h1:hjSy6tcFQZ171igDaN5QHOw2n6vx40juYbC/x67CEhc= google.golang.org/genproto/googleapis/api v0.0.0-20240903143218-8af14fe29dc1/go.mod h1:qpvKtACPCQhAdu3PyQgV4l3LMXZEtft7y8QcarRsp9I= google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 h1:pPJltXNxVzT4pK9yD8vR9X75DaWYYmLGMsEvBfFQZzQ= diff --git a/internal/api/handler.go b/internal/api/handler.go index 5ed6803..29e91d9 100644 --- a/internal/api/handler.go +++ b/internal/api/handler.go @@ -24,6 +24,7 @@ func (mux ServeMux) HandleFunc(method, endpoint string, handler http.HandlerFunc func NewHandler(cfg *config.Config) http.Handler { mux := NewServeMux() mux.HandleFunc("GET", "/v1", v1.Index) + mux.HandleFunc("GET", "/v1/version", v1.GetVersion) mux.HandleFunc("GET", "/v1/checkhealth", wrap(cfg, v1.CheckHealth)) mux.HandleFunc("HEAD", "/v1/checkhealth", wrap(cfg, v1.CheckHealth)) mux.HandleFunc("POST", "/v1/reload", wrap(cfg, v1.Reload)) diff --git a/internal/api/v1/version.go b/internal/api/v1/version.go new file mode 100644 index 0000000..bbfc2c5 --- /dev/null +++ b/internal/api/v1/version.go @@ -0,0 +1,11 @@ +package v1 + +import ( + "net/http" + + "github.com/yusing/go-proxy/pkg" +) + +func GetVersion(w http.ResponseWriter, r *http.Request) { + w.Write([]byte(pkg.GetVersion())) +} diff --git a/internal/net/http/header_utils.go b/internal/net/http/header_utils.go index ffd0f22..ab73658 100644 --- a/internal/net/http/header_utils.go +++ b/internal/net/http/header_utils.go @@ -2,6 +2,7 @@ package http import ( "net/http" + "slices" ) func RemoveHop(h http.Header) { @@ -24,30 +25,18 @@ func CopyHeader(dst, src http.Header) { } } -func FilterHeaders(h http.Header, allowed []string) http.Header { - if len(allowed) == 0 { - return h +func FilterHeaders(h http.Header, allowed []string) { + if allowed == nil { + return } - filtered := make(http.Header) - - for i, header := range allowed { - values := h.Values(header) - if len(values) == 0 { - continue - } - filtered[http.CanonicalHeaderKey(allowed[i])] = append([]string(nil), values...) + for i := range allowed { + allowed[i] = http.CanonicalHeaderKey(allowed[i]) } - return filtered -} - -func HeaderToMap(h http.Header) map[string]string { - result := make(map[string]string) - for k, v := range h { - if len(v) > 0 { - result[k] = v[0] // Take the first value + for key := range h { + if !slices.Contains(allowed, key) { + h.Del(key) } } - return result } diff --git a/internal/net/http/middleware/forward_auth.go b/internal/net/http/middleware/forward_auth.go index 98a0743..1e88eca 100644 --- a/internal/net/http/middleware/forward_auth.go +++ b/internal/net/http/middleware/forward_auth.go @@ -54,7 +54,7 @@ func NewForwardAuthfunc(optsRaw OptionsRaw) (*Middleware, E.NestedError) { } // TODO: use tr from reverse proxy - tr, ok := fa.transport.(*http.Transport) + tr, ok := fa.forwardAuthOpts.transport.(*http.Transport) if ok { tr = tr.Clone() } else { @@ -81,7 +81,7 @@ func (fa *forwardAuth) forward(next http.HandlerFunc, w ResponseWriter, req *Req nil, ) if err != nil { - fa.m.AddTracef("new request err to %s", fa.Address).WithError(err) + fa.m.AddTracef("new request err to %s", fa.Address).With("error", err) w.WriteHeader(http.StatusInternalServerError) return } @@ -89,13 +89,12 @@ func (fa *forwardAuth) forward(next http.HandlerFunc, w ResponseWriter, req *Req gpHTTP.CopyHeader(faReq.Header, req.Header) gpHTTP.RemoveHop(faReq.Header) - faReq.Header = gpHTTP.FilterHeaders(faReq.Header, fa.AuthResponseHeaders) + gpHTTP.FilterHeaders(faReq.Header, fa.AuthResponseHeaders) fa.setAuthHeaders(req, faReq) - fa.m.AddTraceRequest("forward auth request", faReq) faResp, err := fa.client.Do(faReq) if err != nil { - fa.m.AddTracef("failed to call %s", fa.Address).WithError(err) + fa.m.AddTracef("failed to call %s", fa.Address).With("error", err) w.WriteHeader(http.StatusInternalServerError) return } @@ -103,19 +102,19 @@ func (fa *forwardAuth) forward(next http.HandlerFunc, w ResponseWriter, req *Req body, err := io.ReadAll(faResp.Body) if err != nil { - fa.m.AddTracef("failed to read response body from %s", fa.Address).WithError(err) + fa.m.AddTracef("failed to read response body from %s", fa.Address).With("error", err) w.WriteHeader(http.StatusInternalServerError) return } if faResp.StatusCode < http.StatusOK || faResp.StatusCode >= http.StatusMultipleChoices { - fa.m.AddTraceResponse("forward auth response", faResp) + fa.m.AddTracef("status %d", faResp.StatusCode) gpHTTP.CopyHeader(w.Header(), faResp.Header) gpHTTP.RemoveHop(w.Header()) redirectURL, err := faResp.Location() if err != nil { - fa.m.AddTracef("failed to get location from %s", fa.Address).WithError(err) + fa.m.AddTracef("failed to get location from %s", fa.Address).With("error", err) w.WriteHeader(http.StatusInternalServerError) return } else if redirectURL.String() != "" { @@ -126,7 +125,7 @@ func (fa *forwardAuth) forward(next http.HandlerFunc, w ResponseWriter, req *Req w.WriteHeader(faResp.StatusCode) if _, err = w.Write(body); err != nil { - fa.m.AddTracef("failed to write response body from %s", fa.Address).WithError(err) + fa.m.AddTracef("failed to write response body from %s", fa.Address).With("error", err) } return } diff --git a/internal/net/http/middleware/trace.go b/internal/net/http/middleware/trace.go index 593dfcb..a9520c4 100644 --- a/internal/net/http/middleware/trace.go +++ b/internal/net/http/middleware/trace.go @@ -2,22 +2,22 @@ package middleware import ( "fmt" + "net/http" "sync" "time" - gpHTTP "github.com/yusing/go-proxy/internal/net/http" U "github.com/yusing/go-proxy/internal/utils" ) type Trace struct { - Time string `json:"time,omitempty"` - Caller string `json:"caller,omitempty"` - URL string `json:"url,omitempty"` - Message string `json:"msg"` - ReqHeaders map[string]string `json:"req_headers,omitempty"` - RespHeaders map[string]string `json:"resp_headers,omitempty"` - RespStatus int `json:"resp_status,omitempty"` - Additional map[string]any `json:"additional,omitempty"` + Time string `json:"time,omitempty"` + Caller string `json:"caller,omitempty"` + URL string `json:"url,omitempty"` + Message string `json:"msg"` + ReqHeaders http.Header `json:"req_headers,omitempty"` + RespHeaders http.Header `json:"resp_headers,omitempty"` + RespStatus int `json:"resp_status,omitempty"` + Additional map[string]any `json:"additional,omitempty"` } type Traces []*Trace @@ -25,7 +25,7 @@ type Traces []*Trace var traces = Traces{} var tracesMu sync.Mutex -const MaxTraceNum = 100 +const MaxTraceNum = 1000 func GetAllTrace() []*Trace { return traces @@ -36,7 +36,7 @@ func (tr *Trace) WithRequest(req *Request) *Trace { return nil } tr.URL = req.RequestURI - tr.ReqHeaders = gpHTTP.HeaderToMap(req.Header) + tr.ReqHeaders = req.Header.Clone() return tr } @@ -45,8 +45,8 @@ func (tr *Trace) WithResponse(resp *Response) *Trace { return nil } tr.URL = resp.Request.RequestURI - tr.ReqHeaders = gpHTTP.HeaderToMap(resp.Request.Header) - tr.RespHeaders = gpHTTP.HeaderToMap(resp.Header) + tr.ReqHeaders = resp.Request.Header.Clone() + tr.RespHeaders = resp.Header.Clone() tr.RespStatus = resp.StatusCode return tr } @@ -63,18 +63,6 @@ func (tr *Trace) With(what string, additional any) *Trace { return tr } -func (tr *Trace) WithError(err error) *Trace { - if tr == nil { - return nil - } - - if tr.Additional == nil { - tr.Additional = map[string]any{} - } - tr.Additional["error"] = err.Error() - return tr -} - func (m *Middleware) EnableTrace() { m.trace = true for _, child := range m.children { diff --git a/mtrace.json b/mtrace.json deleted file mode 100644 index c225403..0000000 --- a/mtrace.json +++ /dev/null @@ -1,95 +0,0 @@ -[ - { - "time": "2024-10-06 05:53:16", - "caller": "authentik-server", - "msg": "middleware created" - }, - { - "time": "2024-10-06 05:53:16", - "caller": "authentik", - "msg": "middleware created" - }, - { - "time": "2024-10-06 05:53:16", - "caller": "testMRInner", - "msg": "middleware created" - }, - { - "time": "2024-10-06 05:53:16", - "caller": "testMR", - "msg": "middleware created" - }, - { - "time": "2024-10-06 05:53:16", - "caller": "gp-test", - "msg": "middleware created" - }, - { - "time": "2024-10-06 05:53:16", - "caller": "immich-shared", - "msg": "middleware created" - }, - { - "time": "2024-10-06 05:53:16", - "caller": "immich", - "msg": "middleware created" - }, - { - "time": "2024-10-06 05:53:16", - "caller": "authentik", - "msg": "middleware created" - }, - { - "time": "2024-10-06 05:53:16", - "caller": "whoami", - "msg": "middleware created" - }, - { - "time": "2024-10-06 05:53:16", - "caller": "it-tools", - "msg": "middleware created" - }, - { - "time": "2024-10-06 05:53:21", - "caller": "whoami.authentik.authentik[0]", - "msg": "forward auth request", - "req_headers": { - "X-Forwarded-For": "10.0.1.2", - "X-Forwarded-Host": "whoami.i.sh", - "X-Forwarded-Method": "GET", - "X-Forwarded-Proto": "https", - "X-Forwarded-Uri": "/" - } - }, - { - "time": "2024-10-06 05:53:21", - "caller": "whoami.authentik.authentik[0]", - "msg": "forward auth response", - "req_headers": { - "X-Forwarded-For": "10.0.1.2", - "X-Forwarded-Host": "whoami.i.sh", - "X-Forwarded-Method": "GET", - "X-Forwarded-Proto": "https", - "X-Forwarded-Uri": "/" - }, - "resp_headers": { - "Content-Type": "text/html; charset=utf-8", - "Date": "Sat, 05 Oct 2024 21:53:21 GMT", - "Referrer-Policy": "same-origin", - "Vary": "Accept-Encoding", - "X-Authentik-Id": "22e0c95971dc4c1b8033725c6988abc9", - "X-Content-Type-Options": "nosniff", - "X-Frame-Options": "DENY", - "X-Powered-By": "authentik" - }, - "resp_status": 404 - }, - { - "time": "2024-10-06 05:53:21", - "caller": "whoami.authentik.authentik[0]", - "msg": "failed to get location from https://authentik.i.sh/outpost.goauthentik.io/auth/traefik", - "additional": { - "error": "http: no Location header in response" - } - } -] diff --git a/pkg/version.go b/pkg/version.go new file mode 100644 index 0000000..e4ef63f --- /dev/null +++ b/pkg/version.go @@ -0,0 +1,7 @@ +package pkg + +var version = "unset" + +func GetVersion() string { + return version +} diff --git a/scripts/build.sh b/scripts/build.sh new file mode 100755 index 0000000..83e3f39 --- /dev/null +++ b/scripts/build.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +mkdir -p bin +echo building go-proxy version ${VERSION}, build flags \"${BUILD_FLAGS}\" +go build -ldflags "${BUILD_FLAGS}" -pgo=auto -o bin/go-proxy ./cmd