comm

package
v0.0.0-...-66d7059 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 14, 2024 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultMaxRecvMsgSize = 1024 * 1024 * 100 // byte
	DefaultMaxSendMsgSize = 1024 * 1024 * 100 // byte
)

Variables

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

type ClientCredentials struct {
	TLSConfig *tls.Config
}

func (*ClientCredentials) ClientHandshake

func (cc *ClientCredentials) ClientHandshake(ctx context.Context, authority string, rawConn net.Conn) (net.Conn, credentials.AuthInfo, error)

func (*ClientCredentials) Clone

Clone 利用 TLS Config 创建一个 TransportCredentials。

func (*ClientCredentials) Info

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)

func NewGRPCServerFromListener

func NewGRPCServerFromListener(listener net.Listener, serverConfig ServerConfig) (*GRPCServer, error)

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

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 (*ServerCredentials) Info

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 (*ServerStatsHandler) TagRPC

type TLSConfig

type TLSConfig struct {
	// contains filtered or unexported fields
}

func NewTLSConfig

func NewTLSConfig(config *tls.Config) *TLSConfig

func (*TLSConfig) AddClientRootCA

func (tc *TLSConfig) AddClientRootCA(cert *x509.Certificate)

func (*TLSConfig) Config

func (tc *TLSConfig) Config() tls.Config

Config 返回 tls.Config。

func (*TLSConfig) SetClientCAs

func (tc *TLSConfig) SetClientCAs(certPool *x509.CertPool)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL