update config parsing

This commit is contained in:
ginuerzh
2022-02-07 22:15:16 +08:00
parent 0983ecc52f
commit 1ec10ff7dd
23 changed files with 846 additions and 534 deletions

50
pkg/registry/bypass.go Normal file
View File

@ -0,0 +1,50 @@
package registry
import (
"sync"
"github.com/go-gost/gost/pkg/bypass"
)
var (
bypassReg = &bypassRegistry{}
)
func Bypass() *bypassRegistry {
return bypassReg
}
type bypassRegistry struct {
m sync.Map
}
func (r *bypassRegistry) Register(name string, bypass bypass.Bypass) error {
if _, loaded := r.m.LoadOrStore(name, bypass); loaded {
return ErrDup
}
return nil
}
func (r *bypassRegistry) Unregister(name string) {
r.m.Delete(name)
}
func (r *bypassRegistry) Get(name string) bypass.Bypass {
if _, ok := r.m.Load(name); !ok {
return nil
}
return &bypassWrapper{name: name}
}
type bypassWrapper struct {
name string
}
func (w *bypassWrapper) Contains(addr string) bool {
bp := bypassReg.Get(w.name)
if bp == nil {
return false
}
return bp.Contains(addr)
}

50
pkg/registry/chain.go Normal file
View File

@ -0,0 +1,50 @@
package registry
import (
"sync"
"github.com/go-gost/gost/pkg/chain"
)
var (
chainReg = &chainRegistry{}
)
func Chain() *chainRegistry {
return chainReg
}
type chainRegistry struct {
m sync.Map
}
func (r *chainRegistry) Register(name string, chain chain.Chainer) error {
if _, loaded := r.m.LoadOrStore(name, chain); loaded {
return ErrDup
}
return nil
}
func (r *chainRegistry) Unregister(name string) {
r.m.Delete(name)
}
func (r *chainRegistry) Get(name string) chain.Chainer {
if _, ok := r.m.Load(name); !ok {
return nil
}
return &chainWrapper{name: name}
}
type chainWrapper struct {
name string
}
func (w *chainWrapper) Route(network, address string) *chain.Route {
v := Chain().Get(w.name)
if v == nil {
return nil
}
return v.Route(network, address)
}

51
pkg/registry/hosts.go Normal file
View File

@ -0,0 +1,51 @@
package registry
import (
"net"
"sync"
"github.com/go-gost/gost/pkg/hosts"
)
var (
hostsReg = &hostsRegistry{}
)
func Hosts() *hostsRegistry {
return hostsReg
}
type hostsRegistry struct {
m sync.Map
}
func (r *hostsRegistry) Register(name string, hosts hosts.HostMapper) error {
if _, loaded := r.m.LoadOrStore(name, hosts); loaded {
return ErrDup
}
return nil
}
func (r *hostsRegistry) Unregister(name string) {
r.m.Delete(name)
}
func (r *hostsRegistry) Get(name string) hosts.HostMapper {
if _, ok := r.m.Load(name); !ok {
return nil
}
return &hostsWrapper{name: name}
}
type hostsWrapper struct {
name string
}
func (w *hostsWrapper) Lookup(network, host string) ([]net.IP, bool) {
v := Hosts().Get(w.name)
if v == nil {
return nil, false
}
return v.Lookup(network, host)
}

View File

@ -1,6 +1,8 @@
package registry
import (
"errors"
"github.com/go-gost/gost/pkg/connector"
"github.com/go-gost/gost/pkg/dialer"
"github.com/go-gost/gost/pkg/handler"
@ -8,6 +10,11 @@ import (
"github.com/go-gost/gost/pkg/logger"
)
var (
ErrDup = errors.New("registry: duplicate instance")
ErrNotFound = errors.New("registry: instance not found")
)
type NewListener func(opts ...listener.Option) listener.Listener
type NewHandler func(opts ...handler.Option) handler.Handler
type NewDialer func(opts ...dialer.Option) dialer.Dialer

52
pkg/registry/resolver.go Normal file
View File

@ -0,0 +1,52 @@
package registry
import (
"context"
"net"
"sync"
"github.com/go-gost/gost/pkg/resolver"
)
var (
resolverReg = &resolverRegistry{}
)
func Resolver() *resolverRegistry {
return resolverReg
}
type resolverRegistry struct {
m sync.Map
}
func (r *resolverRegistry) Register(name string, resolver resolver.Resolver) error {
if _, loaded := r.m.LoadOrStore(name, resolver); loaded {
return ErrDup
}
return nil
}
func (r *resolverRegistry) Unregister(name string) {
r.m.Delete(name)
}
func (r *resolverRegistry) Get(name string) resolver.Resolver {
if _, ok := r.m.Load(name); !ok {
return nil
}
return &resolverWrapper{name: name}
}
type resolverWrapper struct {
name string
}
func (w *resolverWrapper) Resolve(ctx context.Context, network, host string) ([]net.IP, error) {
r := Resolver().Get(w.name)
if r == nil {
return nil, ErrNotFound
}
return r.Resolve(ctx, network, host)
}

39
pkg/registry/service.go Normal file
View File

@ -0,0 +1,39 @@
package registry
import (
"sync"
"github.com/go-gost/gost/pkg/service"
)
var (
svcReg = &serviceRegistry{}
)
func Service() *serviceRegistry {
return svcReg
}
type serviceRegistry struct {
m sync.Map
}
func (r *serviceRegistry) Register(name string, svc *service.Service) error {
if _, loaded := r.m.LoadOrStore(name, svc); loaded {
return ErrDup
}
return nil
}
func (r *serviceRegistry) Unregister(name string) {
r.m.Delete(name)
}
func (r *serviceRegistry) Get(name string) *service.Service {
v, ok := r.m.Load(name)
if !ok {
return nil
}
return v.(*service.Service)
}