revise accept functions

This commit is contained in:
2025-04-21 15:42:27 -01:06
parent 5c2a92e959
commit 2319954bff
6 changed files with 46 additions and 22 deletions

View File

@@ -101,6 +101,7 @@ var (
{Trace, "TRC", color.New(color.FgHiMagenta).Sprint},
}
NoTimeStamp atomic.Bool
ShortLoc atomic.Bool
)
// NoSprint is a noop for sprint (it returns nothing no matter what is given to it).
@@ -143,7 +144,7 @@ func init() {
// SetLoggers configures a log level.
func SetLoggers(level int) {
Main.Log.W.F("setting log level %s", LevelSpecs[level].Colorizer(LevelNames[level]))
Main.Log.T.F("log level %s", LevelSpecs[level].Colorizer(LevelNames[level]))
Level.Store(int32(level))
}
@@ -355,14 +356,12 @@ func init() {
// GetLoc returns the code location of the caller.
func GetLoc(skip int) (output string) {
_, file, line, _ := runtime.Caller(skip)
var split []string
var s string
if strings.Contains(file, "pkg/mod/") {
s = file
if strings.Contains(file, "pkg/mod/") || !ShortLoc.Load() {
} else {
var split []string
split = strings.Split(file, prefix)
s = split[1]
file = split[1]
}
output = fmt.Sprintf("%s:%d", s, line)
output = fmt.Sprintf("%s:%d", file, line)
return
}

View File

@@ -29,6 +29,7 @@ import (
func main() {
cfg := config.New()
lol.ShortLoc.Store(false)
log.I.F("starting %s %s", cfg.AppName, version.V)
wg := &sync.WaitGroup{}
c, cancel := context.Cancel(context.Bg())

View File

@@ -18,15 +18,18 @@ import (
func (s *Server) acceptEvent(c context.T, evt *event.T, remote string,
authedPubkey []byte) (accept bool, notice string, afterSave func()) {
// if the authenticator is enabled we require auth to accept events
if !s.AuthRequired() && len(s.owners) < 1 {
if !s.AuthRequired() && len(s.owners) == 0 {
log.T.F("%s auth not required and no ACL enabled, accepting event %0x", remote, evt.Id)
return true, "", nil
}
if len(authedPubkey) != 32 && !s.PublicReadable() {
return false, fmt.Sprintf("client not authed with auth required %s", remote), nil
}
// check ACL
if len(s.owners) > 0 {
s.Lock()
defer s.Unlock()
// if one of the follows of the owners or follows of the follows changes
if evt.Kind.Equal(kind.FollowList) || evt.Kind.Equal(kind.MuteList) {
// if owner or any of their follows lists are updated we need to regenerate the
// list this ensures that immediately a follow changes their list that newly
@@ -34,6 +37,7 @@ func (s *Server) acceptEvent(c context.T, evt *event.T, remote string,
// followed users.
for o := range s.ownersFollowed {
if bytes.Equal([]byte(o), evt.Pubkey) {
log.T.F("updating whitelist for access control for %0x", evt.Pubkey)
return true, "", func() {
s.ZeroLists()
s.CheckOwnerLists(context.Bg())
@@ -41,6 +45,14 @@ func (s *Server) acceptEvent(c context.T, evt *event.T, remote string,
}
}
}
// check the mute list, and reject events authored by muted pubkeys, even if
// they come from a pubkey that is on the follow list.
for pk := range s.Muted {
if bytes.Equal(evt.Pubkey, []byte(pk)) {
return false, "rejecting event with pubkey " + hex.Enc(evt.Pubkey) +
" because on owner mute list", nil
}
}
for _, o := range s.owners {
log.T.F("%0x,%0x", o, evt.Pubkey)
if bytes.Equal(o, evt.Pubkey) {
@@ -82,15 +94,8 @@ func (s *Server) acceptEvent(c context.T, evt *event.T, remote string,
}
}
log.W.Ln("event is from owner")
return true, "", nil
}
}
// check the mute list, and reject events authored by muted pubkeys, even if
// they come from a pubkey that is on the follow list.
for pk := range s.Muted {
if bytes.Equal(evt.Pubkey, []byte(pk)) {
return false, "rejecting event with pubkey " + hex.Enc(evt.Pubkey) +
" because on owner mute list", nil
accept = true
return
}
}
// for all else, check the authed pubkey is in the follow list
@@ -99,7 +104,8 @@ func (s *Server) acceptEvent(c context.T, evt *event.T, remote string,
if bytes.Equal(authedPubkey, []byte(pk)) {
log.I.F("accepting event %0x because %0x on owner follow list",
evt.Id, []byte(pk))
return true, "", nil
accept = true
return
}
}
}
@@ -108,6 +114,7 @@ func (s *Server) acceptEvent(c context.T, evt *event.T, remote string,
// has been loaded via the auth function.
if len(authedPubkey) == schnorr.PubKeyBytesLen && s.AuthRequired() {
notice = "auth required but user not authed"
return
}
return
}

View File

@@ -17,8 +17,14 @@ func (s *Server) AcceptReq(c context.T, hr *http.Request, id []byte,
log.T.F("%s AcceptReq pubkey %0x", remote, authedPubkey)
s.Lock()
defer s.Unlock()
if s.PublicReadable() && len(s.Owners()) == 0 && !s.AuthRequired() {
log.T.F("%s accept because public readable and auth not required", remote)
if s.PublicReadable() && !s.AuthRequired() {
log.T.F("%s accept because public readable and not auth required", remote)
allowed = ff
ok = true
}
if len(s.Owners()) == 0 {
log.T.F("%s accept because no access control is enabled", remote)
allowed = ff
ok = true
return

View File

@@ -113,7 +113,7 @@ func (s *Server) CheckOwnerLists(c context.T) {
for _, ev := range evs {
s.OwnersFollowLists = append(s.OwnersFollowLists, ev.Id)
for _, t := range ev.Tags.ToSliceOfTags() {
if bytes.Equal(t.Key(), []byte("p")) {
if t.KeyString() == "p" {
var p []byte
if p, err = hex.Dec(string(t.Value())); chk.E(err) {
continue
@@ -125,7 +125,7 @@ func (s *Server) CheckOwnerLists(c context.T) {
}
evs = evs[:0]
// next, search for the follow lists of all on the follow list
log.D.Ln("searching for owners follows follow lists")
log.T.Ln("searching for owners follows follow lists")
var followed []string
for f := range s.Followed {
followed = append(followed, f)

View File

@@ -190,6 +190,17 @@ func (t *T) Key() []byte {
return nil
}
// KeyString returns the first element of the tags as a string.
func (t *T) KeyString() string {
if t == nil {
return ""
}
if t.Len() > Key {
return string(t.field[Key])
}
return ""
}
// FilterKey returns the first element of a filter tag (the key) with the # removed
func (t *T) FilterKey() []byte {
if t == nil {