add auther config

This commit is contained in:
ginuerzh
2022-02-12 00:33:20 +08:00
parent c1bf501734
commit a8a6bbc3a3
37 changed files with 261 additions and 183 deletions

62
pkg/registry/auther.go Normal file
View File

@ -0,0 +1,62 @@
package registry
import (
"sync"
"github.com/go-gost/gost/pkg/auth"
)
var (
autherReg = &autherRegistry{}
)
func Auther() *autherRegistry {
return autherReg
}
type autherRegistry struct {
m sync.Map
}
func (r *autherRegistry) Register(name string, auth auth.Authenticator) error {
if _, loaded := r.m.LoadOrStore(name, auth); loaded {
return ErrDup
}
return nil
}
func (r *autherRegistry) Unregister(name string) {
r.m.Delete(name)
}
func (r *autherRegistry) IsRegistered(name string) bool {
_, ok := r.m.Load(name)
return ok
}
func (r *autherRegistry) Get(name string) auth.Authenticator {
if name == "" {
return nil
}
return &autherWrapper{name: name}
}
func (r *autherRegistry) get(name string) auth.Authenticator {
if v, ok := r.m.Load(name); ok {
return v.(auth.Authenticator)
}
return nil
}
type autherWrapper struct {
name string
}
func (w *autherWrapper) Authenticate(user, password string) bool {
v := autherReg.get(w.name)
if v == nil {
return true
}
return v.Authenticate(user, password)
}

View File

@ -36,6 +36,9 @@ func (r *bypassRegistry) IsRegistered(name string) bool {
}
func (r *bypassRegistry) Get(name string) bypass.Bypass {
if name == "" {
return nil
}
return &bypassWrapper{name: name}
}

View File

@ -36,6 +36,9 @@ func (r *chainRegistry) IsRegistered(name string) bool {
}
func (r *chainRegistry) Get(name string) chain.Chainer {
if name == "" {
return nil
}
return &chainWrapper{name: name}
}

View File

@ -37,6 +37,9 @@ func (r *hostsRegistry) IsRegistered(name string) bool {
}
func (r *hostsRegistry) Get(name string) hosts.HostMapper {
if name == "" {
return nil
}
return &hostsWrapper{name: name}
}

View File

@ -38,6 +38,9 @@ func (r *resolverRegistry) IsRegistered(name string) bool {
}
func (r *resolverRegistry) Get(name string) resolver.Resolver {
if name == "" {
return nil
}
return &resolverWrapper{name: name}
}

View File

@ -36,6 +36,9 @@ func (r *serviceRegistry) IsRegistered(name string) bool {
}
func (r *serviceRegistry) Get(name string) *service.Service {
if name == "" {
return nil
}
v, ok := r.m.Load(name)
if !ok {
return nil