main.go 861 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package main
  2. import (
  3. "crypto/tls"
  4. "crypto/x509"
  5. "fmt"
  6. "io/ioutil"
  7. "log"
  8. "net/http"
  9. )
  10. func main() {
  11. // Load client cert
  12. cert, err := tls.LoadX509KeyPair("client.pem", "client-key.pem")
  13. if err != nil {
  14. log.Fatal(err)
  15. }
  16. // Load CA cert
  17. caCert, err := ioutil.ReadFile("../certsNkeys/ca.pem")
  18. if err != nil {
  19. log.Fatal(err)
  20. }
  21. caCertPool := x509.NewCertPool()
  22. caCertPool.AppendCertsFromPEM(caCert)
  23. // Setup HTTPS client
  24. tlsConfig := &tls.Config{
  25. Certificates: []tls.Certificate{cert},
  26. RootCAs: caCertPool,
  27. }
  28. tlsConfig.BuildNameToCertificate()
  29. transport := &http.Transport{TLSClientConfig: tlsConfig}
  30. client := &http.Client{Transport: transport}
  31. resp, err := client.Get("https://localhost:8080/hello")
  32. if err != nil {
  33. fmt.Println(err)
  34. }
  35. contents, err := ioutil.ReadAll(resp.Body)
  36. fmt.Printf("%s\n", string(contents))
  37. }