forked from riege/go-netbox-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
82 lines (63 loc) · 1.6 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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":"(?:.+?)",`)
// 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())
}
}