mirror of
https://github.com/go-gost/core.git
synced 2026-10-19 16:16:17 +00:00
code clean
This commit is contained in:
@@ -8,22 +8,3 @@ type Option func(opts *Options)
|
||||
type Admission interface {
|
||||
Admit(ctx context.Context, addr string, opts ...Option) bool
|
||||
}
|
||||
|
||||
type admissionGroup struct {
|
||||
admissions []Admission
|
||||
}
|
||||
|
||||
func AdmissionGroup(admissions ...Admission) Admission {
|
||||
return &admissionGroup{
|
||||
admissions: admissions,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *admissionGroup) Admit(ctx context.Context, addr string, opts ...Option) bool {
|
||||
for _, admission := range p.admissions {
|
||||
if admission != nil && !admission.Admit(ctx, addr, opts...) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -10,29 +10,3 @@ type Option func(opts *Options)
|
||||
type Authenticator interface {
|
||||
Authenticate(ctx context.Context, user, password string, opts ...Option) (id string, ok bool)
|
||||
}
|
||||
|
||||
type authenticatorGroup struct {
|
||||
authers []Authenticator
|
||||
}
|
||||
|
||||
func AuthenticatorGroup(authers ...Authenticator) Authenticator {
|
||||
return &authenticatorGroup{
|
||||
authers: authers,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *authenticatorGroup) Authenticate(ctx context.Context, user, password string, opts ...Option) (string, bool) {
|
||||
if len(p.authers) == 0 {
|
||||
return "", false
|
||||
}
|
||||
for _, auther := range p.authers {
|
||||
if auther == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if id, ok := auther.Authenticate(ctx, user, password, opts...); ok {
|
||||
return id, ok
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package bypass
|
||||
|
||||
import (
|
||||
"context"
|
||||
"slices"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
@@ -30,45 +29,3 @@ type Bypass interface {
|
||||
IsWhitelist() bool
|
||||
Contains(ctx context.Context, network, addr string, opts ...Option) bool
|
||||
}
|
||||
|
||||
type bypassGroup struct {
|
||||
bypasses []Bypass
|
||||
}
|
||||
|
||||
func BypassGroup(bypasses ...Bypass) Bypass {
|
||||
return &bypassGroup{
|
||||
bypasses: bypasses,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *bypassGroup) Contains(ctx context.Context, network, addr string, opts ...Option) bool {
|
||||
var whitelist, blacklist []bool
|
||||
for _, bypass := range p.bypasses {
|
||||
result := bypass.Contains(ctx, network, addr, opts...)
|
||||
if bypass.IsWhitelist() {
|
||||
whitelist = append(whitelist, result)
|
||||
} else {
|
||||
blacklist = append(blacklist, result)
|
||||
}
|
||||
}
|
||||
status := false
|
||||
if len(whitelist) > 0 {
|
||||
if slices.Contains(whitelist, false) {
|
||||
status = false
|
||||
} else {
|
||||
status = true
|
||||
}
|
||||
}
|
||||
if !status && len(blacklist) > 0 {
|
||||
if slices.Contains(blacklist, true) {
|
||||
status = true
|
||||
} else {
|
||||
status = false
|
||||
}
|
||||
}
|
||||
return status
|
||||
}
|
||||
|
||||
func (p *bypassGroup) IsWhitelist() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user