-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonionizer.go
108 lines (83 loc) · 2.68 KB
/
onionizer.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
package main
import (
"bytes"
"flag"
"fmt"
"log"
"strings"
"io/ioutil"
"net/http"
"github.com/elazarl/goproxy"
)
func main() {
verbose := flag.Bool("verbose", false, "should every proxy request be logged to stdout")
http_addr := flag.String("http_addr", ":8080", "proxy listen address")
https_addr := flag.String("https_addr", ":8081", "proxy https listen address")
cert_file := flag.String("cert", "cert.pem", "https certificate")
key_file := flag.String("key", "key.pem", "https private key")
origin := flag.String("origin", "example.com", "origin domain")
onion := flag.String("onion", "example.onion", "onion domain")
server := flag.String("server", "", "proxy requests to host (origin domain by default")
flag.Parse()
proxy := goproxy.NewProxyHttpServer()
proxy.Verbose = *verbose
proxy.NonproxyHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if req.Host == "" {
fmt.Fprintln(w, "Cannot handle requests without Host header, e.g., HTTP 1.0")
return
}
req.URL.Scheme = "http"
req.URL.Host = req.Host
proxy.ServeHTTP(w, req)
})
proxy.OnResponse().DoFunc(
func(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response {
for key, value := range resp.Header {
for index, _ := range value {
resp.Header[key][index] = strings.Replace(value[index], *origin, *onion, -1)
}
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
new_body := strings.Replace(string(body), *origin, *onion, -1)
buf := bytes.NewBufferString(new_body)
resp.Body = ioutil.NopCloser(buf)
return resp
})
proxy.OnRequest().DoFunc(
func(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
if *server == "" {
r.URL.Host = strings.Replace(r.URL.Host, *onion, *origin, -1)
} else {
r.URL.Host = *server
}
for key, value := range r.Header {
for index, _ := range value {
r.Header[key][index] = strings.Replace(value[index], *onion, *origin, -1)
}
}
for key, value := range r.Form {
for index, _ := range value {
r.Form[key][index] = strings.Replace(value[index], *onion, *origin, -1)
}
}
for key, value := range r.PostForm {
for index, _ := range value {
r.PostForm[key][index] = strings.Replace(value[index], *onion, *origin, -1)
}
}
r.Host = strings.Replace(r.Host, *onion, *origin, -1)
return r, nil
})
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
req.Header.Add("X-Forwarded-Proto", "https")
proxy.ServeHTTP(w, req)
})
go func() {
log.Fatal(http.ListenAndServe(*http_addr, proxy))
}()
err := http.ListenAndServeTLS(*https_addr, *cert_file, *key_file, nil)
if err != nil {
log.Fatal(err)
}
}