diff options
Diffstat (limited to 'plugins/grpc')
-rw-r--r-- | plugins/grpc/codec.go | 5 | ||||
-rw-r--r-- | plugins/grpc/config.go | 8 | ||||
-rw-r--r-- | plugins/grpc/plugin.go | 8 | ||||
-rw-r--r-- | plugins/grpc/protoc_plugins/protoc-gen-php-grpc/main.go (renamed from plugins/grpc/protoc-gen-php-grpc/main.go) | 2 | ||||
-rw-r--r-- | plugins/grpc/protoc_plugins/protoc-gen-php-grpc/php/generate.go (renamed from plugins/grpc/protoc-gen-php-grpc/php/generate.go) | 0 | ||||
-rw-r--r-- | plugins/grpc/protoc_plugins/protoc-gen-php-grpc/php/keywords.go (renamed from plugins/grpc/protoc-gen-php-grpc/php/keywords.go) | 0 | ||||
-rw-r--r-- | plugins/grpc/protoc_plugins/protoc-gen-php-grpc/php/ns.go (renamed from plugins/grpc/protoc-gen-php-grpc/php/ns.go) | 0 | ||||
-rw-r--r-- | plugins/grpc/protoc_plugins/protoc-gen-php-grpc/php/template.go (renamed from plugins/grpc/protoc-gen-php-grpc/php/template.go) | 0 | ||||
-rw-r--r-- | plugins/grpc/server.go | 119 |
9 files changed, 141 insertions, 1 deletions
diff --git a/plugins/grpc/codec.go b/plugins/grpc/codec.go index dd299601..aeb373b9 100644 --- a/plugins/grpc/codec.go +++ b/plugins/grpc/codec.go @@ -4,6 +4,7 @@ import "google.golang.org/grpc/encoding" type rawMessage []byte +const cName string = "proto" const rm string = "rawMessage" func (r rawMessage) Reset() {} @@ -31,6 +32,10 @@ func (c *codec) Unmarshal(data []byte, v interface{}) error { return c.base.Unmarshal(data, v) } +func (c *codec) Name() string { + return cName +} + // String return codec name. func (c *codec) String() string { return "raw:" + c.base.Name() diff --git a/plugins/grpc/config.go b/plugins/grpc/config.go index 9a9f8c2c..53c50e22 100644 --- a/plugins/grpc/config.go +++ b/plugins/grpc/config.go @@ -32,3 +32,11 @@ type TLS struct { func (c *Config) InitDefaults() { } + +func (c *Config) EnableTLS() bool { + if c.TLS != nil { + return (c.TLS.RootCA != "" && c.TLS.Key != "" && c.TLS.Cert != "") || (c.TLS.Key != "" && c.TLS.Cert != "") + } + + return false +} diff --git a/plugins/grpc/plugin.go b/plugins/grpc/plugin.go index 4871a8a7..b424c5d0 100644 --- a/plugins/grpc/plugin.go +++ b/plugins/grpc/plugin.go @@ -4,6 +4,8 @@ import ( "github.com/spiral/errors" "github.com/spiral/roadrunner/v2/plugins/config" "github.com/spiral/roadrunner/v2/plugins/logger" + "google.golang.org/grpc" + "google.golang.org/grpc/encoding" ) const ( @@ -11,6 +13,9 @@ const ( ) type Plugin struct { + config *Config + opts []grpc.ServerOption + cfg config.Configurer log logger.Logger } @@ -18,6 +23,9 @@ type Plugin struct { func (p *Plugin) Init(cfg config.Configurer, log logger.Logger) error { const op = errors.Op("grpc_plugin_init") + // register the codec + encoding.RegisterCodec(&codec{}) + return nil } diff --git a/plugins/grpc/protoc-gen-php-grpc/main.go b/plugins/grpc/protoc_plugins/protoc-gen-php-grpc/main.go index c9c4a573..0894a7a8 100644 --- a/plugins/grpc/protoc-gen-php-grpc/main.go +++ b/plugins/grpc/protoc_plugins/protoc-gen-php-grpc/main.go @@ -27,7 +27,7 @@ import ( "io/ioutil" "os" - "github.com/spiral/roadrunner/v2/plugins/grpc/protoc-gen-php-grpc/php" + "github.com/spiral/roadrunner/v2/plugins/grpc/protoc_plugins/protoc-gen-php-grpc/php" "google.golang.org/protobuf/proto" plugin "google.golang.org/protobuf/types/pluginpb" ) diff --git a/plugins/grpc/protoc-gen-php-grpc/php/generate.go b/plugins/grpc/protoc_plugins/protoc-gen-php-grpc/php/generate.go index 03c48ac8..03c48ac8 100644 --- a/plugins/grpc/protoc-gen-php-grpc/php/generate.go +++ b/plugins/grpc/protoc_plugins/protoc-gen-php-grpc/php/generate.go diff --git a/plugins/grpc/protoc-gen-php-grpc/php/keywords.go b/plugins/grpc/protoc_plugins/protoc-gen-php-grpc/php/keywords.go index 32579e33..32579e33 100644 --- a/plugins/grpc/protoc-gen-php-grpc/php/keywords.go +++ b/plugins/grpc/protoc_plugins/protoc-gen-php-grpc/php/keywords.go diff --git a/plugins/grpc/protoc-gen-php-grpc/php/ns.go b/plugins/grpc/protoc_plugins/protoc-gen-php-grpc/php/ns.go index c1dc3898..c1dc3898 100644 --- a/plugins/grpc/protoc-gen-php-grpc/php/ns.go +++ b/plugins/grpc/protoc_plugins/protoc-gen-php-grpc/php/ns.go diff --git a/plugins/grpc/protoc-gen-php-grpc/php/template.go b/plugins/grpc/protoc_plugins/protoc-gen-php-grpc/php/template.go index e00c6fdd..e00c6fdd 100644 --- a/plugins/grpc/protoc-gen-php-grpc/php/template.go +++ b/plugins/grpc/protoc_plugins/protoc-gen-php-grpc/php/template.go diff --git a/plugins/grpc/server.go b/plugins/grpc/server.go new file mode 100644 index 00000000..53b0b2ae --- /dev/null +++ b/plugins/grpc/server.go @@ -0,0 +1,119 @@ +package grpc + +import ( + "context" + "crypto/tls" + "crypto/x509" + "os" + + "github.com/spiral/errors" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials" + "google.golang.org/grpc/keepalive" +) + +func (p *Plugin) createGRPCserver() (*grpc.Server, error) { + const op = errors.Op("grpc_plugin_create_server") + opts, err := p.serverOptions() + if err != nil { + return nil, errors.E(op, err) + } + + server := grpc.NewServer(opts...) + + /* + proto descriptors parser + */ + + return server, nil +} + +func (p *Plugin) interceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) { + // start := time.Now() + resp, err = handler(ctx, req) + + // svc.throw(EventUnaryCall, &UnaryCallEvent{ + // Info: info, + // Context: ctx, + // Error: err, + // start: start, + // elapsed: time.Since(start), + // }) + + return resp, err +} + +func (p *Plugin) serverOptions() ([]grpc.ServerOption, error) { + const op = errors.Op("grpc_plugin_server_options") + + var tcreds credentials.TransportCredentials + var opts []grpc.ServerOption + var cert tls.Certificate + var certPool *x509.CertPool + var rca []byte + var err error + + if p.config.EnableTLS() { + // if client CA is not empty we combine it with Cert and Key + if p.config.TLS.RootCA != "" { + cert, err = tls.LoadX509KeyPair(p.config.TLS.Cert, p.config.TLS.Key) + if err != nil { + return nil, err + } + + certPool, err = x509.SystemCertPool() + if err != nil { + return nil, err + } + if certPool == nil { + certPool = x509.NewCertPool() + } + + rca, err = os.ReadFile(p.config.TLS.RootCA) + if err != nil { + return nil, err + } + + if ok := certPool.AppendCertsFromPEM(rca); !ok { + return nil, errors.E(op, errors.Str("could not append Certs from PEM")) + } + + tcreds = credentials.NewTLS(&tls.Config{ + MinVersion: tls.VersionTLS12, + ClientAuth: tls.RequireAndVerifyClientCert, + Certificates: []tls.Certificate{cert}, + ClientCAs: certPool, + }) + } else { + tcreds, err = credentials.NewServerTLSFromFile(p.config.TLS.Cert, p.config.TLS.Key) + if err != nil { + return nil, err + } + } + + serverOptions := []grpc.ServerOption{ + grpc.MaxSendMsgSize(int(p.config.MaxSendMsgSize)), + grpc.MaxRecvMsgSize(int(p.config.MaxRecvMsgSize)), + grpc.KeepaliveParams(keepalive.ServerParameters{ + MaxConnectionIdle: p.config.MaxConnectionIdle, + MaxConnectionAge: p.config.MaxConnectionAge, + MaxConnectionAgeGrace: p.config.MaxConnectionAge, + Time: p.config.PingTime, + Timeout: p.config.Timeout, + }), + grpc.MaxConcurrentStreams(uint32(p.config.MaxConcurrentStreams)), + } + + opts = append(opts, grpc.Creds(tcreds)) + opts = append(opts, serverOptions...) + } + + opts = append(opts, p.opts...) + + // custom codec is required to bypass protobuf, common interceptor used for debug and stats + return append( + opts, + grpc.UnaryInterceptor(p.interceptor), + // grpc.CustomCodec(&codec{encoding.GetCodec(encCodec)}), + ), nil +} |