forked from cloudfoundry/logging-route-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
139 lines (116 loc) · 3.25 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"bytes"
"crypto/tls"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strconv"
"time"
)
const (
DEFAULT_PORT = "8080"
CF_FORWARDED_URL_HEADER = "X-Cf-Forwarded-Url"
CF_PROXY_SIGNATURE_HEADER = "X-Cf-Proxy-Signature"
)
func main() {
var (
port string
skipSslValidation bool
err error
)
if port = os.Getenv("PORT"); len(port) == 0 {
port = DEFAULT_PORT
}
if skipSslValidation, err = strconv.ParseBool(os.Getenv("SKIP_SSL_VALIDATION")); err != nil {
skipSslValidation = true
}
log.SetOutput(os.Stdout)
roundTripper := NewLoggingRoundTripper(skipSslValidation)
proxy := NewProxy(roundTripper, skipSslValidation)
log.Fatal(http.ListenAndServe(":"+port, proxy))
}
func NewProxy(transport http.RoundTripper, skipSslValidation bool) http.Handler {
reverseProxy := &httputil.ReverseProxy{
Director: func(req *http.Request) {
forwardedURL := req.Header.Get(CF_FORWARDED_URL_HEADER)
sigHeader := req.Header.Get(CF_PROXY_SIGNATURE_HEADER)
body, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Fatalln(err.Error())
}
req.Body = ioutil.NopCloser(bytes.NewBuffer(body))
logRequest(forwardedURL, sigHeader, string(body), req.Header, skipSslValidation)
err = sleep()
if err != nil {
log.Fatalln(err.Error())
}
// Note that url.Parse is decoding any url-encoded characters.
url, err := url.Parse(forwardedURL)
if err != nil {
log.Fatalln(err.Error())
}
req.URL = url
req.Host = url.Host
},
Transport: transport,
}
return reverseProxy
}
func logRequest(forwardedURL, sigHeader, body string, headers http.Header, skipSslValidation bool) {
log.Printf("Skip ssl validation set to %t", skipSslValidation)
log.Println("Received request: ")
log.Printf("%s: %s\n", CF_FORWARDED_URL_HEADER, forwardedURL)
log.Printf("%s: %s\n", CF_PROXY_SIGNATURE_HEADER, sigHeader)
log.Println("")
log.Printf("Headers: %#v\n", headers)
log.Println("")
log.Printf("Request Body: %s\n", body)
}
type LoggingRoundTripper struct {
transport http.RoundTripper
}
func NewLoggingRoundTripper(skipSslValidation bool) *LoggingRoundTripper {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: skipSslValidation},
}
return &LoggingRoundTripper{
transport: tr,
}
}
func (lrt *LoggingRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) {
var err error
var res *http.Response
log.Printf("Forwarding to: %s\n", request.URL.String())
res, err = lrt.transport.RoundTrip(request)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatalln(err.Error())
}
log.Println("")
log.Printf("Response Headers: %#v\n", res.Header)
log.Println("")
log.Printf("Response Body: %s\n", string(body))
log.Println("")
res.Body = ioutil.NopCloser(bytes.NewBuffer(body))
log.Println("Sending response to GoRouter...")
return res, err
}
func sleep() error {
sleepMilliString := os.Getenv("ROUTE_SERVICE_SLEEP_MILLI")
if sleepMilliString != "" {
sleepMilli, err := strconv.ParseInt(sleepMilliString, 0, 64)
if err != nil {
return err
}
log.Printf("Sleeping for %d milliseconds\n", sleepMilli)
time.Sleep(time.Duration(sleepMilli) * time.Millisecond)
}
return nil
}