2022-01-12 10:54:45 -04:00
|
|
|
package nostr
|
|
|
|
|
|
|
|
import (
|
2023-05-04 23:51:15 +02:00
|
|
|
"context"
|
2024-03-28 17:23:02 +03:00
|
|
|
"crypto/tls"
|
2025-01-17 14:08:54 -03:00
|
|
|
"errors"
|
2023-05-04 23:51:15 +02:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2025-01-17 14:08:54 -03:00
|
|
|
"time"
|
2023-04-12 12:14:24 -03:00
|
|
|
|
2025-01-03 01:15:12 -03:00
|
|
|
ws "github.com/coder/websocket"
|
2022-01-12 10:54:45 -04:00
|
|
|
)
|
|
|
|
|
2025-03-04 11:08:31 -03:00
|
|
|
// Connection represents a websocket connection to a Nostr relay.
|
2022-01-12 10:54:45 -04:00
|
|
|
type Connection struct {
|
2025-01-03 01:15:12 -03:00
|
|
|
conn *ws.Conn
|
2022-01-12 10:54:45 -04:00
|
|
|
}
|
|
|
|
|
2025-03-04 11:08:31 -03:00
|
|
|
// NewConnection creates a new websocket connection to a Nostr relay.
|
2024-03-28 17:23:02 +03:00
|
|
|
func NewConnection(ctx context.Context, url string, requestHeader http.Header, tlsConfig *tls.Config) (*Connection, error) {
|
2025-01-03 01:15:12 -03:00
|
|
|
c, _, err := ws.Dial(ctx, url, getConnectionOptions(requestHeader, tlsConfig))
|
2023-05-04 23:51:15 +02:00
|
|
|
if err != nil {
|
2025-01-03 01:15:12 -03:00
|
|
|
return nil, err
|
2022-01-12 10:54:45 -04:00
|
|
|
}
|
2023-05-04 23:51:15 +02:00
|
|
|
|
2025-02-12 15:41:52 -03:00
|
|
|
c.SetReadLimit(2 << 24) // 33MB
|
2025-01-17 13:44:50 -03:00
|
|
|
|
2023-05-04 23:51:15 +02:00
|
|
|
return &Connection{
|
2025-01-03 01:15:12 -03:00
|
|
|
conn: c,
|
2023-05-04 23:51:15 +02:00
|
|
|
}, nil
|
2022-01-12 10:54:45 -04:00
|
|
|
}
|
|
|
|
|
2025-03-04 11:08:31 -03:00
|
|
|
// WriteMessage writes arbitrary bytes to the websocket connection.
|
2024-06-21 09:32:53 -03:00
|
|
|
func (c *Connection) WriteMessage(ctx context.Context, data []byte) error {
|
2025-01-03 01:15:12 -03:00
|
|
|
if err := c.conn.Write(ctx, ws.MessageText, data); err != nil {
|
|
|
|
return fmt.Errorf("failed to write message: %w", err)
|
2023-05-04 23:51:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2025-03-04 11:08:31 -03:00
|
|
|
// ReadMessage reads arbitrary bytes from the websocket connection into the provided buffer.
|
2023-07-30 17:12:30 -03:00
|
|
|
func (c *Connection) ReadMessage(ctx context.Context, buf io.Writer) error {
|
2025-01-03 01:15:12 -03:00
|
|
|
_, reader, err := c.conn.Reader(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get reader: %w", err)
|
2023-05-04 23:51:15 +02:00
|
|
|
}
|
2025-01-03 01:15:12 -03:00
|
|
|
if _, err := io.Copy(buf, reader); err != nil {
|
|
|
|
return fmt.Errorf("failed to read message: %w", err)
|
2023-05-04 23:51:15 +02:00
|
|
|
}
|
2023-07-30 17:12:30 -03:00
|
|
|
return nil
|
2022-01-12 10:54:45 -04:00
|
|
|
}
|
|
|
|
|
2025-03-04 11:08:31 -03:00
|
|
|
// Close closes the websocket connection.
|
2022-01-12 10:54:45 -04:00
|
|
|
func (c *Connection) Close() error {
|
2025-01-03 01:15:12 -03:00
|
|
|
return c.conn.Close(ws.StatusNormalClosure, "")
|
2022-01-12 10:54:45 -04:00
|
|
|
}
|
2025-01-02 21:42:04 +09:00
|
|
|
|
2025-03-04 11:08:31 -03:00
|
|
|
// Ping sends a ping message to the websocket connection.
|
2025-01-02 21:42:04 +09:00
|
|
|
func (c *Connection) Ping(ctx context.Context) error {
|
2025-01-17 14:08:54 -03:00
|
|
|
ctx, cancel := context.WithTimeoutCause(ctx, time.Millisecond*800, errors.New("ping took too long"))
|
|
|
|
defer cancel()
|
2025-01-03 01:15:12 -03:00
|
|
|
return c.conn.Ping(ctx)
|
2025-01-02 21:42:04 +09:00
|
|
|
}
|