6
1
mirror of https://git.mills.io/saltyim/saltyim.git synced 2024-06-25 00:08:26 +00:00

feat: add CNAME Discovery to script implementation (#44)

implements https://git.mills.io/prologic/salty.im/pulls/9

Co-authored-by: Jon Lundy <jon@xuu.cc>
Reviewed-on: https://git.mills.io/prologic/saltyim/pulls/44
Co-authored-by: xuu <xuu@noreply@mills.io>
Co-committed-by: xuu <xuu@noreply@mills.io>
This commit is contained in:
xuu 2022-03-22 22:59:45 +00:00 committed by James Mills
parent 801d6b93bb
commit 7261dd085c
2 changed files with 22 additions and 6 deletions

@ -94,9 +94,14 @@ lookup () {
domain="$(echo "$user" | awk -F@ '{ print $2 }')"
hash="$(printf "%s" "$user" | sha256sum | cut -f 1 -d ' ')"
discovery_host="$(dig +short SRV _salty._tcp."$domain" | cut -f 4 -d' ')"
if [ -z "$discovery_host" ]; then
discovery_host="$domain"
fi
info=$(mktemp /tmp/salty.XXXXXX)
if ! curl -qfsSL "https://$domain/.well-known/salty/${hash}.json" > "$info" 2> /dev/null; then
if ! curl -qfsSL "https://$domain/.well-known/salty/${nick}.json" > "$info"; then
if ! curl -qfsSL "https://$discovery_host/.well-known/salty/${hash}.json" > "$info" 2> /dev/null; then
if ! curl -qfsSL "https://$discovery_host/.well-known/salty/${nick}.json" > "$info"; then
rm "$info"
echo "error: lookup failed"
return 1
@ -253,7 +258,7 @@ make_user () {
echo "# user: $user" >> "$identity_file"
pubkey=$(grep key: "$identity_file" | awk '{print $4}')
hash="$(printf "$user" | sha256sum | cut -f 1 -d ' ')"
hash="$(printf "%s" "$user" | sha256sum | cut -f 1 -d ' ')"
cat <<- EOF
Create this file in your webserver well-known folder. https://hostname.tld/.well-known/salty/$hash.json

@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"strings"
)
@ -13,6 +14,7 @@ import (
type Addr struct {
User string
Domain string
DiscoveryDomain string
}
// IsZero returns true if the address is empty
@ -37,12 +39,20 @@ func (a Addr) Formatted() string {
// URI returns the Well-Known URI for this Addr
func (a Addr) URI() string {
return fmt.Sprintf("https://%s/.well-known/salty/%s.json", a.Domain, a.User)
return fmt.Sprintf("https://%s/.well-known/salty/%s.json", a.DiscoveryDomain, a.User)
}
// HashURI returns the Well-Known HashURI for this Addr
func (a Addr) HashURI() string {
return fmt.Sprintf("https://%s/.well-known/salty/%s.json", a.Domain, a.Hash())
return fmt.Sprintf("https://%s/.well-known/salty/%s.json", a.DiscoveryDomain, a.Hash())
}
func (a *Addr) RefreshDiscovery() {
_, records, err := net.LookupSRV("salty", "tcp", a.Domain)
if err != nil || len(records) == 0 {
return
}
a.DiscoveryDomain = records[0].Target
}
// ParseAddr parsers a Salty Address for a user into it's user and domain
@ -54,7 +64,7 @@ func ParseAddr(addr string) (Addr, error) {
return Addr{}, fmt.Errorf("expected nick@domain found %q", addr)
}
return Addr{parts[0], parts[1]}, nil
return Addr{User: parts[0], Domain: parts[1], DiscoveryDomain: parts[1]}, nil
}
// Lookup looks up a Salty Address for a User by parsing the user's domain and
@ -65,6 +75,7 @@ func Lookup(addr string) (Config, error) {
if err != nil {
return Config{}, err
}
a.RefreshDiscovery()
config, err := fetchConfig(a.HashURI())
if err != nil {
// Fallback to plain user nick