-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (55 loc) · 1.51 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
package main
import (
"encoding/base64"
"encoding/json"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"regexp"
"github.com/urfave/negroni"
)
type QueryRestriction struct {
Key string `json:"key"`
ValueRegex string `json:"valueRegex"`
}
type Config struct {
URL string `json:"url"`
QueryRestrictions []QueryRestriction `json:"queryRestrictions"`
}
func ParseConfigFromBase64(encodedConfig string) (config Config) {
raw, _ := base64.StdEncoding.DecodeString(encodedConfig)
json.Unmarshal(raw, &config)
return
}
func (config Config) serverURL() *url.URL {
uri, err := url.Parse(config.URL)
if err != nil {
log.Fatal("Sever URL is invalid :" + config.URL)
}
return uri
}
func ProxyHandler(config Config) http.HandlerFunc {
log.Println(config)
origin := httputil.NewSingleHostReverseProxy(config.serverURL())
return func(writer http.ResponseWriter, request *http.Request) {
log.Println("Received Request: ", request)
request.ParseForm()
for _, restriction := range config.QueryRestrictions {
value := request.Form.Get(restriction.Key)
if !regexp.MustCompile(restriction.ValueRegex).MatchString(value) {
http.Error(writer, "FORBIDDEN", 403)
return
}
}
origin.ServeHTTP(writer, request)
}
}
func main() {
handler := ProxyHandler(ParseConfigFromBase64(os.Getenv("CONFIG")))
n := negroni.Classic()
n.UseHandlerFunc(handler)
log.Println("Starting ProxyWall on port: ", os.Getenv("PORT"))
log.Fatal(http.ListenAndServe(":"+os.Getenv("PORT"), n))
}