add plugin system
This commit is contained in:
@ -12,6 +12,7 @@ import (
|
||||
"github.com/go-gost/core/hosts"
|
||||
"github.com/go-gost/core/logger"
|
||||
"github.com/go-gost/x/internal/loader"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type Mapping 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
|
||||
@ -104,7 +112,7 @@ func NewHostMapper(opts ...Option) hosts.HostMapper {
|
||||
// Lookup searches the IP address corresponds to the given network and host from the host table.
|
||||
// The network should be 'ip', 'ip4' or 'ip6', default network is 'ip'.
|
||||
// the host should be a hostname (example.org) or a hostname with dot prefix (.example.org).
|
||||
func (h *hostMapper) Lookup(network, host string) (ips []net.IP, ok bool) {
|
||||
func (h *hostMapper) Lookup(ctx context.Context, network, host string) (ips []net.IP, ok bool) {
|
||||
h.options.logger.Debugf("lookup %s/%s", host, network)
|
||||
ips = h.lookup(host)
|
||||
if ips == nil {
|
||||
|
66
hosts/plugin.go
Normal file
66
hosts/plugin.go
Normal file
@ -0,0 +1,66 @@
|
||||
package hosts
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
|
||||
"github.com/go-gost/core/hosts"
|
||||
"github.com/go-gost/plugin/hosts/proto"
|
||||
xlogger "github.com/go-gost/x/logger"
|
||||
)
|
||||
|
||||
type pluginHostMapper struct {
|
||||
client proto.HostMapperClient
|
||||
options options
|
||||
}
|
||||
|
||||
// NewPluginHostMapper creates a plugin HostMapper.
|
||||
func NewPluginHostMapper(opts ...Option) hosts.HostMapper {
|
||||
var options options
|
||||
for _, opt := range opts {
|
||||
opt(&options)
|
||||
}
|
||||
if options.logger == nil {
|
||||
options.logger = xlogger.Nop()
|
||||
}
|
||||
|
||||
p := &pluginHostMapper{
|
||||
options: options,
|
||||
}
|
||||
if options.client != nil {
|
||||
p.client = proto.NewHostMapperClient(options.client)
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *pluginHostMapper) Lookup(ctx context.Context, network, host string) (ips []net.IP, ok bool) {
|
||||
p.options.logger.Debugf("lookup %s/%s", host, network)
|
||||
|
||||
if p.client == nil {
|
||||
return
|
||||
}
|
||||
|
||||
r, err := p.client.Lookup(ctx,
|
||||
&proto.LookupRequest{
|
||||
Network: network,
|
||||
Host: host,
|
||||
})
|
||||
if err != nil {
|
||||
p.options.logger.Error(err)
|
||||
return
|
||||
}
|
||||
for _, s := range r.Ips {
|
||||
if ip := net.ParseIP(s); ip != nil {
|
||||
ips = append(ips, ip)
|
||||
}
|
||||
}
|
||||
ok = r.Ok
|
||||
return
|
||||
}
|
||||
|
||||
func (p *pluginHostMapper) Close() error {
|
||||
if p.options.client != nil {
|
||||
return p.options.client.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user