github.com/Psiphon-Labs/tls-tris@v0.0.0-20230824155421-58bf6d336a9a/_dev/tls_examples/ems_client.go (about)

     1  package main
     2  
     3  import (
     4  	"bufio"
     5  	"crypto/tls"
     6  	"flag"
     7  	"fmt"
     8  	"io"
     9  	"net/http"
    10  	"net/url"
    11  	"os"
    12  )
    13  
    14  var tlsVersionToName = map[string]uint16{
    15  	"tls10": tls.VersionTLS10,
    16  	"tls11": tls.VersionTLS11,
    17  	"tls12": tls.VersionTLS12,
    18  	"tls13": tls.VersionTLS13,
    19  }
    20  
    21  // Usage client args host:port
    22  func main() {
    23  	var version string
    24  	var addr string
    25  	var enableEMS bool
    26  	var resume bool
    27  	var config tls.Config
    28  	var cache tls.ClientSessionCache
    29  	cache = tls.NewLRUClientSessionCache(0)
    30  	flag.StringVar(&version, "version", "tls12", "Version of TLS to use")
    31  	flag.BoolVar(&enableEMS, "m", false, "Enable EMS")
    32  	flag.BoolVar(&resume, "r", false, "Attempt Resumption")
    33  	flag.Parse()
    34  	config.MinVersion = tlsVersionToName[version]
    35  	config.MaxVersion = tlsVersionToName[version]
    36  	config.InsecureSkipVerify = true
    37  	config.UseExtendedMasterSecret = !enableEMS
    38  	config.ClientSessionCache = cache
    39  	var iters int
    40  	if resume {
    41  		iters = 2
    42  	} else {
    43  		iters = 1
    44  	}
    45  	addr = flag.Arg(0)
    46  	for ; iters > 0; iters-- {
    47  		conn, err := tls.Dial("tcp", addr, &config)
    48  		if err != nil {
    49  			fmt.Println("Error %s", err)
    50  			os.Exit(1)
    51  		}
    52  		var req http.Request
    53  		var response *http.Response
    54  		req.Method = "GET"
    55  		req.URL, err = url.Parse("https://" + addr + "/")
    56  		if err != nil {
    57  			fmt.Println("Failed to parse url")
    58  			os.Exit(1)
    59  		}
    60  		req.Write(conn)
    61  		reader := bufio.NewReader(conn)
    62  		response, err = http.ReadResponse(reader, nil)
    63  		if err != nil {
    64  			fmt.Println("HTTP problem")
    65  			fmt.Println(err)
    66  			os.Exit(1)
    67  		}
    68  		io.Copy(os.Stdout, response.Body)
    69  		conn.Close()
    70  		if resume && iters == 2 {
    71  			fmt.Println("Attempting resumption")
    72  		}
    73  	}
    74  	os.Exit(0)
    75  }