fix: webfinger redirects

This commit is contained in:
Jon Lundy 2023-04-02 21:00:22 -06:00
parent 9168f5c7bc
commit b8c2f9f510
Signed by untrusted user who does not match committer: xuu
GPG Key ID: C63E6D61F3035024
15 changed files with 79 additions and 114 deletions

View File

@ -2,7 +2,6 @@ package peerfinder
import ( import (
"bytes" "bytes"
"encoding/json"
"github.com/tj/go-semver" "github.com/tj/go-semver"
@ -12,7 +11,7 @@ import (
type Info struct { type Info struct {
ScriptVersion string `json:"script_version"` ScriptVersion string `json:"script_version"`
event.AggregateRoot event.IsAggregate
} }
var _ event.Aggregate = (*Info)(nil) var _ event.Aggregate = (*Info)(nil)
@ -55,10 +54,3 @@ type VersionChanged struct {
} }
var _ event.Event = (*VersionChanged)(nil) var _ event.Event = (*VersionChanged)(nil)
func (e *VersionChanged) MarshalBinary() (text []byte, err error) {
return json.Marshal(e)
}
func (e *VersionChanged) UnmarshalBinary(b []byte) error {
return json.Unmarshal(b, e)
}

View File

@ -94,7 +94,7 @@ func (p *Peer) CanSupport(ip string) bool {
type PeerResults struct { type PeerResults struct {
set.Set[string] set.Set[string]
event.AggregateRoot event.IsAggregate
} }
func (p *PeerResults) ApplyEvent(lis ...event.Event) { func (p *PeerResults) ApplyEvent(lis ...event.Event) {

View File

@ -14,7 +14,7 @@ import (
) )
type Request struct { type Request struct {
event.AggregateRoot event.IsAggregate
RequestID string `json:"req_id"` RequestID string `json:"req_id"`
RequestIP string `json:"req_ip"` RequestIP string `json:"req_ip"`
@ -213,12 +213,6 @@ func (r *ResultSubmitted) Created() time.Time {
var _ event.Event = (*ResultSubmitted)(nil) var _ event.Event = (*ResultSubmitted)(nil)
func (e *ResultSubmitted) MarshalBinary() (text []byte, err error) {
return json.Marshal(e)
}
func (e *ResultSubmitted) UnmarshalBinary(b []byte) error {
return json.Unmarshal(b, e)
}
func (e *ResultSubmitted) String() string { func (e *ResultSubmitted) String() string {
return fmt.Sprintf("id: %s\npeer: %s\nversion: %s\nlatency: %0.4f", e.RequestID, e.PeerID, e.PeerVersion, e.Latency) return fmt.Sprintf("id: %s\npeer: %s\nversion: %s\nlatency: %0.4f", e.RequestID, e.PeerID, e.PeerVersion, e.Latency)
} }
@ -231,12 +225,6 @@ type RequestTruncated struct {
var _ event.Event = (*RequestTruncated)(nil) var _ event.Event = (*RequestTruncated)(nil)
func (e *RequestTruncated) MarshalBinary() (text []byte, err error) {
return json.Marshal(e)
}
func (e *RequestTruncated) UnmarshalBinary(b []byte) error {
return json.Unmarshal(b, e)
}
func (e *RequestTruncated) String() string { func (e *RequestTruncated) String() string {
return fmt.Sprintf("request truncated id: %s\n", e.RequestID) return fmt.Sprintf("request truncated id: %s\n", e.RequestID)
} }

View File

@ -20,7 +20,7 @@ type SaltyUser struct {
pubkey *keys.EdX25519PublicKey pubkey *keys.EdX25519PublicKey
inbox ulid.ULID inbox ulid.ULID
event.AggregateRoot event.IsAggregate
} }
var _ event.Aggregate = (*SaltyUser)(nil) var _ event.Aggregate = (*SaltyUser)(nil)

View File

@ -1,8 +1,6 @@
package webfinger package webfinger
import ( import (
"encoding/json"
"go.sour.is/ev/pkg/es/event" "go.sour.is/ev/pkg/es/event"
) )
@ -14,13 +12,6 @@ type SubjectSet struct {
event.IsEvent event.IsEvent
} }
func (e *SubjectSet) MarshalBinary() (text []byte, err error) {
return json.Marshal(e)
}
func (e *SubjectSet) UnmarshalBinary(b []byte) error {
return json.Unmarshal(b, e)
}
var _ event.Event = (*SubjectSet)(nil) var _ event.Event = (*SubjectSet)(nil)
type SubjectDeleted struct { type SubjectDeleted struct {
@ -29,13 +20,6 @@ type SubjectDeleted struct {
event.IsEvent event.IsEvent
} }
func (e *SubjectDeleted) MarshalBinary() (text []byte, err error) {
return json.Marshal(e)
}
func (e *SubjectDeleted) UnmarshalBinary(b []byte) error {
return json.Unmarshal(b, e)
}
var _ event.Event = (*SubjectDeleted)(nil) var _ event.Event = (*SubjectDeleted)(nil)
type LinkSet struct { type LinkSet struct {
@ -48,13 +32,6 @@ type LinkSet struct {
event.IsEvent event.IsEvent
} }
func (e *LinkSet) MarshalBinary() (text []byte, err error) {
return json.Marshal(e)
}
func (e *LinkSet) UnmarshalBinary(b []byte) error {
return json.Unmarshal(b, e)
}
var _ event.Event = (*LinkSet)(nil) var _ event.Event = (*LinkSet)(nil)
type LinkDeleted struct { type LinkDeleted struct {
@ -63,11 +40,4 @@ type LinkDeleted struct {
event.IsEvent event.IsEvent
} }
func (e *LinkDeleted) MarshalBinary() (text []byte, err error) {
return json.Marshal(e)
}
func (e *LinkDeleted) UnmarshalBinary(b []byte) error {
return json.Unmarshal(b, e)
}
var _ event.Event = (*LinkDeleted)(nil) var _ event.Event = (*LinkDeleted)(nil)

View File

@ -29,7 +29,7 @@ type JRD struct {
Links Links `json:"links,omitempty" yaml:"links,omitempty"` Links Links `json:"links,omitempty" yaml:"links,omitempty"`
deleted bool deleted bool
event.AggregateRoot `yaml:"-"` event.IsAggregate `yaml:"-"`
} }
func (a *JRD) CloneValues() *JRD { func (a *JRD) CloneValues() *JRD {

View File

@ -231,14 +231,13 @@ func (s *service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
host, _ := splitHostPort(r.Host) if u := Parse(resource); u != nil && !s.self.Has(u.URL.Host) {
if u := Parse(resource); u != nil && !s.self.Has(host) {
redirect := &url.URL{} redirect := &url.URL{}
redirect.Scheme = u.URL.Scheme redirect.Scheme = "https"
redirect.Host = u.URL.Host redirect.Host = u.URL.Host
redirect.RawQuery = r.URL.RawQuery redirect.RawQuery = r.URL.RawQuery
redirect.Path = "/.well-known/webfinger" redirect.Path = "/.well-known/webfinger"
fmt.Println(redirect)
w.Header().Set("location", redirect.String()) w.Header().Set("location", redirect.String())
w.WriteHeader(http.StatusSeeOther) w.WriteHeader(http.StatusSeeOther)
return return

View File

@ -26,7 +26,7 @@ func TestMain(m *testing.M) {
os.Setenv("EV_DATA", "mem:") os.Setenv("EV_DATA", "mem:")
os.Setenv("EV_HTTP", "[::1]:61234") os.Setenv("EV_HTTP", "[::1]:61234")
os.Setenv("WEBFINGER_DOMAINS", "::1") os.Setenv("WEBFINGER_DOMAINS", "sour.is")
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
@ -76,7 +76,7 @@ func TestCreateResource(t *testing.T) {
is.NoErr(err) is.NoErr(err)
jrd := &webfinger.JRD{ jrd := &webfinger.JRD{
Subject: "me@sour.is", Subject: "acct:me@sour.is",
Properties: map[string]*string{ Properties: map[string]*string{
"foo": ptr("bar"), "foo": ptr("bar"),
}, },
@ -104,7 +104,7 @@ func TestCreateResource(t *testing.T) {
is.Equal(res.StatusCode, http.StatusAlreadyReported) is.Equal(res.StatusCode, http.StatusAlreadyReported)
// fetch // fetch
req, err = http.NewRequest(http.MethodGet, "http://[::1]:61234/.well-known/webfinger?resource=me@sour.is", nil) req, err = http.NewRequest(http.MethodGet, "http://[::1]:61234/.well-known/webfinger?resource=acct:me@sour.is", nil)
is.NoErr(err) is.NoErr(err)
res, err = http.DefaultClient.Do(req) res, err = http.DefaultClient.Do(req)

View File

@ -2,7 +2,6 @@ package ev_test
import ( import (
"context" "context"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"testing" "testing"
@ -29,7 +28,7 @@ type Thing struct {
Name string Name string
Value string Value string
event.AggregateRoot event.IsAggregate
} }
func (a *Thing) ApplyEvent(lis ...event.Event) { func (a *Thing) ApplyEvent(lis ...event.Event) {
@ -51,13 +50,6 @@ type ValueSet struct {
event.IsEvent event.IsEvent
} }
func (e *ValueSet) MarshalBinary() ([]byte, error) {
return json.Marshal(e)
}
func (e *ValueSet) UnmarshalBinary(b []byte) error {
return json.Unmarshal(b, e)
}
func TestES(t *testing.T) { func TestES(t *testing.T) {
is := is.New(t) is := is.New(t)

View File

@ -12,7 +12,7 @@ type Aggregate interface {
// ApplyEvent applies the event to the aggrigate state // ApplyEvent applies the event to the aggrigate state
ApplyEvent(...Event) ApplyEvent(...Event)
AggregateRootInterface AggregateRoot
} }
func Start(a Aggregate, i uint64) { func Start(a Aggregate, i uint64) {
@ -49,7 +49,7 @@ func ShouldExist(a Aggregate) error {
return nil return nil
} }
type AggregateRootInterface interface { type AggregateRoot interface {
// Events returns the aggregate events // Events returns the aggregate events
// pass true for only uncommitted events // pass true for only uncommitted events
Events(bool) Events Events(bool) Events
@ -68,9 +68,9 @@ type AggregateRootInterface interface {
Commit() Commit()
} }
var _ AggregateRootInterface = &AggregateRoot{} var _ AggregateRoot = &IsAggregate{}
type AggregateRoot struct { type IsAggregate struct {
events Events events Events
streamID string streamID string
firstIndex uint64 firstIndex uint64
@ -79,12 +79,12 @@ type AggregateRoot struct {
mu sync.RWMutex mu sync.RWMutex
} }
func (a *AggregateRoot) Commit() { a.lastIndex = uint64(len(a.events)) } func (a *IsAggregate) Commit() { a.lastIndex = uint64(len(a.events)) }
func (a *AggregateRoot) StreamID() string { return a.streamID } func (a *IsAggregate) StreamID() string { return a.streamID }
func (a *AggregateRoot) SetStreamID(streamID string) { a.streamID = streamID } func (a *IsAggregate) SetStreamID(streamID string) { a.streamID = streamID }
func (a *AggregateRoot) StreamVersion() uint64 { return a.lastIndex } func (a *IsAggregate) StreamVersion() uint64 { return a.lastIndex }
func (a *AggregateRoot) Version() uint64 { return a.firstIndex + uint64(len(a.events)) } func (a *IsAggregate) Version() uint64 { return a.firstIndex + uint64(len(a.events)) }
func (a *AggregateRoot) Events(new bool) Events { func (a *IsAggregate) Events(new bool) Events {
a.mu.RLock() a.mu.RLock()
defer a.mu.RUnlock() defer a.mu.RUnlock()
@ -99,13 +99,13 @@ func (a *AggregateRoot) Events(new bool) Events {
return lis return lis
} }
func (a *AggregateRoot) start(i uint64) { func (a *IsAggregate) start(i uint64) {
a.firstIndex = i a.firstIndex = i
a.lastIndex = i a.lastIndex = i
} }
//lint:ignore U1000 is called by embeded interface //lint:ignore U1000 is called by embeded interface
func (a *AggregateRoot) raise(lis ...Event) { //nolint func (a *IsAggregate) raise(lis ...Event) { //nolint
a.mu.Lock() a.mu.Lock()
defer a.mu.Unlock() defer a.mu.Unlock()
@ -115,7 +115,7 @@ func (a *AggregateRoot) raise(lis ...Event) { //nolint
} }
//lint:ignore U1000 is called by embeded interface //lint:ignore U1000 is called by embeded interface
func (a *AggregateRoot) append(lis ...Event) { func (a *IsAggregate) append(lis ...Event) {
a.mu.Lock() a.mu.Lock()
defer a.mu.Unlock() defer a.mu.Unlock()
@ -125,7 +125,7 @@ func (a *AggregateRoot) append(lis ...Event) {
a.lastIndex += uint64(len(lis)) a.lastIndex += uint64(len(lis))
} }
func (a *AggregateRoot) posStartAt(lis ...Event) { func (a *IsAggregate) posStartAt(lis ...Event) {
for i, e := range lis { for i, e := range lis {
m := e.EventMeta() m := e.EventMeta()
m.Position = a.lastIndex + uint64(i) + 1 m.Position = a.lastIndex + uint64(i) + 1

View File

@ -1,7 +1,6 @@
package event_test package event_test
import ( import (
"encoding/json"
"testing" "testing"
"go.sour.is/ev/pkg/es/event" "go.sour.is/ev/pkg/es/event"
@ -10,7 +9,7 @@ import (
type Agg struct { type Agg struct {
Value string Value string
event.AggregateRoot event.IsAggregate
} }
var _ event.Aggregate = (*Agg)(nil) var _ event.Aggregate = (*Agg)(nil)
@ -38,13 +37,6 @@ type ValueApplied struct {
var _ event.Event = (*ValueApplied)(nil) var _ event.Event = (*ValueApplied)(nil)
func (e *ValueApplied) MarshalBinary() ([]byte, error) {
return json.Marshal(e)
}
func (e *ValueApplied) UnmarshalBinary(b []byte) error {
return json.Unmarshal(b, e)
}
func TestAggregate(t *testing.T) { func TestAggregate(t *testing.T) {
agg := &Agg{} agg := &Agg{}
event.Append(agg, &ValueApplied{Value: "one"}) event.Append(agg, &ValueApplied{Value: "one"})

View File

@ -4,7 +4,6 @@ package event
import ( import (
"context" "context"
"crypto/rand" "crypto/rand"
"encoding"
"fmt" "fmt"
"io" "io"
"strconv" "strconv"
@ -32,9 +31,6 @@ func getULID() ulid.ULID {
type Event interface { type Event interface {
EventMeta() Meta EventMeta() Meta
SetEventMeta(Meta) SetEventMeta(Meta)
encoding.BinaryMarshaler
encoding.BinaryUnmarshaler
} }
// Events is a list of events // Events is a list of events

View File

@ -3,6 +3,7 @@ package event
import ( import (
"bytes" "bytes"
"context" "context"
"encoding"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/url" "net/url"
@ -151,7 +152,8 @@ func GetContainer(ctx context.Context, s string) Event {
return e return e
} }
func MarshalBinary(e Event) (txt []byte, err error) { func MarshalBinary(e Event) ([]byte, error) {
var err error
b := &bytes.Buffer{} b := &bytes.Buffer{}
m := e.EventMeta() m := e.EventMeta()
@ -167,10 +169,22 @@ func MarshalBinary(e Event) (txt []byte, err error) {
return nil, err return nil, err
} }
b.WriteRune('\t') b.WriteRune('\t')
switch e := e.(type) {
case encoding.BinaryMarshaler:
var txt []byte
if txt, err = e.MarshalBinary(); err != nil { if txt, err = e.MarshalBinary(); err != nil {
return nil, err return nil, err
} }
_, err = b.Write(txt) _, err = b.Write(txt)
case encoding.TextMarshaler:
var txt []byte
if txt, err = e.MarshalText(); err != nil {
return nil, err
}
_, err = b.Write(txt)
default:
err = json.NewEncoder(b).Encode(e)
}
return b.Bytes(), err return b.Bytes(), err
} }
@ -200,12 +214,23 @@ func UnmarshalBinary(ctx context.Context, txt []byte, pos uint64) (e Event, err
eventType := string(sp[2]) eventType := string(sp[2])
e = GetContainer(ctx, eventType) e = GetContainer(ctx, eventType)
span.AddEvent(fmt.Sprintf("%s == %T", eventType, e)) span.AddEvent(fmt.Sprintf("%s == %T", eventType, e))
switch e := e.(type) {
case encoding.BinaryUnmarshaler:
if err = e.UnmarshalBinary(sp[3]); err != nil { if err = e.UnmarshalBinary(sp[3]); err != nil {
span.RecordError(err) span.RecordError(err)
return nil, err return nil, err
} }
case encoding.TextUnmarshaler:
if err = e.UnmarshalText(sp[3]); err != nil {
span.RecordError(err)
return nil, err
}
default:
if err = json.Unmarshal(sp[3], e); err != nil {
span.RecordError(err)
return nil, err
}
}
e.SetEventMeta(m) e.SetEventMeta(m)
return e, nil return e, nil

View File

@ -2,6 +2,8 @@ package es
import ( import (
"context" "context"
"encoding"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"net/http" "net/http"
@ -164,8 +166,17 @@ func (e *GQLEvent) Values() map[string]interface{} {
return event.Values(e.e) return event.Values(e.e)
} }
func (e *GQLEvent) Bytes() (string, error) { func (e *GQLEvent) Bytes() (string, error) {
b, err := e.e.MarshalBinary() switch e := e.e.(type) {
case encoding.BinaryMarshaler:
b, err := e.MarshalBinary()
return string(b), err return string(b), err
case encoding.TextMarshaler:
b, err := e.MarshalText()
return string(b), err
default:
b, err := json.Marshal(e)
return string(b), err
}
} }
func (e *GQLEvent) Meta() *event.Meta { func (e *GQLEvent) Meta() *event.Meta {
meta := e.e.EventMeta() meta := e.e.EventMeta()

View File

@ -82,10 +82,10 @@ func Handler(title string, endpoint string) http.HandlerFunc {
"endpoint": endpoint, "endpoint": endpoint,
"endpointIsAbsolute": endpointHasScheme(endpoint), "endpointIsAbsolute": endpointHasScheme(endpoint),
"subscriptionEndpoint": getSubscriptionEndpoint(endpoint), "subscriptionEndpoint": getSubscriptionEndpoint(endpoint),
"version": "2.0.13", "version": "2.4.1",
"reactVersion": "17.0.2", "reactVersion": "17.0.2",
"cssSRI": "sha256-qKvndYgkAMQOBoa1SZF9NlbIig+kQ3Fk4f8wlrEqBLw=", "cssSRI": "sha256-bGeEsMhcAqeXBjh2w0eQzBTFAxwlxhM0PKIKqMshlnk=",
"jsSRI": "sha256-dExtzxjgqXfOgQ94xw079jAjd4dPAFrO2Qz6I3Yd9Ko=", "jsSRI": "sha256-s+f7CFAPSUIygFnRC2nfoiEKd3liCUy+snSdYFAoLUc=",
"reactSRI": "sha256-Ipu/TQ50iCCVZBUsZyNJfxrDk0E2yhaEIz0vqI+kFG8=", "reactSRI": "sha256-Ipu/TQ50iCCVZBUsZyNJfxrDk0E2yhaEIz0vqI+kFG8=",
"reactDOMSRI": "sha256-nbMykgB6tsOFJ7OdVmPpdqMFVk4ZsqWocT6issAPUF0=", "reactDOMSRI": "sha256-nbMykgB6tsOFJ7OdVmPpdqMFVk4ZsqWocT6issAPUF0=",
}) })