forked from fly-apps/smokescreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
50 lines (42 loc) · 1.13 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
package main
import (
"encoding/base64"
"fmt"
"log"
"net/http"
"os"
"strings"
"github.com/stripe/smokescreen/cmd"
"github.com/stripe/smokescreen/pkg/smokescreen"
)
func main() {
conf, err := cmd.NewConfiguration(nil, nil)
if err != nil {
log.Fatal(err)
}
if conf == nil {
os.Exit(1)
}
password := os.Getenv("PROXY_PASSWORD")
if password == "" {
fmt.Println("missing environment variable: PROXY_PASSWORD")
os.Exit(1)
}
conf.RejectResponseHandler = func(resp *http.Response) {
resp.Header.Set("Proxy-Authenticate", "Basic realm=\"smokescreen\"")
}
conf.RoleFromRequest = func(request *http.Request) (string, error) {
fail := func(err string) (string, error) { return "", fmt.Errorf(err) }
auth := strings.SplitN(request.Header.Get("Proxy-Authorization"), " ", 2)
if len(auth) != 2 || auth[0] != "Basic" {
return fail("authorization failed")
}
payload, _ := base64.StdEncoding.DecodeString(auth[1])
pair := strings.SplitN(string(payload), ":", 2)
if len(pair) != 2 || pair[1] != password {
return fail("authorization failed")
}
return "authed", nil
}
smokescreen.StartWithConfig(conf, nil)
}