2020-01-22 13:02:06 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
2023-07-07 12:08:34 +00:00
|
|
|
"io/fs"
|
2020-11-08 18:28:33 +00:00
|
|
|
"net"
|
2023-03-11 11:27:30 +00:00
|
|
|
"os"
|
2021-09-17 23:39:15 +00:00
|
|
|
"strings"
|
2020-01-22 13:02:06 +00:00
|
|
|
|
2021-09-17 23:39:15 +00:00
|
|
|
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
|
|
|
"github.com/improbable-eng/grpc-web/go/grpcweb"
|
2021-09-30 14:56:51 +00:00
|
|
|
"github.com/rs/cors"
|
2020-01-22 13:02:06 +00:00
|
|
|
"google.golang.org/grpc"
|
2020-11-08 18:28:33 +00:00
|
|
|
|
2023-07-07 23:33:10 +00:00
|
|
|
"go.linka.cloud/grpc-toolkit/certs"
|
|
|
|
"go.linka.cloud/grpc-toolkit/interceptors"
|
|
|
|
"go.linka.cloud/grpc-toolkit/registry"
|
|
|
|
"go.linka.cloud/grpc-toolkit/transport"
|
|
|
|
"go.linka.cloud/grpc-toolkit/utils/addr"
|
2020-01-22 13:02:06 +00:00
|
|
|
)
|
|
|
|
|
2022-07-26 05:55:18 +00:00
|
|
|
var _ Options = (*options)(nil)
|
2020-01-22 13:02:06 +00:00
|
|
|
|
2021-09-17 23:39:15 +00:00
|
|
|
type RegisterGatewayFunc func(ctx context.Context, mux *runtime.ServeMux, cc grpc.ClientConnInterface) error
|
|
|
|
|
2020-01-22 13:02:06 +00:00
|
|
|
type Options interface {
|
|
|
|
Context() context.Context
|
|
|
|
Name() string
|
2020-11-08 18:28:33 +00:00
|
|
|
Version() string
|
2020-01-22 13:02:06 +00:00
|
|
|
Address() string
|
2021-09-17 23:39:15 +00:00
|
|
|
|
2020-08-07 08:59:27 +00:00
|
|
|
Reflection() bool
|
2022-03-10 11:46:36 +00:00
|
|
|
Health() bool
|
2021-09-17 23:39:15 +00:00
|
|
|
|
2020-01-22 13:02:06 +00:00
|
|
|
CACert() string
|
|
|
|
Cert() string
|
|
|
|
Key() string
|
2020-08-07 08:59:27 +00:00
|
|
|
TLSConfig() *tls.Config
|
2020-11-08 18:28:33 +00:00
|
|
|
Secure() bool
|
2021-09-17 23:39:15 +00:00
|
|
|
|
2020-11-08 18:28:33 +00:00
|
|
|
Registry() registry.Registry
|
2021-09-17 23:39:15 +00:00
|
|
|
|
2020-01-22 13:02:06 +00:00
|
|
|
BeforeStart() []func() error
|
|
|
|
AfterStart() []func() error
|
|
|
|
BeforeStop() []func() error
|
|
|
|
AfterStop() []func() error
|
2021-09-17 23:39:15 +00:00
|
|
|
|
2020-01-22 13:02:06 +00:00
|
|
|
ServerOpts() []grpc.ServerOption
|
|
|
|
ServerInterceptors() []grpc.UnaryServerInterceptor
|
|
|
|
StreamServerInterceptors() []grpc.StreamServerInterceptor
|
2021-09-17 23:39:15 +00:00
|
|
|
|
2020-01-22 13:02:06 +00:00
|
|
|
ClientInterceptors() []grpc.UnaryClientInterceptor
|
|
|
|
StreamClientInterceptors() []grpc.StreamClientInterceptor
|
2021-09-17 23:39:15 +00:00
|
|
|
|
2021-09-30 14:56:51 +00:00
|
|
|
Cors() cors.Options
|
2021-10-13 15:05:59 +00:00
|
|
|
Mux() ServeMux
|
2021-09-17 23:39:15 +00:00
|
|
|
GRPCWeb() bool
|
|
|
|
GRPCWebPrefix() string
|
|
|
|
GRPCWebOpts() []grpcweb.Option
|
|
|
|
|
|
|
|
Gateway() bool
|
|
|
|
GatewayPrefix() string
|
|
|
|
GatewayOpts() []runtime.ServeMuxOption
|
|
|
|
|
2021-09-30 14:56:51 +00:00
|
|
|
// TODO(adphi): metrics + tracing
|
2021-09-17 23:39:15 +00:00
|
|
|
|
2023-07-10 14:01:40 +00:00
|
|
|
WithoutCmux() bool
|
|
|
|
ProxyProtocol() bool
|
|
|
|
|
2021-09-17 23:39:15 +00:00
|
|
|
Default()
|
2020-01-22 13:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewOptions() *options {
|
|
|
|
return &options{
|
|
|
|
ctx: context.Background(),
|
|
|
|
address: ":0",
|
2022-03-10 11:46:36 +00:00
|
|
|
health: true,
|
2020-01-22 13:02:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-17 23:39:15 +00:00
|
|
|
func (o *options) Default() {
|
2020-01-22 13:02:06 +00:00
|
|
|
if o.ctx == nil {
|
|
|
|
o.ctx = context.Background()
|
|
|
|
}
|
|
|
|
if o.address == "" {
|
|
|
|
o.address = "0.0.0.0:0"
|
|
|
|
}
|
2021-09-17 23:39:15 +00:00
|
|
|
if o.transport == nil {
|
|
|
|
o.transport = &grpc.Server{}
|
|
|
|
}
|
2021-09-30 14:56:51 +00:00
|
|
|
|
2020-01-22 13:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Option func(*options)
|
|
|
|
|
|
|
|
func WithName(name string) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.name = name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-08 18:28:33 +00:00
|
|
|
func WithVersion(version string) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.version = version
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithRegistry(registry registry.Registry) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.registry = registry
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-07 12:02:31 +00:00
|
|
|
// WithContext specifies a context for the service.
|
2020-01-22 13:02:06 +00:00
|
|
|
// Can be used to signal shutdown of the service.
|
|
|
|
// Can be used for extra option values.
|
|
|
|
func WithContext(ctx context.Context) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.ctx = ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-07 12:02:31 +00:00
|
|
|
// WithAddress sets the address of the server
|
2020-01-22 13:02:06 +00:00
|
|
|
func WithAddress(addr string) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.address = addr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 13:32:33 +00:00
|
|
|
// WithListener specifies a listener for the service.
|
|
|
|
// It can be used to specify a custom listener.
|
|
|
|
// This will override the WithAddress and WithTLSConfig options
|
|
|
|
func WithListener(lis net.Listener) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.lis = lis
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-07 08:59:27 +00:00
|
|
|
func WithReflection(r bool) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.reflection = r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-10 11:46:36 +00:00
|
|
|
func WithHealth(h bool) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.health = h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-07 08:59:27 +00:00
|
|
|
func WithSecure(s bool) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.secure = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 13:02:06 +00:00
|
|
|
func WithGRPCServerOpts(opts ...grpc.ServerOption) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.serverOpts = append(o.serverOpts, opts...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithCACert(path string) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.caCert = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithCert(path string) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.cert = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithKey(path string) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.key = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithTLSConfig(conf *tls.Config) Option {
|
|
|
|
return func(o *options) {
|
2020-08-07 08:59:27 +00:00
|
|
|
o.tlsConfig = conf
|
2020-01-22 13:02:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithBeforeStart(fn ...func() error) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.beforeStart = append(o.beforeStart, fn...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithBeforeStop(fn ...func() error) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.beforeStop = append(o.beforeStop, fn...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithAfterStart(fn ...func() error) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.afterStart = append(o.afterStart, fn...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithAfterStop(fn ...func() error) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.afterStop = append(o.afterStop, fn...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-21 13:58:49 +00:00
|
|
|
func WithInterceptors(i ...interceptors.Interceptors) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
for _, v := range i {
|
|
|
|
o.unaryServerInterceptors = append(o.unaryServerInterceptors, v.UnaryServerInterceptor())
|
|
|
|
o.streamServerInterceptors = append(o.streamServerInterceptors, v.StreamServerInterceptor())
|
|
|
|
o.unaryClientInterceptors = append(o.unaryClientInterceptors, v.UnaryClientInterceptor())
|
|
|
|
o.streamClientInterceptors = append(o.streamClientInterceptors, v.StreamClientInterceptor())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithServerInterceptors(i ...interceptors.ServerInterceptors) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
for _, v := range i {
|
|
|
|
o.unaryServerInterceptors = append(o.unaryServerInterceptors, v.UnaryServerInterceptor())
|
|
|
|
o.streamServerInterceptors = append(o.streamServerInterceptors, v.StreamServerInterceptor())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithClientInterceptors(i ...interceptors.ClientInterceptors) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
for _, v := range i {
|
|
|
|
o.unaryClientInterceptors = append(o.unaryClientInterceptors, v.UnaryClientInterceptor())
|
|
|
|
o.streamClientInterceptors = append(o.streamClientInterceptors, v.StreamClientInterceptor())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 13:02:06 +00:00
|
|
|
func WithUnaryClientInterceptor(i ...grpc.UnaryClientInterceptor) Option {
|
|
|
|
return func(o *options) {
|
2021-11-21 13:58:49 +00:00
|
|
|
o.unaryClientInterceptors = append(o.unaryClientInterceptors, i...)
|
2020-01-22 13:02:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-07 12:02:31 +00:00
|
|
|
// WithUnaryServerInterceptor adds unary Wrapper interceptors to the options passed into the server
|
2020-01-22 13:02:06 +00:00
|
|
|
func WithUnaryServerInterceptor(i ...grpc.UnaryServerInterceptor) Option {
|
|
|
|
return func(o *options) {
|
2021-11-21 13:58:49 +00:00
|
|
|
o.unaryServerInterceptors = append(o.unaryServerInterceptors, i...)
|
2020-01-22 13:02:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithStreamServerInterceptor(i ...grpc.StreamServerInterceptor) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.streamServerInterceptors = append(o.streamServerInterceptors, i...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithStreamClientInterceptor(i ...grpc.StreamClientInterceptor) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.streamClientInterceptors = append(o.streamClientInterceptors, i...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-07 12:02:31 +00:00
|
|
|
// WithSubscriberInterceptor adds subscriber interceptors to the options passed into the server
|
2020-01-22 13:02:06 +00:00
|
|
|
func WithSubscriberInterceptor(w ...interface{}) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-30 14:56:51 +00:00
|
|
|
func WithCors(opts cors.Options) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.cors = opts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-13 15:05:59 +00:00
|
|
|
func WithMux(mux ServeMux) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.mux = mux
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithMiddlewares(m ...Middleware) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.middlewares = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-17 23:39:15 +00:00
|
|
|
func WithGRPCWeb(b bool) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.grpcWeb = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithGRPCWebPrefix(prefix string) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.grpcWebPrefix = strings.TrimSuffix(prefix, "/")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithGRPCWebOpts(opts ...grpcweb.Option) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.grpcWebOpts = opts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithGateway(fn RegisterGatewayFunc) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.gateway = fn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithGatewayPrefix(prefix string) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.gatewayPrefix = strings.TrimSuffix(prefix, "/")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithGatewayOpts(opts ...runtime.ServeMuxOption) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.gatewayOpts = opts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-03 11:12:19 +00:00
|
|
|
// WithReactUI add static single page app serving to the http server
|
|
|
|
// subpath is the path in the read-only file embed.FS to use as root to serve
|
|
|
|
// static content
|
2023-07-07 12:08:34 +00:00
|
|
|
func WithReactUI(fs fs.FS, subpath string) Option {
|
2022-06-03 11:12:19 +00:00
|
|
|
return func(o *options) {
|
|
|
|
o.reactUI = fs
|
|
|
|
o.reactUISubPath = subpath
|
|
|
|
o.hasReactUI = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-16 14:21:51 +00:00
|
|
|
// WithoutCmux disables the use of cmux for http support to instead use grpc.Server.ServeHTTP method when http support is enabled
|
|
|
|
func WithoutCmux() Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.withoutCmux = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-10 14:01:40 +00:00
|
|
|
func WithProxyProtocol(addrs ...string) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.proxyProtocol = true
|
|
|
|
o.proxyProtocolAddrs = addrs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 13:02:06 +00:00
|
|
|
type options struct {
|
2021-09-17 23:39:15 +00:00
|
|
|
ctx context.Context
|
|
|
|
name string
|
2020-11-08 18:28:33 +00:00
|
|
|
version string
|
2021-09-17 23:39:15 +00:00
|
|
|
address string
|
2022-09-05 13:32:33 +00:00
|
|
|
lis net.Listener
|
2021-09-17 23:39:15 +00:00
|
|
|
|
2020-08-07 08:59:27 +00:00
|
|
|
reflection bool
|
2022-03-10 11:46:36 +00:00
|
|
|
health bool
|
2020-01-22 13:02:06 +00:00
|
|
|
|
2021-09-17 23:39:15 +00:00
|
|
|
secure bool
|
|
|
|
caCert string
|
|
|
|
cert string
|
|
|
|
key string
|
|
|
|
tlsConfig *tls.Config
|
|
|
|
|
|
|
|
transport transport.Transport
|
|
|
|
registry registry.Registry
|
2020-11-08 18:28:33 +00:00
|
|
|
|
2020-01-22 13:02:06 +00:00
|
|
|
beforeStart []func() error
|
|
|
|
afterStart []func() error
|
|
|
|
beforeStop []func() error
|
|
|
|
afterStop []func() error
|
|
|
|
|
|
|
|
serverOpts []grpc.ServerOption
|
|
|
|
|
2021-11-21 13:58:49 +00:00
|
|
|
unaryServerInterceptors []grpc.UnaryServerInterceptor
|
2020-01-22 13:02:06 +00:00
|
|
|
streamServerInterceptors []grpc.StreamServerInterceptor
|
|
|
|
|
2021-11-21 13:58:49 +00:00
|
|
|
unaryClientInterceptors []grpc.UnaryClientInterceptor
|
2020-01-22 13:02:06 +00:00
|
|
|
streamClientInterceptors []grpc.StreamClientInterceptor
|
2020-03-03 13:54:53 +00:00
|
|
|
|
2021-10-13 15:05:59 +00:00
|
|
|
mux ServeMux
|
|
|
|
middlewares []Middleware
|
2021-09-17 23:39:15 +00:00
|
|
|
grpcWeb bool
|
|
|
|
grpcWebOpts []grpcweb.Option
|
|
|
|
grpcWebPrefix string
|
2021-10-13 15:05:59 +00:00
|
|
|
gateway RegisterGatewayFunc
|
|
|
|
gatewayOpts []runtime.ServeMuxOption
|
2021-09-30 14:56:51 +00:00
|
|
|
cors cors.Options
|
2021-09-17 23:39:15 +00:00
|
|
|
|
2023-07-07 12:08:34 +00:00
|
|
|
reactUI fs.FS
|
2022-06-03 11:12:19 +00:00
|
|
|
reactUISubPath string
|
|
|
|
hasReactUI bool
|
|
|
|
|
2023-07-10 14:01:40 +00:00
|
|
|
error error
|
|
|
|
gatewayPrefix string
|
|
|
|
withoutCmux bool
|
|
|
|
proxyProtocol bool
|
|
|
|
proxyProtocolAddrs []string
|
2020-01-22 13:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *options) Name() string {
|
|
|
|
return o.name
|
|
|
|
}
|
|
|
|
|
2020-11-08 18:28:33 +00:00
|
|
|
func (o *options) Version() string {
|
|
|
|
return o.version
|
|
|
|
}
|
|
|
|
|
2020-01-22 13:02:06 +00:00
|
|
|
func (o *options) Context() context.Context {
|
|
|
|
return o.ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *options) Address() string {
|
|
|
|
return o.address
|
|
|
|
}
|
|
|
|
|
2020-11-08 18:28:33 +00:00
|
|
|
func (o *options) Registry() registry.Registry {
|
|
|
|
return o.registry
|
2020-08-07 08:59:27 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 18:28:33 +00:00
|
|
|
func (o *options) Reflection() bool {
|
|
|
|
return o.reflection
|
2020-08-07 08:59:27 +00:00
|
|
|
}
|
|
|
|
|
2022-03-10 11:46:36 +00:00
|
|
|
func (o *options) Health() bool {
|
|
|
|
return o.health
|
|
|
|
}
|
|
|
|
|
2020-01-22 13:02:06 +00:00
|
|
|
func (o *options) CACert() string {
|
|
|
|
return o.caCert
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *options) Cert() string {
|
|
|
|
return o.cert
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *options) Key() string {
|
|
|
|
return o.key
|
|
|
|
}
|
|
|
|
|
2020-08-07 08:59:27 +00:00
|
|
|
func (o *options) TLSConfig() *tls.Config {
|
2020-01-22 13:02:06 +00:00
|
|
|
return o.tlsConfig
|
|
|
|
}
|
|
|
|
|
2020-11-08 18:28:33 +00:00
|
|
|
func (o *options) Secure() bool {
|
|
|
|
return o.secure
|
|
|
|
}
|
|
|
|
|
2020-01-22 13:02:06 +00:00
|
|
|
func (o *options) BeforeStart() []func() error {
|
|
|
|
return o.beforeStart
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *options) AfterStart() []func() error {
|
|
|
|
return o.afterStart
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *options) BeforeStop() []func() error {
|
|
|
|
return o.beforeStop
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *options) AfterStop() []func() error {
|
|
|
|
return o.afterStop
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *options) ServerOpts() []grpc.ServerOption {
|
|
|
|
return o.serverOpts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *options) ServerInterceptors() []grpc.UnaryServerInterceptor {
|
2021-11-21 13:58:49 +00:00
|
|
|
return o.unaryServerInterceptors
|
2020-01-22 13:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *options) StreamServerInterceptors() []grpc.StreamServerInterceptor {
|
|
|
|
return o.streamServerInterceptors
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *options) ClientInterceptors() []grpc.UnaryClientInterceptor {
|
2021-11-21 13:58:49 +00:00
|
|
|
return o.unaryClientInterceptors
|
2020-01-22 13:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *options) StreamClientInterceptors() []grpc.StreamClientInterceptor {
|
|
|
|
return o.streamClientInterceptors
|
|
|
|
}
|
|
|
|
|
2021-09-30 14:56:51 +00:00
|
|
|
func (o *options) Cors() cors.Options {
|
|
|
|
return o.cors
|
|
|
|
}
|
|
|
|
|
2021-10-13 15:05:59 +00:00
|
|
|
func (o *options) Mux() ServeMux {
|
|
|
|
return o.mux
|
|
|
|
}
|
|
|
|
|
2021-09-17 23:39:15 +00:00
|
|
|
func (o *options) GRPCWeb() bool {
|
|
|
|
return o.grpcWeb
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *options) GRPCWebPrefix() string {
|
|
|
|
return o.grpcWebPrefix
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *options) GRPCWebOpts() []grpcweb.Option {
|
|
|
|
return o.grpcWebOpts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *options) Gateway() bool {
|
|
|
|
return o.gateway != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *options) GatewayPrefix() string {
|
|
|
|
return o.gatewayPrefix
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *options) GatewayOpts() []runtime.ServeMuxOption {
|
|
|
|
return o.gatewayOpts
|
|
|
|
}
|
|
|
|
|
2023-09-16 14:21:51 +00:00
|
|
|
func (o *options) WithoutCmux() bool {
|
|
|
|
return o.withoutCmux
|
|
|
|
}
|
|
|
|
|
2023-07-10 14:01:40 +00:00
|
|
|
func (o *options) ProxyProtocol() bool {
|
|
|
|
return o.proxyProtocol
|
|
|
|
}
|
|
|
|
|
2020-01-22 13:02:06 +00:00
|
|
|
func (o *options) parseTLSConfig() error {
|
2020-11-08 18:28:33 +00:00
|
|
|
if o.tlsConfig != nil {
|
2020-08-07 08:59:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if !o.hasTLSConfig() {
|
|
|
|
if !o.secure {
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-08 18:28:33 +00:00
|
|
|
var hosts []string
|
|
|
|
if host, _, err := net.SplitHostPort(o.address); err == nil {
|
|
|
|
if len(host) == 0 {
|
|
|
|
hosts = addr.IPs()
|
|
|
|
} else {
|
|
|
|
hosts = []string{host}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i, h := range hosts {
|
|
|
|
a, err := addr.Extract(h)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hosts[i] = a
|
|
|
|
}
|
|
|
|
// generate a certificate
|
|
|
|
cert, err := certs.New(hosts...)
|
2020-08-07 08:59:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
o.tlsConfig = &tls.Config{
|
2020-11-08 18:28:33 +00:00
|
|
|
Certificates: []tls.Certificate{cert},
|
|
|
|
ClientAuth: tls.NoClientCert,
|
2020-08-07 08:59:27 +00:00
|
|
|
}
|
2020-01-22 13:02:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-03-11 11:27:30 +00:00
|
|
|
caCert, err := os.ReadFile(o.caCert)
|
2020-01-22 13:02:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
caCertPool := x509.NewCertPool()
|
|
|
|
ok := caCertPool.AppendCertsFromPEM(caCert)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("failed to load CA Cert from %s", o.caCert)
|
|
|
|
}
|
|
|
|
cert, err := tls.LoadX509KeyPair(o.cert, o.key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-07 08:59:27 +00:00
|
|
|
o.tlsConfig = &tls.Config{
|
2020-01-22 13:02:06 +00:00
|
|
|
Certificates: []tls.Certificate{cert},
|
2020-08-07 08:59:27 +00:00
|
|
|
RootCAs: caCertPool,
|
2020-01-22 13:02:06 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *options) hasTLSConfig() bool {
|
2020-08-07 08:59:27 +00:00
|
|
|
return o.caCert != "" && o.cert != "" && o.key != "" && o.tlsConfig == nil
|
2020-01-22 13:02:06 +00:00
|
|
|
}
|