-
Notifications
You must be signed in to change notification settings - Fork 31
/
client.go
64 lines (54 loc) · 1.44 KB
/
client.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
package main
import (
"crypto/tls"
"crypto/x509"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
)
func main() {
name := flag.String("c", "a", "client name")
flag.Parse()
cert, err := ioutil.ReadFile("./certs/ca.crt")
if err != nil {
log.Fatalf("could not open certificate file: %v", err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(cert)
clientCert := fmt.Sprintf("./certs/client.%s.crt", *name)
clientKey := fmt.Sprintf("./certs/client.%s.key", *name)
log.Println("Load key pairs - ", clientCert, clientKey)
certificate, err := tls.LoadX509KeyPair(clientCert, clientKey)
if err != nil {
log.Fatalf("could not load certificate: %v", err)
}
client := http.Client{
Timeout: time.Minute * 3,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: caCertPool,
Certificates: []tls.Certificate{certificate},
},
},
}
// Request /hello over port 8443 via the GET method
// Using curl the verfiy it :
// curl --trace trace.log -k \
// --cacert ./ca.crt --cert ./client.b.crt --key ./client.b.key \
// https://localhost:8443/hello
r, err := client.Get("https://localhost:8443/hello")
if err != nil {
log.Fatalf("error making get request: %v", err)
}
// Read the response body
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatalf("error reading response: %v", err)
}
// Print the response body to stdout
fmt.Printf("%s\n", body)
}