-
Notifications
You must be signed in to change notification settings - Fork 33
/
proxy.go
67 lines (55 loc) · 1.11 KB
/
proxy.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
package common
import (
"fmt"
"net/url"
"strings"
operatorProxy "github.com/operator-framework/operator-lib/proxy"
)
type ProxyInfo struct {
host string
port string
}
func (pi *ProxyInfo) Host() string {
return pi.host
}
func (pi *ProxyInfo) Port() string {
return pi.port
}
func NewProxyInfo() *ProxyInfo {
proxy := ProxyInfo{}
proxy.host, proxy.port = getProxyInfo()
return &proxy
}
func getProxyInfo() (string, string) {
if len(operatorProxy.ReadProxyVarsFromEnv()) > 0 {
p := operatorProxy.ReadProxyVarsFromEnv()
for _, v := range p {
switch v.Name {
case "HTTP_PROXY":
return proxyHostPort(v.Value)
case "HTTPS_PROXY":
return proxyHostPort(v.Value)
}
}
}
return "", ""
}
func proxyHostPort(proxy string) (string, string) {
url, _ := url.Parse(proxy)
s := url.Scheme
h := url.Host
u := url.User.String()
port := ""
if s != "" {
s = fmt.Sprintf("%s://", s)
}
if strings.Contains(url.Host, ":") {
host := strings.Split(url.Host, ":")
h = host[0]
port = host[1]
}
if u != "" {
u = fmt.Sprintf("%s@", url.User)
}
return fmt.Sprintf("%s%s%s", s, u, h), port
}