35 lines
654 B
Go
35 lines
654 B
Go
package mqtt
|
|
|
|
import (
|
|
"MQTTLogger/config"
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"os"
|
|
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func NewTLSConfig(logger *zap.Logger, config *config.Config) *tls.Config {
|
|
|
|
if config.CACert == "" {
|
|
return nil
|
|
}
|
|
|
|
certpool := x509.NewCertPool()
|
|
pemCerts, err := os.ReadFile(config.CACert)
|
|
if err == nil {
|
|
certpool.AppendCertsFromPEM(pemCerts)
|
|
} else {
|
|
logger.Fatal("error loading CA cert", zap.Error(err))
|
|
}
|
|
|
|
return &tls.Config{
|
|
RootCAs: certpool,
|
|
// We use the provided cert not the one server sends.
|
|
ClientAuth: tls.NoClientCert,
|
|
ClientCAs: nil,
|
|
InsecureSkipVerify: true, // I know
|
|
Certificates: nil,
|
|
}
|
|
}
|