forked from WireGuard/wireguard-go
-
Notifications
You must be signed in to change notification settings - Fork 224
/
default.go
193 lines (171 loc) · 5.36 KB
/
default.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package statute
import (
"context"
"crypto/tls"
"net"
"net/http"
"net/netip"
"time"
"github.com/quic-go/quic-go"
"github.com/quic-go/quic-go/http3"
)
var FinalOptions *ScannerOptions
func DefaultHTTPClientFunc(rawDialer TDialerFunc, tlsDialer TDialerFunc, quicDialer TQuicDialerFunc, targetAddr ...string) *http.Client {
var defaultDialer TDialerFunc
if rawDialer == nil {
defaultDialer = DefaultDialerFunc
} else {
defaultDialer = rawDialer
}
var defaultTLSDialer TDialerFunc
if rawDialer == nil {
defaultTLSDialer = DefaultTLSDialerFunc
} else {
defaultTLSDialer = tlsDialer
}
var defaultQuicDialer TQuicDialerFunc
if quicDialer == nil {
defaultQuicDialer = DefaultQuicDialerFunc
} else {
defaultQuicDialer = quicDialer
}
var transport http.RoundTripper
if FinalOptions.UseHTTP3 {
transport = &http3.RoundTripper{
DisableCompression: FinalOptions.DisableCompression,
Dial: func(ctx context.Context, addr string, tlsCfg *tls.Config, cfg *quic.Config) (quic.EarlyConnection, error) {
dest := addr
if len(targetAddr) > 0 {
dest = targetAddr[0]
}
return defaultQuicDialer(ctx, dest, tlsCfg, cfg)
},
}
} else {
trans := &http.Transport{
DialContext: defaultDialer,
DialTLSContext: defaultTLSDialer,
ForceAttemptHTTP2: FinalOptions.UseHTTP2,
DisableCompression: FinalOptions.DisableCompression,
MaxIdleConnsPerHost: -1,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: FinalOptions.InsecureSkipVerify,
ServerName: FinalOptions.Hostname,
},
}
transport = trans
}
return &http.Client{
Transport: transport,
Timeout: FinalOptions.ConnectionTimeout,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
}
func DefaultDialerFunc(ctx context.Context, network, addr string) (net.Conn, error) {
d := &net.Dialer{
Timeout: FinalOptions.ConnectionTimeout, // Connection timeout
// Add other custom settings as needed
}
return d.DialContext(ctx, network, addr)
}
func getServerName(address string) (string, error) {
host, _, err := net.SplitHostPort(address)
if err != nil {
return "", err // handle the error properly in your real application
}
return host, nil
}
func defaultTLSConfig(addr string) *tls.Config {
allowInsecure := false
sni, err := getServerName(addr)
if err != nil {
allowInsecure = true
}
if FinalOptions.Hostname != "" {
sni = FinalOptions.Hostname
}
alpnProtocols := []string{"http/1.1"}
// Add protocols based on flags
if FinalOptions.UseHTTP3 {
alpnProtocols = []string{"http/1.1"} // ALPN token for HTTP/3
}
if FinalOptions.UseHTTP2 {
alpnProtocols = []string{"h2", "http/1.1"} // ALPN token for HTTP/2
}
// Initiate a TLS handshake over the connection
return &tls.Config{
InsecureSkipVerify: allowInsecure || FinalOptions.InsecureSkipVerify,
ServerName: sni,
MinVersion: FinalOptions.TlsVersion,
MaxVersion: FinalOptions.TlsVersion,
NextProtos: alpnProtocols,
}
}
// DefaultTLSDialerFunc is a custom TLS dialer function
func DefaultTLSDialerFunc(ctx context.Context, network, addr string) (net.Conn, error) {
// Dial the raw connection using the default dialer
rawConn, err := DefaultDialerFunc(ctx, network, addr)
if err != nil {
return nil, err
}
// Ensure the raw connection is closed in case of an error after this point
defer func() {
if err != nil {
_ = rawConn.Close()
}
}()
// Prepare the TLS client connection
tlsClientConn := tls.Client(rawConn, defaultTLSConfig(addr))
// Perform the handshake with a timeout
err = tlsClientConn.SetDeadline(time.Now().Add(FinalOptions.HandshakeTimeout))
if err != nil {
return nil, err
}
err = tlsClientConn.Handshake()
if err != nil {
return nil, err // rawConn will be closed by the deferred function
}
// Reset the deadline for future I/O operations
err = tlsClientConn.SetDeadline(time.Time{})
if err != nil {
return nil, err
}
// Return the established TLS connection
// Cancel the deferred closure of rawConn since everything succeeded
err = nil
return tlsClientConn, nil
}
func DefaultQuicDialerFunc(ctx context.Context, addr string, _ *tls.Config, _ *quic.Config) (quic.EarlyConnection, error) {
quicConfig := &quic.Config{
MaxIdleTimeout: FinalOptions.ConnectionTimeout,
HandshakeIdleTimeout: FinalOptions.HandshakeTimeout,
}
return quic.DialAddrEarly(ctx, addr, defaultTLSConfig(addr), quicConfig)
}
func DefaultCFRanges() []netip.Prefix {
return []netip.Prefix{
netip.MustParsePrefix("103.21.244.0/22"),
netip.MustParsePrefix("103.22.200.0/22"),
netip.MustParsePrefix("103.31.4.0/22"),
netip.MustParsePrefix("104.16.0.0/12"),
netip.MustParsePrefix("108.162.192.0/18"),
netip.MustParsePrefix("131.0.72.0/22"),
netip.MustParsePrefix("141.101.64.0/18"),
netip.MustParsePrefix("162.158.0.0/15"),
netip.MustParsePrefix("172.64.0.0/13"),
netip.MustParsePrefix("173.245.48.0/20"),
netip.MustParsePrefix("188.114.96.0/20"),
netip.MustParsePrefix("190.93.240.0/20"),
netip.MustParsePrefix("197.234.240.0/22"),
netip.MustParsePrefix("198.41.128.0/17"),
netip.MustParsePrefix("2400:cb00::/32"),
netip.MustParsePrefix("2405:8100::/32"),
netip.MustParsePrefix("2405:b500::/32"),
netip.MustParsePrefix("2606:4700::/32"),
netip.MustParsePrefix("2803:f800::/32"),
netip.MustParsePrefix("2c0f:f248::/32"),
netip.MustParsePrefix("2a06:98c0::/29"),
}
}