Http和golang
1、HTTPS/TLS 服务
Section titled “1、HTTPS/TLS 服务”证书可以自签证书,也可以从CA购买证书,或者使用Let’s Encrypt的免费证书。
package main
import ( "fmt" "log" "net/http")
func main() { http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "hello world") })
log.Fatal(http.ListenAndServeTLS(":8080", "./defaultCA.pem", "./defaultCA.key", nil))}1.1、创建自签名证书
Section titled “1.1、创建自签名证书”#生成服务端私钥openssl genrsa -out defaultCA.key 2048#基于私钥生成自签名(x509)证书(.pem|.crt)openssl req -x509 -new -nodes -key defaultCA.key -days 1024 -out defaultCA.pem1.2、Let’s Encrypt
Section titled “1.2、Let’s Encrypt”Let’s Encrypt是一个提供免费证书的非盈利机构,并且提供HTTP API来获取证书。
golang.org/x/crypto/acme/autocert包可以自动从Let’s Encrypt或者其他任何基于ACME CA获取证书。