-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: discover from DNS SRV records (#138)
* feat: discover from DNS SRV records Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * chore: lint issue Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * feat: txt records Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
148 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package srv | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net" | ||
"strings" | ||
|
||
"github.com/charmbracelet/wishlist" | ||
) | ||
|
||
// Endpoints returns the _ssh._tcp SRV records on the given domain as | ||
// Wishlist endpoints. | ||
func Endpoints(domain string) ([]*wishlist.Endpoint, error) { | ||
log.Printf("discovering _ssh._tcp SRV records on domain %q...", domain) | ||
_, srvs, err := net.LookupSRV("ssh", "tcp", domain) | ||
if err != nil { | ||
return nil, fmt.Errorf("srv: could not resolve %s: %w", domain, err) | ||
} | ||
txts, err := net.LookupTXT(domain) | ||
if err != nil { | ||
return nil, fmt.Errorf("srv: could not resolve %s: %w", domain, err) | ||
} | ||
return fromRecords(srvs, txts), nil | ||
} | ||
|
||
const txtPrefix = "wishlist.name " | ||
|
||
func fromRecords(srvs []*net.SRV, txts []string) []*wishlist.Endpoint { | ||
result := make([]*wishlist.Endpoint, 0, len(srvs)) | ||
for _, entry := range srvs { | ||
hostname := strings.TrimSuffix(entry.Target, ".") | ||
name := hostname | ||
port := fmt.Sprintf("%d", entry.Port) | ||
address := net.JoinHostPort(hostname, port) | ||
for _, txt := range txts { | ||
if !strings.HasPrefix(txt, txtPrefix) { | ||
continue | ||
} | ||
txtAddr, txtName, ok := strings.Cut(strings.TrimPrefix(txt, txtPrefix), "=") | ||
if !ok { | ||
continue | ||
} | ||
if txtAddr == address { | ||
name = txtName | ||
} | ||
} | ||
result = append(result, &wishlist.Endpoint{ | ||
Name: name, | ||
Address: address, | ||
}) | ||
} | ||
return result | ||
} |
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,66 @@ | ||
package srv | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
|
||
"github.com/charmbracelet/wishlist" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFromRecords(t *testing.T) { | ||
t.Run("no txt records", func(t *testing.T) { | ||
require.ElementsMatch(t, []*wishlist.Endpoint{ | ||
{ | ||
Name: "foo.local", | ||
Address: "foo.local:2222", | ||
}, | ||
{ | ||
Name: "foo.bar", | ||
Address: "foo.bar:22", | ||
}, | ||
}, fromRecords([]*net.SRV{ | ||
{ | ||
Target: "foo.bar", | ||
Port: 22, | ||
Priority: 10, | ||
Weight: 10, | ||
}, | ||
{ | ||
Target: "foo.local", | ||
Port: 2222, | ||
Priority: 10, | ||
Weight: 10, | ||
}, | ||
}, nil)) | ||
}) | ||
|
||
t.Run("with txt records", func(t *testing.T) { | ||
require.ElementsMatch(t, []*wishlist.Endpoint{ | ||
{ | ||
Name: "local-foo", | ||
Address: "foo.local:2222", | ||
}, | ||
{ | ||
Name: "foobar", | ||
Address: "foo.bar:22", | ||
}, | ||
}, fromRecords([]*net.SRV{ | ||
{ | ||
Target: "foo.bar", | ||
Port: 22, | ||
Priority: 10, | ||
Weight: 10, | ||
}, | ||
{ | ||
Target: "foo.local", | ||
Port: 2222, | ||
Priority: 10, | ||
Weight: 10, | ||
}, | ||
}, []string{ | ||
"wishlist.name foo.bar:22=foobar", | ||
"wishlist.name foo.local:2222=local-foo", | ||
})) | ||
}) | ||
} |
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