简体   繁体   English

Godaddy与GoLang的ssl证书

[英]godaddy's ssl certificate with GoLang

I'm wondering how to use godaddy's ssl certificate with GoLang Https server. 我想知道如何在GoLang Https服务器上使用godaddy的ssl证书。

currently I'm using the following code: 目前我正在使用以下代码:

srv := &http.Server{
    Addr: httpsPortStr,
    Handler: n,
    ReadTimeout: time.Duration(config.CfgIni.ReadTimeout) * time.Second,
    WriteTimeout: time.Duration(config.CfgIni.WriteTimeout) * time.Second,
}
err := srv.ListenAndServeTLS(<CERTIFICATE_FILE>,<PRIVATE_KEY_FILE>)

I still have sf_bundle-g2-g1.crt . 我还有sf_bundle-g2-g1.crt how do I add it to the chain of certificates ? 如何将其添加到证书链?

update 更新

@Vonc's answer is really helpful, i'm just missing one last thing. @Vonc的答案真的很有帮助,我只是遗漏了最后一件事。 I'm using http.Server instance in order to change ReadTimeout and WriteTimeout parameters. 我正在使用http.Server实例来更改ReadTimeout和WriteTimeout参数。 how can I do this with the tls ? 我怎么能用tls做到这一点?

My previous code for this: 我之前的代码:

srv := &http.Server{
    Addr: httpsPortStr,
    Handler: n,
    ReadTimeout: time.Duration(config.CfgIni.ReadTimeout) * time.Second,
    WriteTimeout: time.Duration(config.CfgIni.WriteTimeout) * time.Second,
}
err := srv.ListenAndServeTLS(config.CfgIni.CertificateFile,config.CfgIni.PrivateKeyFile)

thanks! 谢谢!

You can see a full example here in the file-server-go section. 您可以在file-server-go部分中看到完整的示例
The chain of certificate should be loaded in a x509.NewCertPool() 证书链应该加载到x509.NewCertPool()

cert, err := tls.LoadX509KeyPair("certs/server.pem", "certs/server.key")
if err != nil {
    log.Fatalf("server: loadkeys: %s", err)

}
certpool := x509.NewCertPool()
pem, err := ioutil.ReadFile("certs/ca.pem")
if err != nil {
    log.Fatalf("Failed to read client certificate authority: %v", err)
}
if !certpool.AppendCertsFromPEM(pem) {
    log.Fatalf("Can't parse client certificate authority")
}

config := tls.Config{
    Certificates: []tls.Certificate{cert},
    ClientAuth:   tls.RequireAndVerifyClientCert,
    ClientCAs:    certpool,
}
config.Rand = rand.Reader
service := "0.0.0.0:8000"
listener, err := tls.Listen("tcp", service, &config)

In your case, ioutil.ReadFile("sf_bundle-g2-g1.crt") . 在你的情况下, ioutil.ReadFile("sf_bundle-g2-g1.crt")

I'm using http.Server instance in order to change ReadTimeout and WriteTimeout parameters 我正在使用http.Server实例来更改ReadTimeout和WriteTimeout参数

server := &http.Server{
    Addr:      "0.0.0.0:8000",
    TLSConfig: tlsConfig,
    ReadTimeout: time.Duration // maximum duration before timing out read of the request,
    WriteTimeout: time.Duration // maximum duration before timing out write of the response
}

(replace time.Duration with the actual time) (用实际时间替换time.Duration

Then: 然后:

err := server.ListenAndServeTLS(certFile, keyFile string)
log.Fatal(err)

Note ListenAndServeTLS always returns a non-nil error. 注意ListenAndServeTLS始终返回非零错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM