Skip to content

Commit

Permalink
include owners follows follow lists in delete-protected events and ma…
Browse files Browse the repository at this point in the history
…ke them populate properly

they were not reading off the tags correctly before
  • Loading branch information
mleku committed Nov 29, 2024
1 parent fb9eef2 commit 8e208c1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 51 deletions.
3 changes: 1 addition & 2 deletions cmd/realy/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ type Config struct {
Profile S `env:"PROFILE" usage:"root path for all other path configurations (based on APP_NAME and OS specific location)"`
Listen S `env:"LISTEN" default:"0.0.0.0" usage:"network listen address"`
Port N `env:"PORT" default:"3334" usage:"port to listen on"`
AdminListen S `env:"ADMIN_LISTEN" default:"127.0.0.1" usage:"admin listen address"`
AdminUser S `env:"ADMIN_USER" default:"admin" usage:"admin user"`
AdminPass S `env:"ADMIN_PASS" usage:"admin password"`
LogLevel S `env:"LOG_LEVEL" default:"info" usage:"debug level: fatal error warn info debug trace"`
DbLogLevel S `env:"DB_LOG_LEVEL" default:"info" usage:"debug level: fatal error warn info debug trace"`
AuthRequired bool `env:"AUTH_REQUIRED" default:"false" usage:"requires auth for all access"`
Owners []S `env:"OWNERS" usage:"list of npubs of users in hex format whose follow and mute list dictate accepting requests and events - follows and follows follows are allowed, mutes and follows mutes are rejected"`
Owners []S `env:"OWNERS" usage:"list of npubs of users in hex format whose follow and mute list dictate accepting requests and events with AUTH_REQUIRED enabled - follows and follows follows are allowed to read/write, owners mutes events are rejected"`
DBSizeLimit int `env:"DB_SIZE_LIMIT" default:"0" usage:"the number of gigabytes (1,000,000,000 bytes) we want to keep the data store from exceeding, 0 means disabled"`
DBLowWater int `env:"DB_LOW_WATER" default:"60" usage:"the percentage of DBSizeLimit a GC run will reduce the used storage down to"`
DBHighWater int `env:"DB_HIGH_WATER" default:"80" usage:"the trigger point at which a GC run should start if exceeded"`
Expand Down
99 changes: 51 additions & 48 deletions cmd/realy/app/implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ func (r *Relay) Init() (err E) {
}
return fmt.Sprintf("%v", ownerIds)
})

