forked from v2fly/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Routing: Implement Route interface as the routing result of Router
- Loading branch information
Showing
6 changed files
with
122 additions
and
65 deletions.
There are no files selected for viewing
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
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,44 @@ | ||
package dns | ||
|
||
//go:generate errorgen | ||
|
||
import ( | ||
"v2ray.com/core/common/net" | ||
"v2ray.com/core/features/dns" | ||
"v2ray.com/core/features/routing" | ||
) | ||
|
||
// ResolvableContext is an implementation of routing.Context, with domain resolving capability. | ||
type ResolvableContext struct { | ||
routing.Context | ||
dnsClient dns.Client | ||
resolvedIPs []net.IP | ||
} | ||
|
||
// GetTargetIPs overrides original routing.Context's implementation. | ||
func (ctx *ResolvableContext) GetTargetIPs() []net.IP { | ||
if ips := ctx.Context.GetTargetIPs(); len(ips) != 0 { | ||
return ips | ||
} | ||
|
||
if len(ctx.resolvedIPs) > 0 { | ||
return ctx.resolvedIPs | ||
} | ||
|
||
if domain := ctx.GetTargetDomain(); len(domain) != 0 { | ||
ips, err := ctx.dnsClient.LookupIP(domain) | ||
if err == nil { | ||
ctx.resolvedIPs = ips | ||
return ips | ||
} | ||
newError("resolve ip for ", domain).Base(err).WriteToLog() | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// ContextWithDNSClient creates a new routing context with domain resolving capability. | ||
// Resolved domain IPs can be retrieved by GetTargetIPs(). | ||
func ContextWithDNSClient(ctx routing.Context, client dns.Client) routing.Context { | ||
return &ResolvableContext{Context: ctx, dnsClient: client} | ||
} |
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,9 @@ | ||
package dns | ||
|
||
import "v2ray.com/core/common/errors" | ||
|
||
type errPathObjHolder struct{} | ||
|
||
func newError(values ...interface{}) *errors.Error { | ||
return errors.New(values...).WithPathObj(errPathObjHolder{}) | ||
} |
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