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 (
"bytes"
"encoding/json"
"github.com/tj/go-semver"
@ -12,7 +11,7 @@ import (
type Info struct {
ScriptVersion string `json:"script_version"`
event.AggregateRoot
event.IsAggregate
}
var _ event.Aggregate = (*Info)(nil)
@ -55,10 +54,3 @@ type VersionChanged struct {
}
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 {
set.Set[string]
event.AggregateRoot
event.IsAggregate
}
func (p *PeerResults) ApplyEvent(lis ...event.Event) {

View File

@ -14,7 +14,7 @@ import (
)
type Request struct {
event.AggregateRoot
event.IsAggregate
RequestID string `json:"req_id"`
RequestIP string `json:"req_ip"`
@ -213,12 +213,6 @@ func (r *ResultSubmitted) Created() time.Time {
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 {
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)
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 {
return fmt.Sprintf("request truncated id: %s\n", e.RequestID)
}

View File

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

View File

@ -1,8 +1,6 @@
package webfinger
import (
"encoding/json"
"go.sour.is/ev/pkg/es/event"
)
@ -14,13 +12,6 @@ type SubjectSet struct {
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)
type SubjectDeleted struct {
@ -29,13 +20,6 @@ type SubjectDeleted struct {
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)
type LinkSet struct {
@ -48,13 +32,6 @@ type LinkSet struct {
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)
type LinkDeleted struct {
@ -63,11 +40,4 @@ type LinkDeleted struct {
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)

View File

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

View File

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

View File

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

View File

@ -2,7 +2,6 @@ package ev_test
import (
"context"
"encoding/json"
"errors"
"fmt"
"testing"
@ -29,7 +28,7 @@ type Thing struct {
Name string
Value string
event.AggregateRoot
event.IsAggregate
}
func (a *Thing) ApplyEvent(lis ...event.Event) {
@ -51,13 +50,6 @@ type ValueSet struct {
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) {
is := is.New(t)

View File

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

View File

@ -1,7 +1,6 @@
package event_test
import (
"encoding/json"
"testing"
"go.sour.is/ev/pkg/es/event"
@ -10,7 +9,7 @@ import (
type Agg struct {
Value string
event.AggregateRoot
event.IsAggregate
}
var _ event.Aggregate = (*Agg)(nil)
@ -38,13 +37,6 @@ type ValueApplied struct {
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) {
agg := &Agg{}
event.Append(agg, &ValueApplied{Value: "one"})

View File

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

View File

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

View File

@ -2,6 +2,8 @@ package es
import (
"context"
"encoding"
"encoding/json"
"errors"
"fmt"
"net/http"
@ -164,8 +166,17 @@ func (e *GQLEvent) Values() map[string]interface{} {
return event.Values(e.e)
}
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
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 {
meta := e.e.EventMeta()

View File

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