This repository has been archived by the owner on Dec 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.go
127 lines (87 loc) · 2.55 KB
/
utils.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
/*
maryo/utils.go
contains some utilities for things like JSON
written by superwhiskers, licensed under gnu gplv3.
if you want a copy, go to http://www.gnu.org/licenses/
*/
package main
import (
// internals
"crypto/tls"
"crypto/x509"
"fmt"
"net"
"net/http"
"reflect"
// externals
"github.com/elazarl/goproxy"
)
// get the ip address of the machine
func getIP() string {
// dial a connection to another ip
conn, err := net.Dial("udp", "8.8.8.8:80")
// handle errors
if err != nil {
// show error message
fmt.Printf("[err]: error while connecting to another computer\n")
// show traceback
panic(err)
}
// close the connection once dobs
defer conn.Close()
// get the local address
localAddr := conn.LocalAddr().(*net.UDPAddr)
// return it
return localAddr.IP.String()
}
// setting CA in goproxy
func setCA(caCert, caKey []byte) error {
// get the keypair from cert and key data
goproxyCa, err := tls.X509KeyPair(caCert, caKey)
// handle error
if err != nil {
// return the error
return err
}
// again, handle errors
if goproxyCa.Leaf, err = x509.ParseCertificate(goproxyCa.Certificate[0]); err != nil {
// return the error
return err
}
// set the CA
goproxy.GoproxyCa = goproxyCa
// on connections, use it
goproxy.OkConnect = &goproxy.ConnectAction{Action: goproxy.ConnectAccept, TLSConfig: goproxy.TLSConfigFromCA(&goproxyCa)}
// on MITMed connections, use it
goproxy.MitmConnect = &goproxy.ConnectAction{Action: goproxy.ConnectMitm, TLSConfig: goproxy.TLSConfigFromCA(&goproxyCa)}
// on MITMed HTTP connections, use it
goproxy.HTTPMitmConnect = &goproxy.ConnectAction{Action: goproxy.ConnectHTTPMitm, TLSConfig: goproxy.TLSConfigFromCA(&goproxyCa)}
// on rejected connections, use it
goproxy.RejectConnect = &goproxy.ConnectAction{Action: goproxy.ConnectReject, TLSConfig: goproxy.TLSConfigFromCA(&goproxyCa)}
// then return nil since there were no errors
return nil
}
// function for zeroing something
// takes a pointer (&varible)
func erase(v interface{}) {
// get the pointer
p := reflect.ValueOf(v).Elem()
// zero it
p.Set(reflect.Zero(p.Type()))
}
// clone a request into a requestable request object
func cloneReq(request *http.Request) *http.Request {
// clone the request
newReq := &http.Request{
Method: request.Method,
URL: request.URL,
Proto: request.Proto,
ProtoMajor: request.ProtoMajor,
ProtoMinor: request.ProtoMinor,
Header: request.Header,
Body: request.Body,
Host: request.Host,
}
// return the cloned request
return newReq
}