-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
98 lines (90 loc) · 2.8 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"fmt"
"log"
"net/http"
"net/http/httputil"
"net/url"
"strconv"
"strings"
)
func handleMin(cConstraint Constraint, cName string, queryValues *url.Values) {
if cConstraint.Min != nil {
f, err := strconv.ParseFloat(queryValues.Get(cName), 32)
if err == nil && f < *cConstraint.Min {
queryValues.Set(cName, strconv.FormatFloat(*cConstraint.Min, 'G', -1, 32))
}
}
}
func handleMax(cConstraint Constraint, cName string, queryValues *url.Values) {
if cConstraint.Max != nil {
f, err := strconv.ParseFloat(queryValues.Get(cName), 32)
if err == nil && f > *cConstraint.Max {
queryValues.Set(cName, strconv.FormatFloat(*cConstraint.Max, 'G', -1, 32))
}
}
}
func handleRound(cConstraint Constraint, cName string, queryValues *url.Values) {
if cConstraint.Round != nil {
f, err := strconv.ParseFloat(queryValues.Get(cName), 32)
if err == nil {
queryValues.Set(cName, fmt.Sprintf("%."+strconv.Itoa(*cConstraint.Round)+"f", f))
}
}
}
func applyConstraints(u url.URL, conf map[string]Constraint) string {
var newValues url.Values = u.Query()
for cName, cConstraint := range conf {
if _, ok := u.Query()[cName]; ok == true {
handleMax(cConstraint, cName, &newValues)
handleMin(cConstraint, cName, &newValues)
handleRound(cConstraint, cName, &newValues)
}
}
return newValues.Encode()
}
func singleJoiningSlash(a, b string) string {
aslash := strings.HasSuffix(a, "/")
bslash := strings.HasPrefix(b, "/")
switch {
case aslash && bslash:
return a + b[1:]
case !aslash && !bslash:
return a + "/" + b
}
return a + b
}
// UglyReverseProxy is an ugly copy of httputil's NewSingleHostReverseProxy
// that adds the functionality of filtering and editing http request parameters
// by using user defined constraints.
func UglyReverseProxy(target *url.URL, constraints map[string]Constraint) *httputil.ReverseProxy {
targetQuery := target.RawQuery
director := func(req *http.Request) {
req.URL.Scheme = target.Scheme
req.URL.Host = target.Host
req.URL.Path = singleJoiningSlash(target.Path, req.URL.Path)
if targetQuery == "" || req.URL.RawQuery == "" {
req.URL.RawQuery = applyConstraints(*req.URL, constraints)
} else {
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
}
if _, ok := req.Header["User-Agent"]; !ok {
// explicitly disable User-Agent so it's not set to default value
req.Header.Set("User-Agent", "")
}
}
return &httputil.ReverseProxy{Director: director}
}
func main() {
loadConfig()
ourPort := fmt.Sprintf(":%d", Config.Port)
rpURL, err := url.Parse(fmt.Sprintf(Config.ProxiedService))
if err != nil {
log.Panic("invalid proxy url")
}
log.Println("Serving", rpURL, "at port", ourPort)
reverseProxy := UglyReverseProxy(rpURL, Config.Constraints)
if err := http.ListenAndServe(ourPort, reverseProxy); err != nil {
log.Fatal(err)
}
}