Golang 中连接 Ejabberd 和配置 TLS
时间:2024-02-06
浏览:359
作者:佚名
你在学习Golang相关的知识吗?本文《Golang 中连接 Ejabberd 和配置 TLS》,主要介绍的内容就涉及到,如果你想提升自己的开发能力,就不要错过这篇文章,大家要知道编程理论基础和实战操作都是不可或缺的哦!
我已经在 server1 上设置了 ejabberd 设置,我的 golang 代码在 server2 上运行,证书在 server1 上。现在我必须使用 tls 配置与 golang 中的 ejabberd 连接。
我的问题是我必须将证书放在 server1 或 server2 上的哪里?
错误:无法读取根 ca 证书文件:path/to/root_ca.crt:没有此类文件或目录
如果我没有在配置中设置 rootcas,我遇到的错误是:tls:第一个记录看起来不像 tls 握手
下面给出了我的代码
package main
import (
"crypto/tls"
"crypto/x509"
"io/ioutil"
"log"
"github.com/mattn/go-xmpp/xmpp"
)
func main() {
// Load the root CA certificate from a file
caCertFile := "path/to/root_ca.crt"
caCert, err := ioutil.ReadFile(caCertFile)
if err != nil {
log.Fatalf("Failed to read root CA certificate file: %v", err)
}
// Create a new certificate pool and add the root CA certificate
rootCAs := x509.NewCertPool()
if ok := rootCAs.AppendCertsFromPEM(caCert); !ok {
log.Fatalf("Failed to add root CA certificate to the pool")
}
options := xmpp.Options{
Host: "your_ejabberd_host",
User: "your_username",
Password: "your_password",
Debug: true,
NoTLS: false,
TLSConfig: &tls.Config{RootCAs: rootCAs},
}
conn, err := options.NewClient()
if err != nil {
log.Fatal("Failed to create XMPP client:", err)
}
// Send a sample message
err = conn.Send(xmpp.Chat{
Remote: "[email protected]",
Type: "chat",
Text: "Hello, this is a test message!",
})
if err != nil {
log.Fatal("Failed to send message:", err)
}
conn.Close()
return
}
正确答案
错误信息很明显。您应该将此文件从 ejabberd 服务器复制到客户端。
请参阅 the doc of the tls option of ejabberd,tls 方法现已弃用且不推荐。 ejabberd 服务器很可能配置为使用 starttls 加密方法。在这种情况下,xmpp.options 中的 notls 应设置为 true。
并且您应该在 tls.config 中指定 servername 或 insecureskipverify。否则,您将看到以下错误消息:
starttls handshake: tls: either servername or insecureskipverify must be specified in the tls.config
如果您的目的是让您的演示工作,您可以简单地设置
notls:true 和 insecureskipverify:true:
package main
import (
"crypto/tls"
"github.com/mattn/go-xmpp"
)
func main() {
options := xmpp.Options{
Host: "your_ejabberd_host",
User: "your_username",
Password: "your_password",
Debug: true,
NoTLS: true,
TLSConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
conn, err := options.NewClient()
if err != nil {
panic(err)
}
_, err = conn.Send(xmpp.Chat{
Remote: "[email protected]",
Type: "chat",
Text: "Hello, this is a test message!",
})
if err != nil {
panic(err)
}
conn.Close()
}
以上就是《Golang 中连接 Ejabberd 和配置 TLS》的详细内容,更多关于的资料请关注golang学习网公众号!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
站内热门
最新文章
热门专题
