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
package guestcontrolplane
import (
"crypto/tls"
"net"
"net/http"
"net/url"
"strings"
"time"
"github.com/johnandersen777/socialweb-computer-kcp/api/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/internal/component"
"github.com/johnandersen777/socialweb-computer-kcp/internal/kit/httpclient"
"github.com/johnandersen777/socialweb-computer-kcp/internal/kit/networkdial"
)
func networkHTTPClient(hosts component.NetworkHosts, network v1alpha1.MarketNetworkValue, trust *component.Trust, timeout time.Duration) *http.Client {
entries := []networkdial.Host{}
for _, host := range network.Hosts {
entries = append(entries, networkdial.Host{Name: host.Name, Address: host.Address})
}
if name, address := guestTLSHostAddress(network); name != "" && address != "" {
entries = append(entries, networkdial.Host{Name: name, Address: address})
}
var resolver networkdial.Resolver = hosts
if mapped, err := networkdial.NewHostMap(entries); err == nil && mapped.Len() > 0 {
resolver = mapped
}
tlsConfig := trust.Client(&tls.Config{MinVersion: tls.VersionTLS12})
return httpclient.External(httpclient.Options{Timeout: timeout, Hosts: resolver, TLS: tlsConfig})
}
func guestTLSHostAddress(network v1alpha1.MarketNetworkValue) (string, string) {
raw := strings.TrimSpace(network.GuestTLSURL())
if raw == "" {
return "", ""
}
parsed, err := url.Parse(raw)
if err != nil || parsed.Hostname() == "" {
return "", ""
}
port := parsed.Port()
if port == "" {
switch parsed.Scheme {
case "https":
port = "443"
case "http":
port = "80"
}
}
if port == "" {
return "", ""
}
for _, host := range network.Hosts {
if _, candidate, err := net.SplitHostPort(host.Address); err == nil && candidate == port {
return parsed.Hostname(), host.Address
}
}
return "", ""
}