mocrelay

package module
v0.0.0-...-a670316 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2024 License: MIT Imports: 25 Imported by: 0

README

mocrelay

Go

Nostr relay

Documentation

Index

Constants

View Source
const (
	ServerOKMsgPrefixNoPrefix    = ""
	ServerOKMsgPrefixPoW         = "pow: "
	ServerOKMsgPrefixDuplicate   = "duplicate: "
	ServerOkMsgPrefixBlocked     = "blocked: "
	ServerOkMsgPrefixRateLimited = "rate-limited: "
	ServerOkMsgPrefixRateInvalid = "invalid: "
	ServerOkMsgPrefixError       = "error: "
)
View Source
const (
	ServerClosedMsgPrefixNoPrefix    = ""
	ServerClosedMsgPrefixPoW         = "pow: "
	ServerClosedMsgPrefixDuplicate   = "duplicate: "
	ServerClosedMsgPrefixBlocked     = "blocked: "
	ServerClosedMsgPrefixRateLimited = "rate-limited: "
	ServerClosedMsgPrefixRateInvalid = "invalid: "
	ServerClosedMsgPrefixError       = "error: "
)

Variables

View Source
var ErrEventSerialize = errors.New("failed to serialize event")
View Source
var ErrInvalidClientMsg = errors.New("invalid client message")
View Source
var ErrMarshalServerAuthMsg = errors.New("failed to marshal server auth msg")
View Source
var ErrMarshalServerClosedMsg = errors.New("failed to marshal server closed msg")
View Source
var ErrMarshalServerCountMsg = errors.New("failed to marshal server count msg")
View Source
var ErrMarshalServerEOSEMsg = errors.New("failed to marshal server eose msg")
View Source
var ErrMarshalServerEventMsg = errors.New("failed to marshal server event msg")
View Source
var ErrMarshalServerNoticeMsg = errors.New("failed to marshal server notice msg")
View Source
var ErrMarshalServerOKMsg = errors.New("failed to marshal server ok msg")
View Source
var (
	ErrRecvClosed = errors.New("recv closed")
)
View Source
var (
	ErrRelayStop = errors.New("relay stopped")
)
View Source
var ErrServerAuthMsgNilEvent = errors.New("server auth msg event must be non nil value")

Functions

func GetRequest

func GetRequest(ctx context.Context) *http.Request

func ValidClientMsg

func ValidClientMsg(msg ClientMsg) bool

Types

type CacheHandler

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

func NewCacheHandler

func NewCacheHandler(size int) CacheHandler

func (CacheHandler) Dump

func (h CacheHandler) Dump(w io.Writer) error

func (CacheHandler) Restore

func (h CacheHandler) Restore(r io.Reader) error

func (CacheHandler) ServeNostr

func (h CacheHandler) ServeNostr(
	ctx context.Context,
	send chan<- ServerMsg,
	recv <-chan ClientMsg,
) error

type ClientAuthMsg

type ClientAuthMsg struct {
	Challenge string
}

func (*ClientAuthMsg) ClientMsg

func (*ClientAuthMsg) ClientMsg()

func (*ClientAuthMsg) UnmarshalJSON

func (msg *ClientAuthMsg) UnmarshalJSON(b []byte) error

func (*ClientAuthMsg) Valid

func (msg *ClientAuthMsg) Valid() bool

type ClientCloseMsg

type ClientCloseMsg struct {
	SubscriptionID string
}

func (*ClientCloseMsg) ClientMsg

func (*ClientCloseMsg) ClientMsg()

func (*ClientCloseMsg) UnmarshalJSON

func (msg *ClientCloseMsg) UnmarshalJSON(b []byte) error

func (*ClientCloseMsg) Valid

func (msg *ClientCloseMsg) Valid() bool

type ClientCountMsg

type ClientCountMsg struct {
	SubscriptionID string
	ReqFilters     []*ReqFilter
}

func (*ClientCountMsg) ClientMsg

func (*ClientCountMsg) ClientMsg()

func (*ClientCountMsg) UnmarshalJSON

func (msg *ClientCountMsg) UnmarshalJSON(b []byte) error

func (*ClientCountMsg) Valid

func (msg *ClientCountMsg) Valid() (ok bool)

type ClientEventMsg

type ClientEventMsg struct {
	Event *Event
}

func (*ClientEventMsg) ClientMsg

func (*ClientEventMsg) ClientMsg()

func (*ClientEventMsg) UnmarshalJSON

func (msg *ClientEventMsg) UnmarshalJSON(b []byte) error

