app

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 9, 2024 License: CC0-1.0 Imports: 57 Imported by: 0

Documentation

Index

Constants

View Source
const (
	WriteWait           = 10 * time.Second
	PongWait            = 60 * time.Second
	PingPeriod          = 30 * time.Second
	ReadBufferSize      = 4096
	WriteBufferSize     = 4096
	MaxMessageSize  int = 512000 // ???
)
View Source
const IgnoreAfter = 16

Variables

View Source
var Commands []*Command
View Source
var Software = "https://github.com/Hubmakerlabs/replicatr"
View Source
var Version = "v0.0.1"

Functions

func AntiSyncBots

func AntiSyncBots(c context.T, f *filter.T) (rej bool, msg string)

AntiSyncBots tries to prevent people from syncing kind:1s from this relay to else by always requiring an author parameter at least.

func DecryptDM

func DecryptDM(ev *event.T, meSec, youPub string) (decryptedStr string, err error)

DecryptDM decrypts a DM, kind 4, 1059 or 1060

func EncryptDM

func EncryptDM(ev *event.T, meSec, youPub string) (evo *event.T, err error)

EncryptDM encrypts a DM, kind 4, 1059 or 1060

func GetAuthed

func GetAuthed(c context.T) string

func GetConnection

func GetConnection(c context.T) *relayws.WebSocket

func GetIP

func GetIP(c context.T) string

func GetListeningFilters

func GetListeningFilters() (respFilters filters.T)

func GetOpenSubscriptions

func GetOpenSubscriptions(c context.T) filters.T

func GetSubscriptionID

func GetSubscriptionID(c context.T) string

func MakeReply

func MakeReply(ev *event.T, content string) (evo *event.T)

MakeReply creates an appropriate reply event from a provided event that is being replied to (not quoting, just the right tags, timestamps and kind).

func NoComplexFilters

func NoComplexFilters(c context.T, f *filter.T) (rej bool, msg string)

NoComplexFilters disallows filters with more than 2 tags.

func NoEmptyFilters

func NoEmptyFilters(c context.T, f *filter.T) (reject bool, msg string)

NoEmptyFilters disallows filters that don't have at least a tag, a kind, an author or an id.

func NoSearchQueries

func NoSearchQueries(c context.T, f *filter.T) (reject bool, msg string)

func PointerHasher

func PointerHasher[V any](_ maphash.Seed, k *V) uint64

func RemoveListener

func RemoveListener(ws *relayws.WebSocket)

RemoveListener removes WebSocket conn from listeners (no need to cancel contexts as they are all inherited from the main connection context)

func RemoveListenerId

func RemoveListenerId(ws *relayws.WebSocket, id string)

RemoveListenerId removes a specific subscription id from listeners for a given ws client and cancel its specific context

func RemoveSearchQueries

func RemoveSearchQueries(c context.T, f *filter.T)

func RequestAuth

func RequestAuth(c context.T)

func SetListener

func SetListener(id string, ws *relayws.WebSocket, f filters.T, c context.C)

Types

type Command

type Command struct {
	Name string
	Help string
	Func func(rl *Relay, prefix string, ev *event.T, cmd *Command, args ...string) (reply *event.T, err error)
}

type Config