r.Followed = make(map[S]struct{})
r.Muted = make(map[S]struct{})
r.CheckOwnerLists(context.Bg())
return nil
}
Expand Down Expand Up @@ -101,7 +102,7 @@ func (r *Relay) AcceptEvent(c context.T, evt *event.T, hr *http.Request, origin
// potential for a malicious action causing this, first check for the list:
tt := tag.New(append(r.OwnersFollowLists, r.OwnersMuteLists...)...)
if evt.Tags.ContainsAny(B("e"), tt) {
return false, "cannot delete owner's follow or mute events"
return false, "cannot delete owner's follow, owners's follows follow or mute events"
}
// next, check all a tags present are not follow/mute lists of the owners
aTags := evt.Tags.GetAll(tag.New("a"))
Expand Down Expand Up @@ -235,90 +236,92 @@ func (r *Relay) AcceptReq(c Ctx, hr *http.Request, idB, ff *filters.T,
return
}

// CheckOwnerLists regenerates the owner follow and mute lists if they are empty
// CheckOwnerLists regenerates the owner follow and mute lists if they are empty.
//
// It also adds the followed npubs of the follows.
func (r *Relay) CheckOwnerLists(c context.T) {
if len(r.Owners) > 0 {
r.Lock()
defer r.Unlock()
var err error
var evs []*event.T
// need to search DB for moderator npub follow lists, followed npubs are allowed access.
if len(r.Followed) < 1 {
log.D.Ln("regenerating owners follow lists")
var err error
var evs []*event.T
if evs, err = r.Store.QueryEvents(c,
&filter.T{Authors: tag.New(r.Owners...),
Kinds: kinds.New(kind.FollowList)}); chk.E(err) {

}
for i := range evs {
r.OwnersFollowLists = append(r.OwnersFollowLists, evs[i].ID)
}
// preallocate sufficient elements
var count int
for _, ev := range evs {
r.OwnersFollowLists = append(r.OwnersFollowLists, ev.ID)
for _, t := range ev.Tags.F() {
if equals(t.Key(), B{'p'}) {
count++
if equals(t.Key(), B("p")) {
var p B
if p, err = hex.Dec(S(t.Value())); chk.E(err) {
continue
}
r.Followed[S(p)] = struct{}{}
}
}
}
r.Followed = make(map[S]struct{})
evs = evs[:0]
// next, search for the follow lists of all on the follow list
log.D.Ln("searching for owners follows follow lists")
var followed []S
for f := range r.Followed {
followed = append(followed, f)
}
if evs, err = r.Store.QueryEvents(c,
&filter.T{Authors: tag.New(followed...),
Kinds: kinds.New(kind.FollowList)}); chk.E(err) {
}
for _, ev := range evs {
// we want to protect the follow lists of users as well so they also cannot be
// deleted, only replaced.
r.OwnersFollowLists = append(r.OwnersFollowLists, ev.ID)
for _, t := range ev.Tags.F() {
if equals(t.Key(), B{'p'}) {
var dst B
if dst, err = hex.DecAppend(dst, t.Value()); chk.E(err) {
if equals(t.Key(), B("p")) {
var p B
if p, err = hex.Dec(S(t.Value())); chk.E(err) {
continue
}
f := S(dst)
r.Followed[f] = struct{}{}
r.Followed[S(p)] = struct{}{}
}
}
}
evs = evs[:0]
}
if len(r.Muted) < 1 {
log.D.Ln("regenerating owners mute lists")
var err error
var evs []*event.T
r.Muted = make(map[S]struct{})
if evs, err = r.Store.QueryEvents(c,
&filter.T{Authors: tag.New(r.Owners...),
Kinds: kinds.New(kind.MuteList)}); chk.E(err) {

}
for i := range evs {
r.OwnersMuteLists = append(r.OwnersMuteLists, evs[i].ID)
}
r.Muted = make(map[S]struct{})
mutes := "mutes(access blacklist),["
var first bool
for _, ev := range evs {
r.OwnersMuteLists = append(r.OwnersMuteLists, ev.ID)
for _, t := range ev.Tags.F() {
if equals(t.Key(), B{'p'}) {
var dst B
if dst, err = hex.DecAppend(dst, t.Value()); chk.E(err) {
if equals(t.Key(), B("p")) {
var p B
if p, err = hex.Dec(S(t.Value())); chk.E(err) {
continue
}
if !first {
mutes += ","
} else {
first = true
}
m := S(dst)
mutes += `"` + m + `"`
r.Muted[m] = struct{}{}
r.Muted[S(p)] = struct{}{}
}
}
}
o := "followed:\n"
for pk := range r.Followed {
o += fmt.Sprintf("%0x,", pk)
}
o += "\nmuted:\n"
for pk := range r.Muted {
o += fmt.Sprintf("%0x,", pk)
}
log.T.F("%s\n", o)
evs = evs[:0]
}
// log this info
o := "followed:\n"
for pk := range r.Followed {
o += fmt.Sprintf("%0x,", pk)
}
o += "\nmuted:\n"
for pk := range r.Muted {
o += fmt.Sprintf("%0x,", pk)
}
log.T.F("%s\n", o)
}
}

Expand Down
2 changes: 1 addition & 1 deletion realy/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.2.21
v1.2.22

0 comments on commit 8e208c1

Please sign in to comment.