add options for components

This commit is contained in:
ginuerzh 2023-10-26 22:20:10 +08:00
parent 33adbb9027
commit e5fa371586
21 changed files with 42 additions and 42 deletions

View File

@ -101,7 +101,7 @@ func NewAdmission(opts ...Option) admission.Admission {
return p
}
func (p *localAdmission) Admit(ctx context.Context, addr string) bool {
func (p *localAdmission) Admit(ctx context.Context, addr string, opts ...admission.Option) bool {
if addr == "" || p == nil {
return true
}

View File

@ -46,7 +46,7 @@ func NewGRPCPlugin(name string, addr string, opts ...plugin.Option) admission.Ad
return p
}
func (p *grpcPlugin) Admit(ctx context.Context, addr string) bool {
func (p *grpcPlugin) Admit(ctx context.Context, addr string, opts ...admission.Option) bool {
if p.client == nil {
return false
}
@ -102,7 +102,7 @@ func NewHTTPPlugin(name string, url string, opts ...plugin.Option) admission.Adm
}
}
func (p *httpPlugin) Admit(ctx context.Context, addr string) (ok bool) {
func (p *httpPlugin) Admit(ctx context.Context, addr string, opts ...admission.Option) (ok bool) {
if p.client == nil {
return
}

View File

@ -97,7 +97,7 @@ func NewAuthenticator(opts ...Option) auth.Authenticator {
}
// Authenticate checks the validity of the provided user-password pair.
func (p *authenticator) Authenticate(ctx context.Context, user, password string) (string, bool) {
func (p *authenticator) Authenticate(ctx context.Context, user, password string, opts ...auth.Option) (string, bool) {
if p == nil {
return "", true
}

View File

@ -49,7 +49,7 @@ func NewGRPCPlugin(name string, addr string, opts ...plugin.Option) auth.Authent
}
// Authenticate checks the validity of the provided user-password pair.
func (p *grpcPlugin) Authenticate(ctx context.Context, user, password string) (string, bool) {
func (p *grpcPlugin) Authenticate(ctx context.Context, user, password string, opts ...auth.Option) (string, bool) {
if p.client == nil {
return "", false
}
@ -110,7 +110,7 @@ func NewHTTPPlugin(name string, url string, opts ...plugin.Option) auth.Authenti
}
}
func (p *httpPlugin) Authenticate(ctx context.Context, user, password string) (id string, ok bool) {
func (p *httpPlugin) Authenticate(ctx context.Context, user, password string, opts ...auth.Option) (id string, ok bool) {
if p.client == nil {
return
}

View File

@ -104,7 +104,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(ctx context.Context, network, host string) (ips []net.IP, ok bool) {
func (h *hostMapper) Lookup(ctx context.Context, network, host string, opts ...hosts.Option) (ips []net.IP, ok bool) {
h.options.logger.Debugf("lookup %s/%s", host, network)
ips = h.lookup(host)
if ips == nil {

View File

@ -47,7 +47,7 @@ func NewGRPCPlugin(name string, addr string, opts ...plugin.Option) hosts.HostMa
return p
}
func (p *grpcPlugin) Lookup(ctx context.Context, network, host string) (ips []net.IP, ok bool) {
func (p *grpcPlugin) Lookup(ctx context.Context, network, host string, opts ...hosts.Option) (ips []net.IP, ok bool) {
p.log.Debugf("lookup %s/%s", host, network)
if p.client == nil {
@ -116,7 +116,7 @@ func NewHTTPPlugin(name string, url string, opts ...plugin.Option) hosts.HostMap
}
}
func (p *httpPlugin) Lookup(ctx context.Context, network, host string) (ips []net.IP, ok bool) {
func (p *httpPlugin) Lookup(ctx context.Context, network, host string, opts ...hosts.Option) (ips []net.IP, ok bool) {
p.log.Debugf("lookup %s/%s", host, network)
if p.client == nil {

View File

@ -219,7 +219,7 @@ func (ing *localIngress) parseRules(r io.Reader) (rules []Rule, err error) {
return
}
func (ing *localIngress) Get(ctx context.Context, host string) string {
func (ing *localIngress) Get(ctx context.Context, host string, opts ...ingress.GetOption) string {
if host == "" || ing == nil {
return ""
}
@ -259,7 +259,7 @@ func (ing *localIngress) Get(ctx context.Context, host string) string {
return ep
}
func (ing *localIngress) Set(ctx context.Context, host, endpoint string) {
func (ing *localIngress) Set(ctx context.Context, host, endpoint string, opts ...ingress.SetOption) {
}

View File

@ -46,7 +46,7 @@ func NewGRPCPlugin(name string, addr string, opts ...plugin.Option) ingress.Ingr
return p
}
func (p *grpcPlugin) Get(ctx context.Context, host string) string {
func (p *grpcPlugin) Get(ctx context.Context, host string, opts ...ingress.GetOption) string {
if p.client == nil {
return ""
}
@ -62,7 +62,7 @@ func (p *grpcPlugin) Get(ctx context.Context, host string) string {
return r.GetEndpoint()
}
func (p *grpcPlugin) Set(ctx context.Context, host, endpoint string) {
func (p *grpcPlugin) Set(ctx context.Context, host, endpoint string, opts ...ingress.SetOption) {
if p.client == nil {
return
}
@ -121,7 +121,7 @@ func NewHTTPPlugin(name string, url string, opts ...plugin.Option) ingress.Ingre
}
}
func (p *httpPlugin) Get(ctx context.Context, host string) (endpoint string) {
func (p *httpPlugin) Get(ctx context.Context, host string, opts ...ingress.GetOption) (endpoint string) {
if p.client == nil {
return
}
@ -160,7 +160,7 @@ func (p *httpPlugin) Get(ctx context.Context, host string) (endpoint string) {
return res.Endpoint
}
func (p *httpPlugin) Set(ctx context.Context, host, endpoint string) {
func (p *httpPlugin) Set(ctx context.Context, host, endpoint string, opts ...ingress.SetOption) {
if p.client == nil {
return
}

View File

@ -37,7 +37,7 @@ func FileRecorder(filename string, opts ...FileRecorderOption) recorder.Recorder
}
}
func (r *fileRecorder) Record(ctx context.Context, b []byte) error {
func (r *fileRecorder) Record(ctx context.Context, b []byte, opts ...recorder.RecordOption) error {
f, err := os.OpenFile(r.filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err

View File

@ -42,7 +42,7 @@ func HTTPRecorder(url string, opts ...HTTPRecorderOption) recorder.Recorder {
}
}
func (r *httpRecorder) Record(ctx context.Context, b []byte) error {
func (r *httpRecorder) Record(ctx context.Context, b []byte, opts ...recorder.RecordOption) error {
req, err := http.NewRequest(http.MethodPost, r.url, bytes.NewReader(b))
if err != nil {
return err

View File

@ -48,7 +48,7 @@ func NewGRPCPlugin(name string, addr string, opts ...plugin.Option) recorder.Rec
return p
}
func (p *grpcPlugin) Record(ctx context.Context, b []byte) error {
func (p *grpcPlugin) Record(ctx context.Context, b []byte, opts ...recorder.RecordOption) error {
if p.client == nil {
return nil
}
@ -98,13 +98,13 @@ func NewHTTPPlugin(name string, url string, opts ...plugin.Option) recorder.Reco
client: plugin.NewHTTPClient(&options),
header: options.Header,
log: logger.Default().WithFields(map[string]any{
"kind": "recorder",
"kind": "recorder",
"recorder": name,
}),
}
}
func (p *httpPlugin) Record(ctx context.Context, b []byte) error {
func (p *httpPlugin) Record(ctx context.Context, b []byte, opts ...recorder.RecordOption) error {
if len(b) == 0 || p.client == nil {
return nil
}

View File

@ -54,7 +54,7 @@ func RedisSetRecorder(addr string, opts ...RedisRecorderOption) recorder.Recorde
}
}
func (r *redisSetRecorder) Record(ctx context.Context, b []byte) error {
func (r *redisSetRecorder) Record(ctx context.Context, b []byte, opts ...recorder.RecordOption) error {
if r.key == "" {
return nil
}
@ -88,7 +88,7 @@ func RedisListRecorder(addr string, opts ...RedisRecorderOption) recorder.Record
}
}
func (r *redisListRecorder) Record(ctx context.Context, b []byte) error {
func (r *redisListRecorder) Record(ctx context.Context, b []byte, opts ...recorder.RecordOption) error {
if r.key == "" {
return nil
}
@ -122,7 +122,7 @@ func RedisSortedSetRecorder(addr string, opts ...RedisRecorderOption) recorder.R
}
}
func (r *redisSortedSetRecorder) Record(ctx context.Context, b []byte) error {
func (r *redisSortedSetRecorder) Record(ctx context.Context, b []byte, opts ...recorder.RecordOption) error {
if r.key == "" {
return nil
}

View File

@ -40,7 +40,7 @@ func TCPRecorder(addr string, opts ...TCPRecorderOption) recorder.Recorder {
}
}
func (r *tcpRecorder) Record(ctx context.Context, b []byte) error {
func (r *tcpRecorder) Record(ctx context.Context, b []byte, opts ...recorder.RecordOption) error {
c, err := r.dialer.DialContext(ctx, "tcp", r.addr)
if err != nil {
return err

View File

@ -30,10 +30,10 @@ type admissionWrapper struct {
r *admissionRegistry
}
func (w *admissionWrapper) Admit(ctx context.Context, addr string) bool {
func (w *admissionWrapper) Admit(ctx context.Context, addr string, opts ...admission.Option) bool {
p := w.r.get(w.name)
if p == nil {
return false
}
return p.Admit(ctx, addr)
return p.Admit(ctx, addr, opts...)
}

View File

@ -30,10 +30,10 @@ type autherWrapper struct {
r *autherRegistry
}
func (w *autherWrapper) Authenticate(ctx context.Context, user, password string) (string, bool) {
func (w *autherWrapper) Authenticate(ctx context.Context, user, password string, opts ...auth.Option) (string, bool) {
v := w.r.get(w.name)
if v == nil {
return "", true
}
return v.Authenticate(ctx, user, password)
return v.Authenticate(ctx, user, password, opts...)
}

View File

@ -31,10 +31,10 @@ type hostsWrapper struct {
r *hostsRegistry
}
func (w *hostsWrapper) Lookup(ctx context.Context, network, host string) ([]net.IP, bool) {
func (w *hostsWrapper) Lookup(ctx context.Context, network, host string, opts ...hosts.Option) ([]net.IP, bool) {
v := w.r.get(w.name)
if v == nil {
return nil, false
}
return v.Lookup(ctx, network, host)
return v.Lookup(ctx, network, host, opts...)
}

View File

@ -30,19 +30,19 @@ type ingressWrapper struct {
r *ingressRegistry
}
func (w *ingressWrapper) Get(ctx context.Context, host string) string {
func (w *ingressWrapper) Get(ctx context.Context, host string, opts ...ingress.GetOption) string {
v := w.r.get(w.name)
if v == nil {
return ""
}
return v.Get(ctx, host)
return v.Get(ctx, host, opts...)
}
func (w *ingressWrapper) Set(ctx context.Context, host, endpoint string) {
func (w *ingressWrapper) Set(ctx context.Context, host, endpoint string, opts ...ingress.SetOption) {
v := w.r.get(w.name)
if v == nil {
return
}
v.Set(ctx, host, endpoint)
v.Set(ctx, host, endpoint, opts...)
}

View File

@ -30,10 +30,10 @@ type recorderWrapper struct {
r *recorderRegistry
}
func (w *recorderWrapper) Record(ctx context.Context, b []byte) error {
func (w *recorderWrapper) Record(ctx context.Context, b []byte, opts ...recorder.RecordOption) error {
v := w.r.get(w.name)
if v == nil {
return nil
}
return v.Record(ctx, b)
return v.Record(ctx, b, opts...)
}

View File

@ -31,10 +31,10 @@ type resolverWrapper struct {
r *resolverRegistry
}
func (w *resolverWrapper) Resolve(ctx context.Context, network, host string) ([]net.IP, error) {
func (w *resolverWrapper) Resolve(ctx context.Context, network, host string, opts ...resolver.Option) ([]net.IP, error) {
r := w.r.get(w.name)
if r == nil {
return nil, resolver.ErrInvalid
}
return r.Resolve(ctx, network, host)
return r.Resolve(ctx, network, host, opts...)
}

View File

@ -13,8 +13,8 @@ import (
"github.com/go-gost/core/logger"
"github.com/go-gost/core/resolver"
"github.com/go-gost/plugin/resolver/proto"
auth_util "github.com/go-gost/x/internal/util/auth"
"github.com/go-gost/x/internal/plugin"
auth_util "github.com/go-gost/x/internal/util/auth"
"google.golang.org/grpc"
)
@ -49,7 +49,7 @@ func NewGRPCPlugin(name string, addr string, opts ...plugin.Option) (resolver.Re
return p, nil
}
func (p *grpcPlugin) Resolve(ctx context.Context, network, host string) (ips []net.IP, err error) {
func (p *grpcPlugin) Resolve(ctx context.Context, network, host string, opts ...resolver.Option) (ips []net.IP, err error) {
p.log.Debugf("resolve %s/%s", host, network)
if p.client == nil {
@ -117,7 +117,7 @@ func NewHTTPPlugin(name string, url string, opts ...plugin.Option) resolver.Reso
}
}
func (p *httpPlugin) Resolve(ctx context.Context, network, host string) (ips []net.IP, err error) {
func (p *httpPlugin) Resolve(ctx context.Context, network, host string, opts ...resolver.Option) (ips []net.IP, err error) {
p.log.Debugf("resolve %s/%s", host, network)
if p.client == nil {

View File

@ -91,7 +91,7 @@ func NewResolver(nameservers []NameServer, opts ...Option) (resolver.Resolver, e
}, nil
}
func (r *localResolver) Resolve(ctx context.Context, network, host string) (ips []net.IP, err error) {
func (r *localResolver) Resolve(ctx context.Context, network, host string, opts ...resolver.Option) (ips []net.IP, err error) {
if ip := net.ParseIP(host); ip != nil {
return []net.IP{ip}, nil
}