-
Notifications
You must be signed in to change notification settings - Fork 472
/
proxy.go
73 lines (69 loc) · 1.92 KB
/
proxy.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
package dashboard
import (
"fmt"
"log"
"net/http"
"net/http/httputil"
"net/url"
"strings"
)
//var messageRe = regexp.MustCompile(`<p>(.*)([\s\S]*?)<\/p>`)
func NewDashboardHTTPReverseProxy(serverAddr string, transport http.RoundTripper) func(writer http.ResponseWriter, request *http.Request) {
target, err := url.Parse(serverAddr)
if err != nil {
log.Fatal(err)
}
proxy := httputil.NewSingleHostReverseProxy(target)
proxy.Transport = transport
proxy.Director = func(request *http.Request) {
path := request.URL.Path
request.URL.Host = target.Host
request.URL.Scheme = target.Scheme
request.URL.Path = rewriteRequestUrl(path)
fmt.Printf("%s\n", request.URL.Path)
}
//proxy.Director = func(request *http.Request) {
// path := request.URL.Path
// request.URL.Path = rewriteRequestUrl(path)
//}
//proxy.ModifyResponse = func(resp *http.Response) error {
// if resp.StatusCode == 500 {
// b, err := ioutil.ReadAll(resp.Body)
// if err != nil {
// return err
// }
// err = resp.Body.Close()
// if err != nil {
// return err
// }
// var message string
// matches := messageRe.FindSubmatch(b)
// if len(matches) > 1 {
// message = html.UnescapeString(string(matches[1]))
// } else {
// message = "Unknown error"
// }
// resp.ContentLength = 0
// resp.Header.Set("Content-Length", strconv.Itoa(0))
// resp.Header.Set(Location, fmt.Sprintf("/dashboard/login?sso_error=%s", url.QueryEscape(message)))
// resp.StatusCode = http.StatusSeeOther
// resp.Body = ioutil.NopCloser(bytes.NewReader(make([]byte, 0)))
// return nil
// }
// return nil
//}
return func(w http.ResponseWriter, r *http.Request) {
proxy.ServeHTTP(w, r)
}
}
func rewriteRequestUrl(path string) string {
parts := strings.Split(path, "/")
var finalParts []string
for _, part := range parts {
if part == "dashboard" {
continue
}
finalParts = append(finalParts, part)
}
return strings.Join(finalParts, "/")
}