type Config struct {
	ExportCmd    *ExportCmd `arg:"subcommand:export" json:"-" help:"export database as line structured JSON"`
	ImportCmd    *ImportCmd `arg:"subcommand:import" json:"-" help:"import data from line structured JSON"`
	InitCfgCmd   *InitCfg   `arg:"subcommand:initcfg" json:"-" help:"initialize relay configuration files"`
	Listen       string     `arg:"-l,--listen" default:"0.0.0.0:3334" json:"listen" help:"network address to listen on"`
	EventStore   string     `arg:"-e,--eventstore" default:"ic" json:"eventstore" help:"select event store backend [ic,badger]"`
	CanisterAddr string     `arg:"-C,--canisteraddr" default:"127.0.0.1:46847" json:"canister_addr" help:"IC canister address to use"`
	CanisterID   string     `arg:"-I,--canisterid" json:"canister_id" help:"IC canister ID to use"`
	Profile      string     `arg:"-p,--profile" json:"-" default:"replicatr" help:"profile name to use for storage"`
	Name         string     `arg:"-n,--name" json:"name" default:"replicatr relay" help:"name of relay for NIP-11"`
	Description  string     `arg:"-d,--description" json:"description" help:"description of relay for NIP-11"`
	Pubkey       string     `arg:"--pubkey" json:"pubkey" help:"public key of relay operator"`
	Contact      string     `arg:"-c,--contact" json:"contact,omitempty" help:"non-nostr relay operator contact details"`
	Icon         string     `arg:"-i,--icon" json:"icon" default:"https://i.nostr.build/n8vM.png" help:"icon to show on relay information pages"`
	// Whitelist permits ONLY inbound connections from specified IP addresses.
	Whitelist []string `arg:"-w,--whitelist,separate" json:"ip_whitelist" help:"IP addresses that are only allowed to access"`
	// AllowIPs is for bypassing authentication required for clients based on IP
	// addresses... primarily for testing with wireguard VPN clients run by the
	// developer, as these are stable, non-routeable addresses, this skips the
	// requirement enforced by AuthRequired.
	AllowIPs     []string `arg:"-A,--allow,separate" json:"allow_ip" help:"IP addresses that are always allowed to access"`
	AuthRequired bool     `arg:"-a,--auth" json:"auth_required" default:"false" help:"NIP-42 authentication required for all access"`
	Public       bool     `arg:"--public" json:"public" default:"true" help:"allow public read access to users not on ACL"`
	Owners       []string `arg:"-o,--owner,separate" json:"owners" help:"specify public keys of users with owner level permissions on relay"`
	SecKey       string   `arg:"-s" json:"seckey" help:"identity key of relay, used to sign 30066 and 30166 events and for message control interface"`
	MaxProcs     int      `args:"-m" json:"max_procs" default:"128" help:"maximum number of goroutines to use"`
}

func (*Config) Load

func (c *Config) Load(filename string) (err error)

func (*Config) Save

func (c *Config) Save(filename string) (err error)

type CountEvents

type CountEvents func(c context.T, f *filter.T) (cnt int, err error)

function types used in the relay state

type Events

type Events func(c context.T, ev *event.T) error

function types used in the relay state

type ExportCmd

type ExportCmd struct {
	ToFile string `arg:"-f,--tofile" help:"write to file instead of stdout"`
}

type Hook

type Hook func(c context.T)

function types used in the relay state

type ImportCmd

type ImportCmd struct {
	FromFile []string `arg:"-f,--fromfile,separate" help:"read from files instead of stdin (can use flag repeatedly for multiple files)"`
}

type InitCfg

type InitCfg struct {
}

type Listener

type Listener struct {
	// contains filtered or unexported fields
}

type ListenerMap

type ListenerMap = *xsync.MapOf[string, *Listener]

type OnEventSaved

type OnEventSaved func(c context.T, ev *event.T)

function types used in the relay state

type OverrideDeletionOutcome

type OverrideDeletionOutcome func(c context.T, tgt, del *event.T) (ok bool,
	msg string)

function types used in the relay state

type OverwriteFilter

type OverwriteFilter func(c context.T, f *filter.T)

function types used in the relay state

func RemoveAllButKinds

func RemoveAllButKinds(k ...kind.T) OverwriteFilter

func RemoveAllButTags

func RemoveAllButTags(tagNames ...string) OverwriteFilter

type OverwriteRelayInformation

type OverwriteRelayInformation func(c context.T, r *http.Request,
	info *relayinfo.T) *relayinfo.T

function types used in the relay state

type OverwriteResponseEvent

type OverwriteResponseEvent func(c context.T, ev *event.T)

function types used in the relay state

type QueryEvents

type QueryEvents func(c context.T, C chan *event.T,
	f *filter.T) (err error)

function types used in the relay state

type RejectEvent

type RejectEvent func(c context.T, ev *event.T) (rej bool,
	msg string)

function types used in the relay state

func PreventExcessTags

func PreventExcessTags(max int, ign kinds.T, only kinds.T) RejectEvent

PreventExcessTags returns a function that can be used as a RejectFilter that will reject events with more indexable (single-character) tags than the specified number.

If ignoreKinds is given this restriction will not apply to these kinds (useful for allowing a bigger). If onlyKinds is given then all other kinds will be ignored.

func PreventLargeTags

func PreventLargeTags(maxTagValueLen int) RejectEvent

PreventLargeTags rejects events that have indexable tag values greater than maxTagValueLen.

func PreventTimestampsInTheFuture

func PreventTimestampsInTheFuture(thresholdSeconds timestamp.T) RejectEvent

func PreventTimestampsInThePast

func PreventTimestampsInThePast(thresholdSeconds timestamp.T) RejectEvent

func RestrictToSpecifiedKinds

func RestrictToSpecifiedKinds(kinds ...kind.T) RejectEvent

RestrictToSpecifiedKinds returns a function that can be used as a RejectFilter that will reject any events with kinds different than the specified ones.