func (*ClientEventMsg) Valid

func (msg *ClientEventMsg) Valid() bool

type ClientMsg

type ClientMsg interface {
	ClientMsg()
}

func ParseClientMsg

func ParseClientMsg(b []byte) (msg ClientMsg, err error)

type ClientReqMsg

type ClientReqMsg struct {
	SubscriptionID string
	ReqFilters     []*ReqFilter
}

func (*ClientReqMsg) ClientMsg

func (*ClientReqMsg) ClientMsg()

func (*ClientReqMsg) UnmarshalJSON

func (msg *ClientReqMsg) UnmarshalJSON(b []byte) error

func (*ClientReqMsg) Valid

func (msg *ClientReqMsg) Valid() (ok bool)

type ClientUnknownMsg

type ClientUnknownMsg struct {
	Label string
	Msg   []interface{}
}

func (*ClientUnknownMsg) ClientMsg

func (*ClientUnknownMsg) ClientMsg()

func (*ClientUnknownMsg) UnmarshalJSON

func (msg *ClientUnknownMsg) UnmarshalJSON(b []byte) error

func (*ClientUnknownMsg) Valid

func (msg *ClientUnknownMsg) Valid() bool

type CreatedAtLowerLimitMiddleware

type CreatedAtLowerLimitMiddleware Middleware

func NewCreatedAtLowerLimitMiddleware

func NewCreatedAtLowerLimitMiddleware(lower int64) CreatedAtLowerLimitMiddleware

type CreatedAtUpperLimitMiddleware

type CreatedAtUpperLimitMiddleware Middleware

func NewCreatedAtUpperLimitMiddleware

func NewCreatedAtUpperLimitMiddleware(upper int64) CreatedAtUpperLimitMiddleware

type DefaultSimpleHandlerBase

type DefaultSimpleHandlerBase struct{}

func (DefaultSimpleHandlerBase) ServeNostrClientMsg

func (DefaultSimpleHandlerBase) ServeNostrClientMsg(
	ctx context.Context,
	msg ClientMsg,
) (<-chan ServerMsg, error)

func (DefaultSimpleHandlerBase) ServeNostrEnd

func (DefaultSimpleHandlerBase) ServeNostrEnd(ctx context.Context) error

func (DefaultSimpleHandlerBase) ServeNostrStart

func (DefaultSimpleHandlerBase) ServeNostrStart(ctx context.Context) (context.Context, error)

type Event

type Event struct {
	ID        string `json:"id"`
	Pubkey    string `json:"pubkey"`
	CreatedAt int64  `json:"created_at"`
	Kind      int64  `json:"kind"`
	Tags      []Tag  `json:"tags"`
	Content   string `json:"content"`
	Sig       string `json:"sig"`
}

func (*Event) CreatedAtTime

func (ev *Event) CreatedAtTime() time.Time

func (*Event) EventType

func (ev *Event) EventType() EventType

func (*Event) Serialize

func (ev *Event) Serialize() ([]byte, error)

func (*Event) UnmarshalJSON

func (ev *Event) UnmarshalJSON(b []byte) error

func (*Event) Valid

func (ev *Event) Valid() bool

func (*Event) Verify

func (ev *Event) Verify() (bool, error)

type EventCache

type EventCache struct {
	Cap int
	// contains filtered or unexported fields
}

func NewEventCache

func NewEventCache(capacity int) *EventCache

func (*EventCache) Add

func (c *EventCache) Add(event *Event) (added bool)

func (*EventCache) Find

func (c *EventCache) Find(filters []*ReqFilter) []*Event

func (*EventCache) Len

func (c *EventCache) Len() int

type EventCountMatcher

type EventCountMatcher interface {
	EventMatcher
	CountMatch(*Event) bool
	Done() bool
}

type EventCountMatchers

type EventCountMatchers[T EventCountMatcher] []T

func NewReqFiltersEventMatchers

func NewReqFiltersEventMatchers(
	filters []*ReqFilter,
) EventCountMatchers[*ReqFilterEventMatcher]

func (EventCountMatchers[T]) CountMatch

func (m EventCountMatchers[T]) CountMatch(event *Event) bool

func (EventCountMatchers[T]) Done

func (m EventCountMatchers[T]) Done() bool

func (EventCountMatchers[T]) Match

func (m EventCountMatchers[T]) Match(event *Event) bool

type EventCreatedAtMiddleware

type EventCreatedAtMiddleware Middleware

func NewEventCreatedAtMiddleware

func NewEventCreatedAtMiddleware(
	from, to time.Duration,
) EventCreatedAtMiddleware

