This repository was archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d01c221
Showing
5 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
name: Build and Push Container | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
branches: [main] | ||
|
||
env: | ||
IMAGE_BASENAME: 'go-netbox-proxy' | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- uses: actions/setup-go@v2 | ||
|
||
|
||
# Required for docker/build-push-action | ||
- name: Set up Docker Buildx | ||
uses: docker/setup-buildx-action@v1 | ||
|
||
- name: Login to Container Registry | ||
uses: docker/login-action@v1 | ||
with: | ||
registry: ${{ secrets.ACR_LOGIN_SERVER }} | ||
username: ${{ secrets.ACR_USERNAME }} | ||
password: ${{ secrets.ACR_PASSWORD }} | ||
|
||
- name: Determine Image Tags | ||
id: tags | ||
uses: docker/metadata-action@v3 | ||
with: | ||
images: ${{ secrets.ACR_LOGIN_SERVER }}/${{ env.IMAGE_BASENAME }} | ||
sep-tags: ',' | ||
flavor: | | ||
latest=false | ||
tags: | | ||
type=ref,event=pr | ||
type=sha,enable=${{ github.event_name == 'push' }},prefix=${{ github.event.repository.default_branch }}-${{ github.run_number }}- | ||
- name: Build and Push | ||
id: docker_build | ||
uses: docker/build-push-action@v2 | ||
with: | ||
context: . | ||
push: true | ||
file: Dockerfile | ||
tags: ${{ steps.tags.outputs.tags }} | ||
cache-from: type=gha | ||
cache-to: type=gha,mode=max |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
|
||
name: Tag and Push Container on Release | ||
|
||
on: | ||
push: | ||
tags: | ||
- v** | ||
|
||
env: | ||
IMAGE_BASENAME: 'go-netbox-proxy' | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- uses: actions/setup-go@v2 | ||
|
||
|
||
# Required for docker/build-push-action | ||
- name: Set up Docker Buildx | ||
uses: docker/setup-buildx-action@v1 | ||
|
||
- name: Login to Container Registry | ||
uses: docker/login-action@v1 | ||
with: | ||
registry: ${{ secrets.ACR_LOGIN_SERVER }} | ||
username: ${{ secrets.ACR_USERNAME }} | ||
password: ${{ secrets.ACR_PASSWORD }} | ||
|
||
- name: Determine Image Tags | ||
id: tags | ||
uses: docker/metadata-action@v3 | ||
with: | ||
images: ${{ secrets.ACR_LOGIN_SERVER }}/${{ env.IMAGE_BASENAME }} | ||
sep-tags: ',' | ||
flavor: | | ||
latest=false | ||
tags: | | ||
type=semver,pattern={{version}} | ||
type=semver,pattern={{major}}.{{minor}} | ||
type=semver,pattern={{major}} | ||
- name: Build and Push | ||
id: docker_build | ||
uses: docker/build-push-action@v2 | ||
with: | ||
context: . | ||
push: true | ||
file: Dockerfile | ||
tags: ${{ steps.tags.outputs.tags }} | ||
cache-from: type=gha | ||
cache-to: type=gha,mode=max |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Start by building the application. | ||
FROM golang:1.18.2-buster as build | ||
|
||
WORKDIR /go/src/go-netbox-proxy | ||
COPY . /go/src/go-netbox-proxy | ||
|
||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags '-w -extldflags "-static"' -o /go/bin/go-netbox-proxy | ||
|
||
# Now copy it into our base image. | ||
FROM gcr.io/distroless/static:latest-amd64 | ||
COPY --from=build /go/bin/go-netbox-proxy / | ||
|
||
CMD ["/go-netbox-proxy"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/riege/go-netbox-proxy | ||
|
||
go 1.18 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"flag" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"net/http/httputil" | ||
"net/url" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
type HttpHandler struct{} | ||
var upstream *url.URL | ||
|
||
func (h *HttpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
|
||
r.Host = upstream.Host | ||
r.URL.Scheme = upstream.Scheme | ||
proxy := httputil.NewSingleHostReverseProxy(upstream) | ||
proxy.ModifyResponse = func(r *http.Response) error { | ||
|
||
if strings.Contains(r.Request.URL.Path, "/api/") { | ||
|
||
// Read the response body | ||
b, _ := ioutil.ReadAll(r.Body) | ||
|
||
// Compile the regex | ||
var re = regexp.MustCompile(`"created":".+(T.+Z)",`) | ||
|
||
// Replace the current created datetime by a fake date | ||
b = re.ReplaceAllFunc(b, func(s []byte) []byte { | ||
return []byte(`"created":"2020-01-01",`) | ||
}) | ||
buf := bytes.NewBuffer(b) | ||
|
||
// Replace the body with our new body | ||
r.Body = ioutil.NopCloser(buf) | ||
// Set the content length | ||
r.Header["Content-Length"] = []string{fmt.Sprint(buf.Len())} | ||
// Set Proxy Header | ||
r.Header.Set("X-Proxy", "go-netbox-proxy 1.0") | ||
return nil | ||
} | ||
|
||
return nil | ||
} | ||
// Serve request | ||
proxy.ServeHTTP(w, r) | ||
|
||
} | ||
|
||
func main() { | ||
|
||
// Parse flags | ||
addr := flag.String("addr", ":8080", "proxy listen address") | ||
up := flag.String("upstream", "", "upstream http address") | ||
flag.Parse() | ||
|
||
// Parse upstream url | ||
parsedUpstream, err := url.Parse(*up) | ||
|
||
if err != nil { | ||
log.Fatal(err.Error()) | ||
} | ||
|
||
// Set upstream | ||
upstream = parsedUpstream | ||
|
||
// Setup the reverse proxy server | ||
httpHandler := &HttpHandler{} | ||
http.Handle("/", httpHandler) | ||
err = http.ListenAndServe(*addr, nil) | ||
|
||
if err != nil { | ||
log.Fatal(err.Error()) | ||
} | ||
|
||
} |