type RejectFilter

type RejectFilter func(c context.T, id subscriptionid.T,
	f *filter.T) (reject bool, msg string)

function types used in the relay state

type Relay

type Relay struct {
	ServiceURL             atomic.String
	RejectEvent            []RejectEvent
	RejectFilter           []RejectFilter
	RejectCountFilter      []RejectFilter
	OverrideDeletion       []OverrideDeletionOutcome
	OverwriteResponseEvent []OverwriteResponseEvent
	OverwriteFilter        []OverwriteFilter
	OverwriteCountFilter   []OverwriteFilter
	OverwriteRelayInfo     []OverwriteRelayInformation
	StoreEvent             []Events
	DeleteEvent            []Events
	QueryEvents            []QueryEvents
	CountEvents            []CountEvents
	OnConnect              []Hook
	OnDisconnect           []Hook
	OnEventSaved           []OnEventSaved
	Config                 *Config
	Info                   *relayinfo.T

	// in case you call Server.Start
	Addr string

	// websocket options
	// WriteWait is the time allowed to write a message to the peer.
	WriteWait time.Duration
	// PongWait is the time allowed to read the next pong message from the peer.
	PongWait time.Duration
	// PingPeriod is the tend pings to peer with this period. Must be less than
	// pongWait.
	PingPeriod     time.Duration
	MaxMessageSize int64    // Maximum message size allowed from peer.
	Whitelist      []string // whitelist of allowed IPs for access
	RelayPubHex    string
	RelayNpub      string
	// ACL is the list of users and privileges on this relay
	ACL *acl.T
	// contains filtered or unexported fields
}

func NewRelay

func NewRelay(inf *relayinfo.T,
	conf *Config) (r *Relay)

func (*Relay) AddEvent

func (rl *Relay) AddEvent(c context.T, ev *event.T) (err error)

AddEvent sends an event through then normal add pipeline, as if it was received from a websocket.

func (*Relay) AuthCheck

func (rl *Relay) AuthCheck(c context.T)

AuthCheck sends out a request if auth is required (this is an OnConnect method).

func (*Relay) BroadcastEvent

func (rl *Relay) BroadcastEvent(evt *event.T)

BroadcastEvent emits an event to all listeners whose filters' match, skipping all filters and actions it also doesn't attempt to store the event or trigger any reactions or callbacks

func (*Relay) Chat

func (rl *Relay) Chat(c context.T, ev *event.T) (err error)

Chat implements the control interface, intercepting kind 4 encrypted direct messages and processing them if they are for the relay's pubkey

func (*Relay) Export

func (rl *Relay) Export(db *badger.Backend, filename string)

Export prints the JSON of all events or writes them to a file.

func (*Relay) FilterPrivileged

func (rl *Relay) FilterPrivileged(c context.T, id subscriptionid.T,
	f *filter.T) (reject bool, msg string)

FilterPrivileged interacts between filters and the privileges of the users according to the access control settings of the relay, checking whether the request is authorised, if not, requesting authorisation.

If there is an ACL configured, it acts as a whitelist, no access without being on the ACL.

If the message is a private message, only authenticated users may get these events who also match one of the parties in the conversation.

func (*Relay) HandleNIP11

func (rl *Relay) HandleNIP11(w http.ResponseWriter, r *http.Request)

func (*Relay) HandleWebsocket

func (rl *Relay) HandleWebsocket(w http.ResponseWriter, r *http.Request)

func (*Relay) Import

func (rl *Relay) Import(db *badger.Backend, files []string)

Import a collection of JSON events from stdin or from one or more files, line structured JSON.

func (*Relay) Init

func (rl *Relay) Init()

func (*Relay) OverrideDelete

func (rl *Relay) OverrideDelete(c context.T, tgt, del *event.T) (ok bool,
	msg string)

OverrideDelete decides whether to veto a delete event.

Temporarily removing delete functionality until a proper tombstone/indexing strategy is devised to filter out these events from database results.

func (*Relay) Router

func (rl *Relay) Router() *http.ServeMux

func (*Relay) ServeHTTP

func (rl *Relay) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler interface.

func (*Relay) Shutdown

func (rl *Relay) Shutdown(c context.T)

Shutdown sends a websocket close control message to all connected clients.

func (*Relay) Start

func (rl *Relay) Start(host string, port int, started ...chan bool) (err error)

Start creates an http server and starts listening on given host and port.

type Spam

type Spam struct {
	Spammers []Spam
}

type Spammer

type Spammer struct {
	Address     string
	Offenses    int
	BannedUntil time.Time
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL