chore: add apps from go.sour.is/ev
This commit is contained in:
241
app/salty/blobs.go
Normal file
241
app/salty/blobs.go
Normal file
@@ -0,0 +1,241 @@
|
||||
package salty
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
"go.sour.is/pkg/authreq"
|
||||
"go.sour.is/pkg/lg"
|
||||
"go.uber.org/multierr"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrAddressExists = errors.New("error: address already exists")
|
||||
ErrBlobNotFound = errors.New("error: blob not found")
|
||||
)
|
||||
|
||||
func WithBlobStore(path string) *withBlobStore {
|
||||
return &withBlobStore{path: path}
|
||||
}
|
||||
|
||||
type withBlobStore struct {
|
||||
path string
|
||||
|
||||
m_get_blob metric.Int64Counter
|
||||
m_put_blob metric.Int64Counter
|
||||
m_delete_blob metric.Int64Counter
|
||||
}
|
||||
|
||||
func (o *withBlobStore) ApplySalty(s *service) {}
|
||||
|
||||
func (o *withBlobStore) Setup(ctx context.Context) error {
|
||||
ctx, span := lg.Span(ctx)
|
||||
defer span.End()
|
||||
|
||||
var err, errs error
|
||||
|
||||
err = os.MkdirAll(o.path, 0700)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
m := lg.Meter(ctx)
|
||||
o.m_get_blob, err = m.Int64Counter("salty_get_blob",
|
||||
metric.WithDescription("salty get blob called"),
|
||||
)
|
||||
errs = multierr.Append(errs, err)
|
||||
|
||||
o.m_put_blob, err = m.Int64Counter("salty_put_blob",
|
||||
metric.WithDescription("salty put blob called"),
|
||||
)
|
||||
errs = multierr.Append(errs, err)
|
||||
|
||||
o.m_delete_blob, err = m.Int64Counter("salty_delete_blob",
|
||||
metric.WithDescription("salty delete blob called"),
|
||||
)
|
||||
errs = multierr.Append(errs, err)
|
||||
|
||||
return errs
|
||||
}
|
||||
|
||||
func (o *withBlobStore) RegisterAPIv1(mux *http.ServeMux) {
|
||||
mux.Handle("/blob/", authreq.Authorization(o))
|
||||
}
|
||||
|
||||
func (o *withBlobStore) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, span := lg.Span(r.Context())
|
||||
defer span.End()
|
||||
|
||||
claims := authreq.FromContext(ctx)
|
||||
if claims == nil {
|
||||
httpError(w, http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
signer := claims.Issuer
|
||||
|
||||
key := strings.TrimPrefix(r.URL.Path, "/blob/")
|
||||
|
||||
switch r.Method {
|
||||
case http.MethodDelete:
|
||||
if err := deleteBlob(o.path, key, signer); err != nil {
|
||||
if errors.Is(err, ErrBlobNotFound) {
|
||||
httpError(w, http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
span.RecordError(fmt.Errorf("%w: getting blob %s for %s", err, key, signer))
|
||||
httpError(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
http.Error(w, "Blob Deleted", http.StatusOK)
|
||||
case http.MethodGet, http.MethodHead:
|
||||
blob, err := getBlob(o.path, key, signer)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrBlobNotFound) {
|
||||
httpError(w, http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
span.RecordError(fmt.Errorf("%w: getting blob %s for %s", err, key, signer))
|
||||
httpError(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer blob.Close()
|
||||
|
||||
blob.SetHeaders(r)
|
||||
|
||||
if r.Method == http.MethodGet {
|
||||
_, _ = io.Copy(w, blob)
|
||||
}
|
||||
case http.MethodPut:
|
||||
data, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
httpError(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer r.Body.Close()
|
||||
|
||||
if err := putBlob(o.path, key, data, signer); err != nil {
|
||||
span.RecordError(fmt.Errorf("%w: putting blob %s for %s", err, key, signer))
|
||||
|
||||
httpError(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
http.Error(w, "Blob Created", http.StatusCreated)
|
||||
default:
|
||||
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func putBlob(path string, key string, data []byte, signer string) error {
|
||||
p := filepath.Join(path, signer, key)
|
||||
if err := os.MkdirAll(p, 0700); err != nil {
|
||||
return fmt.Errorf("error creating blobs paths %s: %w", p, err)
|
||||
}
|
||||
fn := filepath.Join(p, "content")
|
||||
|
||||
if err := os.WriteFile(fn, data, os.FileMode(0600)); err != nil {
|
||||
return fmt.Errorf("error writing blob %s: %w", fn, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getBlob(path string, key string, signer string) (*Blob, error) {
|
||||
p := filepath.Join(path, signer, key)
|
||||
|
||||
if err := os.MkdirAll(p, 0755); err != nil {
|
||||
return nil, fmt.Errorf("error creating blobs paths %s: %w", p, err)
|
||||
}
|
||||
|
||||
fn := filepath.Join(p, "content")
|
||||
|
||||
if !FileExists(fn) {
|
||||
return nil, ErrBlobNotFound
|
||||
}
|
||||
|
||||
return OpenBlob(fn)
|
||||
}
|
||||
|
||||
func deleteBlob(path string, key string, signer string) error {
|
||||
|
||||
p := filepath.Join(path, signer, key)
|
||||
|
||||
if !FileExists(p) {
|
||||
return ErrBlobNotFound
|
||||
}
|
||||
|
||||
return os.RemoveAll(p)
|
||||
}
|
||||
|
||||
// FileExists returns true if the given file exists
|
||||
func FileExists(name string) bool {
|
||||
if _, err := os.Stat(name); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func httpError(w http.ResponseWriter, code int) {
|
||||
http.Error(w, http.StatusText(code), code)
|
||||
}
|
||||
|
||||
// Blob defines the type, filename and whether or not a blob is publicly accessible or not.
|
||||
// A Blob also holds zero or more properties as a map of key/value pairs of string interpreted
|
||||
// by the client.
|
||||
type Blob struct {
|
||||
r io.ReadSeekCloser `json:"-"`
|
||||
|
||||
Type string `json:"type"`
|
||||
Public bool `json:"public"`
|
||||
Filename string `json:"-"`
|
||||
Properties map[string]string `json:"props"`
|
||||
}
|
||||
|
||||
// Close closes the blob and the underlying io.ReadSeekCloser
|
||||
func (b *Blob) Close() error { return b.r.Close() }
|
||||
|
||||
// Read reads data from the blob from the underlying io.ReadSeekCloser
|
||||
func (b *Blob) Read(p []byte) (n int, err error) { return b.r.Read(p) }
|
||||
|
||||
// SetHeaders sets HTTP headers on the net/http.Request object based on the blob's type, filename
|
||||
// and various other properties (if any).
|
||||
func (b *Blob) SetHeaders(r *http.Request) {
|
||||
// TODO: Implement this...
|
||||
}
|
||||
|
||||
// OpenBlob opens a blob at the given path and returns a Blob object
|
||||
func OpenBlob(fn string) (*Blob, error) {
|
||||
f, err := os.Open(fn)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: opening blob %s", err, fn)
|
||||
}
|
||||
b := &Blob{r: f, Filename: fn}
|
||||
|
||||
props := filepath.Join(filepath.Dir(fn), "props.json")
|
||||
|
||||
if FileExists(filepath.Dir(props)) {
|
||||
pf, err := os.Open(props)
|
||||
if err != nil {
|
||||
return b, fmt.Errorf("%w: opening blob props %s", err, props)
|
||||
}
|
||||
err = json.NewDecoder(pf).Decode(b)
|
||||
if err != nil {
|
||||
return b, fmt.Errorf("%w: opening blob props %s", err, props)
|
||||
}
|
||||
}
|
||||
|
||||
return b, nil
|
||||
}
|
||||
156
app/salty/salty-addr.go
Normal file
156
app/salty/salty-addr.go
Normal file
@@ -0,0 +1,156 @@
|
||||
package salty
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/keys-pub/keys"
|
||||
|
||||
"go.sour.is/pkg/lg"
|
||||
)
|
||||
|
||||
// Config represents a Salty Config for a User which at a minimum is required
|
||||
// to have an Endpoint and Key (Public Key)
|
||||
type Config struct {
|
||||
Endpoint string `json:"endpoint"`
|
||||
Key string `json:"key"`
|
||||
}
|
||||
|
||||
type Capabilities struct {
|
||||
AcceptEncoding string
|
||||
}
|
||||
|
||||
func (c Capabilities) String() string {
|
||||
if c.AcceptEncoding == "" {
|
||||
return "<nil>"
|
||||
}
|
||||
return fmt.Sprint("accept-encoding: ", c.AcceptEncoding)
|
||||
}
|
||||
|
||||
type Addr struct {
|
||||
User string
|
||||
Domain string
|
||||
|
||||
capabilities Capabilities
|
||||
discoveredDomain string
|
||||
dns DNSResolver
|
||||
endpoint *url.URL
|
||||
key *keys.EdX25519PublicKey
|
||||
}
|
||||
|
||||
// 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 (s *service) ParseAddr(addr string) (*Addr, error) {
|
||||
parts := strings.Split(strings.ToLower(addr), "@")
|
||||
if len(parts) != 2 {
|
||||
return nil, fmt.Errorf("expected nick@domain found %q", addr)
|
||||
}
|
||||
|
||||
return &Addr{User: parts[0], Domain: parts[1], dns: s.dns}, nil
|
||||
}
|
||||
|
||||
func (a *Addr) String() string {
|
||||
return fmt.Sprintf("%s@%s", a.User, a.Domain)
|
||||
}
|
||||
|
||||
// Hash returns the Hex(SHA256Sum()) of the Address
|
||||
func (a *Addr) Hash() string {
|
||||
return fmt.Sprintf("%x", sha256.Sum256([]byte(strings.ToLower(a.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.DiscoveredDomain(), 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.DiscoveredDomain(), a.Hash())
|
||||
}
|
||||
|
||||
// DiscoveredDomain returns the discovered domain (if any) of fallbacks to the Domain
|
||||
func (a *Addr) DiscoveredDomain() string {
|
||||
if a.discoveredDomain != "" {
|
||||
return a.discoveredDomain
|
||||
}
|
||||
return a.Domain
|
||||
}
|
||||
|
||||
func (a *Addr) Refresh(ctx context.Context) error {
|
||||
ctx, span := lg.Span(ctx)
|
||||
defer span.End()
|
||||
|
||||
span.AddEvent(fmt.Sprintf("Looking up SRV record for _salty._tcp.%s", a.Domain))
|
||||
if _, srv, err := a.dns.LookupSRV(ctx, "salty", "tcp", a.Domain); err == nil {
|
||||
if len(srv) > 0 {
|
||||
a.discoveredDomain = strings.TrimSuffix(srv[0].Target, ".")
|
||||
}
|
||||
span.AddEvent(fmt.Sprintf("Discovered salty services %s", a.discoveredDomain))
|
||||
} else if err != nil {
|
||||
span.RecordError(fmt.Errorf("error looking up SRV record for _salty._tcp.%s : %s", a.Domain, err))
|
||||
}
|
||||
|
||||
config, cap, err := fetchConfig(ctx, a.HashURI())
|
||||
if err != nil {
|
||||
// Fallback to plain user nick
|
||||
span.RecordError(err)
|
||||
|
||||
config, cap, err = fetchConfig(ctx, a.URI())
|
||||
}
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error looking up user %s: %w", a, err)
|
||||
span.RecordError(err)
|
||||
return err
|
||||
}
|
||||
key, err := keys.NewEdX25519PublicKeyFromID(keys.ID(config.Key))
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error parsing public key %s: %w", config.Key, err)
|
||||
span.RecordError(err)
|
||||
return err
|
||||
}
|
||||
a.key = key
|
||||
|
||||
u, err := url.Parse(config.Endpoint)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error parsing endpoint %s: %w", config.Endpoint, err)
|
||||
span.RecordError(err)
|
||||
return err
|
||||
}
|
||||
a.endpoint = u
|
||||
a.capabilities = cap
|
||||
|
||||
span.AddEvent(fmt.Sprintf("Discovered endpoint: %v", a.endpoint))
|
||||
span.AddEvent(fmt.Sprintf("Discovered capability: %v", a.capabilities))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func fetchConfig(ctx context.Context, addr string) (config Config, cap Capabilities, err error) {
|
||||
ctx, span := lg.Span(ctx)
|
||||
defer span.End()
|
||||
|
||||
var req *http.Request
|
||||
req, err = http.NewRequestWithContext(ctx, http.MethodGet, addr, nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err = json.NewDecoder(res.Body).Decode(&config); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
cap.AcceptEncoding = res.Header.Get("Accept-Encoding")
|
||||
|
||||
return
|
||||
}
|
||||
94
app/salty/salty-user.go
Normal file
94
app/salty/salty-user.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package salty
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/keys-pub/keys"
|
||||
"github.com/oklog/ulid/v2"
|
||||
|
||||
"go.sour.is/ev/event"
|
||||
"go.sour.is/pkg/gql"
|
||||
)
|
||||
|
||||
type SaltyUser struct {
|
||||
pubkey *keys.EdX25519PublicKey
|
||||
inbox ulid.ULID
|
||||
|
||||
event.IsAggregate
|
||||
}
|
||||
|
||||
var _ event.Aggregate = (*SaltyUser)(nil)
|
||||
|
||||
// ApplyEvent applies the event to the aggrigate state
|
||||
func (a *SaltyUser) ApplyEvent(lis ...event.Event) {
|
||||
for _, e := range lis {
|
||||
switch e := e.(type) {
|
||||
case *UserRegistered:
|
||||
// a.name = e.Name
|
||||
a.pubkey = e.Pubkey
|
||||
a.inbox = e.EventMeta().EventID
|
||||
// a.SetStreamID(a.streamID())
|
||||
default:
|
||||
log.Printf("unknown event %T", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (a *SaltyUser) OnUserRegister(pubkey *keys.EdX25519PublicKey) error {
|
||||
if err := event.NotExists(a); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
event.Raise(a, &UserRegistered{Pubkey: pubkey})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *SaltyUser) Inbox() string { return a.inbox.String() }
|
||||
func (a *SaltyUser) Pubkey() string { return a.pubkey.String() }
|
||||
func (s *SaltyUser) Endpoint(ctx context.Context) (string, error) {
|
||||
svc := gql.FromContext[contextKey, *service](ctx, saltyKey)
|
||||
return url.JoinPath(svc.BaseURL(), s.inbox.String())
|
||||
}
|
||||
|
||||
type UserRegistered struct {
|
||||
Name string
|
||||
Pubkey *keys.EdX25519PublicKey
|
||||
|
||||
event.IsEvent
|
||||
}
|
||||
|
||||
var _ event.Event = (*UserRegistered)(nil)
|
||||
|
||||
func (e *UserRegistered) MarshalBinary() (text []byte, err error) {
|
||||
var b bytes.Buffer
|
||||
b.WriteString(e.Name)
|
||||
b.WriteRune('\t')
|
||||
b.WriteString(e.Pubkey.String())
|
||||
|
||||
return b.Bytes(), nil
|
||||
}
|
||||
func (e *UserRegistered) UnmarshalBinary(b []byte) error {
|
||||
name, pub, ok := bytes.Cut(b, []byte{'\t'})
|
||||
if !ok {
|
||||
return fmt.Errorf("parse error")
|
||||
}
|
||||
|
||||
var err error
|
||||
e.Name = string(name)
|
||||
e.Pubkey, err = keys.NewEdX25519PublicKeyFromID(keys.ID(pub))
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func NickToStreamID(nick string) string {
|
||||
return fmt.Sprintf("saltyuser-%x", sha256.Sum256([]byte(strings.ToLower(nick))))
|
||||
}
|
||||
func HashToStreamID(hash string) string {
|
||||
return fmt.Sprint("saltyuser-", hash)
|
||||
}
|
||||
13
app/salty/salty.graphqls
Normal file
13
app/salty/salty.graphqls
Normal file
@@ -0,0 +1,13 @@
|
||||
extend type Query {
|
||||
saltyUser(nick: String!): SaltyUser
|
||||
}
|
||||
|
||||
extend type Mutation {
|
||||
createSaltyUser(nick: String! pubkey: String!): SaltyUser
|
||||
}
|
||||
|
||||
type SaltyUser @goModel(model: "go.sour.is/tools/app/salty.SaltyUser"){
|
||||
pubkey: String!
|
||||
inbox: String!
|
||||
endpoint: String!
|
||||
}
|
||||
397
app/salty/service.go
Normal file
397
app/salty/service.go
Normal file
@@ -0,0 +1,397 @@
|
||||
package salty
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/keys-pub/keys"
|
||||
"go.mills.io/saltyim"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
"go.uber.org/multierr"
|
||||
|
||||
"go.sour.is/ev"
|
||||
"go.sour.is/ev/event"
|
||||
"go.sour.is/pkg/gql"
|
||||
"go.sour.is/pkg/lg"
|
||||
)
|
||||
|
||||
type DNSResolver interface {
|
||||
LookupSRV(ctx context.Context, service, proto, name string) (string, []*net.SRV, error)
|
||||
}
|
||||
|
||||
type service struct {
|
||||
baseURL string
|
||||
es *ev.EventStore
|
||||
dns DNSResolver
|
||||
|
||||
m_create_user metric.Int64Counter
|
||||
m_get_user metric.Int64Counter
|
||||
m_api_ping metric.Int64Counter
|
||||
m_api_register metric.Int64Counter
|
||||
m_api_lookup metric.Int64Counter
|
||||
m_api_send metric.Int64Counter
|
||||
m_req_time metric.Int64Histogram
|
||||
|
||||
opts []Option
|
||||
}
|
||||
|
||||
type Option interface {
|
||||
ApplySalty(*service)
|
||||
}
|
||||
|
||||
type WithBaseURL string
|
||||
|
||||
func (o WithBaseURL) ApplySalty(s *service) {
|
||||
s.baseURL = string(o)
|
||||
}
|
||||
|
||||
type contextKey struct {
|
||||
name string
|
||||
}
|
||||
|
||||
var saltyKey = contextKey{"salty"}
|
||||
|
||||
type SaltyResolver interface {
|
||||
CreateSaltyUser(ctx context.Context, nick string, pub string) (*SaltyUser, error)
|
||||
SaltyUser(ctx context.Context, nick string) (*SaltyUser, error)
|
||||
IsResolver()
|
||||
}
|
||||
|
||||
func New(ctx context.Context, es *ev.EventStore, opts ...Option) (*service, error) {
|
||||
ctx, span := lg.Span(ctx)
|
||||
defer span.End()
|
||||
|
||||
if err := event.Register(ctx, &UserRegistered{}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := event.RegisterName(ctx, "domain.UserRegistered", &UserRegistered{}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
m := lg.Meter(ctx)
|
||||
|
||||
svc := &service{opts: opts, es: es, dns: net.DefaultResolver}
|
||||
|
||||
for _, o := range opts {
|
||||
o.ApplySalty(svc)
|
||||
|
||||
if o, ok := o.(interface{ Setup(context.Context) error }); ok {
|
||||
if err := o.Setup(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var err, errs error
|
||||
svc.m_create_user, err = m.Int64Counter("salty_create_user",
|
||||
metric.WithDescription("salty create user graphql called"),
|
||||
)
|
||||
errs = multierr.Append(errs, err)
|
||||
|
||||
svc.m_get_user, err = m.Int64Counter("salty_get_user",
|
||||
metric.WithDescription("salty get user graphql called"),
|
||||
)
|
||||
errs = multierr.Append(errs, err)
|
||||
|
||||
svc.m_api_ping, err = m.Int64Counter("salty_api_ping",
|
||||
metric.WithDescription("salty api ping called"),
|
||||
)
|
||||
errs = multierr.Append(errs, err)
|
||||
|
||||
svc.m_api_register, err = m.Int64Counter("salty_api_register",
|
||||
metric.WithDescription("salty api register"),
|
||||
)
|
||||
errs = multierr.Append(errs, err)
|
||||
|
||||
svc.m_api_lookup, err = m.Int64Counter("salty_api_lookup",
|
||||
metric.WithDescription("salty api ping lookup"),
|
||||
)
|
||||
errs = multierr.Append(errs, err)
|
||||
|
||||
svc.m_api_send, err = m.Int64Counter("salty_api_send",
|
||||
metric.WithDescription("salty api ping send"),
|
||||
)
|
||||
errs = multierr.Append(errs, err)
|
||||
|
||||
svc.m_req_time, err = m.Int64Histogram("salty_request_time",
|
||||
metric.WithDescription("histogram of requests"),
|
||||
metric.WithUnit("ns"),
|
||||
)
|
||||
errs = multierr.Append(errs, err)
|
||||
|
||||
span.RecordError(err)
|
||||
|
||||
return svc, errs
|
||||
}
|
||||
|
||||
func (s *service) BaseURL() string {
|
||||
if s == nil {
|
||||
return "http://missing.context/"
|
||||
}
|
||||
return s.baseURL
|
||||
}
|
||||
|
||||
func (s *service) RegisterHTTP(mux *http.ServeMux) {
|
||||
for _, o := range s.opts {
|
||||
if o, ok := o.(interface{ RegisterHTTP(mux *http.ServeMux) }); ok {
|
||||
o.RegisterHTTP(mux)
|
||||
}
|
||||
}
|
||||
}
|
||||
func (s *service) RegisterAPIv1(mux *http.ServeMux) {
|
||||
mux.HandleFunc("/ping", s.apiv1)
|
||||
mux.HandleFunc("/register", s.apiv1)
|
||||
mux.HandleFunc("/lookup/", s.apiv1)
|
||||
mux.HandleFunc("/send", s.apiv1)
|
||||
|
||||
for _, o := range s.opts {
|
||||
if o, ok := o.(interface{ RegisterAPIv1(mux *http.ServeMux) }); ok {
|
||||
o.RegisterAPIv1(mux)
|
||||
}
|
||||
}
|
||||
}
|
||||
func (s *service) RegisterWellKnown(mux *http.ServeMux) {
|
||||
mux.Handle("/salty/", lg.Htrace(s, "lookup"))
|
||||
|
||||
for _, o := range s.opts {
|
||||
if o, ok := o.(interface{ RegisterWellKnown(mux *http.ServeMux) }); ok {
|
||||
o.RegisterWellKnown(mux)
|
||||
}
|
||||
}
|
||||
}
|
||||
func (s *service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
ctx, span := lg.Span(ctx)
|
||||
defer span.End()
|
||||
|
||||
start := time.Now()
|
||||
defer s.m_req_time.Record(ctx, time.Since(start).Milliseconds())
|
||||
|
||||
addr := "saltyuser-" + strings.TrimPrefix(r.URL.Path, "/salty/")
|
||||
addr = strings.TrimSuffix(addr, ".json")
|
||||
|
||||
span.AddEvent(fmt.Sprint("find ", addr))
|
||||
a, err := ev.Update(ctx, s.es, addr, func(ctx context.Context, agg *SaltyUser) error { return nil })
|
||||
switch {
|
||||
case errors.Is(err, event.ErrShouldExist):
|
||||
span.RecordError(err)
|
||||
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
case err != nil:
|
||||
span.RecordError(err)
|
||||
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
basePath, _ := url.JoinPath(s.baseURL, a.inbox.String())
|
||||
|
||||
err = json.NewEncoder(w).Encode(
|
||||
struct {
|
||||
Endpoint string `json:"endpoint"`
|
||||
Key string `json:"key"`
|
||||
}{
|
||||
Endpoint: basePath,
|
||||
Key: a.pubkey.ID().String(),
|
||||
})
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *service) IsResolver() {}
|
||||
func (s *service) GetMiddleware() func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r = r.WithContext(gql.ToContext(r.Context(), saltyKey, s))
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
}
|
||||
func (s *service) CreateSaltyUser(ctx context.Context, nick string, pub string) (*SaltyUser, error) {
|
||||
ctx, span := lg.Span(ctx)
|
||||
defer span.End()
|
||||
|
||||
s.m_create_user.Add(ctx, 1)
|
||||
start := time.Now()
|
||||
defer s.m_req_time.Record(ctx, time.Since(start).Milliseconds())
|
||||
|
||||
streamID := NickToStreamID(nick)
|
||||
span.AddEvent(streamID)
|
||||
|
||||
return s.createSaltyUser(ctx, streamID, pub)
|
||||
}
|
||||
func (s *service) createSaltyUser(ctx context.Context, streamID, pub string) (*SaltyUser, error) {
|
||||
ctx, span := lg.Span(ctx)
|
||||
defer span.End()
|
||||
|
||||
key, err := keys.NewEdX25519PublicKeyFromID(keys.ID(pub))
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
a, err := ev.Create(ctx, s.es, streamID, func(ctx context.Context, agg *SaltyUser) error {
|
||||
return agg.OnUserRegister(key)
|
||||
})
|
||||
switch {
|
||||
case errors.Is(err, ev.ErrShouldNotExist):
|
||||
span.RecordError(err)
|
||||
return nil, fmt.Errorf("user exists: %w", err)
|
||||
|
||||
case err != nil:
|
||||
span.RecordError(err)
|
||||
return nil, fmt.Errorf("internal error: %w", err)
|
||||
}
|
||||
|
||||
return a, nil
|
||||
}
|
||||
func (s *service) SaltyUser(ctx context.Context, nick string) (*SaltyUser, error) {
|
||||
ctx, span := lg.Span(ctx)
|
||||
defer span.End()
|
||||
|
||||
s.m_get_user.Add(ctx, 1)
|
||||
start := time.Now()
|
||||
defer s.m_req_time.Record(ctx, time.Since(start).Milliseconds())
|
||||
|
||||
streamID := fmt.Sprintf("saltyuser-%x", sha256.Sum256([]byte(strings.ToLower(nick))))
|
||||
span.AddEvent(streamID)
|
||||
|
||||
a, err := ev.Update(ctx, s.es, streamID, func(ctx context.Context, agg *SaltyUser) error { return nil })
|
||||
switch {
|
||||
case errors.Is(err, ev.ErrShouldExist):
|
||||
span.RecordError(err)
|
||||
return nil, fmt.Errorf("user not found")
|
||||
|
||||
case err != nil:
|
||||
span.RecordError(err)
|
||||
return nil, fmt.Errorf("%w internal error", err)
|
||||
}
|
||||
|
||||
return a, err
|
||||
}
|
||||
|
||||
func (s *service) apiv1(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
ctx, span := lg.Span(ctx)
|
||||
defer span.End()
|
||||
|
||||
start := time.Now()
|
||||
defer s.m_req_time.Record(ctx, time.Since(start).Nanoseconds())
|
||||
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
switch {
|
||||
case r.URL.Path == "/ping":
|
||||
s.m_api_ping.Add(ctx, 1)
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{}`))
|
||||
|
||||
case strings.HasPrefix(r.URL.Path, "/lookup/"):
|
||||
s.m_api_lookup.Add(ctx, 1)
|
||||
|
||||
addr, err := s.ParseAddr(strings.TrimPrefix(r.URL.Path, "/lookup/"))
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
err = addr.Refresh(ctx)
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = json.NewEncoder(w).Encode(addr)
|
||||
span.RecordError(err)
|
||||
return
|
||||
|
||||
default:
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
case http.MethodPost:
|
||||
switch r.URL.Path {
|
||||
case "/register":
|
||||
s.m_api_register.Add(ctx, 1)
|
||||
|
||||
req, signer, err := saltyim.NewRegisterRequest(r.Body)
|
||||
if err != nil {
|
||||
span.RecordError(fmt.Errorf("error parsing register request: %w", err))
|
||||
http.Error(w, "Bad Request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if signer != req.Key {
|
||||
http.Error(w, "Bad Request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = s.createSaltyUser(ctx, HashToStreamID(req.Hash), req.Key)
|
||||
if errors.Is(err, event.ErrShouldNotExist) {
|
||||
http.Error(w, "Already Exists", http.StatusConflict)
|
||||
return
|
||||
} else if err != nil {
|
||||
http.Error(w, "Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
http.Error(w, "Endpoint Created", http.StatusCreated)
|
||||
return
|
||||
|
||||
case "/send":
|
||||
s.m_api_send.Add(ctx, 1)
|
||||
|
||||
req, signer, err := saltyim.NewSendRequest(r.Body)
|
||||
if err != nil {
|
||||
span.RecordError(fmt.Errorf("error parsing send request: %w", err))
|
||||
http.Error(w, "Bad Request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
// TODO: Do something with signer?
|
||||
span.AddEvent(fmt.Sprintf("request signed by %s", signer))
|
||||
|
||||
u, err := url.Parse(req.Endpoint)
|
||||
if err != nil {
|
||||
span.RecordError(fmt.Errorf("error parsing endpoint %s: %w", req.Endpoint, err))
|
||||
http.Error(w, "Bad Endpoint", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if !u.IsAbs() {
|
||||
span.RecordError(fmt.Errorf("endpoint %s is not an absolute uri: %w", req.Endpoint, err))
|
||||
http.Error(w, "Bad Endpoint", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: Queue up an internal retry and return immediately on failure?
|
||||
if err := saltyim.Send(req.Endpoint, req.Message, req.Capabilities); err != nil {
|
||||
span.RecordError(fmt.Errorf("error sending message to %s: %w", req.Endpoint, err))
|
||||
http.Error(w, "Send Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
http.Error(w, "Message Accepted", http.StatusAccepted)
|
||||
|
||||
return
|
||||
|
||||
default:
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
default:
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user