doutan5844 2016-07-03 06:13
浏览 132
已采纳

GoDaddy的Godaddy的ssl证书

I'm wondering how to use godaddy's ssl certificate with GoLang Https server.

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. how do I add it to the chain of certificates ?

update

@Vonc's answer is really helpful, i'm just missing one last thing. I'm using http.Server instance in order to change ReadTimeout and WriteTimeout parameters. how can I do this with the 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!

  • 写回答

1条回答 默认 最新

  • douzhongju8780 2016-07-03 06:50
    关注

    You can see a full example here in the file-server-go section.
    The chain of certificate should be loaded in a 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").

    I'm using http.Server instance in order to change ReadTimeout and WriteTimeout parameters

    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)

    Then:

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

    Note ListenAndServeTLS always returns a non-nil error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?