From a2115a3d3876b5216755a3adfecd93446ef8cd09 Mon Sep 17 00:00:00 2001 From: ginuerzh Date: Tue, 18 Apr 2023 20:46:44 +0800 Subject: [PATCH] add context for modules --- admission/admission.go | 8 +++++--- auth/auth.go | 8 +++++--- bypass/bypass.go | 8 +++++--- chain/resovle.go | 2 +- go.mod | 2 +- go.sum | 4 ++-- hosts/hosts.go | 3 ++- ingress/ingress.go | 4 +++- 8 files changed, 24 insertions(+), 15 deletions(-) diff --git a/admission/admission.go b/admission/admission.go index 5d29a6b..bfdcdac 100644 --- a/admission/admission.go +++ b/admission/admission.go @@ -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 } } diff --git a/auth/auth.go b/auth/auth.go index 50450f6..c0d4bf3 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -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 } } diff --git a/bypass/bypass.go b/bypass/bypass.go index f35fc30..8b37a11 100644 --- a/bypass/bypass.go +++ b/bypass/bypass.go @@ -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 } } diff --git a/chain/resovle.go b/chain/resovle.go index 2d23e2f..9064faa 100644 --- a/chain/resovle.go +++ b/chain/resovle.go @@ -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 } diff --git a/go.mod b/go.mod index 1d003f2..e421316 100644 --- a/go.mod +++ b/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 diff --git a/go.sum b/go.sum index beac707..ba21016 100644 --- a/go.sum +++ b/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= diff --git a/hosts/hosts.go b/hosts/hosts.go index 35590cb..2286d4b 100644 --- a/hosts/hosts.go +++ b/hosts/hosts.go @@ -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) } diff --git a/ingress/ingress.go b/ingress/ingress.go index f0f6c0a..67bef6f 100644 --- a/ingress/ingress.go +++ b/ingress/ingress.go @@ -1,5 +1,7 @@ package ingress +import "context" + type Ingress interface { - Get(host string) string + Get(ctx context.Context, host string) string }