publish exit node events

This commit is contained in:
dd dd 2024-08-02 00:37:01 +02:00
parent d2be9c000e
commit 2ccb01f1f3
5 changed files with 79 additions and 46 deletions

View File

@ -61,6 +61,12 @@ func NewExit(ctx context.Context, exitNodeConfig *config.ExitConfig) *Exit {
// generate new private key // generate new private key
exitNodeConfig.NostrPrivateKey = nostr.GeneratePrivateKey() exitNodeConfig.NostrPrivateKey = nostr.GeneratePrivateKey()
slog.Warn(generateKeyMessage, "key", exitNodeConfig.NostrPrivateKey) slog.Warn(generateKeyMessage, "key", exitNodeConfig.NostrPrivateKey)
} else {
pubKey, err := nostr.GetPublicKey(exitNodeConfig.NostrPrivateKey)
if err != nil {
panic(err)
}
slog.Info("using public key", "key", pubKey)
} }
// get public key from private key // get public key from private key
pubKey, err := nostr.GetPublicKey(exitNodeConfig.NostrPrivateKey) pubKey, err := nostr.GetPublicKey(exitNodeConfig.NostrPrivateKey)

View File

@ -3,6 +3,8 @@ package exit
import ( import (
"context" "context"
"log/slog" "log/slog"
"strconv"
"time"
"github.com/nbd-wtf/go-nostr" "github.com/nbd-wtf/go-nostr"
) )
@ -11,26 +13,37 @@ func (e *Exit) announceExitNode(ctx context.Context) error {
if !e.config.Public { if !e.config.Public {
return nil return nil
} }
// create a event
event := nostr.Event{ go func() {
PubKey: e.publicKey, for {
CreatedAt: nostr.Now(), time.Sleep(time.Second * 10)
Kind: nostr.KindTextNote, // create update event
Content: "", // create a event
Tags: nostr.Tags{nostr.Tag{"n", "nws"}},
} event := nostr.Event{
err := event.Sign(e.config.NostrPrivateKey) PubKey: e.publicKey,
if err != nil { CreatedAt: nostr.Now(),
return err Kind: nostr.KindTextNote,
} Content: "",
// publish the event Tags: nostr.Tags{nostr.Tag{"n", "nws"}, nostr.Tag{"expiration", strconv.FormatInt(time.Now().Add(time.Second*10).Unix(), 20)}},
for _, relay := range e.relays { }
err = relay.Publish(ctx, event)
if err != nil { err := event.Sign(e.config.NostrPrivateKey)
slog.Error("could not publish event", "error", err) if err != nil {
// do not return here, try to publish the event to other relays slog.Error("could not sign event", "error", err)
continue
}
// publish the event
for _, relay := range e.relays {
err = relay.Publish(ctx, event)
if err != nil {
slog.Error("could not publish event", "error", err)
// do not return here, try to publish the event to other relays
}
}
} }
} }()
return nil return nil
} }

View File

