utils

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AuthRequiredProvider func() bool

AuthRequiredProvider is set by higher-level packages to report the current auth requirement without introducing an import cycle into config.

View Source
var ErrOwnerAlreadySet = errors.New("relay owner is already set")

ErrOwnerAlreadySet is returned by SetRelayOwner when the on-disk metadata already names an owner. Callers (the /setup POST handler) translate this to a 409 so the page can show the "already claimed" state without conflating it with a real write failure.

Functions

func DetermineEventCategory

func DetermineEventCategory(kind int) string

func EnsureFileExists

func EnsureFileExists(filePath string, content []byte)

EnsureFileExists creates a file from embedded content if it doesn't exist

func FetchDomainNames added in v0.7.0

func FetchDomainNames(domain string) (map[string]string, error)

FetchDomainNames returns the parsed names map from a single domain's .well-known/nostr.json. Exported for the admin dashboard's domain-preview endpoint — the cache refresher uses fetchDomainPubkeys instead (which throws away the names).

func FetchPubkeysFromDomains

func FetchPubkeysFromDomains(domains []string) ([]string, error)

FetchPubkeysFromDomains fetches nostr.json pubkeys from multiple domains This function is called by the pubkey cache system and doesn't maintain its own cache

func GenerateChallenge added in v0.5.0

func GenerateChallenge(length int) string

GenerateChallenge creates a random hex string for NIP-42 authentication

func GetClientIP

func GetClientIP(r *http.Request) string

func GetRelayOwnerPubkey added in v0.7.0

func GetRelayOwnerPubkey() string

GetRelayOwnerPubkey returns the relay owner's hex pubkey from relay_metadata.json. This is the pubkey allowed to authenticate against admin/management HTTP endpoints via NIP-98.

Note: returns the raw on-disk value, which may be the all-zeros sentinel from the example metadata. Use IsRelayUnowned() when you want to know whether ownership has been claimed.

func IsRelayUnowned added in v0.7.0

func IsRelayUnowned() bool

IsRelayUnowned reports whether the relay has no owner claimed yet. True when the pubkey field is empty OR set to the all-zeros sentinel the example ships with. Used by /setup and /admin gates so a fresh deployment routes operators to /setup regardless of which form the example used.

func LoadRelayMetadata

func LoadRelayMetadata(filename string) error

func LoadRelayMetadataJSON

func LoadRelayMetadataJSON() error

func OverrideRelayOwnerInMemory added in v0.7.0

func OverrideRelayOwnerInMemory(pubkey string)

OverrideRelayOwnerInMemory sets relayMetadata.Pubkey without touching disk. Used by the env-var bootstrap path (GRAIN_OWNER_PUBKEY) so the override is re-applied on every startup and the on-disk file stays clean — operators who unset the env var don't silently inherit a stale baked-in owner.

Accepts lowercased hex only; the env-var parser normalizes first. Holds ownerClaimMu so a /setup POST racing startup can't write a claim that gets overwritten in memory a moment later.

func RelayInfoHandler

func RelayInfoHandler(w http.ResponseWriter, r *http.Request)

func SendToBackupRelay

func SendToBackupRelay(backupURL string, evt nostr.Event) error

SendToBackupRelay forwards a single event to one backup relay. It is best-effort and bounded in time: both the dial and the write are deadlined so a slow or unreachable backup can't pin the calling goroutine. Concurrency across events is capped by the caller (see handlers/event.go), and a fresh connection is used per call. See #95.

func SetAdminWriteHooks added in v0.7.0

func SetAdminWriteHooks(write adminWriter, suppress watcherSuppressor)

SetAdminWriteHooks lets server startup wire the write + suppression implementations from the config package, avoiding an import cycle while still keeping a single source of truth for the watcher key.

func SetRelayMetadataField added in v0.7.0

func SetRelayMetadataField(field, value string) error

SetRelayMetadataField writes a single top-level field to relay_metadata.json and reloads the in-memory copy. Used by the generic NIP-86 changerelay* handlers — same atomic write + watcher suppression pipeline as UpdateRelayMetadata but without the per-field pointer-arg ceremony, so new metadata fields (contact, posting_policy, etc.) can be added by extending the handler's allow-list rather than the function signature.

The field name is the JSON key as it lives on disk. Caller is responsible for validation (empty / URL / etc.) — this helper just persists.

func SetRelayMetadataWritePath added in v0.7.0

func SetRelayMetadataWritePath(p string)

