-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcreateClient.go
73 lines (58 loc) · 1.83 KB
/
createClient.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
package SecureClient
/*
Create new SSL-pinned client
- The dialerConfig contains all of the generated pins
- dialerConfig only needs to be made once, it should be shared and used on creation of new clients
*/
import (
"fmt"
"strings"
"errors"
"net/url"
"net/http"
)
//--- Wrapper for go's standard implementation of the http.RoundTripper interface
type SSLPinnerTransport struct{
DefaultTransport *http.Transport
BadPinDetected func(proxy string)
Proxy string
}
//--- Implementing http.RoundTripper
func (t *SSLPinnerTransport) RoundTrip(r *http.Request) (*http.Response, error) {
resp, err := t.DefaultTransport.RoundTrip(r)
if err != nil {
if strings.Contains(fmt.Sprintf("%v", err), "x509: certificate signed by unknown authority") {
t.BadPinDetected(t.Proxy)
return resp, errors.New("Security Error - Unsafe proxy")
}
}
return resp, err
}
func (p *SSLPinner) NewClient(proxy string) (*http.Client, error) {
client, defaultTransport := &http.Client{}, &http.Transport{}
if proxy != "" {
u, err := url.Parse(proxy)
if err != nil { return client, err }
defaultTransport = &http.Transport{
DialTLS: p.DialerConfig.NewDialer(),
Proxy: http.ProxyURL(u),
}
} else {
defaultTransport = &http.Transport{
DialTLS: p.DialerConfig.NewDialer(),
//--- No proxy
}
}
client.Transport = &SSLPinnerTransport{
//--- Standard go transport - If you have a custom transport, plug it in here
//--- uTLS, etc.
DefaultTransport: defaultTransport,
//--- Triggered when SSL pin is not matched, ie mitm proxy passes an invalid cert
BadPinDetected: p.BadPinDetected,
//--- Store proxy here for logging in BadPinDetected
//--- It would be easy to see if the proxy seems like a MITM proxy
//--- http://localhost:5555, http://localhost:8080 etc.
Proxy: proxy,
}
return client, nil
}