package tcp_listen import ( "crypto/rand" "crypto/tls" "net" ) func Servertls(addr, crt, key string) (net.Listener, error) { cert, err := tls.LoadX509KeyPair(crt, key) if err != nil { return nil, err } config := tls.Config{Certificates: []tls.Certificate{cert}} config.Rand = rand.Reader listener, err := tls.Listen("tcp", addr, &config) if err != nil { return nil, err } return listener, nil }<pre name="code" class="html">import ( "crypto/tls" "net" ) func ClientTls(addr, crt, key string) (net.Conn, error) { cert, err := tls.LoadX509KeyPair(crt, key) if err != nil { return nil, err } config := tls.Config{Certificates: []tls.Certificate{cert}, InsecureSkipVerify: true} conn, err := tls.Dial("tcp", addr, &config) if err != nil { return nil, err } return conn, nil }
有疑问加站长微信联系(非本文作者)