add context for modules

This commit is contained in:
ginuerzh 2023-04-18 20:46:44 +08:00
parent f3482d7cd8
commit a2115a3d38
8 changed files with 24 additions and 15 deletions

View File

@ -1,7 +1,9 @@
package admission package admission
import "context"
type Admission interface { type Admission interface {
Admit(addr string) bool Admit(ctx context.Context, addr string) bool
} }
type admissionGroup struct { 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 { for _, admission := range p.admissions {
if admission != nil && !admission.Admit(addr) { if admission != nil && !admission.Admit(ctx, addr) {
return false return false
} }
} }

View File

@ -1,8 +1,10 @@
package auth package auth
import "context"
// Authenticator is an interface for user authentication. // Authenticator is an interface for user authentication.
type Authenticator interface { type Authenticator interface {
Authenticate(user, password string) bool Authenticate(ctx context.Context, user, password string) bool
} }
type authenticatorGroup struct { 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 { if len(p.authers) == 0 {
return true return true
} }
for _, auther := range p.authers { for _, auther := range p.authers {
if auther != nil && auther.Authenticate(user, password) { if auther != nil && auther.Authenticate(ctx, user, password) {
return true return true
} }
} }

View File

@ -1,9 +1,11 @@
package bypass package bypass
import "context"
// Bypass is a filter of address (IP or domain). // Bypass is a filter of address (IP or domain).
type Bypass interface { type Bypass interface {
// Contains reports whether the bypass includes addr. // Contains reports whether the bypass includes addr.
Contains(addr string) bool Contains(ctx context.Context, addr string) bool
} }
type bypassGroup struct { 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 { for _, bypass := range p.bypasses {
if bypass != nil && bypass.Contains(addr) { if bypass != nil && bypass.Contains(ctx, addr) {
return true return true
} }
} }

View File

@ -24,7 +24,7 @@ func Resolve(ctx context.Context, network, addr string, r resolver.Resolver, hos
} }
if hosts != nil { 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) log.Debugf("hit host mapper: %s -> %s", host, ips)
return net.JoinHostPort(ips[0].String(), port), nil return net.JoinHostPort(ips[0].String(), port), nil
} }

2
go.mod
View File

@ -2,4 +2,4 @@ module github.com/go-gost/core
go 1.18 go 1.18
require golang.org/x/sys v0.2.0 require golang.org/x/sys v0.5.0

4
go.sum
View File

@ -1,2 +1,2 @@
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A= golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

View File

@ -1,10 +1,11 @@
package hosts package hosts
import ( import (
"context"
"net" "net"
) )
// HostMapper is a mapping from hostname to IP. // HostMapper is a mapping from hostname to IP.
type HostMapper interface { type HostMapper interface {
Lookup(network, host string) ([]net.IP, bool) Lookup(ctx context.Context, network, host string) ([]net.IP, bool)
} }

View File

@ -1,5 +1,7 @@
package ingress package ingress
import "context"
type Ingress interface { type Ingress interface {
Get(host string) string Get(ctx context.Context, host string) string
} }