1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
client.go 927 Bytes
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2022-06-01 16:22 . init
package tls
import (
"crypto/tls"
"crypto/x509"
"google.golang.org/grpc/credentials"
"io/ioutil"
)
type Client struct {
ServerName string
CaFile string
CertFile string
KeyFile string
}
func (t *Client) GetCredentialsByCA() (credentials.TransportCredentials, error) {
cert, err := tls.LoadX509KeyPair(t.CertFile, t.KeyFile)
if err != nil {
return nil, err
}
certPool := x509.NewCertPool()
ca, err := ioutil.ReadFile(t.CaFile)
if err != nil {
return nil, err
}
if ok := certPool.AppendCertsFromPEM(ca); !ok {
return nil, err
}
c := credentials.NewTLS(&tls.Config{
Certificates: []tls.Certificate{cert},
ServerName: t.ServerName,
RootCAs: certPool,
})
return c, err
}
func (t *Client) GetTLSCredentials() (credentials.TransportCredentials, error) {
c, err := credentials.NewClientTLSFromFile(t.CertFile, t.ServerName)
if err != nil {
return nil, err
}
return c, err
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.8.21

搜索帮助