2024-07-22 21:00:21 +00:00
|
|
|
package netstr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-08-01 12:35:28 +00:00
|
|
|
"fmt"
|
|
|
|
"github.com/nbd-wtf/go-nostr"
|
2024-07-22 21:00:21 +00:00
|
|
|
"net"
|
2024-08-01 12:35:28 +00:00
|
|
|
"strings"
|
2024-07-22 21:00:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NostrDNS does not resolve anything
|
2024-08-01 12:35:28 +00:00
|
|
|
type NostrDNS struct {
|
|
|
|
Pool *nostr.SimplePool
|
|
|
|
NostrRelays []string
|
|
|
|
}
|
2024-07-22 21:00:21 +00:00
|
|
|
|
|
|
|
func (d NostrDNS) Resolve(ctx context.Context, name string) (context.Context, net.IP, error) {
|
2024-08-01 12:35:28 +00:00
|
|
|
if strings.HasSuffix(name, ".nostr") || strings.HasPrefix(name, "npub") || strings.HasPrefix(name, "nprofile") {
|
|
|
|
|
|
|
|
return ctx, nil, nil
|
|
|
|
}
|
|
|
|
addr, err := net.ResolveIPAddr("ip", name)
|
|
|
|
if err != nil {
|
|
|
|
return ctx, nil, err
|
|
|
|
}
|
|
|
|
ev := d.Pool.QuerySingle(ctx, d.NostrRelays, nostr.Filter{
|
|
|
|
Tags: nostr.TagMap{"n": []string{"nws"}},
|
|
|
|
})
|
|
|
|
if ev == nil {
|
|
|
|
return ctx, nil, fmt.Errorf("failed to find exit node event")
|
|
|
|
}
|
|
|
|
ctx = context.WithValue(ctx, "publicKey", ev.PubKey)
|
|
|
|
return ctx, addr.IP, err
|
2024-07-22 21:00:21 +00:00
|
|
|
}
|