Documentation
¶
Index ¶
- Constants
- Variables
- func GetRemoteFromReq(r *http.Request) (rr string)
- func Run(ctx context.Context, cfg *config.C, db *database.D) (quit chan struct{})
- type Listener
- func (l *Listener) Ctx() context.Context
- func (l *Listener) GetSerialsFromFilter(f *filter.F) (sers types.Uint40s, err error)
- func (l *Listener) HandleAuth(b []byte) (err error)
- func (l *Listener) HandleClose(req []byte) (err error)
- func (l *Listener) HandleDelete(env *eventenvelope.Submission) (err error)
- func (l *Listener) HandleEvent(msg []byte) (err error)
- func (l *Listener) HandleMessage(msg []byte, remote string)
- func (l *Listener) HandleReq(msg []byte) (err error)
- func (l *Listener) Write(p []byte) (n int, err error)
- type Map
- type OK
- type OKs
- type P
- type Server
- func (s *Server) HandleRelayInfo(w http.ResponseWriter, r *http.Request)
- func (s *Server) HandleWebsocket(w http.ResponseWriter, r *http.Request)
- func (s *Server) Pinger(ctx context.Context, conn *websocket.Conn, ticker *time.Ticker, ...)
- func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (s *Server) ServiceURL(req *http.Request) (st string)
- type Subscription
- type W
Constants ¶
const ( DefaultWriteWait = 10 * time.Second DefaultPongWait = 60 * time.Second DefaultPingWait = DefaultPongWait / 2 DefaultReadTimeout = 7 * time.Second // Read timeout to detect stalled connections DefaultWriteTimeout = 3 * time.Second DefaultMaxMessageSize = 1 * units.Mb // CloseMessage denotes a close control message. The optional message // payload contains a numeric code and text. Use the FormatCloseMessage // function to format a close message payload. CloseMessage = 8 // PingMessage denotes a ping control message. The optional message payload // is UTF-8 encoded text. PingMessage = 9 // PongMessage denotes a pong control message. The optional message payload // is UTF-8 encoded text. PongMessage = 10 )
const Type = "socketapi"
Variables ¶
var Ok = OKs{ Ok: func( l *Listener, env eventenvelope.I, format string, params ...any, ) (err error) { return okenvelope.NewFrom( env.Id(), true, []byte{}, ).Write(l) }, AuthRequired: func( l *Listener, env eventenvelope.I, format string, params ...any, ) (err error) { return okenvelope.NewFrom( env.Id(), false, reason.AuthRequired.F(format, params...), ).Write(l) }, PoW: func( l *Listener, env eventenvelope.I, format string, params ...any, ) (err error) { return okenvelope.NewFrom( env.Id(), false, reason.PoW.F(format, params...), ).Write(l) }, Duplicate: func( l *Listener, env eventenvelope.I, format string, params ...any, ) (err error) { return okenvelope.NewFrom( env.Id(), false, reason.Duplicate.F(format, params...), ).Write(l) }, Blocked: func( l *Listener, env eventenvelope.I, format string, params ...any, ) (err error) { return okenvelope.NewFrom( env.Id(), false, reason.Blocked.F(format, params...), ).Write(l) }, RateLimited: func( l *Listener, env eventenvelope.I, format string, params ...any, ) (err error) { return okenvelope.NewFrom( env.Id(), false, reason.RateLimited.F(format, params...), ).Write(l) }, Invalid: func( l *Listener, env eventenvelope.I, format string, params ...any, ) (err error) { return okenvelope.NewFrom( env.Id(), false, reason.Invalid.F(format, params...), ).Write(l) }, Error: func( l *Listener, env eventenvelope.I, format string, params ...any, ) (err error) { return okenvelope.NewFrom( env.Id(), false, reason.Error.F(format, params...), ).Write(l) }, Unsupported: func( l *Listener, env eventenvelope.I, format string, params ...any, ) (err error) { return okenvelope.NewFrom( env.Id(), false, reason.Unsupported.F(format, params...), ).Write(l) }, Restricted: func( l *Listener, env eventenvelope.I, format string, params ...any, ) (err error) { return okenvelope.NewFrom( env.Id(), false, reason.Restricted.F(format, params...), ).Write(l) }, }
Ok provides a collection of handler functions for managing different types of operational outcomes, each corresponding to specific error or status conditions such as authentication requirements, rate limiting, and invalid inputs.
Functions ¶
func GetRemoteFromReq ¶
GetRemoteFromReq retrieves the originating IP address of the client from an HTTP request, considering standard and non-standard proxy headers.
Parameters ¶
- r: The HTTP request object containing details of the client and routing information.
Return Values ¶
- rr: A string value representing the IP address of the originating remote client.
Expected behaviour ¶
The function first checks for the standardized "Forwarded" header (RFC 7239) to identify the original client IP. If that isn't available, it falls back to the "X-Forwarded-For" header. If both headers are absent, it defaults to using the request's RemoteAddr.
For the "Forwarded" header, it extracts the client IP from the "for" parameter. For the "X-Forwarded-For" header, if it contains one IP, it returns that. If it contains two IPs, it returns the second.
Types ¶
type Listener ¶
type Listener struct {
*Server
// contains filtered or unexported fields
}
func (*Listener) Ctx ¶ added in v0.4.0
Ctx returns the listener's context, but creates a new context for each operation to prevent cancellation from affecting subsequent operations
func (*Listener) GetSerialsFromFilter ¶
func (*Listener) HandleClose ¶
HandleClose processes a CLOSE envelope by unmarshalling the request, validates the presence of an <id> field, and signals cancellation for the associated listener through the server's publisher mechanism.
func (*Listener) HandleDelete ¶
func (l *Listener) HandleDelete(env *eventenvelope.Submission) (err error)
type Map ¶
type Map map[*websocket.Conn]map[string]Subscription
Map is a map of filters associated with a collection of ws.Listener connections.
type OK ¶
OK represents a function that processes events or operations, using provided parameters to generate formatted messages and return errors if any issues occur during processing.
type OKs ¶
type OKs struct {
Ok OK
AuthRequired OK
PoW OK
Duplicate OK
Blocked OK
RateLimited OK
Invalid OK
Error OK
Unsupported OK
Restricted OK
}
OKs provides a collection of handler functions for managing different types of operational outcomes, each corresponding to specific error or status conditions such as authentication requirements, rate limiting, and invalid inputs.
type P ¶
type P struct {
// Mx is the mutex for the Map.
Mx sync.RWMutex
// Map is the map of subscribers and subscriptions from the websocket api.
Map
// contains filtered or unexported fields
}
P is a structure that manages subscriptions and associated filters for websocket listeners. It uses a mutex to synchronize access to a map storing subscriber connections and their filter configurations.
func (*P) Deliver ¶
Deliver processes and distributes an event to all matching subscribers based on their filter configurations.
Parameters ¶
- ev (*event.E): The event to be delivered to subscribed clients.
Expected behaviour ¶
Delivers the event to all subscribers whose filters match the event. It applies authentication checks if required by the server and skips delivery for unauthenticated users when events are privileged.
func (*P) Receive ¶
Receive handles incoming messages to manage websocket listener subscriptions and associated filters.
Parameters ¶
- msg (publisher.Message): The incoming message to process; expected to be of type *W to trigger subscription management actions.
Expected behaviour ¶
- Checks if the message is of type *W.
- If Cancel is true, removes a subscriber by ID or the entire listener.
- Otherwise, adds the subscription to the map under a mutex lock.
- Logs actions related to subscription creation or removal.
type Server ¶
type Server struct {
Config *config.C
Ctx context.Context
Admins [][]byte
*database.D
// contains filtered or unexported fields
}
func (*Server) HandleRelayInfo ¶
func (s *Server) HandleRelayInfo(w http.ResponseWriter, r *http.Request)
HandleRelayInfo generates and returns a relay information document in JSON format based on the server's configuration and supported NIPs.
Parameters ¶
w: HTTP response writer used to send the generated document.
r: HTTP request object containing incoming client request data.
Expected Behaviour ¶
The function constructs a relay information document using either the Informer interface implementation or predefined server configuration. It returns this document as a JSON response to the client.
func (*Server) HandleWebsocket ¶
func (s *Server) HandleWebsocket(w http.ResponseWriter, r *http.Request)
func (*Server) Pinger ¶
type Subscription ¶
type W ¶
type W struct {
*websocket.Conn
// If Cancel is true, this is a close command.
Cancel bool
// Id is the subscription Id. If Cancel is true, cancel the named
// subscription, otherwise, cancel the publisher for the socket.
Id string
// The Receiver holds the event channel for receiving notifications or data
// relevant to this WebSocket connection.
Receiver event.C
// Filters holds a collection of filters used to match or process events
// associated with this WebSocket connection. It is used to determine which
// notifications or data should be received by the subscriber.
Filters *filter.S
// AuthedPubkey is the authenticated pubkey associated with the listener (if any).
AuthedPubkey []byte
// contains filtered or unexported fields
}
Source Files
¶
- handle-auth.go
- handle-close.go
- handle-delete.go
- handle-event.go
- handle-message.go
- handle-relayinfo.go
- handle-req.go
- handle-websocket.go
- helpers.go
- listener.go
- main.go
- ok.go
- publisher.go
- server.go
Directories
¶
| Path | Synopsis |
|---|---|
|
Package config provides a go-simpler.org/env configuration table and helpers for working with the list of key/value lists stored in .env files.
|
Package config provides a go-simpler.org/env configuration table and helpers for working with the list of key/value lists stored in .env files. |