add some helper structs
This commit is contained in:
@ -3,3 +3,22 @@ package admission
|
|||||||
type Admission interface {
|
type Admission interface {
|
||||||
Admit(addr string) bool
|
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
|
||||||
|
}
|
||||||
|
22
auth/auth.go
22
auth/auth.go
@ -4,3 +4,25 @@ package auth
|
|||||||
type Authenticator interface {
|
type Authenticator interface {
|
||||||
Authenticate(user, password string) bool
|
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
|
||||||
|
}
|
||||||
|
@ -5,3 +5,22 @@ type Bypass interface {
|
|||||||
// Contains reports whether the bypass includes addr.
|
// Contains reports whether the bypass includes addr.
|
||||||
Contains(addr string) bool
|
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
|
||||||
|
}
|
||||||
|
@ -39,7 +39,7 @@ func resolve(ctx context.Context, network, addr string, r resolver.Resolver, hos
|
|||||||
log.Error(err)
|
log.Error(err)
|
||||||
}
|
}
|
||||||
if len(ips) == 0 {
|
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
|
return net.JoinHostPort(ips[0].String(), port), nil
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user