Skip to content

Instantly share code, notes, and snippets.

@bithavoc
Forked from michaljemala/tls-client.go
Last active September 22, 2015 14:22

Revisions

  1. @michaljemala michaljemala revised this gist May 28, 2014. 1 changed file with 17 additions and 4 deletions.
    21 changes: 17 additions & 4 deletions tls-client
    Original file line number Diff line number Diff line change
    @@ -2,16 +2,17 @@ package main

    import (
    "crypto/tls"
    "crypto/x509"
    "flag"
    "io/ioutil"
    "log"
    "net/http"
    "os"
    )

    var (
    certFile = flag.String("cert", "someCertFile", "A PEM eoncoded certificate file.")
    keyFile = flag.String("key", "someKeyFile", "A PEM encoded private key file.")
    caFile = flag.String("CA", "someCertCAFile", "A PEM eoncoded CA's certificate file.")
    )

    func main() {
    @@ -23,13 +24,25 @@ func main() {
    log.Fatal(err)
    }

    // Setup HTTP client
    tlsConfig := &tls.Config{Certificates: []tls.Certificate{cert}, InsecureSkipVerify: true}
    // Load CA cert
    caCert, err := ioutil.ReadFile(*caFile)
    if err != nil {
    log.Fatal(err)
    }
    caCertPool := x509.NewCertPool()
    caCertPool.AppendCertsFromPEM(caCert)

    // Setup HTTPS client
    tlsConfig := &tls.Config{
    Certificates: []tls.Certificate{cert},
    RootCAs: caCertPool,
    }
    tlsConfig.BuildNameToCertificate()
    transport := &http.Transport{TLSClientConfig: tlsConfig}
    client := &http.Client{Transport: transport}

    // Do GET something
    resp, err := client.Get("https://localhost:8443")
    resp, err := client.Get("https://goldportugal.local:8443")
    if err != nil {
    log.Fatal(err)
    }
  2. @michaljemala michaljemala created this gist May 26, 2014.
    44 changes: 44 additions & 0 deletions tls-client
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    package main

    import (
    "crypto/tls"
    "flag"
    "io/ioutil"
    "log"
    "net/http"
    "os"
    )

    var (
    certFile = flag.String("cert", "someCertFile", "A PEM eoncoded certificate file.")
    keyFile = flag.String("key", "someKeyFile", "A PEM encoded private key file.")
    )

    func main() {
    flag.Parse()

    // Load client cert
    cert, err := tls.LoadX509KeyPair(*certFile, *keyFile)
    if err != nil {
    log.Fatal(err)
    }

    // Setup HTTP client
    tlsConfig := &tls.Config{Certificates: []tls.Certificate{cert}, InsecureSkipVerify: true}
    transport := &http.Transport{TLSClientConfig: tlsConfig}
    client := &http.Client{Transport: transport}

    // Do GET something
    resp, err := client.Get("https://localhost:8443")
    if err != nil {
    log.Fatal(err)
    }
    defer resp.Body.Close()

    // Dump response
    data, err := ioutil.ReadAll(resp.Body)
    if err != nil {
    log.Fatal(err)
    }
    log.Println(string(data))
    }