-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresty-main.go
73 lines (63 loc) · 1.75 KB
/
resty-main.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 main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"github.com/go-resty/resty/v2"
"log"
"net/http"
"net/url"
"os"
"time"
)
func main() {
// Load CA certificate (used to validate the proxy's server certificate)
caCert, err := os.ReadFile("ca.crt")
if err != nil {
log.Fatalf("Failed to read CA certificate: %v", err)
}
caCertPool, err := x509.SystemCertPool()
if err != nil {
log.Fatalf("Failed to load system CA pool: %v", err)
}
if caCertPool == nil {
caCertPool = x509.NewCertPool()
}
if !caCertPool.AppendCertsFromPEM(caCert) {
log.Fatalf("Failed to append CA certificate")
}
// Load client certificate and key (used for client authentication)
clientCert, err := tls.LoadX509KeyPair("client.crt", "client.key")
if err != nil {
log.Fatalf("Failed to load client certificate and key: %v", err)
}
// Create a Resty client
client := resty.New()
// Set up the proxy
client.SetProxy("https://localhost:8080") // Use the same hostname as in ServerName
// Configure the transport to use custom TLS settings for the proxy
client.SetTransport(&http.Transport{
Proxy: http.ProxyURL(mustParseURL("https://localhost:8080")),
TLSClientConfig: &tls.Config{
RootCAs: caCertPool,
Certificates: []tls.Certificate{clientCert},
},
})
// Set timeout for requests
client.SetTimeout(10 * time.Second)
// Make HTTPS request through the proxy
resp, err := client.R().Get("https://ipv4.icanhazip.com")
if err != nil {
log.Fatalf("Failed to make request: %v", err)
}
// Print the response
fmt.Println(string(resp.Body()))
}
// mustParseURL parses a URL or panics if it fails.
func mustParseURL(rawURL string) *url.URL {
parsedURL, err := url.Parse(rawURL)
if err != nil {
log.Fatalf("Invalid URL: %v", err)
}
return parsedURL
}