add token for plugin

This commit is contained in:
ginuerzh 2023-04-20 19:05:59 +08:00
parent 32c8188351
commit 73b563f0c1
2 changed files with 21 additions and 2 deletions

View File

@ -110,8 +110,9 @@ type TLSConfig struct {
}
type PluginConfig struct {
Addr string `json:"addr"`
TLS *TLSConfig `json:"tls"`
Addr string `json:"addr"`
TLS *TLSConfig `yaml:",omitempty" json:"tls,omitempty"`
Token string `yaml:",omitempty" json:"token,omitempty"`
}
type AutherConfig struct {

View File

@ -1,6 +1,7 @@
package parsing
import (
"context"
"crypto/tls"
"net"
"net/url"
@ -691,5 +692,22 @@ func newPluginConn(cfg *config.PluginConfig) (*grpc.ClientConn, error) {
} else {
grpcOpts = append(grpcOpts, grpc.WithTransportCredentials(insecure.NewCredentials()))
}
if cfg.Token != "" {
grpcOpts = append(grpcOpts, grpc.WithPerRPCCredentials(&rpcCredentials{token: cfg.Token}))
}
return grpc.Dial(cfg.Addr, grpcOpts...)
}
type rpcCredentials struct {
token string
}
func (c *rpcCredentials) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
return map[string]string{
"token": c.token,
}, nil
}
func (c *rpcCredentials) RequireTransportSecurity() bool {
return false
}