-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from multiformats/feat/rework-resolver
refactor Resolver to support custom per-TLD resolvers
- Loading branch information
Showing
4 changed files
with
241 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package madns | ||
|
||
import ( | ||
"context" | ||
"net" | ||
) | ||
|
||
type MockResolver struct { | ||
IP map[string][]net.IPAddr | ||
TXT map[string][]string | ||
} | ||
|
||
var _ BasicResolver = (*MockResolver)(nil) | ||
|
||
func (r *MockResolver) LookupIPAddr(ctx context.Context, name string) ([]net.IPAddr, error) { | ||
results, ok := r.IP[name] | ||
if ok { | ||
return results, nil | ||
} else { | ||
return []net.IPAddr{}, nil | ||
} | ||
} | ||
|
||
func (r *MockResolver) LookupTXT(ctx context.Context, name string) ([]string, error) { | ||
results, ok := r.TXT[name] | ||
if ok { | ||
return results, nil | ||
} else { | ||
return []string{}, 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
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,57 @@ | ||
package madns | ||
|
||
import ( | ||
"context" | ||
|
||
ma "github.com/multiformats/go-multiaddr" | ||
) | ||
|
||
func Matches(maddr ma.Multiaddr) (matches bool) { | ||
ma.ForEach(maddr, func(c ma.Component) bool { | ||
switch c.Protocol().Code { | ||
case DnsProtocol.Code, Dns4Protocol.Code, Dns6Protocol.Code, DnsaddrProtocol.Code: | ||
matches = true | ||
} | ||
return !matches | ||
}) | ||
return matches | ||
} | ||
|
||
func Resolve(ctx context.Context, maddr ma.Multiaddr) ([]ma.Multiaddr, error) { | ||
return DefaultResolver.Resolve(ctx, maddr) | ||
} | ||
|
||
// counts the number of components in the multiaddr | ||
func addrLen(maddr ma.Multiaddr) int { | ||
length := 0 | ||
ma.ForEach(maddr, func(_ ma.Component) bool { | ||
length++ | ||
return true | ||
}) | ||
return length | ||
} | ||
|
||
// trims `offset` components from the beginning of the multiaddr. | ||
func offset(maddr ma.Multiaddr, offset int) ma.Multiaddr { | ||
_, after := ma.SplitFunc(maddr, func(c ma.Component) bool { | ||
if offset == 0 { | ||
return true | ||
} | ||
offset-- | ||
return false | ||
}) | ||
return after | ||
} | ||
|
||
// takes the cross product of two sets of multiaddrs | ||
// | ||
// assumes `a` is non-empty. | ||
func cross(a, b []ma.Multiaddr) []ma.Multiaddr { | ||
res := make([]ma.Multiaddr, 0, len(a)*len(b)) | ||
for _, x := range a { | ||
for _, y := range b { | ||
res = append(res, x.Encapsulate(y)) | ||
} | ||
} | ||
return res | ||
} |