revising aliases c

This commit is contained in:
2025-02-08 13:51:56 -01:06
parent 43d67e9ee1
commit b2530e89f7
14 changed files with 105 additions and 207 deletions

View File

@@ -1,6 +1,7 @@
package app
import (
"bytes"
"fmt"
"net/http"
"strconv"
@@ -22,14 +23,14 @@ import (
"realy.lol/tag/atag"
)
type List map[st]struct{}
type List map[string]struct{}
type Relay struct {
sync.Mutex
*config.C
Store store.I
// Owners' pubkeys
Owners []by
Owners [][]byte
// Followed are the pubkeys that are in the Owners' follow lists and have full
// access permission.
Followed List
@@ -41,29 +42,29 @@ type Relay struct {
Muted List
// OwnersFollowLists are the event IDs of owners follow lists, which must not be
// deleted, only replaced.
OwnersFollowLists []by
OwnersFollowLists [][]byte
// OwnersMuteLists are the event IDs of owners mute lists, which must not be
// deleted, only replaced.
OwnersMuteLists []by
OwnersMuteLists [][]byte
}
func (r *Relay) Name() st { return r.C.AppName }
func (r *Relay) Name() string { return r.C.AppName }
func (r *Relay) Storage() store.I { return r.Store }
func (r *Relay) Init() (err er) {
func (r *Relay) Init() (err error) {
for _, src := range r.C.Owners {
if len(src) < 1 {
continue
}
dst := make(by, len(src)/2)
if _, err = hex.DecBytes(dst, by(src)); chk.E(err) {
dst := make([]byte, len(src)/2)
if _, err = hex.DecBytes(dst, []byte(src)); chk.E(err) {
continue
}
r.Owners = append(r.Owners, dst)
}
if len(r.Owners) > 0 {
log.T.C(func() st {
log.T.C(func() string {
ownerIds := make([]string, len(r.Owners))
for i, npub := range r.Owners {
ownerIds[i] = hex.Enc(npub)
@@ -77,23 +78,23 @@ func (r *Relay) Init() (err er) {
return nil
}
func (r *Relay) NoLimiter(pubKey by) (ok bo) {
func (r *Relay) NoLimiter(pubKey []byte) (ok bool) {
r.Lock()
defer r.Unlock()
_, ok = r.Followed[st(pubKey)]
_, ok = r.Followed[string(pubKey)]
return
}
func (r *Relay) ZeroLists() {
r.Followed = make(map[st]struct{})
r.OwnersFollowed = make(map[st]struct{})
r.Followed = make(map[string]struct{})
r.OwnersFollowed = make(map[string]struct{})
r.OwnersFollowLists = r.OwnersFollowLists[:0]
r.Muted = make(map[st]struct{})
r.Muted = make(map[string]struct{})
r.OwnersMuteLists = r.OwnersMuteLists[:0]
}
func (r *Relay) AcceptEvent(c cx, evt *event.T, hr *http.Request, origin st,
authedPubkey by) (accept bo, notice st, afterSave func()) {
func (r *Relay) AcceptEvent(c context.T, evt *event.T, hr *http.Request, origin string,
authedPubkey []byte) (accept bool, notice string, afterSave func()) {
// if the authenticator is enabled we require auth to accept events
if !r.AuthEnabled() {
return true, "", nil
@@ -115,7 +116,7 @@ func (r *Relay) AcceptEvent(c cx, evt *event.T, hr *http.Request, origin st,
// followed can access the relay and upload DM events and such for owner
// followed users.
for o := range r.OwnersFollowed {
if equals(by(o), evt.PubKey) {
if bytes.Equal([]byte(o), evt.PubKey) {
return true, "", func() {
r.ZeroLists()
r.CheckOwnerLists(context.Bg())
@@ -126,7 +127,7 @@ func (r *Relay) AcceptEvent(c cx, evt *event.T, hr *http.Request, origin st,
if evt.Kind.Equal(kind.MuteList) {
// only owners control the mute list
for _, o := range r.Owners {
if equals(o, evt.PubKey) {
if bytes.Equal(o, evt.PubKey) {
return true, "", func() {
r.ZeroLists()
r.CheckOwnerLists(context.Bg())
@@ -136,7 +137,7 @@ func (r *Relay) AcceptEvent(c cx, evt *event.T, hr *http.Request, origin st,
}
for _, o := range r.Owners {
log.T.F("%0x,%0x", o, evt.PubKey)
if equals(o, evt.PubKey) {
if bytes.Equal(o, evt.PubKey) {
// prevent owners from deleting their own mute/follow lists in case of bad
// client implementation
if evt.Kind.Equal(kind.Deletion) {
@@ -144,8 +145,8 @@ func (r *Relay) AcceptEvent(c cx, evt *event.T, hr *http.Request, origin st,
aTags := evt.Tags.GetAll(tag.New("a"))
for _, at := range aTags.F() {
a := &atag.T{}
var rem by
var err er
var rem []byte
var err error
if rem, err = a.Unmarshal(at.Value()); chk.E(err) {
continue
}
@@ -166,7 +167,7 @@ func (r *Relay) AcceptEvent(c cx, evt *event.T, hr *http.Request, origin st,
// don't allow owners to delete their mute or follow lists because
// they should not want to, can simply replace it, and malicious
// clients may do this specifically to attack the owner's relay (s)
if equals(own, a.PubKey) ||
if bytes.Equal(own, a.PubKey) ||
a.Kind.Equal(kind.MuteList) ||
a.Kind.Equal(kind.FollowList) {
return false, "owners may not delete their own " +
@@ -181,7 +182,7 @@ func (r *Relay) AcceptEvent(c cx, evt *event.T, hr *http.Request, origin st,
// 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 r.Muted {
if equals(evt.PubKey, by(pk)) {
if bytes.Equal(evt.PubKey, []byte(pk)) {
return false, "rejecting event with pubkey " + hex.Enc(evt.PubKey) +
" because on owner mute list", nil
}
@@ -189,9 +190,9 @@ func (r *Relay) AcceptEvent(c cx, evt *event.T, hr *http.Request, origin st,
// for all else, check the authed pubkey is in the follow list
for pk := range r.Followed {
// allow all events from follows of owners
if equals(authedPubkey, by(pk)) {
if bytes.Equal(authedPubkey, []byte(pk)) {
log.I.F("accepting event %0x because %0x on owner follow list",
evt.ID, by(pk))
evt.ID, []byte(pk))
return true, "", nil
}
}
@@ -206,8 +207,8 @@ func (r *Relay) AcceptEvent(c cx, evt *event.T, hr *http.Request, origin st,
return
}
func (r *Relay) AcceptReq(c cx, hr *http.Request, id by, ff *filters.T,
authedPubkey by) (allowed *filters.T, ok bo) {
func (r *Relay) AcceptReq(c context.T, hr *http.Request, id []byte, ff *filters.T,
authedPubkey []byte) (allowed *filters.T, ok bool) {
if !r.AuthEnabled() || r.PublicReadable && len(authedPubkey) > 0 {
allowed = &filters.T{}
// non-authed users may not make filters that have too broad criteria, resource
@@ -270,7 +271,7 @@ func (r *Relay) AcceptReq(c cx, hr *http.Request, id by, ff *filters.T,
defer r.Unlock()
if len(r.Owners) > 0 {
for pk := range r.Followed {
if equals(authedPubkey, by(pk)) {
if bytes.Equal(authedPubkey, []byte(pk)) {
ok = true
return
}
@@ -287,17 +288,17 @@ func (r *Relay) AcceptReq(c cx, hr *http.Request, id by, ff *filters.T,
// 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 cx) {
func (r *Relay) CheckOwnerLists(c context.T) {
if len(r.Owners) > 0 {
r.Lock()
defer r.Unlock()
var err er
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 {
// add the owners themselves of course
for i := range r.Owners {
r.Followed[st(r.Owners[i])] = struct{}{}
r.Followed[string(r.Owners[i])] = struct{}{}
}
log.D.Ln("regenerating owners follow lists")
if evs, err = r.Store.QueryEvents(c,
@@ -307,20 +308,20 @@ func (r *Relay) CheckOwnerLists(c cx) {
for _, ev := range evs {
r.OwnersFollowLists = append(r.OwnersFollowLists, ev.ID)
for _, t := range ev.Tags.F() {
if equals(t.Key(), by("p")) {
var p by
if p, err = hex.Dec(st(t.Value())); chk.E(err) {
if bytes.Equal(t.Key(), []byte("p")) {
var p []byte
if p, err = hex.Dec(string(t.Value())); chk.E(err) {
continue
}
r.Followed[st(p)] = struct{}{}
r.OwnersFollowed[st(p)] = struct{}{}
r.Followed[string(p)] = struct{}{}
r.OwnersFollowed[string(p)] = 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 []st
var followed []string
for f := range r.Followed {
followed = append(followed, f)
}
@@ -333,12 +334,12 @@ func (r *Relay) CheckOwnerLists(c cx) {
// deleted, only replaced.
r.OwnersFollowLists = append(r.OwnersFollowLists, ev.ID)
for _, t := range ev.Tags.F() {
if equals(t.Key(), by("p")) {
var p by
if p, err = hex.Dec(st(t.Value())); err != nil {
if bytes.Equal(t.Key(), []byte("p")) {
var p []byte
if p, err = hex.Dec(string(t.Value())); err != nil {
continue
}
r.Followed[st(p)] = struct{}{}
r.Followed[string(p)] = struct{}{}
}
}
}
@@ -346,7 +347,7 @@ func (r *Relay) CheckOwnerLists(c cx) {
}
if len(r.Muted) < 1 {
log.D.Ln("regenerating owners mute lists")
r.Muted = make(map[st]struct{})
r.Muted = make(map[string]struct{})
if evs, err = r.Store.QueryEvents(c,
&filter.T{Authors: tag.New(r.Owners...),
Kinds: kinds.New(kind.MuteList)}); chk.E(err) {
@@ -354,12 +355,12 @@ func (r *Relay) CheckOwnerLists(c cx) {
for _, ev := range evs {
r.OwnersMuteLists = append(r.OwnersMuteLists, ev.ID)
for _, t := range ev.Tags.F() {
if equals(t.Key(), by("p")) {
var p by
if p, err = hex.Dec(st(t.Value())); chk.E(err) {
if bytes.Equal(t.Key(), []byte("p")) {
var p []byte
if p, err = hex.Dec(string(t.Value())); chk.E(err) {
continue
}
r.Muted[st(p)] = struct{}{}
r.Muted[string(p)] = struct{}{}
}
}
}
@@ -369,11 +370,11 @@ func (r *Relay) CheckOwnerLists(c cx) {
}
}
func (r *Relay) AuthEnabled() bo { return r.AuthRequired || len(r.Owners) > 0 }
func (r *Relay) AuthEnabled() bool { return r.AuthRequired || len(r.Owners) > 0 }
// ServiceUrl returns the address of the relay to send back in auth responses.
// If auth is disabled this returns an empty string.
func (r *Relay) ServiceUrl(req *http.Request) (s st) {
func (r *Relay) ServiceUrl(req *http.Request) (s string) {
if !r.AuthEnabled() {
return
}

View File

@@ -4,9 +4,11 @@ import (
"os"
"runtime"
"time"
"realy.lol/context"
)
func MonitorResources(c cx) {
func MonitorResources(c context.T) {
tick := time.NewTicker(time.Minute)
log.I.Ln("running process", os.Args[0], os.Getpid())
// memStats := &runtime.MemStats{}

View File

@@ -1,22 +1,9 @@
package app
import (
"bytes"
"realy.lol/context"
"realy.lol/lol"
)
type (
bo = bool
by = []byte
st = string
er = error
no = int
cx = context.T
)
var (
log, chk, errorf = lol.Main.Log, lol.Main.Check, lol.Main.Errorf
equals = bytes.Equal
)

View File

@@ -6,28 +6,28 @@ import (
_ "net/http/pprof"
"os"
"runtime/debug"
"strings"
"sync"
"time"
"github.com/pkg/profile"
"realy.lol/bech32encoding"
"realy.lol/cmd/realy/app"
"realy.lol/context"
"realy.lol/interrupt"
"realy.lol/lol"
"realy.lol/p256k"
"realy.lol/ratel"
"realy.lol/realy"
"realy.lol/realy/config"
"realy.lol/units"
"realy.lol/realy/options"
"strings"
"realy.lol/bech32encoding"
"realy.lol/p256k"
"realy.lol/signer"
"realy.lol/units"
)
func main() {
var err er
var err error
var cfg *config.C
if cfg, err = config.New(); chk.T(err) {
if err != nil {
@@ -64,11 +64,11 @@ func main() {
MaxLimit: ratel.DefaultMaxLimit,
UseCompact: cfg.UseCompact,
Compression: cfg.Compression,
Extra: []no{
Extra: []int{
cfg.DBSizeLimit,
cfg.DBLowWater,
cfg.DBHighWater,
cfg.GCFrequency * no(time.Second),
cfg.GCFrequency * int(time.Second),
},
},
)
@@ -81,8 +81,8 @@ func main() {
if len(admin) < 1 {
continue
}
var pk by
if pk, err = bech32encoding.NpubToBytes(by(admin)); chk.E(err) {
var pk []byte
if pk, err = bech32encoding.NpubToBytes([]byte(admin)); chk.E(err) {
return
}
log.I.S(pk)

View File

@@ -1,22 +1,9 @@
package main
import (
"bytes"
"realy.lol/context"
"realy.lol/lol"
)
type (
bo = bool
by = []byte
st = string
er = error
no = int
cx = context.T
)
var (
log, chk, errorf = lol.Main.Log, lol.Main.Check, lol.Main.Errorf
equals = bytes.Equal
)

View File

@@ -31,14 +31,14 @@ const (
type Result struct {
sec *secp256k1.SecretKey
npub by
npub []byte
pub *secp256k1.PublicKey
}
var args struct {
String st `arg:"positional" help:"the string you want to appear in the npub"`
Position st `arg:"positional" default:"end" help:"[begin|contain|end] default: end"`
Threads no `help:"number of threads to mine with - defaults to using all CPU threads available"`
String string `arg:"positional" help:"the string you want to appear in the npub"`
Position string `arg:"positional" default:"end" help:"[begin|contain|end] default: end"`
Threads int `help:"number of threads to mine with - defaults to using all CPU threads available"`
}
func main() {
@@ -56,7 +56,7 @@ Options:
--help, -h display this help and exit`)
os.Exit(0)
}
var where no
var where int
canonical := strings.ToLower(args.Position)
switch {
case strings.HasPrefix(canonical, "begin"):
@@ -74,7 +74,7 @@ Options:
}
}
func Vanity(str string, where no, threads no) (e er) {
func Vanity(str string, where int, threads int) (e error) {
// check the string has valid bech32 ciphers
for i := range str {
@@ -146,12 +146,12 @@ out:
return
}
func mine(str st, where no, quit qu.C, resC chan Result, wg *sync.WaitGroup,
func mine(str string, where int, quit qu.C, resC chan Result, wg *sync.WaitGroup,
counter *atomic.Int64) {
wg.Add(1)
var r Result
var e er
var e error
found := false
out:
for {
@@ -182,17 +182,17 @@ out:
}
switch where {
case PositionBeginning:
if bytes.HasPrefix(r.npub, append(prefix, by(str)...)) {
if bytes.HasPrefix(r.npub, append(prefix, []byte(str)...)) {
found = true
quit.Q()
}
case PositionEnding:
if bytes.HasSuffix(r.npub, by(str)) {
if bytes.HasSuffix(r.npub, []byte(str)) {
found = true
quit.Q()
}
case PositionContains:
if bytes.Contains(r.npub, by(str)) {
if bytes.Contains(r.npub, []byte(str)) {
found = true
quit.Q()
}
@@ -203,7 +203,7 @@ out:
// GenKeyPair creates a fresh new key pair using the entropy source used by
// crypto/rand (ie, /dev/random on posix systems).
func GenKeyPair() (sec *secp256k1.SecretKey,
pub *secp256k1.PublicKey, err er) {
pub *secp256k1.PublicKey, err error) {
sec, err = secp256k1.GenerateSecretKey()
if err != nil {

View File

@@ -1,22 +1,9 @@
package main
import (
"bytes"
"realy.lol/context"
"realy.lol/lol"
)
type (
bo = bool
by = []byte
st = string
er = error
no = int
cx = context.T
)
var (
log, chk, errorf = lol.Main.Log, lol.Main.Check, lol.Main.Errorf
equals = bytes.Equal
)

View File

@@ -5,25 +5,25 @@ import (
)
type Envelope interface {
Label() st
Write(w io.Writer) (err er)
Label() string
Write(w io.Writer) (err error)
JSON
}
type JSON interface {
// Marshal converts the data of the type into JSON, appending it to the provided
// slice and returning the extended slice.
Marshal(dst by) (b by)
Marshal(dst []byte) (b []byte)
// Unmarshal decodes a JSON form of a type back into the runtime form, and
// returns whatever remains after the type has been decoded out.
Unmarshal(b by) (r by, err er)
Unmarshal(b []byte) (r []byte, err error)
}
type Binary interface {
// MarshalBinary converts the data of the type into binary form, appending it to
// the provided slice.
MarshalBinary(dst by) (b by)
MarshalBinary(dst []byte) (b []byte)
// UnmarshalBinary decodes a binary form of a type back into the runtime form,
// and returns whatever remains after the type has been decoded out.
UnmarshalBinary(b by) (r by, err er)
UnmarshalBinary(b []byte) (r []byte, err error)
}

View File

@@ -1,22 +1,9 @@
package codec
import (
"bytes"
"realy.lol/context"
"realy.lol/lol"
)
type (
bo = bool
by = []byte
st = string
er = error
no = int
cx = context.T
)
var (
log, chk, errorf = lol.Main.Log, lol.Main.Check, lol.Main.Errorf
equals = bytes.Equal
)

View File

@@ -5,19 +5,19 @@ import (
"strings"
)
type Env map[st]st
type Env map[string]string
// GetEnv reads a file expected to represent a collection of KEY=value in
// standard shell environment variable format - ie, key usually in all upper
// case no spaces and words separated by underscore, value can have any separator, but usually
// comma, for an array of values.
func GetEnv(path st) (env Env, err er) {
var s by
func GetEnv(path string) (env Env, err error) {
var s []byte
env = make(Env)
if s, err = os.ReadFile(path); chk.T(err) {
return
}
lines := strings.Split(st(s), "\n")
lines := strings.Split(string(s), "\n")
for _, line := range lines {
if len(line) == 0 {
continue
@@ -31,4 +31,4 @@ func GetEnv(path st) (env Env, err er) {
// LookupEnv returns the raw string value associated with a provided key name, used as a custom
// environment variable loader for go-simpler.org/env to enable .env file loading.
func (env Env) LookupEnv(key st) (value st, ok bo) { value, ok = env[key]; return }
func (env Env) LookupEnv(key string) (value string, ok bool) { value, ok = env[key]; return }

View File

@@ -1,22 +1,9 @@
package config
import (
"bytes"
"realy.lol/context"
"realy.lol/lol"
)
type (
bo = bool
by = []byte
st = string
er = error
no = int
cx = context.T
)
var (
log, chk, errorf = lol.Main.Log, lol.Main.Check, lol.Main.Errorf
equals = bytes.Equal
)

View File

@@ -1,19 +0,0 @@
package context
import (
"bytes"
"context"
)
type (
bo = bool
by = []byte
st = string
er = error
no = int
cx = context.Context
)
var (
equals = bytes.Equal
)

View File

@@ -5,7 +5,6 @@ import (
"io"
"os"
"runtime"
"strings"
"time"
"github.com/davecgh/go-spew/spew"
@@ -40,7 +39,7 @@ type (
// Ln prints lists of interfaces with spaces in between
Ln func(a ...interface{})
// F prints like fmt.Println surrounded by log details
// F prints like fmt.Println surrounded []byte log details
F func(format string, a ...interface{})
// S prints a spew.Sdump for an enveloper slice
S func(a ...interface{})
@@ -48,10 +47,10 @@ type (
// not being viewed
C func(closure func() string)
// Chk is a shortcut for printing if there is an error, or returning true
Chk func(e er) bo
Chk func(e error) bool
// Err is a pass-through function that uses fmt.Errorf to construct an error
// and returns the error after printing it to the log
Err func(format string, a ...interface{}) er
Err func(format string, a ...any) error
LevelPrinter struct {
Ln
F
@@ -61,9 +60,9 @@ type (
Err
}
LevelSpec struct {
ID no
ID int
Name string
Colorizer func(a ...interface{}) string
Colorizer func(a ...any) string
}
// Entry is a log entry to be printed as json to the log file
@@ -124,12 +123,12 @@ func init() {
SetLoggers(Info)
}
func SetLoggers(level no) {
func SetLoggers(level int) {
Main.Log.T.F("log level %s", LevelSpecs[level].Colorizer(LevelNames[level]))
Level.Store(int32(level))
}
func GetLogLevel(level string) (i no) {
func GetLogLevel(level string) (i int) {
for i = range LevelNames {
if level == LevelNames[i] {
return i
@@ -209,7 +208,7 @@ func GetPrinter(l int32, writer io.Writer) LevelPrinter {
msgCol(GetLoc(2)),
)
},
Chk: func(e er) bo {
Chk: func(e error) bool {
if Level.Load() < l {
return e != nil
}
@@ -225,7 +224,7 @@ func GetPrinter(l int32, writer io.Writer) LevelPrinter {
}
return false
},
Err: func(format string, a ...interface{}) er {
Err: func(format string, a ...interface{}) error {
if Level.Load() < l {
fmt.Fprintf(writer,
"%s%s %s %s\n",
@@ -246,8 +245,8 @@ func GetNullPrinter() LevelPrinter {
F: func(format string, a ...interface{}) {},
S: func(a ...interface{}) {},
C: func(closure func() string) {},
Chk: func(e er) bo { return e != nil },
Err: func(format string, a ...interface{}) er { return fmt.Errorf(format, a...) },
Chk: func(e error) bool { return e != nil },
Err: func(format string, a ...interface{}) error { return fmt.Errorf(format, a...) },
}
}
@@ -287,7 +286,7 @@ func Timestamper() (s string) {
timeText := fmt.Sprint(time.Now().UnixNano())
lt := len(timeText)
lb := lt + 1
var timeBytes = make(by, lb)
var timeBytes = make([]byte, lb)
copy(timeBytes[lb-9:lb], timeText[lt-9:lt])
timeBytes[lb-10] = '.'
lb -= 10
@@ -296,22 +295,22 @@ func Timestamper() (s string) {
return fmt.Sprint(string(timeBytes), " ")
}
var wd, _ = os.Getwd()
// var wd, _ = os.Getwd()
func GetNLoc(n no) (output string) {
func GetNLoc(n int) (output string) {
for ; n > 1; n-- {
output += fmt.Sprintf("%s\n", GetLoc(n))
}
return
}
func GetLoc(skip no) (output string) {
func GetLoc(skip int) (output string) {
_, file, line, _ := runtime.Caller(skip)
split := strings.Split(file, wd+string(os.PathSeparator))
if len(split) < 2 {
output = fmt.Sprintf("%s:%d", file, line)
} else {
output = fmt.Sprintf("%s:%d", split[1], line)
}
// split := strings.Split(file, wd+string(os.PathSeparator))
// if len(split) < 2 {
output = fmt.Sprintf("%s:%d", file, line)
// } else {
// output = fmt.Sprintf("%s:%d", split[1], line)
// }
return
}

View File

@@ -1,20 +0,0 @@
package lol
import (
"bytes"
"realy.lol/context"
)
type (
bo = bool
by = []byte
st = string
er = error
no = int
cx = context.T
)
var (
equals = bytes.Equal
)