replicatr

package
v1.0.11 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2024 License: CC0-1.0, Unlicense Imports: 43 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  int64 = 512000 // ???
)

Variables

View Source
var Software = "https://github.com/Hubmakerlabs/replicatr/cmd/replicatrd"
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 GetAuthed

func GetAuthed(c context.T) string

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 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 added in v1.0.11

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

func RejectKind4Snoopers

func RejectKind4Snoopers(c context.T, f *filter.T) (bool, string)

RejectKind4Snoopers prevents reading NIP-04 messages from people not involved in the conversation.

func RemoveListener

func RemoveListener(ws *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 *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 *WebSocket, f filters.T, c context.C)

Types

type CountEvents

type CountEvents func(c context.T, f *filter.T) (cnt int64, 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 Hook

type Hook func(c context.T)

function types used in the relay state

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 OverwriteDeletionOutcome

type OverwriteDeletionOutcome 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 *nip11.Info) *nip11.Info

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, f *filter.T) (C chan *event.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 added in v1.0.11

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, f *filter.T) (reject bool, msg string)

function types used in the relay state

type Relay

type Relay struct {
	ServiceURL               string
	RejectEvent              []RejectEvent
	RejectFilter             []RejectFilter
	RejectCountFilter        []RejectFilter
	OverwriteDeletionOutcome []OverwriteDeletionOutcome
	OverwriteResponseEvent   []OverwriteResponseEvent
	OverwriteFilter          []OverwriteFilter
	OverwriteCountFilter     []OverwriteFilter
	OverwriteRelayInfo       []OverwriteRelayInformation
	StoreEvent               []Events
	DeleteEvent              []Events
	QueryEvents              []QueryEvents
	CountEvents              []CountEvents
	OnConnect                []Hook
	OnDisconnect             []Hook
	OnEventSaved             []OnEventSaved
	// editing info will affect
	Info *nip11.Info
	*slog.Log

	// in case you call Server.Start
	Addr string

	// websocket options
	WriteWait      time.Duration // Time allowed to write a message to the peer.
	PongWait       time.Duration // Time allowed to read the next pong message from the peer.
	PingPeriod     time.Duration // Send pings to peer with this period. Must be less than pongWait.
	MaxMessageSize int64         // Maximum message size allowed from peer.
	// contains filtered or unexported fields
}

func NewRelay

func NewRelay(logger *slog.Log) (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) 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) 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) 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 WebSocket

type WebSocket struct {
	Request         *http.Request // original request
	Challenge       string        // nip42
	AuthedPublicKey string
	Authed          chan struct{}
	// contains filtered or unexported fields
}

WebSocket is a wrapper around a fasthttp/websocket with mutex locking and NIP-42 Auth support

func GetConnection

func GetConnection(c context.T) *WebSocket

func (*WebSocket) WriteJSON

func (ws *WebSocket) WriteJSON(any any) (err error)

WriteJSON writes an object as JSON to the websocket

func (*WebSocket) WriteMessage

func (ws *WebSocket) WriteMessage(t int, b []byte) (err error)

WriteMessage writes a message with a given websocket type specifier

Jump to

Keyboard shortcuts

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