-
Notifications
You must be signed in to change notification settings - Fork 0
/
prxy3.go
110 lines (90 loc) · 2.26 KB
/
prxy3.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
package main
import (
"bytes"
"flag"
"fmt"
"io"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strconv"
)
type transport struct {
http.RoundTripper
}
func processRequest(request *http.Request) (*http.Request, error) {
fmt.Print("\nREQUEST\n")
fmt.Println(request.Method, request.URL)
request.Header.Write(os.Stdout)
if request.Body != nil {
reqB, err := io.ReadAll(request.Body)
if err != nil {
return nil, err
}
err = request.Body.Close()
if err != nil {
return nil, err
}
fmt.Print("\nREQUEST BODY\n")
fmt.Println(string(reqB))
fmt.Print("\n")
request.Body = io.NopCloser(bytes.NewReader(reqB))
}
return request, nil
}
func processResponse(response *http.Response) (*http.Response, error) {
respB, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
err = response.Body.Close()
if err != nil {
return nil, err
}
fmt.Print("\nRESPONSE\n")
fmt.Println(response.Proto, response.Status)
response.Header.Write(os.Stdout)
fmt.Print("\nRESPONSE BODY\n")
fmt.Println(string(respB))
fmt.Print("\n")
body := io.NopCloser(bytes.NewReader(respB))
response.Body = body
response.ContentLength = int64(len(respB))
response.Header.Set("Content-Length", strconv.Itoa(len(respB)))
return response, nil
}
func (t *transport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
req, err = processRequest(req)
if err != nil {
return nil, err
}
resp, err = t.RoundTripper.RoundTrip(req)
if err != nil {
return nil, err
}
resp, err = processResponse(resp)
return resp, err
}
func main() {
dst := flag.String("d", "localhost:8080", "the address of the target")
prx := flag.String("p", "localhost:9090", "the address of the proxy")
flag.Parse()
reverseUrlStr := "http://" + *dst + "/"
reverseUrl, _ := url.Parse(reverseUrlStr)
handler := func(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
// log.Println(r.URL)
r.Host = reverseUrl.Host
//w.Header().Set("X-PRXY", "rad")
p.ServeHTTP(w, r)
}
}
proxy := httputil.NewSingleHostReverseProxy(reverseUrl)
proxy.Transport = &transport{http.DefaultTransport}
http.HandleFunc("/", handler(proxy))
err := http.ListenAndServe(*prx, nil)
if err != nil {
panic(err)
}
}