type EventInvalidIDError

type EventInvalidIDError struct {
	Correct, Actual string
}

func (*EventInvalidIDError) Error

func (e *EventInvalidIDError) Error() string

type EventInvalidSigError

type EventInvalidSigError struct {
	Correct, Actual string
}

func (*EventInvalidSigError) Error

func (e *EventInvalidSigError) Error() string

type EventMatcher

type EventMatcher interface {
	Match(*Event) bool
}

type EventType

type EventType int
const (
	EventTypeUnknown EventType = iota
	EventTypeRegular
	EventTypeReplaceable
	EventTypeEphemeral
	EventTypeParamReplaceable
)

type Handler

type Handler interface {
	ServeNostr(ctx context.Context, send chan<- ServerMsg, recv <-chan ClientMsg) error
}

func NewDefaultHandler

func NewDefaultHandler() Handler

func NewMergeHandler

func NewMergeHandler(handlers ...Handler) Handler

func NewSimpleHandler

func NewSimpleHandler(h SimpleHandlerBase) Handler

type HandlerFunc

type HandlerFunc func(ctx context.Context, send chan<- ServerMsg, recv <-chan ClientMsg) error

func (HandlerFunc) ServeNostr

func (f HandlerFunc) ServeNostr(
	ctx context.Context,
	send chan<- ServerMsg,
	recv <-chan ClientMsg,
) error

type LoggingMiddleware

type LoggingMiddleware Middleware

func NewLoggingMiddleware

func NewLoggingMiddleware(logger *slog.Logger) LoggingMiddleware

type MaxContentLengthMiddleware

type MaxContentLengthMiddleware Middleware

func NewMaxContentLengthMiddleware

func NewMaxContentLengthMiddleware(maxContentLength int) MaxContentLengthMiddleware

type MaxEventTagsMiddleware

type MaxEventTagsMiddleware Middleware

func NewMaxEventTagsMiddleware

func NewMaxEventTagsMiddleware(maxEventTags int) MaxEventTagsMiddleware

type MaxLimitMiddleware

type MaxLimitMiddleware Middleware

func NewMaxLimitMiddleware

func NewMaxLimitMiddleware(maxLimit int) MaxLimitMiddleware

type MaxReqFiltersMiddleware

type MaxReqFiltersMiddleware Middleware

func NewMaxReqFiltersMiddleware

func NewMaxReqFiltersMiddleware(maxFilters int) MaxReqFiltersMiddleware

type MaxSubIDLengthMiddleware

type MaxSubIDLengthMiddleware Middleware

func NewMaxSubIDLengthMiddleware

func NewMaxSubIDLengthMiddleware(maxSubIDLength int) MaxSubIDLengthMiddleware

type MaxSubscriptionsMiddleware

type MaxSubscriptionsMiddleware Middleware

func NewMaxSubscriptionsMiddleware

func NewMaxSubscriptionsMiddleware(maxSubs int) MaxSubscriptionsMiddleware

type MergeHandler

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

func (*MergeHandler) ServeNostr

func (h *MergeHandler) ServeNostr(
	ctx context.Context,
	send chan<- ServerMsg,
	recv <-chan ClientMsg,
) error

type Middleware

type Middleware func(Handler) Handler

func BuildMiddlewareFromNIP11

func BuildMiddlewareFromNIP11(nip11 *NIP11) Middleware

func NewSimpleMiddleware

func NewSimpleMiddleware(base SimpleMiddlewareBase) Middleware

type NIP11

type NIP11 struct {
	Name          string           `json:"name,omitempty"`
	Description   string           `json:"description,omitempty"`
	Pubkey        string           `json:"pubkey,omitempty"`
	Contact       string           `json:"contact,omitempty"`
	SupportedNIPs []int            `json:"supported_nips,omitempty"`
	Software      string           `json:"software,omitempty"`
	Version       string           `json:"version,omitempty"`
	Limitation    *NIP11Limitation `json:"limitation,omitempty"`
	Retention     *NIP11Retention  `json:"retention,omitempty"`
	RelayContries []string         `json:"relay_countries,omitempty"`
	LanguageTags  []string         `json:"language_tags,omitempty"`
	Tags          []string         `json:"tags,omitempty"`
	PostingPolicy string           `json:"posting_policy,omitempty"`
	PaymentsURL   string           `json:"payments_url,omitempty"`
	Fees          *NIP11Fees       `json:"fees,omitempty"`
	Icon          string           `json:"icon,omitempty"`
}

func (*NIP11) ServeHTTP

