-
Notifications
You must be signed in to change notification settings - Fork 0
/
main3.html
159 lines (128 loc) · 3.7 KB
/
main3.html
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strconv"
"encoding/base64"
)
const (
DefaultHost = "http://localhost"
DefaultPort = "8080"
)
func main() {
proxyUrl := host() + ":" + port()
http.HandleFunc("/favicon.ico", func(rw http.ResponseWriter, r *http.Request) {
favicon(rw)
})
http.HandleFunc("/", func(rw http.ResponseWriter, request *http.Request) {
rw.Header().Set("Access-Control-Allow-Origin", "*")
query := request.URL.Query()
if query.Get("curl") == "" {
displayError(rw, "Nothing requested.")
return
}
//target, err := string(base64.StdEncoding.DecodeString(url.Parse(query.Get("curl"))))
//target, err := url.Parse(query.Get("curl"))
//if err != nil || target.IsAbs() == false {
//displayError(rw, "URL is invalid.")
//return
//}
target2, err := base64.StdEncoding.DecodeString(query.Get("curl"))
if err != nil {
log.Fatalf("Some error occured during base64 decode. Error %s", err.Error())
}
target, err := url.Parse(string(target2))
if err != nil || target.IsAbs() == false {
displayError(rw, "URL is invalid.")
return
}
// Make reverse proxy
proxy := &httputil.ReverseProxy{
Director: func(r *http.Request) {
for name, value := range request.URL.Query() {
if name[0:2] == "h_" {
r.Header.Del(name[2:])
for _, v := range value {
r.Header.Set(name[2:], v)
}
}
}
//r.Host = base64.StdEncoding.DecodeString(string(target.Host))
//r.URL = base64.StdEncoding.DecodeString(string(target))
r.Host = target.Host
r.URL = target
log.Println(r)
},
ModifyResponse: func(r *http.Response) error {
r.Header.Del("Access-Control-Allow-Origin")
// Handle redirection responses
if r.StatusCode >= 300 && r.StatusCode < 400 && r.Header.Get("Location") != "" {
if query.Get("redirection") == "follow" {
r.Header.Set("Location", proxyUrl+"/?redirection=follow&url="+r.Header.Get("Location"))
} else if query.Get("redirection") == "stop" {
displayLocation(r, r.Header.Get("Location"))
}
}
return nil
},
}
proxy.ServeHTTP(rw, request)
})
log.Println("Serving on " + proxyUrl)
log.Fatal(http.ListenAndServe(":"+port(), nil))
}
// host will return the proxy host
func host() string {
if host := os.Getenv("HOST"); host == "" {
return DefaultHost
} else {
return host
}
}
// port will return the proxy port
func port() string {
if port := os.Getenv("PORT"); port == "" {
return DefaultPort
} else {
return port
}
}
// favicon will return the favicon icon
func favicon(rw http.ResponseWriter) {
content, _ := ioutil.ReadFile("favicon.ico")
rw.Header().Set("Content-Type", "image/x-icon")
_, _ = fmt.Fprintln(rw, string(content))
}
// displayError will create an error response
func displayError(rw http.ResponseWriter, error string) {
rw.WriteHeader(http.StatusBadRequest)
rw.Header().Set("Content-type", "application/json")
body, err := json.Marshal(map[string]string{"error": error})
if err != nil {
panic("Cannot generate the JSON.")
}
_, err = rw.Write(body)
if err != nil {
panic("Cannot display the error.")
}
}
// displayLocation will modify the response to a display location instead of redirecting
func displayLocation(r *http.Response, location string) {
body, err := json.Marshal(map[string]string{"location": location})
if err != nil {
panic("Cannot display the location.")
}
r.Body = ioutil.NopCloser(bytes.NewReader(body))
r.ContentLength = int64(len(body))
r.StatusCode = http.StatusOK
r.Header = http.Header{}
r.Header.Set("Content-Type", "application/json")
r.Header.Set("Content-Length", strconv.Itoa(len(body)))
}