/
main.go
156 lines (128 loc) · 3.76 KB
/
main.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
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
package main
import (
"crypto/tls"
"fmt"
"io"
"net"
"net/http"
"net/url"
"strings"
"time"
"golang.org/x/net/proxy"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
socks string
host string
followRedirect bool
quiet bool
log logger
)
var cmdRoot = &cobra.Command{
Use: "wassit target",
Short: "wassit is simple request proxy",
Long: `A fast and simple request http proxy
with easy to use configuration options
created by sbani in Go.`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("Missing target url")
}
// Test target url
targetURL, err := url.Parse(args[0])
if err != nil {
return errors.Wrap(err, "Error parsing url")
}
// If tor is specified
if t, terr := cmd.PersistentFlags().GetBool("tor"); terr == nil && t {
socks = "127.0.0.1:9150"
}
log = simpleLogger(quiet)
if !quiet {
fmt.Printf(`
__ __ _ _
\ \ / /_ _ ___ ___(_) |_
\ \ /\ / / _ / __/ __| | __|
\ V V / (_| \__ \__ \ | |_
\_/\_/ \__,_|___/___/_|\__|
Running on: %s
Proxying to: %s
`, host, targetURL)
}
if socks != "" {
log.Infof("Using socks proxy %s\n", socks)
}
err = RunServer(targetURL)
log.Errorf("Server stopped due to the following error:\n%s", err)
return nil
},
}
func init() {
cmdRoot.PersistentFlags().StringVarP(&host, "listen", "l", ":9001", "Host and port that the wassit server is listening to")
cmdRoot.PersistentFlags().StringVarP(&socks, "socks5", "s", "", "Use a socks5 socks for connections to the target")
cmdRoot.PersistentFlags().BoolP("tor", "t", false, "Enable tor socks5 proxy usage. Please don't forget to start tor")
cmdRoot.PersistentFlags().BoolVarP(&followRedirect, "follow-redirect", "f", false, "Follow the first redirect (if present) and proxies content")
cmdRoot.PersistentFlags().BoolVarP(&quiet, "quiet", "q", false, "Do not log output to sdtout. Silent mode")
}
func createHTTPTransport() (*http.Transport, error) {
tr := &http.Transport{
MaxIdleConns: 10,
IdleConnTimeout: 30 * time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
// Use socks proxy to tunnel traffic
if socks != "" {
dialer, err := proxy.SOCKS5("tcp", socks, nil, &net.Dialer{})
if err != nil {
return nil, errors.Wrap(err, "proxy transport")
}
tr.Dial = func(network, addr string) (net.Conn, error) {
return dialer.Dial(network, addr)
}
}
return tr, nil
}
func getRequest(method string, url string) (resp *http.Response, err error) {
req, err := http.NewRequest(method, url, nil)
req.Header.Set("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)")
if err != nil {
return nil, err
}
client := http.DefaultClient
t, tErr := createHTTPTransport()
if tErr != nil {
log.Critical(tErr.Error())
return
}
client.Transport = t
return client.Do(req)
}
// RunServer let's you start the server in foreground
func RunServer(target *url.URL) error {
targetString := strings.TrimRight(target.String(), "/")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
response, err := getRequest(r.Method, fmt.Sprintf("%s%s", targetString, r.RequestURI))
if err != nil {
return
}
// Handle redirects
if followRedirect && (response.StatusCode == 302 || response.StatusCode == 301) {
location := response.Header.Get("Location")
response, err = getRequest(r.Method, location)
if err != nil {
return
}
}
w.WriteHeader(response.StatusCode)
io.Copy(w, response.Body)
log.Infof("Request: %s %s - Response: %d", r.Method, r.URL, response.StatusCode)
})
return http.ListenAndServe(host, nil)
}
// main it baby
func main() {
if err := cmdRoot.Execute(); err != nil {
log.Critical(err.Error())
}
}