func (nip11 *NIP11) ServeHTTP(w http.ResponseWriter, r *http.Request)

type NIP11Fees

type NIP11Fees struct {
}

type NIP11Limitation

type NIP11Limitation struct {
	MaxMessageLength    int   `json:"max_message_length,omitempty"`
	MaxSubscriptions    int   `json:"max_subscriptions,omitempty"`
	MaxFilters          int   `json:"max_filters,omitempty"`
	MaxLimit            int   `json:"max_limit,omitempty"`
	MaxSubIDLength      int   `json:"max_subid_length,omitempty"`
	MaxEventTags        int   `json:"max_event_tags,omitempty"`
	MaxContentLength    int   `json:"max_content_length,omitempty"`
	MinPoWDifficulty    int   `json:"min_pow_difficulty,omitempty"`
	AuthRequired        bool  `json:"auth_required,omitempty"`
	PaymentRequired     bool  `json:"payment_required,omitempty"`
	CreatedAtLowerLimit int64 `json:"created_at_lower_limit,omitempty"`
	CreatedAtUpperLimit int64 `json:"created_at_upper_limit,omitempty"`
}

type NIP11Retention

type NIP11Retention struct {
}

type RecvEventAllowFilterMiddleware

type RecvEventAllowFilterMiddleware Middleware

func NewRecvEventAllowFilterMiddleware

func NewRecvEventAllowFilterMiddleware(matcher EventMatcher) RecvEventAllowFilterMiddleware

type RecvEventDenyFilterMiddleware

type RecvEventDenyFilterMiddleware Middleware

func NewRecvEventDenyFilterMiddleware

func NewRecvEventDenyFilterMiddleware(matcher EventMatcher) RecvEventDenyFilterMiddleware

type RecvEventUniqueFilterMiddleware

type RecvEventUniqueFilterMiddleware Middleware

func NewRecvEventUniqueFilterMiddleware

func NewRecvEventUniqueFilterMiddleware(size int) RecvEventUniqueFilterMiddleware

type Relay

type Relay struct {
	Handler Handler
	// contains filtered or unexported fields
}

func NewRelay

func NewRelay(handler Handler, option *RelayOption) *Relay

func (*Relay) ServeHTTP

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

func (*Relay) Wait

func (relay *Relay) Wait()

type RelayOption

type RelayOption struct {
	Logger *slog.Logger

	SendTimeout time.Duration

	RecvRateLimitRate  float64
	RecvRateLimitBurst int

	MaxMessageLength int64

	PingDuration time.Duration
}

func NewDefaultRelayOption

func NewDefaultRelayOption() *RelayOption

type ReqFilter

type ReqFilter struct {
	IDs     []string
	Authors []string
	Kinds   []int64
	Tags    map[string][]string
	Since   *int64
	Until   *int64
	Limit   *int64
}

func (*ReqFilter) UnmarshalJSON

func (fil *ReqFilter) UnmarshalJSON(b []byte) error

func (*ReqFilter) Valid

func (fil *ReqFilter) Valid() (ok bool)

type ReqFilterEventMatcher

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

func NewReqFilterMatcher

func NewReqFilterMatcher(filter *ReqFilter) *ReqFilterEventMatcher

func (*ReqFilterEventMatcher) CountMatch

func (m *ReqFilterEventMatcher) CountMatch(event *Event) bool

func (*ReqFilterEventMatcher) Done

func (m *ReqFilterEventMatcher) Done() bool

func (*ReqFilterEventMatcher) Match

func (m *ReqFilterEventMatcher) Match(event *Event) bool

type ReqFiltersMatcher

type ReqFiltersMatcher []*ReqFilterEventMatcher

type RouterHandler

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

func NewRouterHandler

func NewRouterHandler(buflen int) *RouterHandler

func (*RouterHandler) ServeNostr

func (router *RouterHandler) ServeNostr(
	ctx context.Context,
	send chan<- ServerMsg,
	recv <-chan ClientMsg,
) (err error)

type SendEventUniqueFilterMiddleware

type SendEventUniqueFilterMiddleware Middleware

func NewSendEventUniqueFilterMiddleware

func NewSendEventUniqueFilterMiddleware(size int) SendEventUniqueFilterMiddleware

type ServeMux

type ServeMux struct {
	Relay   *Relay
	NIP11   *NIP11
	Default http.Handler
	Logger  *slog.Logger
}

func (*ServeMux) ServeHTTP

func (mux *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request)

type ServerAuthMsg

type ServerAuthMsg struct {
	Event *Event
}