SetRelayMetadataWritePath records the resolved absolute path of the metadata file. Called from startup once the data dir is known. UpdateRelayMetadata uses this path so the watcher suppression key matches what fsnotify monitors.

func SetRelayOwner added in v0.7.0

func SetRelayOwner(pubkey string) error

SetRelayOwner persists the relay owner pubkey to relay_metadata.json and reloads the in-memory copy. Intended for one-time first-run provisioning from the /setup flow; runtime owner rotation would go through a future NIP-86 method, not this path.

Accepts lowercased hex only — callers (the /setup handler) normalize first. Returns ErrOwnerAlreadySet without writing if the on-disk owner is non-empty, so the first POST through this function wins and the second sees a clean "already claimed" signal.

Same atomic-write + watcher-suppression pipeline UpdateRelayMetadata uses, plus the package-level ownerClaimMu guarding the check+write atomicity.

func SetVersion added in v0.5.0

func SetVersion(v string)

SetVersion records the build-time version string. Called from server.SetVersionInfo during startup; kept in this leaf package to avoid a server -> server/utils import cycle.

func ToInt

func ToInt(i interface{}) *int

func ToInt64

func ToInt64(i interface{}) *int64

func ToIntArray

func ToIntArray(i interface{}) []int

func ToStringArray

func ToStringArray(i interface{}) []string

func ToTagsMap

func ToTagsMap(i interface{}) map[string][]string

func ToTime

func ToTime(data interface{}) *time.Time

func UpdateRelayMetadata added in v0.7.0

func UpdateRelayMetadata(name, description, icon, banner *string) error

UpdateRelayMetadata applies non-nil patches to relay_metadata.json and reloads the in-memory copy so NIP-11 responses + the owner check see the new values immediately.

JSON is read into a map[string]any rather than the typed RelayMetadata struct so unknown fields (custom NIP-11 extensions an operator may have added) round-trip cleanly. Marshaling through the typed struct would silently drop them.

Used by NIP-86 changerelayname / changerelaydescription / changerelayicon — each passes exactly one non-nil patch. Pointer args so the dispatcher can express "don't touch this field" by passing nil.

Atomic write + watcher suppression are handled here so callers don't have to remember. The function is safe for concurrent use: it holds the same package-internal mutex used by the suppression machinery on the config side.

Types

type NostrJSON

type NostrJSON struct {
	Names map[string]string `json:"names"`
}

NostrJSON represents the structure of .well-known/nostr.json

type RelayMetadata

type RelayMetadata struct {
	Name           string `json:"name"`
	Description    string `json:"description"`
	Banner         string `json:"banner"`
	Icon           string `json:"icon"`
	Pubkey         string `json:"pubkey"`
	Contact        string `json:"contact"`
	SupportedNIPs  []int  `json:"supported_nips"`
	Software       string `json:"software"`
	Version        string `json:"version"`
	PrivacyPolicy  string `json:"privacy_policy"`
	TermsOfService string `json:"terms_of_service"`
	Limitation     struct {
		MaxMessageLength    int    `json:"max_message_length"`
		MaxContentLength    int    `json:"max_content_length"`
		MaxSubscriptions    int    `json:"max_subscriptions"`
		MaxLimit            int    `json:"max_limit"`
		AuthRequired        bool   `json:"auth_required"`
		PaymentRequired     bool   `json:"payment_required"`
		RestrictedWrites    bool   `json:"restricted_writes"`
		CreatedAtLowerLimit *int64 `json:"created_at_lower_limit"`
		CreatedAtUpperLimit *int64 `json:"created_at_upper_limit"`
	} `json:"limitation"`
	RelayCountries []string `json:"relay_countries"`
	LanguageTags   []string `json:"language_tags"`
	Tags           []string `json:"tags"`
	PostingPolicy  string   `json:"posting_policy"`
}

func GetRelayMetadataCopy added in v0.7.0

func GetRelayMetadataCopy() RelayMetadata

GetRelayMetadataCopy returns a value copy of the parsed relay_metadata.json so callers can read fields (name, description, icon, contact, …) without sharing internal state. The admin dashboard's ops section uses this to pre-fill the relay-identity edit form; tests use it to assert post-update state.

Directories

Path Synopsis
Package relayurl applies NIP-42-flavored URL normalization for the purpose of comparing a client-supplied AUTH `relay` tag against the relay's configured Auth.RelayURL.
Package relayurl applies NIP-42-flavored URL normalization for the purpose of comparing a client-supplied AUTH `relay` tag against the relay's configured Auth.RelayURL.

Jump to

Keyboard shortcuts

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