add context for modules
This commit is contained in:
parent
f3482d7cd8
commit
a2115a3d38
@ -1,7 +1,9 @@
|
||||
package admission
|
||||
|
||||
import "context"
|
||||
|
||||
type Admission interface {
|
||||
Admit(addr string) bool
|
||||
Admit(ctx context.Context, addr string) bool
|
||||
}
|
||||
|
||||
type admissionGroup struct {
|
||||
@ -14,9 +16,9 @@ func AdmissionGroup(admissions ...Admission) Admission {
|
||||
}
|
||||
}
|
||||
|
||||
func (p *admissionGroup) Admit(addr string) bool {
|
||||
func (p *admissionGroup) Admit(ctx context.Context, addr string) bool {
|
||||
for _, admission := range p.admissions {
|
||||
if admission != nil && !admission.Admit(addr) {
|
||||
if admission != nil && !admission.Admit(ctx, addr) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,10 @@
|
||||
package auth
|
||||
|
||||
import "context"
|
||||
|
||||
// Authenticator is an interface for user authentication.
|
||||
type Authenticator interface {
|
||||
Authenticate(user, password string) bool
|
||||
Authenticate(ctx context.Context, user, password string) bool
|
||||
}
|
||||
|
||||
type authenticatorGroup struct {
|
||||
@ -15,12 +17,12 @@ func AuthenticatorGroup(authers ...Authenticator) Authenticator {
|
||||
}
|
||||
}
|
||||
|
||||
func (p *authenticatorGroup) Authenticate(user, password string) bool {
|
||||
func (p *authenticatorGroup) Authenticate(ctx context.Context, user, password string) bool {
|
||||
if len(p.authers) == 0 {
|
||||
return true
|
||||
}
|
||||
for _, auther := range p.authers {
|
||||
if auther != nil && auther.Authenticate(user, password) {
|
||||
if auther != nil && auther.Authenticate(ctx, user, password) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,11 @@
|
||||
package bypass
|
||||
|
||||
import "context"
|
||||
|
||||
// Bypass is a filter of address (IP or domain).
|
||||
type Bypass interface {
|
||||
// Contains reports whether the bypass includes addr.
|
||||
Contains(addr string) bool
|
||||
Contains(ctx context.Context, addr string) bool
|
||||
}
|
||||
|
||||
type bypassGroup struct {
|
||||
@ -16,9 +18,9 @@ func BypassGroup(bypasses ...Bypass) Bypass {
|
||||
}
|
||||
}
|
||||
|
||||
func (p *bypassGroup) Contains(addr string) bool {
|
||||
func (p *bypassGroup) Contains(ctx context.Context, addr string) bool {
|
||||
for _, bypass := range p.bypasses {
|
||||
if bypass != nil && bypass.Contains(addr) {
|
||||
if bypass != nil && bypass.Contains(ctx, addr) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ func Resolve(ctx context.Context, network, addr string, r resolver.Resolver, hos
|
||||
}
|
||||
|
||||
if hosts != nil {
|
||||
if ips, _ := hosts.Lookup(network, host); len(ips) > 0 {
|
||||
if ips, _ := hosts.Lookup(ctx, network, host); len(ips) > 0 {
|
||||
log.Debugf("hit host mapper: %s -> %s", host, ips)
|
||||
return net.JoinHostPort(ips[0].String(), port), nil
|
||||
}
|
||||
|
2
go.mod
2
go.mod
@ -2,4 +2,4 @@ module github.com/go-gost/core
|
||||
|
||||
go 1.18
|
||||
|
||||
require golang.org/x/sys v0.2.0
|
||||
require golang.org/x/sys v0.5.0
|
||||
|
4
go.sum
4
go.sum
@ -1,2 +1,2 @@
|
||||
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
|
||||
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
|
@ -1,10 +1,11 @@
|
||||
package hosts
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
)
|
||||
|
||||
// HostMapper is a mapping from hostname to IP.
|
||||
type HostMapper interface {
|
||||
Lookup(network, host string) ([]net.IP, bool)
|
||||
Lookup(ctx context.Context, network, host string) ([]net.IP, bool)
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package ingress
|
||||
|
||||
import "context"
|
||||
|
||||
type Ingress interface {
|
||||
Get(host string) string
|
||||
Get(ctx context.Context, host string) string
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user