@ -6,12 +6,20 @@ import (
"github.com/nbd-wtf/go-nostr" "github.com/nbd-wtf/go-nostr"
"net" "net"
"strings" "strings"
"time"
) )
// NostrDNS does not resolve anything // NostrDNS does not resolve anything
type NostrDNS struct { type NostrDNS struct {
Pool *nostr.SimplePool pool *nostr.SimplePool
NostrRelays []string nostrRelays []string
}
func NewNostrDNS(pool *nostr.SimplePool, nostrRelays []string) *NostrDNS {
return &NostrDNS{
pool: pool,
nostrRelays: nostrRelays,
}
} }
func (d NostrDNS) Resolve(ctx context.Context, name string) (context.Context, net.IP, error) { func (d NostrDNS) Resolve(ctx context.Context, name string) (context.Context, net.IP, error) {
@ -23,12 +31,21 @@ func (d NostrDNS) Resolve(ctx context.Context, name string) (context.Context, ne
if err != nil { if err != nil {
return ctx, nil, err return ctx, nil, err
} }
ev := d.Pool.QuerySingle(ctx, d.NostrRelays, nostr.Filter{ if d.pool == nil {
Tags: nostr.TagMap{"n": []string{"nws"}}, return ctx, nil, fmt.Errorf("pool is nil")
}
since := nostr.Timestamp(time.Now().Add(-time.Second * 10).Unix())
ev := d.pool.QuerySingle(ctx, d.nostrRelays, nostr.Filter{
Kinds: []int{nostr.KindTextNote},
Since: &since,
Tags: nostr.TagMap{"n": []string{"nws"}},
}) })
if ev == nil { if ev == nil {
return ctx, nil, fmt.Errorf("failed to find exit node event") return ctx, nil, fmt.Errorf("failed to find exit node event")
} }
if ev.CreatedAt < since {
return ctx, nil, fmt.Errorf("exit node event is expired")
}
ctx = context.WithValue(ctx, "publicKey", ev.PubKey) ctx = context.WithValue(ctx, "publicKey", ev.PubKey)
return ctx, addr.IP, err return ctx, addr.IP, err
} }

View File

@ -26,15 +26,12 @@ func New(ctx context.Context, config *config.EntryConfig) *Proxy {
socksServer, err := socks5.New(&socks5.Config{ socksServer, err := socks5.New(&socks5.Config{
AuthMethods: nil, AuthMethods: nil,
Credentials: nil, Credentials: nil,
Resolver: netstr.NostrDNS{ Resolver: netstr.NewNostrDNS(s.pool, config.NostrRelays),
Pool: s.pool, Rules: nil,
NostrRelays: config.NostrRelays, Rewriter: nil,
}, BindIP: net.IP{0, 0, 0, 0},
Rules: nil, Logger: nil,
Rewriter: nil, Dial: nil,
BindIP: net.IP{0, 0, 0, 0},
Logger: nil,
Dial: nil,
}, s.pool, config) }, s.pool, config)
if err != nil { if err != nil {
panic(err) panic(err)

View File

@ -34,7 +34,7 @@ const (
) )
var ( var (
unrecognizedAddrType = fmt.Errorf("Unrecognized address type") unrecognizedAddrType = fmt.Errorf("unrecognized address type")
) )
// AddressRewriter is used to rewrite a destination transparently // AddressRewriter is used to rewrite a destination transparently
@ -96,7 +96,7 @@ func NewRequest(bufConn io.Reader) (*Request, error) {
// Read the version byte // Read the version byte
header := []byte{0, 0, 0} header := []byte{0, 0, 0}
if _, err := io.ReadAtLeast(bufConn, header, 3); err != nil { if _, err := io.ReadAtLeast(bufConn, header, 3); err != nil {
return nil, fmt.Errorf("Failed to get command version: %v", err) return nil, fmt.Errorf("failed to get command version: %w", err)
} }
// Ensure we are compatible // Ensure we are compatible
@ -129,9 +129,9 @@ func (s *Server) handleRequest(req *Request, conn net.Conn) error {
ctx_, addr, err := s.config.Resolver.Resolve(ctx, dest.FQDN) ctx_, addr, err := s.config.Resolver.Resolve(ctx, dest.FQDN)
if err != nil { if err != nil {
if err := SendReply(conn, hostUnreachable, nil); err != nil { if err := SendReply(conn, hostUnreachable, nil); err != nil {
return fmt.Errorf("Failed to send reply: %v", err) return fmt.Errorf("failed to send reply: %w", err)
} }
return fmt.Errorf("Failed to resolve destination '%v': %v", dest.FQDN, err) return fmt.Errorf("failed to resolve destination '%v': %w", dest.FQDN, err)
} }
ctx = ctx_ ctx = ctx_
dest.IP = addr dest.IP = addr
@ -162,7 +162,7 @@ func (s *Server) handleRequest(req *Request, conn net.Conn) error {
return s.handleAssociate(ctx, conn, req) return s.handleAssociate(ctx, conn, req)
default: default:
if err := SendReply(conn, commandNotSupported, nil); err != nil { if err := SendReply(conn, commandNotSupported, nil); err != nil {
return fmt.Errorf("failed to send reply: %v", err) return fmt.Errorf("failed to send reply: %w", err)
} }
return fmt.Errorf("unsupported command: %d", req.Command) return fmt.Errorf("unsupported command: %d", req.Command)
} }
@ -173,9 +173,9 @@ func (s *Server) handleConnect(ctx context.Context, conn net.Conn, req *Request,
// Check if this is allowed // Check if this is allowed
if ctx_, ok := s.config.Rules.Allow(ctx, req); !ok { if ctx_, ok := s.config.Rules.Allow(ctx, req); !ok {
if err := SendReply(conn, ruleFailure, nil); err != nil { if err := SendReply(conn, ruleFailure, nil); err != nil {
return fmt.Errorf("Failed to send reply: %v", err) return fmt.Errorf("failed to send reply: %w", err)
} }
return fmt.Errorf("Connect to %v blocked by rules", req.DestAddr) return fmt.Errorf("connect to %v blocked by rules", req.DestAddr)
} else { } else {
ctx = ctx_ ctx = ctx_
} }
@ -214,7 +214,7 @@ func (s *Server) handleConnect(ctx context.Context, conn net.Conn, req *Request,
local := target.LocalAddr().(*net.TCPAddr) local := target.LocalAddr().(*net.TCPAddr)
bind := AddrSpec{IP: local.IP, Port: local.Port} bind := AddrSpec{IP: local.IP, Port: local.Port}
if err := SendReply(conn, successReply, &bind); err != nil { if err := SendReply(conn, successReply, &bind); err != nil {
return fmt.Errorf("failed to send reply: %v", err) return fmt.Errorf("failed to send reply: %w", err)
} }
// read // read
if options.MessageType == protocol.MessageConnectReverse { if options.MessageType == protocol.MessageConnectReverse {
@ -244,7 +244,7 @@ func (s *Server) handleBind(ctx context.Context, conn net.Conn, req *Request) er
// Check if this is allowed // Check if this is allowed
if ctx_, ok := s.config.Rules.Allow(ctx, req); !ok { if ctx_, ok := s.config.Rules.Allow(ctx, req); !ok {
if err := SendReply(conn, ruleFailure, nil); err != nil { if err := SendReply(conn, ruleFailure, nil); err != nil {
return fmt.Errorf("Failed to send reply: %v", err) return fmt.Errorf("failed to send reply: %w", err)
} }
return fmt.Errorf("Bind to %v blocked by rules", req.DestAddr) return fmt.Errorf("Bind to %v blocked by rules", req.DestAddr)
} else { } else {
@ -253,7 +253,7 @@ func (s *Server) handleBind(ctx context.Context, conn net.Conn, req *Request) er
// TODO: Support bind // TODO: Support bind
if err := SendReply(conn, commandNotSupported, nil); err != nil { if err := SendReply(conn, commandNotSupported, nil); err != nil {
return fmt.Errorf("Failed to send reply: %v", err) return fmt.Errorf("failed to send reply: %w", err)
} }
return nil return nil
} }
@ -263,16 +263,16 @@ func (s *Server) handleAssociate(ctx context.Context, conn net.Conn, req *Reques
// Check if this is allowed // Check if this is allowed
if ctx_, ok := s.config.Rules.Allow(ctx, req); !ok { if ctx_, ok := s.config.Rules.Allow(ctx, req); !ok {
if err := SendReply(conn, ruleFailure, nil); err != nil { if err := SendReply(conn, ruleFailure, nil); err != nil {
return fmt.Errorf("Failed to send reply: %v", err) return fmt.Errorf("failed to send reply: %w", err)
} }
return fmt.Errorf("Associate to %v blocked by rules", req.DestAddr) return fmt.Errorf("associate to %v blocked by rules", req.DestAddr)
} else { } else {
ctx = ctx_ ctx = ctx_
} }
// TODO: Support associate // TODO: Support associate
if err := SendReply(conn, commandNotSupported, nil); err != nil { if err := SendReply(conn, commandNotSupported, nil); err != nil {
return fmt.Errorf("Failed to send reply: %v", err) return fmt.Errorf("failed to send reply: %w", err)
} }
return nil return nil
} }
@ -357,7 +357,7 @@ func SendReply(w io.Writer, resp uint8, addr *AddrSpec) error {
addrPort = uint16(addr.Port) addrPort = uint16(addr.Port)
default: default:
return fmt.Errorf("Failed to format address: %v", addr) return fmt.Errorf("failed to format address: %v", addr)
} }
// Format the message // Format the message