fix error handling in dial function (#44)

* fix error handling in dial function

* fix error handling in dial function

* minor linting fixes
This commit is contained in:
asmogo 2024-11-11 15:55:53 +01:00 committed by GitHub
parent 844bf987a8
commit 1da2eaea9f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,12 +3,13 @@ package netstr
import (
"context"
"fmt"
"log/slog"
"net"
"github.com/asmogo/nws/config"
"github.com/asmogo/nws/protocol"
"github.com/google/uuid"
"github.com/nbd-wtf/go-nostr"
"log/slog"
"net"
)
type DialOptions struct {
@ -25,8 +26,11 @@ type DialOptions struct {
// It creates a signed event using the private key, public key, and destination address.
// It ensures that the relays are available in the pool and publishes the signed event to each relay.
// Finally, it returns the Connection and nil error. If there are any errors, nil connection and the error are returned.
func DialSocks(options DialOptions, config *config.EntryConfig) func(ctx context.Context, net_, addr string) (net.Conn, error) {
return func(ctx context.Context, net_, addr string) (net.Conn, error) {
func DialSocks(
options DialOptions,
config *config.EntryConfig,
) func(ctx context.Context, _, addr string) (net.Conn, error) {
return func(ctx context.Context, _, addr string) (net.Conn, error) {
key := nostr.GeneratePrivateKey()
connection := NewConnection(ctx,
WithPrivateKey(key),
@ -51,7 +55,7 @@ func DialSocks(options DialOptions, config *config.EntryConfig) func(ctx context
// create nostr signed event
signer, err := protocol.NewEventSigner(key)
if err != nil {
return nil, err
return nil, fmt.Errorf("error creating signer: %w", err)
}
opts := []protocol.MessageOption{
protocol.WithType(options.MessageType),
@ -62,21 +66,43 @@ func DialSocks(options DialOptions, config *config.EntryConfig) func(ctx context
}
opts = append(opts, protocol.WithDestination(addr))
ev, err := signer.CreateSignedEvent(publicKey, protocol.KindEphemeralEvent,
nostr.Tags{nostr.Tag{"p", publicKey}},
opts...)
for _, relayUrl := range relays {
relay, err := options.Pool.EnsureRelay(relayUrl)
if err != nil {
slog.Error("error creating relay", err)
continue
}
err = relay.Publish(ctx, ev)
if err != nil {
return nil, err
}
err = createAndPublish(ctx, signer, publicKey, opts, relays, options)
if err != nil {
return nil, fmt.Errorf("error publishing event: %w", err)
}
return connection, nil
}
}
// createAndPublish creates a signed event using the provided signer, public key, message options, and relays.
// It then publishes the event to each relay. Returns an error if signing or publishing fails.
func createAndPublish(
ctx context.Context,
signer *protocol.EventSigner,
publicKey string,
opts []protocol.MessageOption,
relays []string,
options DialOptions,
) error {
signedEvent, err := signer.CreateSignedEvent(
publicKey,
protocol.KindEphemeralEvent,
nostr.Tags{nostr.Tag{"p", publicKey}},
opts...)
if err != nil {
return fmt.Errorf("error creating signed event: %w", err)
}
for _, relayURL := range relays {
var relay *nostr.Relay
relay, err = options.Pool.EnsureRelay(relayURL)
if err != nil {
slog.Error("error creating relay", "error", err)
continue
}
err = relay.Publish(ctx, signedEvent)
if err != nil {
return fmt.Errorf("error publishing event: %w", err)
}
}
return nil
}