/
anonymizer.go
114 lines (103 loc) · 3.57 KB
/
anonymizer.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
package main
import (
"io"
"regexp"
"strings"
"io/ioutil"
"os"
"fmt"
"http"
)
func root(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Index!")
}
var ReplacemendContentType = map[string] bool {
"text/html": true,
"text/javascript": true,
"application/x-javascript": true,
"application/javascript": true,
"text/plain": true,
"text/css": true,
}
func replace_url(body string, requestHost string, proxyHost string) string {
reCSS,_ := regexp.Compile("url\\((http://|https://|/)[^\\)]+\\)")
reSRCandHREF, _ := regexp.Compile("(src|href)=((\"(http://|/|https://)[^\"]+\")|('(https://|/|http://)[^']+')|((http://|/|https://)[^ ]+))")
FromURLtoAnonURL := func (url string) string {
if strings.HasPrefix(url, "//") {
url = requestHost+url[2:]
} else if strings.HasPrefix(url, "/") {
url = requestHost+proxyHost+url
} else if strings.HasPrefix(url, "https://") {
url = "/https/"+url[len("https://"):]
} else if strings.HasPrefix(url, "http://") {
url = "/http/"+url[len("http://"):]
}
return url
}
fnCSS := func(s string) string {
url := s[4:]
if url[0] == '"' || url[0] == '\'' {
url = url[1:len(url)-1]
}
return "url("+FromURLtoAnonURL(url[:len(url)-1])+")"
}
fnSRCandHREF := func(s string) string {
var isHREF bool
var url string
if strings.HasPrefix(s, "src=") {
url = s[4:]
isHREF = false
} else {
url = s[5:]
isHREF = true
}
if url[0] == '"' || url[0] == '\'' {
url = url[1:len(url)-1]
}
if isHREF{
return "href=\""+FromURLtoAnonURL(url)+"\""
}
return "src=\""+FromURLtoAnonURL(url)+"\""
}
body = reCSS.ReplaceAllStringFunc(body, fnCSS)
body = reSRCandHREF.ReplaceAllStringFunc(body, fnSRCandHREF)
body = strings.Replace(body, "\"https://", "\"/https/", -1)
body = strings.Replace(body, "\"http://", "\"/http/", -1)
body = strings.Replace(body, "'https://", "'/https/", -1)
body = strings.Replace(body, "'http://", "'/http/", -1)
return body
}
func handle_http(responseWrite http.ResponseWriter, request *http.Request) {
var proxyResponse *http.Response;
var proxyResponseError os.Error;
proxyHost := strings.Split(request.URL.Path[6:], "/")[0]
fmt.Printf("%s %s %s\n", request.Method, request.RawURL, request.RemoteAddr)
//TODO https
url := "http://"+request.URL.Path[6:]
proxyRequest,_ := http.NewRequest(request.Method, url, nil)
proxy := &http.Client{}
proxyResponse, proxyResponseError = proxy.Do(proxyRequest)
if proxyResponseError != nil {
http.NotFound(responseWrite, request)
return
}
for header := range proxyResponse.Header {
responseWrite.Header().Add(header, proxyResponse.Header.Get(header))
}
contentType := strings.Split(proxyResponse.Header.Get("Content-Type"), ";")[0]
if proxyResponseError != nil {
fmt.Fprintf(responseWrite, "pizda\n")
} else if ReplacemendContentType[contentType] {
body,_ := ioutil.ReadAll(proxyResponse.Body)
defer proxyResponse.Body.Close()
bodyString := replace_url(string(body), "/http/", proxyHost)
responseWrite.Write([]byte(bodyString))
} else {
io.Copy(responseWrite, proxyResponse.Body)
}
}
func main() {
http.HandleFunc("/http/", handle_http)
http.HandleFunc("/", root)
http.ListenAndServe(":8080", nil)
}