Golang crypto/tls.Conn type and inner functions examples
package crypto/tls
A Conn represents a secured connection. It implements the net.Conn interface.
func (*Conn) Close usage example
config := tls.Config{ RootCAs: roots, ServerName: "mail.google.com", InsecureSkipVerify:true,}
config.Rand = rand.Reader
config.BuildNameToCertificate()
conn, err := tls.Dial("tcp", "mail.google.com:443", &config)
if err != nil {
panic("failed to connect: " + err.Error())
}
conn.Close()
func (*Conn) ConnectionState usage example
// populate tls.Config with dummy data
tlsConn := tls.Server(conn, &tls.Config{
RootCAs: roots,ServerName : "mail.google.com", InsecureSkipVerify: true,
})
// skip Handshake ... not with google mail server
connstate := tlsConn.ConnectionState()
func (*Conn) Handshake usage example
// Note : tlsConn.Read and tlsConn.Write functions will automatically
// initiate handshake protocol if not called
err = tlsConn.Handshake()
func (*Conn) LocalAddr usage example
fmt.Printf("Local Address : %s\n", tlsConn.LocalAddr().String())
func (*Conn) OCSPResponse usage example
fmt.Printf("OCSPResponse : %v\n", tlsConn.OCSPResponse())
func (*Conn) Read usage example
buffer := make([]byte, 1)
if _, err := tlsConn.Read(buffer); err != nil {
if err != io.EOF {
fmt.Printf("Failed to read byte: %s\n", err)
}
}
func (*Conn) RemoteAddr usage example
fmt.Printf("Remote Address : %s\n", tlsConn.RemoteAddr().String())
func (*Conn) SetDeadline usage example
// applicable to BOTH tls.Read and tls.Write functions
err = tlsConn.SetDeadline(time.Now().Add(500 * time.Millisecond))
if err != nil {
fmt.Println("Failed to set dead line for tls.Read and tls.Write", err)
}
func (*Conn) SetReadDeadline usage example
// only applicable to tls.Read function
err = tlsConn.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
if err != nil {
fmt.Println("Failed to set dead line for tls.Read", err)
}
func (*Conn) SetWriteDeadline usage example
// applicable to tls.Write function
err = tlsConn.SetWriteDeadline(time.Now().Add(500 * time.Millisecond))
if err != nil {
fmt.Println("Failed to set dead line for tls.Write", err)
}
func (*Conn) VerifyHostname usage example
err = tlsConn.VerifyHostname("localhost") // or tslConn.VerifyHostname(config.ServerName) ... read the official documentation ;-)
if err != nil {
fmt.Println(err)
}
func (*Conn) Write usage example
if _, err := tlsConn.Write([]byte("Hello World!\n")); err != nil {
fmt.Printf("Failed to write byte : %s\n", err)
}
Advertisement
Something interesting
Tutorials
+15.2k Golang : How to check if IP address is in range
+7.5k Golang : Rename part of filename
+22.9k Golang : Test file read write permission example
+17.2k Google Chrome : Your connection to website is encrypted with obsolete cryptography
+10.4k Golang : cannot assign type int to value (type uint8) in range error
+29.4k Golang : JQuery AJAX post data to server and send data back to client example
+15.3k Golang : Get query string value on a POST request
+13.1k Golang : Convert(cast) uintptr to string example
+17.2k Golang : Find file size(disk usage) with filepath.Walk
+22.2k Golang : Print leading(padding) zero or spaces in fmt.Printf?
+23.5k Golang : Get ASCII code from a key press(cross-platform) example