Documentation
¶
Index ¶
- Constants
- Variables
- type ClientConfig
- type ClientCredentials
- func (cc *ClientCredentials) ClientHandshake(ctx context.Context, authority string, rawConn net.Conn) (net.Conn, credentials.AuthInfo, error)
- func (cc *ClientCredentials) Clone() credentials.TransportCredentials
- func (cc *ClientCredentials) Info() credentials.ProtocolInfo
- func (cc *ClientCredentials) OverrideServerName(name string) error
- func (cc *ClientCredentials) ServerHandshake(rawConn net.Conn) (net.Conn, credentials.AuthInfo, error)
- type CredentialSupport
- type GRPCServer
- func (gServer *GRPCServer) Address() string
- func (gServer *GRPCServer) GetServerCertificate() tls.Certificate
- func (gServer *GRPCServer) Listener() net.Listener
- func (gServer *GRPCServer) RequireAndVerifyClientCert() bool
- func (gServer *GRPCServer) Server() *grpc.Server
- func (gServer *GRPCServer) SetClientRootCAs(clientRootCAs [][]byte) error
- func (gServer *GRPCServer) SetServerCertificate(cert tls.Certificate)
- func (gServer *GRPCServer) Start() error
- func (gServer *GRPCServer) Stop()
- func (gServer *GRPCServer) TLSEnabled() bool
- type KeepaliveOptions
- type SecureOptions
- type ServerConfig
- type ServerCredentials
- func (sc *ServerCredentials) ClientHandshake(context.Context, string, net.Conn) (net.Conn, credentials.AuthInfo, error)
- func (sc *ServerCredentials) Clone() credentials.TransportCredentials
- func (sc *ServerCredentials) Info() credentials.ProtocolInfo
- func (sc *ServerCredentials) OverrideServerName(string) error
- func (sc *ServerCredentials) ServerHandshake(rawConn net.Conn) (net.Conn, credentials.AuthInfo, error)
- type ServerStatsHandler
- func (ssh *ServerStatsHandler) HandleConn(ctx context.Context, s stats.ConnStats)
- func (ssh *ServerStatsHandler) HandleRPC(ctx context.Context, s stats.RPCStats)
- func (ssh *ServerStatsHandler) TagConn(ctx context.Context, info *stats.ConnTagInfo) context.Context
- func (ssh *ServerStatsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context
- type TLSConfig
Constants ¶
const ( DefaultMaxRecvMsgSize = 1024 * 1024 * 100 // byte DefaultMaxSendMsgSize = 1024 * 1024 * 100 // byte )
Variables ¶
var ( DefaultKeepaliveOptions = KeepaliveOptions{ ClientInterval: time.Duration(1) * time.Minute, ClientTimeout: time.Duration(20) * time.Second, ServerInterval: time.Duration(2) * time.Hour, ServerTimeout: time.Duration(20) * time.Second, ServerMinInterval: time.Duration(1) * time.Minute, } DefaultTLSCipherSuites = []uint16{ tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, tls.TLS_RSA_WITH_AES_128_GCM_SHA256, tls.TLS_RSA_WITH_AES_256_GCM_SHA384, } DefaultConnectionTimeout = 5 * time.Second )
Functions ¶
This section is empty.
Types ¶
type ClientConfig ¶
type ClientConfig struct { // SecOpts 客户端的 TLS 安全参数。 SecOpts SecureOptions // KaOpts 客户端侧的保持连接活跃性的选项参数。 KaOpts KeepaliveOptions // DialTimeout 客户端与服务端建立连接的等待超时时间。 DialTimeout time.Duration // AsyncConnect 以非阻塞的形式与服务端建立连接的选项。 AsyncConnect bool // MaxRecvMsgSize 设置客户端可以接收的最大消息大小(以字节为单位)。如果没有设置,使用默认的 100MB。 MaxRecvMsgSize int // MaxSendMsgSize 设置客户端可以发送的最大消息大小(以字节为单位)。如果没有设置,使用默认的 100MB。 MaxSendMsgSize int }
func (ClientConfig) DialOptions ¶
func (cc ClientConfig) DialOptions() ([]grpc.DialOption, error)
type ClientCredentials ¶
func (*ClientCredentials) ClientHandshake ¶
func (*ClientCredentials) Clone ¶
func (cc *ClientCredentials) Clone() credentials.TransportCredentials
Clone 利用 TLS Config 创建一个 TransportCredentials。
func (*ClientCredentials) Info ¶
func (cc *ClientCredentials) Info() credentials.ProtocolInfo
func (*ClientCredentials) OverrideServerName ¶
func (cc *ClientCredentials) OverrideServerName(name string) error
func (*ClientCredentials) ServerHandshake ¶
func (cc *ClientCredentials) ServerHandshake(rawConn net.Conn) (net.Conn, credentials.AuthInfo, error)
type CredentialSupport ¶
type CredentialSupport struct {
// contains filtered or unexported fields
}
func NewCredentialSupport ¶
func NewCredentialSupport(rootCAs ...[]byte) *CredentialSupport
func (*CredentialSupport) AppRootCAsByChain ¶
func (cs *CredentialSupport) AppRootCAsByChain() [][]byte
func (*CredentialSupport) GetClientCertificate ¶
func (cs *CredentialSupport) GetClientCertificate() tls.Certificate
func (*CredentialSupport) GetPeerCredentials ¶
func (cs *CredentialSupport) GetPeerCredentials() credentials.TransportCredentials
func (*CredentialSupport) SetClientCertificate ¶
func (cs *CredentialSupport) SetClientCertificate(cert tls.Certificate)
type GRPCServer ¶
type GRPCServer struct {
// contains filtered or unexported fields
}
func NewGRPCServer ¶
func NewGRPCServer(address string, serverConfig ServerConfig) (*GRPCServer, error)
NewGRPCServer 提供一个网络地址,例如 "127.0.0.1:",这样会随机选择一个可用的端口号,然后监听 127.0.0.1:port 地址, 等待外部连接。
func NewGRPCServerFromListener ¶
func NewGRPCServerFromListener(listener net.Listener, serverConfig ServerConfig) (*GRPCServer, error)
NewGRPCServerFromListener 创建一个安全的 gRPC 服务。
func (*GRPCServer) Address ¶
func (gServer *GRPCServer) Address() string
func (*GRPCServer) GetServerCertificate ¶
func (gServer *GRPCServer) GetServerCertificate() tls.Certificate
func (*GRPCServer) Listener ¶
func (gServer *GRPCServer) Listener() net.Listener
func (*GRPCServer) RequireAndVerifyClientCert ¶
func (gServer *GRPCServer) RequireAndVerifyClientCert() bool
func (*GRPCServer) Server ¶
func (gServer *GRPCServer) Server() *grpc.Server
func (*GRPCServer) SetClientRootCAs ¶
func (gServer *GRPCServer) SetClientRootCAs(clientRootCAs [][]byte) error
func (*GRPCServer) SetServerCertificate ¶
func (gServer *GRPCServer) SetServerCertificate(cert tls.Certificate)
func (*GRPCServer) Start ¶
func (gServer *GRPCServer) Start() error
func (*GRPCServer) Stop ¶
func (gServer *GRPCServer) Stop()
func (*GRPCServer) TLSEnabled ¶
func (gServer *GRPCServer) TLSEnabled() bool
type KeepaliveOptions ¶
type KeepaliveOptions struct { // ClientInterval 如果服务端在此时间段内没有发送消息给客户端,则客户端会给服务端发送一条 PING 消息,默认为 60 秒。 ClientInterval time.Duration // ClientTimeout 客户端在给服务端发送了 PING 消息后,会等待一段时间,如果在此时间内服务端没有响应, // 则客户端会断开与服务端的连接,默认为 20 秒。 ClientTimeout time.Duration // ServerInterval 如果客户端在此时间段内没有发送消息给服务端,则服务端会给客户端发送一条 PING 消息,默认为 2 小时。 ServerInterval time.Duration // ServerTimeout 服务端在给客户端发送了 PING 消息后,会等待一段时间,如果在此时间内客户端没有响应, // 则服务端会断开与客户端的连接,默认为 20 秒。 ServerTimeout time.Duration // ServerMinInterval 这是服务端设置的一个时间,如果客户端两次向服务端发送 PING 消息的时间间隔小于 // 此时间,则客户端 ping 的太频繁了,服务端会断开与客户端之间的连接,默认为 60 秒。 ServerMinInterval time.Duration }
func (KeepaliveOptions) ClientKeepaliveOptions ¶
func (ko KeepaliveOptions) ClientKeepaliveOptions() []grpc.DialOption
ClientKeepaliveOptions 提取 KeepaliveOptions 结构体消息中的 ClientInterval、ClientTimeout 三个字段构造客户端的 keepalive 配置项。
func (KeepaliveOptions) IsClientNil ¶
func (ko KeepaliveOptions) IsClientNil() bool
func (KeepaliveOptions) IsServerNil ¶
func (ko KeepaliveOptions) IsServerNil() bool
IsServerNil 判断 server 端的 keepalive 配置参数是否设置,如果都没设置,则返回 true,否则返回 false。
func (KeepaliveOptions) ServerKeepaliveOptions ¶
func (ko KeepaliveOptions) ServerKeepaliveOptions() []grpc.ServerOption
ServerKeepaliveOptions 提取 KeepaliveOptions 结构体消息中的 ServerInterval、ServerTimeout 和 ServerMinInterval 三个 字段构造服务端的 keepalive 配置项。
type SecureOptions ¶
type SecureOptions struct { // VerifyCertificate 如果该字段不为空,则会调用此字段定义的检验方法去核验证书的合法性。 VerifyCertificate func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error // Certificate 该字段存储着 PEM 格式编码的 x509 证书,用于 TLS 通信。 Certificate []byte // Key 此字段存储着 PEM 格式编码的私钥,用于 TLS 通信。 Key []byte // ServerRootCAs 该字段存储着服务端的根 CA 证书,内容格式为 PEM 格式编码,客户端利用此 // 字段核验服务端的身份。 ServerRootCAs [][]byte // ClientRootCAs 该字段存储着客户端的根 CA 证书,内容格式为 PEM 格式编码,服务端利用此 // 字段核验客户端的身份。 ClientRootCAs [][]byte // UseTLS 该字段用于指示通信连接是否采用 TLS 安全协议。 UseTLS bool // RequireClientCert 该字段用于指示在 TLS 通信中,客户端是否需要出示证书用于身份验证。 RequireClientCert bool // CipherSuites 该字段列出了 TLS 通信所支持的密码套件。 CipherSuites []uint16 // TimeShift 该字段用于在客户端与服务端之间进行时间同步。 TimeShift time.Duration // ServerNameOverride 用于验证返回证书中的主机名。它还包含在客户端握手中,以支持虚拟主机,除非它是一个 IP 地址。 ServerNameOverride string }
SecureOptions 定义了服务端和客户端的 TLS 安全参数。
func (SecureOptions) ClientTLSCertificate ¶
func (so SecureOptions) ClientTLSCertificate() (tls.Certificate, error)
func (SecureOptions) TLSConfig ¶
func (so SecureOptions) TLSConfig() (*tls.Config, error)
TLSConfig 将 SecureOptions 消息结构体转换为 *tls.Config,用到了 SecureOptions 结构体中的以下字段:
ServerNameOverride,string,一个字符串,用于验证返回证书中的主机名。
VerifyCertificate,func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error,一个函数,用来检验证书的合法性。
ServerRootCAs,[][]byte,客户端利用它核验服务端的身份。
RequireClientCert,bool,用于指示在 TLS 通信中,客户端是否需要出示证书用于身份验证。
如果 RequireClientCert 为 true
>> Key,[]byte,客户端的私钥,与 Certificate 一同构造客户端的 TLS 证书。
>> Certificate,[]byte,客户端的 x509 证书,与 Key 一同构造客户端的 TLS 证书。
TimeShift,time.Duration,用来保持客户端与服务端之间的时间同步。
type ServerConfig ¶
type ServerConfig struct { // ConnectionTimeout 定义了建立新连接的超时时间,默认为 5 秒。 ConnectionTimeout time.Duration // SecOpts 定义了 grpc 服务的安全参数。 SecOpts SecureOptions // KaOpts 定义了服务端与客户端之间连接保活的参数。 KaOpts KeepaliveOptions // StreamInterceptors 提供了若干个钩子来拦截流 RPC 在服务器上的执行。 StreamInterceptors []grpc.StreamServerInterceptor // UnaryInterceptors 提供了若干个钩子来拦截服务器上一元 RPC 的执行。 UnaryInterceptors []grpc.UnaryServerInterceptor // HealthCheckEnabled 开启对 grpc 服务的健康检查。 HealthCheckEnabled bool // ServerStatsHandler 用于检测报告 grpc 服务的状态。 ServerStatsHandler *ServerStatsHandler // MaxRecvMsgSize 设置服务端可以接收的最大消息大小(以字节为单位)。如果没有设置,使用默认的 100MB。 MaxRecvMsgSize int // MaxSendMsgSize 设置服务端可以发送的最大消息大小(以字节为单位)。如果没有设置,使用默认的 100MB。 MaxSendMsgSize int }
type ServerCredentials ¶
type ServerCredentials struct {
// contains filtered or unexported fields
}
func NewServerCredentials ¶
func NewServerCredentials(config *TLSConfig) *ServerCredentials
func (*ServerCredentials) ClientHandshake ¶
func (*ServerCredentials) Clone ¶
func (sc *ServerCredentials) Clone() credentials.TransportCredentials
func (*ServerCredentials) Info ¶
func (sc *ServerCredentials) Info() credentials.ProtocolInfo
func (*ServerCredentials) OverrideServerName ¶
func (sc *ServerCredentials) OverrideServerName(string) error
func (*ServerCredentials) ServerHandshake ¶
func (sc *ServerCredentials) ServerHandshake(rawConn net.Conn) (net.Conn, credentials.AuthInfo, error)
type ServerStatsHandler ¶
type ServerStatsHandler struct { RunningConnCounter metrics.Counter ClosedConnCounter metrics.Counter }
func (*ServerStatsHandler) HandleConn ¶
func (ssh *ServerStatsHandler) HandleConn(ctx context.Context, s stats.ConnStats)
func (*ServerStatsHandler) HandleRPC ¶
func (ssh *ServerStatsHandler) HandleRPC(ctx context.Context, s stats.RPCStats)
func (*ServerStatsHandler) TagConn ¶
func (ssh *ServerStatsHandler) TagConn(ctx context.Context, info *stats.ConnTagInfo) context.Context
func (*ServerStatsHandler) TagRPC ¶
func (ssh *ServerStatsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context
type TLSConfig ¶
type TLSConfig struct {
// contains filtered or unexported fields
}
func NewTLSConfig ¶
func (*TLSConfig) AddClientRootCA ¶
func (tc *TLSConfig) AddClientRootCA(cert *x509.Certificate)