duaner5714 2016-06-30 17:12
浏览 315
已采纳

Golang ListenAndServeTLS无法获得我的证书

This has really stumped me...I'm just trying to setup a basic server using Cobra and the standard http package. I've followed the golang example here for how to create a certificate and key, but no matter what I throw at it, I'm not getting it to go through and keep getting this error:

Cannot serve on http port: crypto/tls: failed to find certificate PEM data in certificate input, but did find a private key; PEM inputs may have been switched

so I am left with the belief that it must be something wrong with my program that won't let me process certificates.

This is what I use to parse my flags to start the server:

var serverCmd = &cobra.Command{
    Use:   "server",
    Short: "start a compiler server",
    Run: func(cmd *cobra.Command, args []string) {
        addrUnsecure := ""
        addrSecure := ""

        addrUnsecure += ":" + strconv.FormatUint(serverPort, 10)
        addrSecure += ":" + strconv.FormatUint(securePort, 10)

        if noSSL {
            addrSecure = ""
        } else {
            if secureOnly {
                addrUnsecure = ""
            }
            if _, err := os.Stat(serverKey); os.IsNotExist(err) {
                log.Error("Can't find ssl key %s. Use --no-ssl flag to disable", serverKey)
                os.Exit(1)
            }
            if _, err := os.Stat(serverCert); os.IsNotExist(err) {
                log.Error("Can't find ssl cert %s. Use --no-ssl flag to disable", serverCert)
                os.Exit(1)
            }
        }

        server.StartServer(addrUnsecure, addrSecure, serverCert, serverKey)
    },
}

func addServerFlags() {
    serverCmd.Flags().Uint64VarP(&serverPort, "port", "p", setServerPort(), "set the listening port for http")
    serverCmd.Flags().Uint64VarP(&securePort, "secure-port", "s", setSecurePort(), "set the listening port for https")
    serverCmd.Flags().BoolVarP(&noSSL, "no-ssl", "n", setSSL(), "use only http")
    serverCmd.Flags().BoolVarP(&secureOnly, "secure-only", "o", setSecureOnly(), "use only https")
    serverCmd.Flags().StringVarP(&serverCert, "cert", "c", setDefaultServerCert(), "set the https certificate")
    serverCmd.Flags().StringVarP(&serverKey, "key", "k", setDefaultServerKey(), "set the key to interact with the https certificate")
}

func setServerPort() uint64 {
    return 9099
}

func setSecurePort() uint64 {
    return 9098
}

func setSSL() bool {
    return false
}

func setSecureOnly() bool {
    return false
}

func setDefaultServerCert() string {
    return ""
}

func setDefaultServerKey() string {
    return ""
}

and this is the function where the actual server is started:

func StartServer(addrUnsecure, addrSecure, key, cert string) {
    log.Warn("Hello I'm the marmots' compilers server")
    common.InitErisDir()
    // Routes

    http.HandleFunc("/", CompileHandler)
    // Use SSL ?
    log.Debug(cert)
    if addrSecure != "" {
        log.Debug("Using HTTPS")
        log.WithField("=>", addrSecure).Debug("Listening on...")
        if err := http.ListenAndServeTLS(addrUnsecure, cert, key, nil); err != nil {
            log.Error("Cannot serve on http port: ", err)
            os.Exit(1)
        }
    }
    if addrUnsecure != "" {
        log.Debug("Using HTTP")
        log.WithField("=>", addrUnsecure).Debug("Listening on...")
        if err := http.ListenAndServe(addrUnsecure, nil); err != nil {
            log.Error("Cannot serve on http port: ", err)
            os.Exit(1)
        }
    }
}

I've gotten this in both Docker running Ubuntu 14.04 and OS X locally on my machine. Thanks in advance to whoever can help me out. It's kind of embarassing because this seems like it should be a really simple thing to do and I'm willing to bet it's going to be something dumb that's got me here.

  • 写回答

1条回答 默认 最新

  • dongwei7048 2016-06-30 17:18
    关注

    func StartServer(addrUnsecure, addrSecure, key, cert string) {

    so key is at 3. place, cert at 4. place in the parameter list.

    server.StartServer(addrUnsecure, addrSecure, serverCert, serverKey)

    Now you are calling that func with cert being at the 3. place and key at the 4. place.

    Maybe swapping them will help.

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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料