From ff51aef51873666eaeffcd32fae82b9ae2dce08f Mon Sep 17 00:00:00 2001 From: ginuerzh Date: Tue, 23 Aug 2022 21:46:27 +0800 Subject: [PATCH] add some helper structs --- admission/admission.go | 19 +++++++++++++++++++ auth/auth.go | 22 ++++++++++++++++++++++ bypass/bypass.go | 19 +++++++++++++++++++ chain/resovle.go | 2 +- 4 files changed, 61 insertions(+), 1 deletion(-) diff --git a/admission/admission.go b/admission/admission.go index ab36f02..0187d2a 100644 --- a/admission/admission.go +++ b/admission/admission.go @@ -3,3 +3,22 @@ package admission type Admission interface { Admit(addr string) bool } + +type admissionList struct { + admissions []Admission +} + +func AdmissionList(admissions ...Admission) Admission { + return &admissionList{ + admissions: admissions, + } +} + +func (p *admissionList) Admit(addr string) bool { + for _, admission := range p.admissions { + if admission != nil && !admission.Admit(addr) { + return false + } + } + return true +} diff --git a/auth/auth.go b/auth/auth.go index 8becea0..7b88c35 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -4,3 +4,25 @@ package auth type Authenticator interface { Authenticate(user, password string) bool } + +type authenticatorList struct { + authers []Authenticator +} + +func AuthenticatorList(authers ...Authenticator) Authenticator { + return &authenticatorList{ + authers: authers, + } +} + +func (p *authenticatorList) Authenticate(user, password string) bool { + if len(p.authers) == 0 { + return true + } + for _, auther := range p.authers { + if auther != nil && auther.Authenticate(user, password) { + return true + } + } + return false +} diff --git a/bypass/bypass.go b/bypass/bypass.go index 698dbd7..babfd16 100644 --- a/bypass/bypass.go +++ b/bypass/bypass.go @@ -5,3 +5,22 @@ type Bypass interface { // Contains reports whether the bypass includes addr. Contains(addr string) bool } + +type bypassList struct { + bypasses []Bypass +} + +func BypassList(bypasses ...Bypass) Bypass { + return &bypassList{ + bypasses: bypasses, + } +} + +func (p *bypassList) Contains(addr string) bool { + for _, bypass := range p.bypasses { + if bypass != nil && bypass.Contains(addr) { + return true + } + } + return false +} diff --git a/chain/resovle.go b/chain/resovle.go index 3464302..60b8fd8 100644 --- a/chain/resovle.go +++ b/chain/resovle.go @@ -39,7 +39,7 @@ func resolve(ctx context.Context, network, addr string, r resolver.Resolver, hos log.Error(err) } if len(ips) == 0 { - return "", fmt.Errorf("resolver: domain %s does not exists", host) + return "", fmt.Errorf("resolver: domain %s does not exist", host) } return net.JoinHostPort(ips[0].String(), port), nil }