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

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.

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

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)