-
Notifications
You must be signed in to change notification settings - Fork 0
/
invs.go
90 lines (76 loc) · 1.88 KB
/
invs.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
package main
import (
"flag"
"fmt"
"net/http"
"net/http/httputil"
"strings"
)
type options struct {
port, filterURL, proxyHost, excludeURL string
}
var opts = options{}
func (opts options) addr() string {
return fmt.Sprintf(":%s", opts.port)
}
func setOptions() {
flag.StringVar(&opts.port, "p", "8080", "port")
flag.StringVar(&opts.proxyHost, "proxy-host", "", "port")
flag.StringVar(&opts.filterURL, "filter-url", "", "Output only URLs that contain the string")
flag.StringVar(&opts.excludeURL, "exclude-url", "", "Exclude output URLs that contain the string")
flag.Parse()
}
func displayURL(url string) bool {
if (opts.filterURL != "") && !strings.Contains(url, opts.filterURL) {
return false
}
if (opts.excludeURL != "") && strings.Contains(url, opts.excludeURL) {
return false
}
return true
}
func index(w http.ResponseWriter, request *http.Request) {
fmt.Fprintf(w, "200")
if displayURL(request.URL.Path) {
dump, _ := httputil.DumpRequest(request, true)
fmt.Println(string(dump))
}
}
func handler() *http.ServeMux {
m := http.NewServeMux()
m.Handle("/", http.HandlerFunc(index))
return m
}
func rpHandler() *httputil.ReverseProxy {
director := func(request *http.Request) {
request.URL.Scheme = "http"
request.URL.Host = opts.proxyHost
if displayURL(request.URL.Path) {
dump, _ := httputil.DumpRequest(request, true)
fmt.Println(string(dump))
}
}
return &httputil.ReverseProxy{Director: director}
}
func main() {
setOptions()
var server http.Server
if opts.proxyHost == "" {
server = http.Server{
Addr: opts.addr(),
Handler: handler(),
}
} else {
server = http.Server{
Addr: opts.addr(),
Handler: rpHandler(),
}
}
fmt.Printf("server starting on port %s\n", opts.port)
if opts.proxyHost != "" {
fmt.Printf("proxy to %s\n", opts.proxyHost)
}
if err := server.ListenAndServe(); err != nil {
fmt.Println(err.Error())
}
}