avatard/avatard.go

495 lines
12 KiB
Go
Raw Normal View History

2023-12-18 08:08:37 +00:00
// Copyright (C) 2023 Umorpha Systems
// SPDX-License-Identifier: AGPL-3.0-or-later
package main
import (
"archive/tar"
"bytes"
"crypto/md5"
"crypto/sha256"
"embed"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"os/exec"
"sort"
"strconv"
"strings"
"sync"
"time"
sd "git.lukeshu.com/go/libsystemd/sd_daemon"
)
// Hard-coded config ///////////////////////////////////////////////////////////
const (
UserlistCacheTime = 5 * time.Second
AllowableNetSkew = 5 * time.Second
DefaultSockName = "tcp::8080"
EMailDomain = "umorpha.io"
KChatDomain = "umorpha-systems"
EnableListing = true
)
// Embed other files ///////////////////////////////////////////////////////////
//go:embed COPYING.txt Makefile go.mod go.sum avatard.go index.html favicon.ico 404.png
var dirSource embed.FS
var fileIndexHTML = func() []byte {
fh, _ := dirSource.Open("index.html")
ret, _ := io.ReadAll(fh)
_ = fh.Close()
return ret
}()
var fileFaviconICO = func() []byte {
fh, _ := dirSource.Open("favicon.ico")
ret, _ := io.ReadAll(fh)
_ = fh.Close()
return ret
}()
var fileMissingAvatar = func() []byte {
fh, _ := dirSource.Open("404.png")
ret, _ := io.ReadAll(fh)
_ = fh.Close()
return ret
}()
var fileSourceTar = func() []byte {
const tarPrefix = "avatard/"
entries, _ := dirSource.ReadDir(".")
var names []string
for _, entry := range entries {
names = append(names, entry.Name())
}
sort.Strings(names)
var buf bytes.Buffer
w := tar.NewWriter(&buf)
for _, name := range names {
fh, _ := dirSource.Open(name)
fb, _ := io.ReadAll(fh)
_ = fh.Close()
w.WriteHeader(&tar.Header{
Typeflag: tar.TypeReg,
Name: tarPrefix + name,
Size: int64(len(fb)),
Mode: 0644,
})
_, _ = w.Write(fb)
}
_ = w.Close()
return buf.Bytes()
}()
// Main entrypoint /////////////////////////////////////////////////////////////
// Standard exit codes defined by [LSB].
//
// [LSB]: http://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
const (
ExitFailure = 1
ExitInvalidArgument = 2
ExitNotConfigured = 6
)
type ErrorWithCode struct {
Err error
Code int
}
// Error implements [error].
func (e *ErrorWithCode) Error() string { return e.Err.Error() }
// Unwrap implements the interface for [errors.Unwrap].
func (e *ErrorWithCode) Unwrap() error { return e.Err }
func main() {
if err := Main(); err != nil {
fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err)
var ecode *ErrorWithCode
if errors.As(err, &ecode) {
if ecode.Code == ExitInvalidArgument {
fmt.Fprintf(os.Stderr, "Try '%s --help' for more information.\n", os.Args[0])
}
os.Exit(ecode.Code)
}
os.Exit(1)
}
}
func usage() {
fmt.Printf(`Usage: %s [--help|LISTEN_ADDR]
Run a Libravatar origin server that uses Infomaniak kChat as data-source
If LISTEN_ADDR isn't specified, then %q is used.
LISTEN_ADDR is a "family:address" pair, where the "family" is one of
"tcp", "tcp4", "tcp6", "unix", "unixpacket" (the usual Go net.Listen()
network names), or "fd" (to listen on an already-open file
descriptor).
You must also set the environment variable INFOMANIAK_APIKEY.
`, os.Args[0], DefaultSockName)
}
func Main() error {
var sockname string
switch len(os.Args) {
case 0, 1:
sockname = DefaultSockName
case 2:
if os.Args[1] == "--help" {
usage()
return nil
}
sockname = os.Args[1]
default:
return &ErrorWithCode{
Err: fmt.Errorf("expected 0 or 1 arguments, got %d: %q", len(os.Args)-1, os.Args[1:]),
Code: ExitInvalidArgument,
}
}
stype, saddr, ok := strings.Cut(sockname, ":")
if !ok {
return &ErrorWithCode{
Err: fmt.Errorf("invalid address: %q\n", sockname),
Code: ExitInvalidArgument,
}
}
apikey := os.Getenv("INFOMANIAK_APIKEY")
if apikey == "" {
return &ErrorWithCode{
Err: fmt.Errorf("must set the INFOMANIAK_APIKEY environment variable"),
Code: ExitNotConfigured,
}
}
os.Unsetenv("INFOMANIAK_APIKEY")
sock, err := openSocket(stype, saddr)
if err != nil {
return err
}
server := &serverState{
infomaniak: infomaniakConfig{
APIKey: apikey,
EMailDomain: EMailDomain,
KChatDomain: KChatDomain,
},
cacheRawAvatars: make(map[string]*cachedAvatar),
}
router := http.NewServeMux()
router.HandleFunc("/", serveStatic)
router.Handle("/avatar/", server)
log.Printf("Serving on %v...\n", sock.Addr())
return http.Serve(sock, router)
}
func openSocket(stype, saddr string) (net.Listener, error) {
switch stype {
case "fd":
switch saddr {
case "stdin":
return net.FileListener(os.Stdin)
case "stdout":
return net.FileListener(os.Stdout)
case "stderr":
return net.FileListener(os.Stderr)
case "systemd":
sdFds := sd.ListenFds(true)
if len(sdFds) == 0 {
return nil, fmt.Errorf("fd:systemd given, but no systemd file descriptors passed in")
}
return net.FileListener(sdFds[0])
default:
if fd, _ := strconv.Atoi(saddr); fd > 0 {
return net.FileListener(os.NewFile(uintptr(fd), fmt.Sprintf("/dev/fd/%d", fd)))
}
if name, ok := strings.CutPrefix(saddr, "systemd:"); ok {
sdFiles := sd.ListenFds(true)
for _, file := range sdFiles {
if file.Name() == name {
return net.FileListener(file)
}
}
if n, err := strconv.Atoi(name); err == nil && n >= 0 && n < len(sdFiles) {
return net.FileListener(sdFiles[n])
}
return nil, fmt.Errorf("does not match any systemd file descriptor: %q", name)
}
return nil, fmt.Errorf("invalid file descriptor name: %q", saddr)
}
default:
return net.Listen(stype, saddr)
}
}
func serveStatic(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "", http.StatusMethodNotAllowed)
return
}
switch r.URL.Path {
case "/":
w.Header().Set("Content-Type", "text/html; charset=utf-8")
_, _ = w.Write(fileIndexHTML)
case "/favicon.ico":
w.Header().Set("Content-Type", "image/vnd.microsoft.icon")
_, _ = w.Write(fileFaviconICO)
case "/avatard.tar":
w.Header().Set("Content-Type", "image/application/x-tar")
_, _ = w.Write(fileSourceTar)
default:
http.NotFound(w, r)
return
}
}
////////////////////////////////////////////////////////////////////////////////
type infomaniakConfig struct {
// Static-after-initialization
APIKey string
EMailDomain string
KChatDomain string
}
// kChatUser is a subset of what the Infomaniak kChat API returns from
// `/api/v4/users`.
type kChatUser struct {
ID string `json:"id"` // a UUID
LastPictureUpdate int64 `json:"last_picture_update"` // milliseconds since the Unix epoch
EMail string `json:"email"`
}
// fetchUsers returns a listing of the users on the kChat instance,
// keyed by their Libravatar hashes.
func (cfg infomaniakConfig) fetchUsers() (map[string]*kChatUser, error) {
req, _ := http.NewRequest(http.MethodGet, "https://"+cfg.KChatDomain+".kchat.infomaniak.com/api/v4/users", nil)
req.Header.Set("Authorization", "Bearer "+cfg.APIKey)
log.Printf("fetch: %s %s\n", req.Method, req.URL)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
respBody, err := io.ReadAll(resp.Body)
_ = resp.Body.Close()
if err != nil {
return nil, err
}
var users []kChatUser
if err := json.Unmarshal(respBody, &users); err != nil {
return nil, err
}
hash2user := make(map[string]*kChatUser, len(users)*2)
for i := range users {
lowerEMail := strings.ToLower(users[i].EMail)
if !strings.HasSuffix(lowerEMail, "@"+cfg.EMailDomain) {
continue
}
hashMD5 := md5.Sum([]byte(lowerEMail))
hash2user[hex.EncodeToString(hashMD5[:])] = &users[i]
hashSHA256 := sha256.Sum256([]byte(lowerEMail))
hash2user[hex.EncodeToString(hashSHA256[:])] = &users[i]
}
return hash2user, nil
}
func (cfg infomaniakConfig) fetchAvatar(userID string) ([]byte, error) {
req, _ := http.NewRequest(http.MethodGet, "https://"+cfg.KChatDomain+".kchat.infomaniak.com/api/v4/users/"+userID+"/image", nil)
req.Header.Set("Authorization", "Bearer "+cfg.APIKey)
log.Printf("fetch: %s %s\n", req.Method, req.URL)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}
////////////////////////////////////////////////////////////////////////////////
type serverState struct {
// Immutable state
infomaniak infomaniakConfig
// Mutable state
cacheUsersMu sync.Mutex
cacheUsersUntil time.Time
cacheUsers map[string]*kChatUser // keyed by Libravatar hash
cacheRawAvatarsMu sync.Mutex
cacheRawAvatars map[string]*cachedAvatar // keyed by kChat user ID
}
func (o *serverState) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "", http.StatusMethodNotAllowed)
return
}
hash := strings.TrimPrefix(r.URL.Path, "/avatar/")
if hash == "" {
if !EnableListing {
http.Error(w, "listing users is forbidden", http.StatusForbidden)
return
}
users, err := o.getUsers()
if users == nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
email2hashes := make(map[string][]string, len(users)/2)
for hash, user := range users {
email2hashes[user.EMail] = append(email2hashes[user.EMail], hash)
}
emails := make([]string, 0, len(email2hashes))
for email := range email2hashes {
emails = append(emails, email)
}
sort.Strings(emails)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
io.WriteString(w, `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>avatars</title>
</head>
<body>
<ul>`)
for _, email := range emails {
fmt.Fprintf(w, " <li><tt>%s :", email)
sort.Slice(email2hashes[email], func(i, j int) bool {
return len(email2hashes[email][i]) < len(email2hashes[email][j])
})
for _, hash := range email2hashes[email] {
fmt.Fprintf(w, " <a href=\"%[1]s\">%[1]s</a>\n", hash)
}
io.WriteString(w, "</tt></li>\n")
}
io.WriteString(w, ` </ul>
</body>
</html>
`)
return
}
options := r.URL.Query()
sizeStr := options.Get("size")
if sizeStr == "" {
sizeStr = options.Get("s")
}
size, _ := strconv.ParseUint(sizeStr, 10, 16)
if size < 1 || size > 512 {
size = 80
}
rawAvatar, err := o.getRawAvatar(hash)
status := http.StatusOK
if rawAvatar == nil {
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rawAvatar = fileMissingAvatar
status = http.StatusNotFound
}
cmd := exec.Command("magick", "-", "-resize", fmt.Sprintf("%[1]dx%[1]dx", size), "PNG:-")
cmd.Stdin = bytes.NewReader(rawAvatar)
resizedAvatar, err := cmd.Output()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "image/png")
w.WriteHeader(status)
w.Write(resizedAvatar)
}
func (o *serverState) getUsers() (map[string]*kChatUser, error) {
o.cacheUsersMu.Lock()
defer o.cacheUsersMu.Unlock()
if time.Now().Before(o.cacheUsersUntil) {
return o.cacheUsers, nil
}
users, err := o.infomaniak.fetchUsers()
if err != nil {
log.Printf("error fetching user list: %v\n", err)
return o.cacheUsers, err
}
o.cacheUsers = users
o.cacheUsersUntil = time.Now().Add(UserlistCacheTime)
return users, nil
}
type cachedAvatar struct {
Mu sync.Mutex
FetchedAt time.Time
Content []byte
}
func (o *serverState) getRawAvatar(hash string) ([]byte, error) {
users, err := o.getUsers()
if users == nil {
return nil, err
}
user := users[hash]
if user == nil {
return nil, nil
}
o.cacheRawAvatarsMu.Lock()
cur := o.cacheRawAvatars[user.ID]
if cur == nil {
cur = &cachedAvatar{}
cur.Mu.Lock()
o.cacheRawAvatars[user.ID] = cur
o.cacheRawAvatarsMu.Unlock()
} else {
o.cacheRawAvatarsMu.Unlock()
cur.Mu.Lock()
}
defer cur.Mu.Unlock()
if cur.FetchedAt.Before(time.UnixMilli(user.LastPictureUpdate).Add(AllowableNetSkew)) {
content, err := o.infomaniak.fetchAvatar(user.ID)
if err != nil {
log.Printf("error fetching avatar: %s: %v\n", user.ID, err)
return cur.Content, err
}
cur.Content = content
cur.FetchedAt = time.Now()
}
return cur.Content, nil
}