add plugin system

This commit is contained in:
ginuerzh
2023-04-18 20:52:56 +08:00
parent 7576651a67
commit 32c8188351
48 changed files with 761 additions and 115 deletions

View File

@ -12,6 +12,7 @@ import (
ingress_pkg "github.com/go-gost/core/ingress"
"github.com/go-gost/core/logger"
"github.com/go-gost/x/internal/loader"
"google.golang.org/grpc"
)
type Rule struct {
@ -24,6 +25,7 @@ type options struct {
fileLoader loader.Loader
redisLoader loader.Loader
httpLoader loader.Loader
client *grpc.ClientConn
period time.Duration
logger logger.Logger
}
@ -60,6 +62,12 @@ func HTTPLoaderOption(httpLoader loader.Loader) Option {
}
}
func PluginConnOption(c *grpc.ClientConn) Option {
return func(opts *options) {
opts.client = c
}
}
func LoggerOption(logger logger.Logger) Option {
return func(opts *options) {
opts.logger = logger
@ -219,7 +227,7 @@ func (ing *ingress) parseRules(r io.Reader) (rules []Rule, err error) {
return
}
func (ing *ingress) Get(host string) string {
func (ing *ingress) Get(ctx context.Context, host string) string {
if host == "" || ing == nil {
return ""
}

56
ingress/plugin.go Normal file
View File

@ -0,0 +1,56 @@
package ingress
import (
"context"
ingress_pkg "github.com/go-gost/core/ingress"
"github.com/go-gost/plugin/ingress/proto"
xlogger "github.com/go-gost/x/logger"
)
type pluginIngress struct {
client proto.IngressClient
options options
}
// NewPluginIngress creates a plugin ingress.
func NewPluginIngress(opts ...Option) ingress_pkg.Ingress {
var options options
for _, opt := range opts {
opt(&options)
}
if options.logger == nil {
options.logger = xlogger.Nop()
}
p := &pluginIngress{
options: options,
}
if options.client != nil {
p.client = proto.NewIngressClient(options.client)
}
return p
}
func (p *pluginIngress) Get(ctx context.Context, host string) string {
if p.client == nil {
return ""
}
r, err := p.client.Get(ctx,
&proto.GetRequest{
Host: host,
})
if err != nil {
p.options.logger.Error(err)
return ""
}
return r.GetEndpoint()
}
func (p *pluginIngress) Close() error {
if p.options.client != nil {
return p.options.client.Close()
}
return nil
}