func NewServerAuthMsg

func NewServerAuthMsg(event *Event) (*ServerAuthMsg, error)

func (*ServerAuthMsg) MarshalJSON

func (msg *ServerAuthMsg) MarshalJSON() ([]byte, error)

func (*ServerAuthMsg) ServerMsg

func (*ServerAuthMsg) ServerMsg()

type ServerClosedMsg

type ServerClosedMsg struct {
	SubscriptionID string
	Msg            string
	MsgPrefix      string
}

func NewServerClosedMsg

func NewServerClosedMsg(subID string, prefix, msg string) *ServerClosedMsg

func NewServerClosedMsgf

func NewServerClosedMsgf(subID string, prefix, format string, a ...any) *ServerClosedMsg

func (*ServerClosedMsg) MarshalJSON

func (msg *ServerClosedMsg) MarshalJSON() ([]byte, error)

func (*ServerClosedMsg) Message

func (msg *ServerClosedMsg) Message() string

func (*ServerClosedMsg) ServerMsg

func (*ServerClosedMsg) ServerMsg()

type ServerCountMsg

type ServerCountMsg struct {
	SubscriptionID string
	Count          uint64
	Approximate    *bool
}

func NewServerCountMsg

func NewServerCountMsg(subID string, count uint64, approx *bool) *ServerCountMsg

func (*ServerCountMsg) MarshalJSON

func (msg *ServerCountMsg) MarshalJSON() ([]byte, error)

func (*ServerCountMsg) ServerMsg

func (*ServerCountMsg) ServerMsg()

type ServerEOSEMsg

type ServerEOSEMsg struct {
	SubscriptionID string
}

func NewServerEOSEMsg

func NewServerEOSEMsg(subID string) *ServerEOSEMsg

func (*ServerEOSEMsg) MarshalJSON

func (msg *ServerEOSEMsg) MarshalJSON() ([]byte, error)

func (*ServerEOSEMsg) ServerMsg

func (*ServerEOSEMsg) ServerMsg()

type ServerEventMsg

type ServerEventMsg struct {
	SubscriptionID string
	Event          *Event
}

func NewServerEventMsg

func NewServerEventMsg(subID string, event *Event) *ServerEventMsg

func (*ServerEventMsg) MarshalJSON

func (msg *ServerEventMsg) MarshalJSON() ([]byte, error)

func (*ServerEventMsg) ServerMsg

func (*ServerEventMsg) ServerMsg()

type ServerMsg

type ServerMsg interface {
	ServerMsg()
	MarshalJSON() ([]byte, error)
}

type ServerNoticeMsg

type ServerNoticeMsg struct {
	Message string
}

func NewServerNoticeMsg

func NewServerNoticeMsg(message string) *ServerNoticeMsg

func NewServerNoticeMsgf

func NewServerNoticeMsgf(format string, a ...any) *ServerNoticeMsg

func (*ServerNoticeMsg) MarshalJSON

func (msg *ServerNoticeMsg) MarshalJSON() ([]byte, error)

func (*ServerNoticeMsg) ServerMsg

func (*ServerNoticeMsg) ServerMsg()

type ServerOKMsg

type ServerOKMsg struct {
	EventID   string
	Accepted  bool
	Msg       string
	MsgPrefix string
}

func NewServerOKMsg

func NewServerOKMsg(eventID string, accepted bool, prefix, msg string) *ServerOKMsg

func (*ServerOKMsg) MarshalJSON

func (msg *ServerOKMsg) MarshalJSON() ([]byte, error)

func (*ServerOKMsg) Message

func (msg *ServerOKMsg) Message() string

func (*ServerOKMsg) ServerMsg

func (*ServerOKMsg) ServerMsg()

type SimpleHandlerBase

type SimpleHandlerBase interface {
	ServeNostrStart(context.Context) (context.Context, error)
	ServeNostrEnd(context.Context) error
	ServeNostrClientMsg(context.Context, ClientMsg) (<-chan ServerMsg, error)
}

type SimpleMiddlewareBase

type SimpleMiddlewareBase interface {
	ServeNostrStart(context.Context) (context.Context, error)
	ServeNostrEnd(context.Context) error
	ServeNostrClientMsg(context.Context, ClientMsg) (<-chan ClientMsg, <-chan ServerMsg, error)
	ServeNostrServerMsg(context.Context, ServerMsg) (<-chan ServerMsg, error)
}

type Tag

type Tag []string

Directories

Path Synopsis
cmd
handler
middleware

Jump to

Keyboard shortcuts

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