-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
128 lines (112 loc) · 3.28 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
package main
import (
"io"
"io/ioutil"
"net/http"
"bytes"
"fmt"
"os"
"strings"
"net/url"
"strconv"
)
const (
URL_PREFIX = "/"
SPARK_MASTER_HOST = "127.0.0.1:8080"
)
var sparkMasterAddr string
func main() {
fmt.Println(os.Args[1])
sparkMasterAddr = os.Args[1]
if len(sparkMasterAddr) == 0 {
sparkMasterAddr = SPARK_MASTER_HOST
}
http.HandleFunc("/healthz", Healthz)
http.ListenAndServe(":9999", http.HandlerFunc(proxyHandleFunc))
}
func Healthz(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(200)
w.Write([]byte("ok"))
}
func proxyHandleFunc(w http.ResponseWriter, req *http.Request) {
fmt.Printf("Received request %s %s \n", req.Method, req.URL.String())
if req.URL.Path == "" || req.URL.Path == URL_PREFIX {
http.Redirect(w, req, URL_PREFIX+"proxy:"+ sparkMasterAddr, 302)
return
}
targetHost, targetPath := extractURLDetails(req.URL.Path)
// replace out-request with target info
outReq := new(http.Request)
*outReq = *req
outReq.URL.Path = targetPath
outReq.URL.Host = targetHost
outReq.URL.Scheme = "http"
outReq.Header.Set("Accept-Encoding", "")
res, err := http.DefaultTransport.RoundTrip(outReq)
if err != nil {
fmt.Printf("transport outReq error: %s \n", err.Error())
w.WriteHeader(http.StatusBadGateway)
return
}
defer res.Body.Close()
w.Header().Set("Content-Type", res.Header.Get("Content-Type"))
if res.StatusCode == 200 {
w.WriteHeader(res.StatusCode)
if IsStaticPath(targetPath) {
io.Copy(w, res.Body)
} else {
data := rewriteLinks(res.Body, targetHost)
w.Header().Set("Content-Length", strconv.Itoa(len(data)))
w.Write(data)
}
return
} else if res.StatusCode == 302 {
locationURL, err := url.Parse(res.Header.Get("Location"))
if err != nil {
http.Redirect(w, req, URL_PREFIX+"proxy:"+ sparkMasterAddr, 302)
}
rURL := URL_PREFIX + "proxy:" + outReq.URL.Host + locationURL.Path + "?" + locationURL.Query().Encode()
http.Redirect(w, req, rURL, 302)
return
}
return
}
func rewriteLinks(src io.Reader, targetHost string) []byte {
data, err := ioutil.ReadAll(src)
if err != nil {
fmt.Printf("proxy ioutil read failed, error:%s \n", err.Error())
return nil
}
target := fmt.Sprintf("%sproxy:%s/", URL_PREFIX, targetHost)
page := bytes.Replace(data, []byte(`href="/`), []byte(`href="`+target), -1)
page = bytes.Replace(page, []byte(`href="log`), []byte(`href="`+target+"log"), -1)
page = bytes.Replace(page, []byte(`href="http://`), []byte(`href="`+URL_PREFIX+"proxy:"), -1)
page = bytes.Replace(page, []byte(`src="/`), []byte(`src="`+target), -1)
page = bytes.Replace(page, []byte(`action="`), []byte(`action="`+target), -1)
return page
}
func extractURLDetails(path string) (string, string) {
start, idx := 0, 0
targetHost, proxyPath := "", ""
if strings.HasPrefix(path, URL_PREFIX+"proxy:") {
start = len(URL_PREFIX) + 6 // len('proxy:') == 6
idx = strings.Index(path[start:], "/")
if idx == -1 {
targetHost = path[start:]
return targetHost, proxyPath
}
targetHost = path[start:(idx + start)]
proxyPath = path[(idx + start):]
return targetHost, proxyPath
}
return sparkMasterAddr, path
}
func IsStaticPath(path string) bool {
staticFileSuffixs := []string{"js", "png", "css"}
for _, v := range staticFileSuffixs {
if strings.HasSuffix(path, v) {
return true
}
}
return false
}