A friend's server works out the peaks of a track in a shared library and gives
them to a signed friend (GET /peer/tracks/{id}/peaks). This server asks for them
and passes them on to the listener after checking them: 1 to 1000 levels from 0
to 255, as many as the count says (GET /friends/{friend}/tracks/{id}/peaks). The
player uses it for a friend's track, so its bar is no longer flat.
279 lines
9.6 KiB
Go
279 lines
9.6 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"crypto/ed25519"
|
|
"encoding/base64"
|
|
"errors"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"codeberg.org/squidd/sonda/internal/federation"
|
|
)
|
|
|
|
type federationService interface {
|
|
Status(ctx context.Context) (federation.Status, error)
|
|
Update(ctx context.Context, p federation.Patch) (federation.Status, error)
|
|
CreateInvite(ctx context.Context) (federation.Invite, error)
|
|
Redeem(ctx context.Context, text string) (federation.Friend, error)
|
|
Pair(ctx context.Context, req federation.PairRequest) (federation.PairReply, error)
|
|
Approve(ctx context.Context, id int64) (federation.Friend, error)
|
|
Remove(ctx context.Context, id int64) error
|
|
Check(ctx context.Context, id int64) (federation.Friend, error)
|
|
Requests(ctx context.Context) ([]federation.FriendRequest, error)
|
|
// What the routes friends use need.
|
|
Authenticate(ctx context.Context, in federation.Signed) (federation.Friend, error)
|
|
StartStream(ctx context.Context, friendID int64) (*federation.Stream, error)
|
|
Pace(ctx context.Context, friendID int64) (*federation.Stream, error)
|
|
Record(ctx context.Context, r federation.FriendRequest)
|
|
// What a listener may ask about friends.
|
|
ActiveFriends(ctx context.Context) ([]federation.Friend, error)
|
|
Browse(ctx context.Context, friendID int64, resource string, id int64, query url.Values) ([]byte, error)
|
|
OpenMedia(ctx context.Context, friendID int64, kind string, id int64, query url.Values, in http.Header) (*federation.Media, error)
|
|
Peaks(ctx context.Context, friendID, trackID int64) ([]byte, error)
|
|
}
|
|
|
|
// federationHandlers are the admin routes that set up federation and its friends, and the one public route
|
|
// another server calls to pair.
|
|
type federationHandlers struct{ svc federationService }
|
|
|
|
type friendJSON struct {
|
|
ID int64 `json:"id"`
|
|
Name string `json:"name"`
|
|
Address string `json:"address"`
|
|
KeyID string `json:"key_id"`
|
|
State string `json:"state"`
|
|
CreatedAt *string `json:"created_at"`
|
|
ApprovedAt *string `json:"approved_at"`
|
|
}
|
|
|
|
func toFriendJSON(f federation.Friend) friendJSON {
|
|
out := friendJSON{
|
|
ID: f.ID, Name: f.Name, Address: f.Address, KeyID: federation.KeyID(f.PublicKey),
|
|
State: string(f.State), CreatedAt: rfc3339(f.CreatedAt),
|
|
}
|
|
if !f.ApprovedAt.IsZero() {
|
|
out.ApprovedAt = rfc3339(f.ApprovedAt)
|
|
}
|
|
return out
|
|
}
|
|
|
|
type federationJSON struct {
|
|
Enabled bool `json:"enabled"`
|
|
Name string `json:"name"`
|
|
Address string `json:"address"`
|
|
KeyID string `json:"key_id"`
|
|
Sends string `json:"sends"`
|
|
MaxStreams int `json:"max_streams"`
|
|
MaxKiBPerSec int `json:"max_kib_per_sec"`
|
|
Friends []friendJSON `json:"friends"`
|
|
}
|
|
|
|
func toFederationJSON(st federation.Status) federationJSON {
|
|
out := federationJSON{
|
|
Enabled: st.Enabled, Name: st.Name, Address: st.Address, KeyID: st.KeyID, Sends: st.Sends,
|
|
MaxStreams: st.MaxStreams, MaxKiBPerSec: st.MaxKiBPerSec, Friends: make([]friendJSON, len(st.Friends)),
|
|
}
|
|
for i, f := range st.Friends {
|
|
out.Friends[i] = toFriendJSON(f)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// writeFederationError maps what the service says to the standard envelope, for the routes of an admin.
|
|
func writeFederationError(c *gin.Context, err error) {
|
|
switch {
|
|
case errors.Is(err, federation.ErrOff):
|
|
writeError(c, http.StatusConflict, "federation_off", "turn federation on first")
|
|
case errors.Is(err, federation.ErrNoAddress):
|
|
writeError(c, http.StatusConflict, "address_required", "set the public address of this server first")
|
|
case errors.Is(err, federation.ErrInvalidInput):
|
|
writeError(c, http.StatusBadRequest, "invalid_input", err.Error())
|
|
case errors.Is(err, federation.ErrInvalidInvite):
|
|
writeError(c, http.StatusBadRequest, "invalid_invite", "this is not an invite from a Sonda server")
|
|
case errors.Is(err, federation.ErrRefused):
|
|
writeError(c, http.StatusUnprocessableEntity, "invite_refused", err.Error())
|
|
case errors.Is(err, federation.ErrKeyMismatch):
|
|
writeError(c, http.StatusBadGateway, "key_mismatch", err.Error())
|
|
case errors.Is(err, federation.ErrUnreachable):
|
|
writeError(c, http.StatusBadGateway, "friend_unreachable", err.Error())
|
|
case errors.Is(err, federation.ErrAlreadyFriend):
|
|
writeError(c, http.StatusConflict, "already_friends", err.Error())
|
|
case errors.Is(err, federation.ErrTooMany):
|
|
writeError(c, http.StatusTooManyRequests, "too_many_requests", err.Error())
|
|
case errors.Is(err, federation.ErrFriendMissing):
|
|
writeError(c, http.StatusNotFound, "friend_not_found", "no such friend")
|
|
case errors.Is(err, federation.ErrWrongState):
|
|
writeError(c, http.StatusConflict, "not_waiting", err.Error())
|
|
case errors.Is(err, federation.ErrNotApproved):
|
|
writeError(c, http.StatusConflict, "not_approved", "the other admin has not approved yet")
|
|
case errors.Is(err, federation.ErrNotFriend):
|
|
writeError(c, http.StatusConflict, "friend_gone", err.Error())
|
|
default:
|
|
_ = c.Error(err)
|
|
writeError(c, http.StatusInternalServerError, "internal", "internal error")
|
|
}
|
|
}
|
|
|
|
func (h *federationHandlers) get(c *gin.Context) {
|
|
st, err := h.svc.Status(c.Request.Context())
|
|
if err != nil {
|
|
writeFederationError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, toFederationJSON(st))
|
|
}
|
|
|
|
type updateFederationRequest struct {
|
|
Enabled *bool `json:"enabled"` // pointers: an absent field is left alone
|
|
Name *string `json:"name"`
|
|
Address *string `json:"address"`
|
|
MaxStreams *int `json:"max_streams"`
|
|
MaxKiBPerSec *int `json:"max_kib_per_sec"`
|
|
}
|
|
|
|
func (h *federationHandlers) update(c *gin.Context) {
|
|
var req updateFederationRequest
|
|
if !bindJSON(c, &req) {
|
|
return
|
|
}
|
|
st, err := h.svc.Update(c.Request.Context(), federation.Patch{
|
|
Enabled: req.Enabled, Name: req.Name, Address: req.Address, MaxStreams: req.MaxStreams, MaxKiBPerSec: req.MaxKiBPerSec,
|
|
})
|
|
if err != nil {
|
|
writeFederationError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, toFederationJSON(st))
|
|
}
|
|
|
|
func (h *federationHandlers) invite(c *gin.Context) {
|
|
inv, err := h.svc.CreateInvite(c.Request.Context())
|
|
if err != nil {
|
|
writeFederationError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusCreated, gin.H{"invite": inv.Text, "expires_at": rfc3339(inv.ExpiresAt)})
|
|
}
|
|
|
|
type redeemRequest struct {
|
|
Invite string `json:"invite"`
|
|
}
|
|
|
|
func (h *federationHandlers) redeem(c *gin.Context) {
|
|
var req redeemRequest
|
|
if !bindJSON(c, &req) {
|
|
return
|
|
}
|
|
f, err := h.svc.Redeem(c.Request.Context(), req.Invite)
|
|
if err != nil {
|
|
writeFederationError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusCreated, toFriendJSON(f))
|
|
}
|
|
|
|
func (h *federationHandlers) approve(c *gin.Context) {
|
|
id, ok := pathID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
f, err := h.svc.Approve(c.Request.Context(), id)
|
|
if err != nil {
|
|
writeFederationError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, toFriendJSON(f))
|
|
}
|
|
|
|
func (h *federationHandlers) remove(c *gin.Context) {
|
|
id, ok := pathID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
if err := h.svc.Remove(c.Request.Context(), id); err != nil {
|
|
writeFederationError(c, err)
|
|
return
|
|
}
|
|
c.Status(http.StatusNoContent)
|
|
}
|
|
|
|
type pairRequestJSON struct {
|
|
Code string `json:"code"`
|
|
Name string `json:"name"`
|
|
Address string `json:"address"`
|
|
PublicKey string `json:"public_key"`
|
|
}
|
|
|
|
// pair is what another server calls when its admin pastes an invite of this one. It has no login: the code of
|
|
// the invite is the proof. While federation is off, or has no address, it is as good as a missing route.
|
|
func (h *federationHandlers) pair(c *gin.Context) {
|
|
var req pairRequestJSON
|
|
if !bindJSON(c, &req) {
|
|
return
|
|
}
|
|
key, err := base64.RawURLEncoding.DecodeString(req.PublicKey)
|
|
if err != nil || len(key) != ed25519.PublicKeySize {
|
|
writeError(c, http.StatusBadRequest, "invalid_input", "public_key is not an Ed25519 key")
|
|
return
|
|
}
|
|
reply, err := h.svc.Pair(c.Request.Context(), federation.PairRequest{
|
|
Code: req.Code, Name: req.Name, Address: req.Address, PublicKey: key,
|
|
})
|
|
switch {
|
|
case err == nil:
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"name": reply.Name, "address": reply.Address, "public_key": base64.RawURLEncoding.EncodeToString(reply.PublicKey),
|
|
})
|
|
case errors.Is(err, federation.ErrOff), errors.Is(err, federation.ErrNoAddress):
|
|
writeError(c, http.StatusNotFound, "not_found", "no such route")
|
|
case errors.Is(err, federation.ErrInvalidInvite):
|
|
writeError(c, http.StatusForbidden, "invalid_invite", "the invite is not valid")
|
|
case errors.Is(err, federation.ErrInvalidInput):
|
|
writeError(c, http.StatusBadRequest, "invalid_input", err.Error())
|
|
case errors.Is(err, federation.ErrAlreadyFriend):
|
|
writeError(c, http.StatusConflict, "already_friends", "this server is already a friend")
|
|
case errors.Is(err, federation.ErrTooMany):
|
|
writeError(c, http.StatusTooManyRequests, "too_many_requests", "too many attempts: try again in a minute")
|
|
default:
|
|
_ = c.Error(err)
|
|
writeError(c, http.StatusInternalServerError, "internal", "internal error")
|
|
}
|
|
}
|
|
|
|
func (h *federationHandlers) check(c *gin.Context) {
|
|
id, ok := pathID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
f, err := h.svc.Check(c.Request.Context(), id)
|
|
if err != nil {
|
|
writeFederationError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, toFriendJSON(f))
|
|
}
|
|
|
|
type requestJSON struct {
|
|
At *string `json:"at"`
|
|
FriendID int64 `json:"friend_id"`
|
|
Friend string `json:"friend"`
|
|
Route string `json:"route"`
|
|
Status int `json:"status"`
|
|
Bytes int64 `json:"bytes"`
|
|
}
|
|
|
|
func (h *federationHandlers) requests(c *gin.Context) {
|
|
list, err := h.svc.Requests(c.Request.Context())
|
|
if err != nil {
|
|
writeFederationError(c, err)
|
|
return
|
|
}
|
|
out := make([]requestJSON, len(list))
|
|
for i, r := range list {
|
|
out[i] = requestJSON{At: rfc3339(r.At), FriendID: r.FriendID, Friend: r.FriendName, Route: r.Route, Status: r.Status, Bytes: r.Bytes}
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"requests": out})
|
|
}
|