mirror of
https://git.mills.io/saltyim/saltyim.git
synced 2024-06-29 18:21:06 +00:00
![xuu](/assets/img/avatar_default.png)
I added sha256 for discovery and fallback ability. And implemented sending message using msgbus. Co-authored-by: Jon Lundy <jon@xuu.cc> Reviewed-on: https://git.mills.io/prologic/salty-chat/pulls/8 Co-authored-by: xuu <xuu@noreply@mills.io> Co-committed-by: xuu <xuu@noreply@mills.io>
99 lines
2.3 KiB
Go
99 lines
2.3 KiB
Go
package saltyim
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"strings"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// Addr represents a Salty IM User's Address
|
|
type Addr struct {
|
|
User string
|
|
Domain string
|
|
}
|
|
|
|
func (a Addr) String() string {
|
|
return fmt.Sprint(a.User, "@", a.Domain)
|
|
}
|
|
func (a Addr) Formatted() string {
|
|
return fmt.Sprintf("(%s)", a)
|
|
}
|
|
|
|
|
|
// 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)
|
|
}
|
|
|
|
// HashURI returns the Well-Known HashURI for this Addr
|
|
func (s Addr) HashURI() string {
|
|
return fmt.Sprintf("https://%s/.well-known/salty/%x.json", s.Domain, sha256.Sum256([]byte(s.String())))
|
|
}
|
|
|
|
// ParseAddr parsers a Salty Address for a user into it's user and domain
|
|
// parts and returns an Addr object with the User and Domain and a method
|
|
// for returning the expected User's Well-Known URI
|
|
func ParseAddr(addr string) (Addr, error) {
|
|
parts := strings.Split(addr, "@")
|
|
if len(parts) != 2 {
|
|
return Addr{}, fmt.Errorf("error parsing addr %q, expected nick@domain", addr)
|
|
}
|
|
|
|
return Addr{parts[0], parts[1]}, nil
|
|
}
|
|
|
|
// Lookup looks up a Salty Address for a User by parsing the user's domain and
|
|
// making a request to the user's Well-Known URI expected to be located at
|
|
// https://domain/.well-known/salty/<user>.json
|
|
func Lookup(addr string) (Config, error) {
|
|
a, err := ParseAddr(addr)
|
|
if err != nil {
|
|
log.WithError(err).Errorf("error parsing addr %q", addr)
|
|
return Config{}, err
|
|
}
|
|
|
|
// Attempt using hash
|
|
data, err := fetch(a.HashURI())
|
|
if err != nil {
|
|
// Fallback to plain user nick
|
|
data, err = fetch(a.URI())
|
|
}
|
|
|
|
if err != nil {
|
|
log.WithError(err).Error("error requesting well-known uri")
|
|
return Config{}, err
|
|
}
|
|
|
|
var config Config
|
|
if err := json.Unmarshal(data, &config); err != nil {
|
|
log.WithError(err).Error("error parsing well-known config")
|
|
}
|
|
|
|
return config, err
|
|
}
|
|
|
|
func fetch(uri string) ([]byte, error) {
|
|
res, err := http.Get(uri)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error requesting well-known uri: %w", err)
|
|
}
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode/100 != 2 {
|
|
return nil, fmt.Errorf("non-2xx response received: %s", res.Status)
|
|
}
|
|
|
|
data, err := ioutil.ReadAll(res.Body)
|
|
if err != nil {
|
|
log.WithError(err).Error()
|
|
return nil, fmt.Errorf("error reading response: %w", err)
|
|
}
|
|
|
|
return data, err
|
|
}
|