-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathnet.go
More file actions
264 lines (227 loc) · 6.68 KB
/
Copy pathnet.go
File metadata and controls
264 lines (227 loc) · 6.68 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package utils
import (
"crypto/tls"
"fmt"
"log"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"golang.org/x/net/http/httpproxy"
)
// default timeout 30 seconds
var defaultTimeout = 30 * time.Second
type VjbNet struct {
Client *http.Client
timeout time.Duration
Insecure bool
HTTPProxy string
HTTPSProxy string
NoProxy string
UseProxyFromEnv bool
proxyCfg *httpproxy.Config
HTTPProxyUsername string
HTTPProxyPassword string
HTTPSProxyUsername string
HTTPSProxyPassword string
}
func withProxyCredentials(rawURL, username, password string) string {
if rawURL == "" || username == "" {
return rawURL
}
u, err := url.Parse(rawURL)
if err != nil || u.Host == "" {
u2, err2 := url.Parse("http://" + rawURL)
if err2 != nil || u2.Host == "" {
return rawURL
}
u = u2
}
u.User = url.UserPassword(username, password)
return u.String()
}
func (v *VjbNet) getNetTransport(tlsConfig *tls.Config) *http.Transport {
transport := &http.Transport{
TLSClientConfig: tlsConfig,
}
if v.Insecure {
transport.TLSClientConfig.InsecureSkipVerify = true
}
// Configure proxy behavior using httpproxy.Config, which correctly
// handles HTTP(S)_PROXY and NO_PROXY semantics including lists and
// domain / CIDR matching.
if v.UseProxyFromEnv || v.HTTPProxy != "" || v.HTTPSProxy != "" || v.NoProxy != "" {
if v.proxyCfg == nil {
v.proxyCfg = httpproxy.FromEnvironment()
}
if v.HTTPProxy != "" {
v.proxyCfg.HTTPProxy = v.HTTPProxy
}
if v.HTTPSProxy != "" {
v.proxyCfg.HTTPSProxy = v.HTTPSProxy
}
if v.NoProxy != "" {
v.proxyCfg.NoProxy = v.NoProxy
}
transport.Proxy = func(req *http.Request) (*url.URL, error) {
cfg := *v.proxyCfg
cfg.HTTPProxy = withProxyCredentials(cfg.HTTPProxy, v.HTTPProxyUsername, v.HTTPProxyPassword)
cfg.HTTPSProxy = withProxyCredentials(cfg.HTTPSProxy, v.HTTPSProxyUsername, v.HTTPSProxyPassword)
proxyURL, err := cfg.ProxyFunc()(req.URL)
if err != nil {
return nil, err
}
if proxyURL != nil {
fmt.Printf("Proxy config: HTTPProxy=%s, HTTPSProxy=%s, NoProxy=%s\n",
v.proxyCfg.HTTPProxy, v.proxyCfg.HTTPSProxy, v.proxyCfg.NoProxy)
fmt.Printf("VjbNet proxy config: HTTPProxy=%s, HTTPSProxy=%s, NoProxy=%s\n",
v.HTTPProxy, v.HTTPSProxy, v.NoProxy)
}
return proxyURL, nil
}
}
return transport
}
// CreateHTTPClient creates an HTTP client with TLS configuration and retry logic
func (v *VjbNet) CreateHTTPClient() error {
tlsConfig := &tls.Config{
MinVersion: tls.VersionTLS12,
}
if v.Insecure {
tlsConfig.InsecureSkipVerify = true
}
transport := v.getNetTransport(tlsConfig)
v.Client = &http.Client{
Transport: transport,
Timeout: v.timeout,
}
return nil
}
// CreateSecureHTTPClient creates a secure HTTP client with TLS configuration
func (v *VjbNet) CreateSecureHTTPClient() error {
tlsConfig := &tls.Config{
MinVersion: tls.VersionTLS12,
}
if v.Insecure {
tlsConfig.InsecureSkipVerify = true
}
transport := v.getNetTransport(tlsConfig)
v.Client = &http.Client{
Transport: transport,
Timeout: v.timeout,
}
return nil
}
func (v *VjbNet) SetTimeout(timeout time.Duration) {
v.timeout = timeout
}
func (v *VjbNet) SetInsecure(insecure bool) {
v.Insecure = insecure
}
func (v *VjbNet) SetHTTPProxy(proxy string) {
v.HTTPProxy = proxy
}
func (v *VjbNet) SetHTTPSProxy(proxy string) {
v.HTTPSProxy = proxy
}
func (v *VjbNet) SetNoProxy(noProxy string) {
v.NoProxy = noProxy
}
func (v *VjbNet) SetUseProxyFromEnv(use bool) {
v.UseProxyFromEnv = use
}
func (v *VjbNet) SetHTTPProxyCredentials(username, password string) {
v.HTTPProxyUsername = username
v.HTTPProxyPassword = password
}
func (v *VjbNet) SetHTTPSProxyCredentials(username, password string) {
v.HTTPSProxyUsername = username
v.HTTPSProxyPassword = password
}
func (v *VjbNet) GetClient() *http.Client {
return v.Client
}
func (v *VjbNet) proxy4URL(reqURL *url.URL) (*url.URL, error) {
if v.proxyCfg == nil {
v.proxyCfg = httpproxy.FromEnvironment()
}
//override the values with the ones set by the user
//else use the ones in the ENV by default.
if v.HTTPProxy != "" {
v.proxyCfg.HTTPProxy = v.HTTPProxy
}
if v.HTTPSProxy != "" {
v.proxyCfg.HTTPSProxy = v.HTTPSProxy
}
if v.NoProxy != "" {
v.proxyCfg.NoProxy = v.NoProxy
}
cfg := *v.proxyCfg
cfg.HTTPProxy = withProxyCredentials(cfg.HTTPProxy, v.HTTPProxyUsername, v.HTTPProxyPassword)
cfg.HTTPSProxy = withProxyCredentials(cfg.HTTPSProxy, v.HTTPSProxyUsername, v.HTTPSProxyPassword)
// Delegate proxy decision to httpproxy's ProxyFunc for correct
// NO_PROXY and scheme handling.
proxyURL, err := cfg.ProxyFunc()(reqURL)
if err != nil {
return nil, err
}
// Preserve historical behavior: for HTTPS requests, ensure the proxy URL
// uses the https scheme, even though httpproxy.ProxyFunc typically returns
// an http scheme (CONNECT over HTTP).
if proxyURL != nil && reqURL.Scheme == "https" && proxyURL.Scheme == "http" {
proxyURL.Scheme = "https"
}
if proxyURL != nil {
fmt.Printf("Proxy config: HTTPProxy=%s, HTTPSProxy=%s, NoProxy=%s\n",
v.proxyCfg.HTTPProxy, v.proxyCfg.HTTPSProxy, v.proxyCfg.NoProxy)
fmt.Printf("VjbNet proxy config: HTTPProxy=%s, HTTPSProxy=%s, NoProxy=%s\n",
v.HTTPProxy, v.HTTPSProxy, v.NoProxy)
}
return proxyURL, nil
}
func (v *VjbNet) IsProxyEnabled() bool {
return v.HTTPProxy != "" || v.HTTPSProxy != ""
}
func (v *VjbNet) GetTimeout() time.Duration {
return v.timeout
}
func NewVjbNet() *VjbNet {
timeout := defaultTimeout
if val := os.Getenv("HTTP_TIMEOUT_SECONDS"); val != "" {
if secs, err := strconv.Atoi(val); err != nil {
log.Printf("WARNING: invalid HTTP_TIMEOUT_SECONDS %q: %v; using default %v", val, err, defaultTimeout)
} else if secs <= 0 {
log.Printf("WARNING: HTTP_TIMEOUT_SECONDS must be positive, got %d; using default %v", secs, defaultTimeout)
} else {
timeout = time.Duration(secs) * time.Second
}
}
return &VjbNet{
timeout: timeout,
Client: &http.Client{},
Insecure: true,
HTTPProxy: "",
HTTPSProxy: "",
NoProxy: "",
UseProxyFromEnv: true,
proxyCfg: httpproxy.FromEnvironment(),
HTTPProxyUsername: os.Getenv("HTTP_PROXY_USERNAME"),
HTTPProxyPassword: os.Getenv("HTTP_PROXY_PASSWORD"),
HTTPSProxyUsername: os.Getenv("HTTPS_PROXY_USERNAME"),
HTTPSProxyPassword: os.Getenv("HTTPS_PROXY_PASSWORD"),
}
}
// NormalizeVCenterURL normalizes a vCenter host URL
func NormalizeVCenterURL(host string) (*url.URL, error) {
rawHost := strings.TrimSpace(host)
if !strings.Contains(rawHost, "://") {
rawHost = "https://" + rawHost
}
u, err := url.Parse(rawHost)
if err != nil {
return nil, fmt.Errorf("failed to parse URL: %w", err)
}
return u.JoinPath("sdk"), nil
}