Add .pk.ygg to Yggstack resolver

This commit is contained in:
Neil Alexander 2022-02-20 10:42:00 +00:00 committed by Vasyl Gello
parent ae4dbc2a27
commit 33e513af67
2 changed files with 19 additions and 1 deletions

View file

@ -143,9 +143,11 @@ func main() {
address := n.Address()
subnet := n.Subnet()
public := n.GetSelf().Key
logger.Infof("Your public key is %s", hex.EncodeToString(public[:]))
publicstr := hex.EncodeToString(public[:])
logger.Infof("Your public key is %s", publicstr)
logger.Infof("Your IPv6 address is %s", address.String())
logger.Infof("Your IPv6 subnet is %s", subnet.String())
logger.Infof("Your Yggstack resolver name is %s%s", publicstr, types.NameMappingSuffix)
s, err := netstack.CreateYggdrasilNetstack(&n.Core)
if err != nil {

View file

@ -2,12 +2,18 @@ package types
import (
"context"
"crypto/ed25519"
"encoding/hex"
"fmt"
"net"
"strings"
"github.com/yggdrasil-network/yggdrasil-go/contrib/netstack"
"github.com/yggdrasil-network/yggdrasil-go/src/address"
)
const NameMappingSuffix = ".pk.ygg"
type NameResolver struct {
resolver *net.Resolver
}
@ -31,6 +37,16 @@ func NewNameResolver(stack *netstack.YggdrasilNetstack, nameserver string) *Name
}
func (r *NameResolver) Resolve(ctx context.Context, name string) (context.Context, net.IP, error) {
if strings.HasSuffix(name, NameMappingSuffix) {
name = strings.TrimSuffix(name, NameMappingSuffix)
var pk [ed25519.PublicKeySize]byte
if b, err := hex.DecodeString(name); err != nil {
return nil, nil, fmt.Errorf("hex.DecodeString: %w", err)
} else {
copy(pk[:], b)
return ctx, net.IP(address.AddrForKey(pk[:])[:]), nil
}
}
ip := net.ParseIP(name)
if ip == nil {
addrs, err := r.resolver.LookupIP(ctx, "ip6", name)