mirror of
https://git.hackfreedom.org/mesh.cat/meshname.git
synced 2026-05-22 06:22:32 +00:00
Update domain.go and tests
This commit is contained in:
@@ -7,22 +7,24 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// DomainFromIP derives a meshname subdomain for the authoritative DNS server address
|
||||
func DomainFromIP(target *net.IP) string {
|
||||
return strings.ToLower(base32.StdEncoding.EncodeToString(*target)[0:26])
|
||||
}
|
||||
|
||||
// IPFromDomain derives authoritative DNS server address from the meshname subdomain
|
||||
func IPFromDomain(domain *string) (net.IP, error) {
|
||||
name := strings.ToUpper(*domain) + "======"
|
||||
data, err := base32.StdEncoding.DecodeString(name)
|
||||
if err != nil {
|
||||
return net.IP{}, err
|
||||
return nil, err
|
||||
}
|
||||
if len(data) != 16 {
|
||||
return net.IP{}, errors.New("Invalid subdomain")
|
||||
return nil, errors.New("can't decode IP address, invalid subdomain")
|
||||
}
|
||||
ipAddr := net.IP(data)
|
||||
if ipAddr == nil {
|
||||
return net.IP{}, errors.New("Invalid IP address")
|
||||
return nil, errors.New("can't decode IP address, invalid data")
|
||||
}
|
||||
return ipAddr, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package meshname
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net"
|
||||
"testing"
|
||||
"fmt"
|
||||
|
||||
"github.com/zhoreeq/meshname/pkg/meshname"
|
||||
)
|
||||
|
||||
func TestIPFromDomain(t *testing.T) {
|
||||
test_subdomain := "aib7cwwdeob2vtnqf2cfnm7ilq"
|
||||
test_ip := net.ParseIP("203:f15a:c323:83aa:cdb0:2e84:56b3:e85c")
|
||||
|
||||
if ip, err := meshname.IPFromDomain(&test_subdomain); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if bytes.Compare(ip, test_ip) != 0 {
|
||||
t.Fatalf("Decoding IP error %s != %s", ip.String(), test_ip.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestDomainFromIP(t *testing.T) {
|
||||
test_subdomain := "aib7cwwdeob2vtnqf2cfnm7ilq"
|
||||
test_ip := net.ParseIP("203:f15a:c323:83aa:cdb0:2e84:56b3:e85c")
|
||||
|
||||
subdomain := meshname.DomainFromIP(&test_ip)
|
||||
if subdomain != test_subdomain {
|
||||
t.Fatalf("Encoding domain error: %s != %s", subdomain, test_subdomain)
|
||||
}
|
||||
}
|
||||
|
||||
func ExampleIPFromDomain() {
|
||||
test_subdomain := "aib7cwwdeob2vtnqf2cfnm7ilq"
|
||||
|
||||
if ip, err := meshname.IPFromDomain(&test_subdomain); err == nil {
|
||||
fmt.Println(ip)
|
||||
}
|
||||
// Output: 203:f15a:c323:83aa:cdb0:2e84:56b3:e85c
|
||||
}
|
||||
|
||||
func ExampleDomainFromIP() {
|
||||
test_ip := net.ParseIP("203:f15a:c323:83aa:cdb0:2e84:56b3:e85c")
|
||||
|
||||
fmt.Println(meshname.DomainFromIP(&test_ip))
|
||||
// Output: aib7cwwdeob2vtnqf2cfnm7ilq
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package meshname
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/zhoreeq/meshname/pkg/meshname"
|
||||
)
|
||||
|
||||
func TestIPFromDomain(t *testing.T) {
|
||||
test_subdomain := "aib7cwwdeob2vtnqf2cfnm7ilq"
|
||||
test_ip := net.ParseIP("203:f15a:c323:83aa:cdb0:2e84:56b3:e85c")
|
||||
|
||||
ip, err := meshname.IPFromDomain(&test_subdomain)
|
||||
if err != nil {
|
||||
t.Errorf("Decoding IP from domain failed %s", err)
|
||||
} else if bytes.Compare(ip, test_ip) != 0 {
|
||||
t.Errorf("Decoding IP error %s != %s", ip.String(), test_ip.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestDomainFromIP(t *testing.T) {
|
||||
test_subdomain := "aib7cwwdeob2vtnqf2cfnm7ilq"
|
||||
test_ip := net.ParseIP("203:f15a:c323:83aa:cdb0:2e84:56b3:e85c")
|
||||
|
||||
subdomain := meshname.DomainFromIP(&test_ip)
|
||||
if subdomain != test_subdomain {
|
||||
t.Errorf("Encoding domain error: %s != %s", subdomain, test_subdomain)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user