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

@ -13,6 +13,7 @@ import (
"github.com/go-gost/core/logger"
"github.com/go-gost/x/internal/loader"
"github.com/go-gost/x/internal/matcher"
"google.golang.org/grpc"
)
type options struct {
@ -21,6 +22,7 @@ type options struct {
fileLoader loader.Loader
redisLoader loader.Loader
httpLoader loader.Loader
client *grpc.ClientConn
period time.Duration
logger logger.Logger
}
@ -63,6 +65,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
@ -232,7 +240,7 @@ func (bp *bypass) parsePatterns(r io.Reader) (patterns []string, err error) {
return
}
func (bp *bypass) Contains(addr string) bool {
func (bp *bypass) Contains(ctx context.Context, addr string) bool {
if addr == "" || bp == nil {
return false
}

56
bypass/plugin.go Normal file
View File

@ -0,0 +1,56 @@
package bypass
import (
"context"
bypass_pkg "github.com/go-gost/core/bypass"
"github.com/go-gost/plugin/bypass/proto"
xlogger "github.com/go-gost/x/logger"
)
type pluginBypass struct {
client proto.BypassClient
options options
}
// NewPluginBypass creates a plugin bypass.
func NewPluginBypass(opts ...Option) bypass_pkg.Bypass {
var options options
for _, opt := range opts {
opt(&options)
}
if options.logger == nil {
options.logger = xlogger.Nop()
}
p := &pluginBypass{
options: options,
}
if options.client != nil {
p.client = proto.NewBypassClient(options.client)
}
return p
}
func (p *pluginBypass) Contains(ctx context.Context, addr string) bool {
if p.client == nil {
return false
}
r, err := p.client.Bypass(ctx,
&proto.BypassRequest{
Addr: addr,
})
if err != nil {
p.options.logger.Error(err)
return false
}
return r.Ok
}
func (p *pluginBypass) Close() error {
if p.options.client != nil {
return p.options.client.Close()
}
return nil
}