-
-
Notifications
You must be signed in to change notification settings - Fork 258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: refactor dns #250
Open
LiquidTheDangerous
wants to merge
3
commits into
xvzc:main
Choose a base branch
from
LiquidTheDangerous:dns-refactor/main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
chore: refactor dns #250
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package resolver | ||
|
||
import ( | ||
"context" | ||
"net" | ||
) | ||
|
||
type Factory interface { | ||
Get(ctx context.Context, host string, qTypes []uint16) (Resolver, error) | ||
} | ||
|
||
type FactoryFunc func(ctx context.Context, host string, qTypes []uint16) (Resolver, error) | ||
|
||
func (f FactoryFunc) Get(ctx context.Context, host string, qTypes []uint16) (Resolver, error) { | ||
return f(ctx, host, qTypes) | ||
} | ||
|
||
type FactoryResolver struct { | ||
f Factory | ||
} | ||
|
||
func NewFactoryResolver(f Factory) *FactoryResolver { | ||
return &FactoryResolver{f} | ||
} | ||
|
||
func (c *FactoryResolver) Resolve(ctx context.Context, host string, qTypes []uint16) ([]net.IPAddr, error) { | ||
resolver, err := c.f.Get(ctx, host, qTypes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return resolver.Resolve(ctx, host, qTypes) | ||
} | ||
|
||
func (c *FactoryResolver) String() string { | ||
return "FactoryResolver" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package handler | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
|
||
"github.com/xvzc/SpoofDPI/dns/resolver" | ||
) | ||
|
||
type ErrorHandler struct { | ||
} | ||
|
||
func NewErrorHandler() *ErrorHandler { | ||
return &ErrorHandler{} | ||
} | ||
|
||
func (e *ErrorHandler) DoHandle(ctx context.Context, host string, qTypes []uint16, next resolver.Resolver) ([]net.IPAddr, error) { | ||
addrs, err := next.Resolve(ctx, host, qTypes) | ||
if err != nil { | ||
return nil, fmt.Errorf("%s: %w", next, err) | ||
} | ||
if len(addrs) == 0 { | ||
return nil, fmt.Errorf("could not resolve %s using %s", host, next) | ||
} | ||
return addrs, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package handler | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
|
||
"github.com/xvzc/SpoofDPI/dns/resolver" | ||
) | ||
|
||
type DnsHandler interface { | ||
DoHandle(ctx context.Context, host string, qTypes []uint16, next resolver.Resolver) ([]net.IPAddr, error) | ||
} | ||
type DnsHandlerFunc func(ctx context.Context, host string, qTypes []uint16, r resolver.Resolver) ([]net.IPAddr, error) | ||
type wrappedResolver struct { | ||
next resolver.Resolver | ||
h DnsHandler | ||
} | ||
|
||
func (d *wrappedResolver) Resolve(ctx context.Context, host string, qTypes []uint16) ([]net.IPAddr, error) { | ||
return d.h.DoHandle(ctx, host, qTypes, d.next) | ||
} | ||
|
||
func Apply(r resolver.Resolver, h ...DnsHandler) resolver.Resolver { | ||
for i := len(h) - 1; i >= 0; i-- { | ||
r = wrap(r, h[i]) | ||
} | ||
return r | ||
} | ||
|
||
func wrap(r resolver.Resolver, handler DnsHandler) resolver.Resolver { | ||
return &wrappedResolver{r, handler} | ||
} | ||
|
||
func (d *wrappedResolver) String() string { | ||
return fmt.Sprintf("DNSWrapper(%v)", d.next) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package handler | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"time" | ||
|
||
"github.com/xvzc/SpoofDPI/dns/resolver" | ||
"github.com/xvzc/SpoofDPI/util" | ||
"github.com/xvzc/SpoofDPI/util/log" | ||
) | ||
|
||
type LoggingHandler struct { | ||
} | ||
|
||
const scopeDNS = "DNS" | ||
|
||
func NewLoggingHandler() *LoggingHandler { | ||
return &LoggingHandler{} | ||
} | ||
|
||
func (l *LoggingHandler) DoHandle(ctx context.Context, host string, qTypes []uint16, resolver resolver.Resolver) ([]net.IPAddr, error) { | ||
ctx = util.GetCtxWithScope(ctx, scopeDNS) | ||
logger := log.GetCtxLogger(ctx) | ||
logger.Debug().Msgf("resolving %s using %s", host, resolver) | ||
t := time.Now() | ||
addrs, err := resolver.Resolve(ctx, host, qTypes) | ||
if err != nil { | ||
logger.Debug().Msgf("failed to resolve %s using %s", host, resolver) | ||
return nil, err | ||
} | ||
if len(addrs) > 0 { | ||
d := time.Since(t).Milliseconds() | ||
logger.Debug().Msgf("resolved %s from %s in %d ms", addrs[0].String(), host, d) | ||
} | ||
return addrs, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package proxy | ||
|
||
import ( | ||
"context" | ||
"net" | ||
|
||
"github.com/xvzc/SpoofDPI/dns/resolver" | ||
dnshandler "github.com/xvzc/SpoofDPI/dns/resolver/handler" | ||
"github.com/xvzc/SpoofDPI/util" | ||
"github.com/xvzc/SpoofDPI/util/log" | ||
) | ||
|
||
type proxyDnsFactory struct { | ||
systemClient *resolver.SystemResolver | ||
generalClient *resolver.GeneralResolver | ||
dohClient *resolver.DOHResolver | ||
shouldUseSystemDns func(host string) bool | ||
enableDOH bool | ||
} | ||
|
||
func newProxyDnsResolver(dnsAddr string, dnsPort string, enableDOH bool, shouldUseSystemResolver func(host string) bool) resolver.Resolver { | ||
factoryResolver := resolver.NewFactoryResolver( | ||
newProxyDnsFactory( | ||
dnsAddr, | ||
dnsPort, | ||
shouldUseSystemResolver, | ||
enableDOH, | ||
), | ||
) | ||
return dnshandler.Apply(factoryResolver, dnshandler.NewLoggingHandler(), dnshandler.NewErrorHandler()) | ||
} | ||
|
||
func newProxyDnsFactory( | ||
dnsAddr string, | ||
dnsPort string, | ||
shouldUseSystemDnsFunc func(host string) bool, | ||
enableDOH bool) *proxyDnsFactory { | ||
return &proxyDnsFactory{ | ||
systemClient: resolver.NewSystemResolver(), | ||
dohClient: resolver.NewDOHResolver(dnsAddr), | ||
generalClient: resolver.NewGeneralResolver(net.JoinHostPort(dnsAddr, dnsPort)), | ||
enableDOH: enableDOH, | ||
shouldUseSystemDns: shouldUseSystemDnsFunc, | ||
} | ||
} | ||
|
||
func (p *proxyDnsFactory) Get(ctx context.Context, host string, _ []uint16) (resolver.Resolver, error) { | ||
l := util.GetCtxWithScope(ctx, scopeProxy) | ||
logger := log.GetCtxLogger(l) | ||
r, _ := p.findInternal(host) | ||
logger.Debug().Msgf("proxy dns factory returns: %v", r) | ||
return r, nil | ||
} | ||
|
||
func (p *proxyDnsFactory) findInternal(host string) (resolver.Resolver, error) { | ||
if p.shouldUseSystemDns(host) { | ||
return p.systemClient, nil | ||
} | ||
if p.enableDOH { | ||
return p.dohClient, nil | ||
} | ||
return p.generalClient, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To add cross-cutting logic, need to add a handler implementation to this chain.
Handlers applies in given order