golang ssl连接服务器

package main
import (
        "crypto/tls"
        "fmt"
        "log"
)
func main() {
        cert, err := tls.LoadX509KeyPair("newcert.pem.crt", "key.pem.unencrypted")
        if err != nil {
                log.Fatalf("server: loadkeys: %s", err)
        }
        config := tls.Config{Certificates: []tls.Certificate{cert}, InsecureSkipVerify: true}
        conn, err := tls.Dial("tcp", "ngote.cnnic.cn:3121", &config)
        if err != nil {
                log.Fatalf("client: dial: %s", err)
        }
        defer conn.Close()
        log.Println("client: connected to: ", conn.RemoteAddr())
        state := conn.ConnectionState()
        for _, v := range state.PeerCertificates {
                fmt.Println(v.Subject)
        }
        log.Println("client: handshake: ", state.HandshakeComplete)
        log.Println("client: mutual: ", state.NegotiatedProtocolIsMutual)
}

如果你的KEY文件是有密码的可以用下面的命令把KEY转成无密码的文件
openssl rsa -in key.pem -out key.pem.unencrypted

这样就解决了密码的问题。

发表评论

电子邮件地址不会被公开。 必填项已用*标注