-
Notifications
You must be signed in to change notification settings - Fork 0
/
main6-epoch-works.html
194 lines (151 loc) · 4.37 KB
/
main6-epoch-works.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strconv"
"encoding/base64"
"time"
)
const (
DefaultHost = "http://localhost"
DefaultPort = "8080"
)
func main() {
proxyUrl := host() + ":" + port()
//now := time.Now()
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
}
target2, err := base64.StdEncoding.DecodeString(query.Get("curl"))
if err != nil {
if _, ok := err.(base64.CorruptInputError); ok {
panic("\nbase64 input is corrupt, check service Key")
}
panic(err)
}
epochFromUrl, err := strconv.Atoi(string(target2[len(target2)-10:]))
if err != nil {
panic(err)
}
//target2 = (target2[:len(target2)-10])
curEpoch := int(time.Now().Unix())
if curEpoch - epochFromUrl < 150 {
target2 = (target2[:len(target2)-10])
}
target3 := []byte(target2)
target4 := string(target3[:])
target4 = target4[:5] + "" + target4[6:]
target4 = target4[:9] + "" + target4[10:]
target4 = target4[:12] + "" + target4[13:]
target4 = target4[:14] + "" + target4[15:]
target4 = target4[:15] + "" + target4[16:]
target4 = target4[:15] + "" + target4[16:]
target5, err := base64.StdEncoding.DecodeString(target4)
if err != nil {
if _, ok := err.(base64.CorruptInputError); ok {
panic("\nbase64 input is corrupt, check service Key")
}
panic(err)
}
target, err := url.Parse(string(target5))
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)))
}