【问题标题】:Golang ListenAndServeTLS won't take my certificateGolang ListenAndServeTLS 不会接受我的证书
【发布时间】:2016-07-01 02:39:14
【问题描述】:

这真的难倒我...我只是想使用 Cobra 和标准 http 包设置一个基本服务器。我已经按照 golang 示例 here 了解如何创建证书和密钥,但无论我向它扔什么,我都无法通过并不断收到此错误:

无法在 http 端口上服务:crypto/tls:未能在证书输入中找到证书 PEM 数据,但确实找到了私钥; PEM 输入可能已切换

所以我相信我的程序一定有问题,不允许我处理证书。

这是我用来解析标志以启动服务器的:

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 ""
}

这是启动实际服务器的函数:

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)
        }
    }
}

我已经在我的机器上本地运行 Ubuntu 14.04 和 OS X 的 Docker 中得到了这个。提前感谢任何可以帮助我的人。这有点令人尴尬,因为这看起来应该是一件非常简单的事情,我敢打赌这会让我来到这里。

【问题讨论】:

    标签: ssl go https


    【解决方案1】:

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

    所以key 在参数列表中的第 3 位,cert 在第 4 位。

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

    现在您正在调用该函数,cert 位于 3. 位置,key 位于 4. 位置。

    也许交换它们会有所帮助。

    【讨论】:

    • 哇...我现在感觉非常愚蠢。就像我说的......可能是一些愚蠢的事情。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-24
    • 2012-02-21
    • 2019-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多