Merge pull request #34 from asmogo/exit_log

Add logging for error handling and connection status
This commit is contained in:
asmogo 2024-08-07 09:08:11 +02:00 committed by GitHub
commit 41d7a454fb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 5 deletions

View File

@ -23,7 +23,7 @@ Exit node [domain names](#nws-domain-names) make private services accessible to
There are two types of domain names resolved by NWS entry nodes: There are two types of domain names resolved by NWS entry nodes:
1. `.nostr` domains, which have base32 encoded public key hostnames and base32 encoded relays as subdomains. 1. `.nostr` domains, which have base32 encoded public key hostnames and base32 encoded relays as subdomains.
2. [nprofiles](https://nostr-nips.com/nip-19), which are combinations of a Nostr public key and multiple relays. 2. [nprofiles](https://nostr-nips.com/nip-19#shareable-identifiers-with-extra-metadata), which are combinations of a Nostr public key and multiple relays.
Both types of domains will be generated and printed in the console on startup Both types of domains will be generated and printed in the console on startup
@ -45,7 +45,7 @@ This will start an example environment, including:
- Exit node - Exit node
- Exit node with HTTPS reverse proxy - Exit node with HTTPS reverse proxy
- [Cashu Nutshell](https://github.com/cashubtc/nutshell) (backend service) - [Cashu Nutshell](https://github.com/cashubtc/nutshell) (backend service)
- [nostr-relay](https://github.com/scsibug/nostr-rs-relay) - [nostr-relay](https://github.com/hoytech/strfry)
You can run the following commands to receive your NWS domain: You can run the following commands to receive your NWS domain:

View File

@ -229,10 +229,12 @@ func (e *Exit) ListenAndServe(ctx context.Context) {
func (e *Exit) processMessage(ctx context.Context, msg nostr.IncomingEvent) { func (e *Exit) processMessage(ctx context.Context, msg nostr.IncomingEvent) {
sharedKey, err := nip04.ComputeSharedSecret(msg.PubKey, e.config.NostrPrivateKey) sharedKey, err := nip04.ComputeSharedSecret(msg.PubKey, e.config.NostrPrivateKey)
if err != nil { if err != nil {
slog.Error("could not compute shared key", "error", err)
return return
} }
decodedMessage, err := nip04.Decrypt(msg.Content, sharedKey) decodedMessage, err := nip04.Decrypt(msg.Content, sharedKey)
if err != nil { if err != nil {
slog.Error("could not decrypt message", "error", err)
return return
} }
protocolMessage, err := protocol.UnmarshalJSON([]byte(decodedMessage)) protocolMessage, err := protocol.UnmarshalJSON([]byte(decodedMessage))
@ -242,6 +244,7 @@ func (e *Exit) processMessage(ctx context.Context, msg nostr.IncomingEvent) {
} }
destination, err := protocol.Parse(protocolMessage.Destination) destination, err := protocol.Parse(protocolMessage.Destination)
if err != nil { if err != nil {
slog.Error("could not parse destination", "error", err)
return return
} }
if destination.TLD == "nostr" { if destination.TLD == "nostr" {
@ -289,17 +292,17 @@ func (e *Exit) handleConnect(
} }
e.nostrConnectionMap.Store(protocolMessage.Key.String(), connection) e.nostrConnectionMap.Store(protocolMessage.Key.String(), connection)
slog.Info("connected to backend", "key", protocolMessage.Key)
go socks5.Proxy(dst, connection, nil) go socks5.Proxy(dst, connection, nil)
go socks5.Proxy(connection, dst, nil) go socks5.Proxy(connection, dst, nil)
} }
func (e *Exit) handleConnectReverse(protocolMessage *protocol.Message) { func (e *Exit) handleConnectReverse(protocolMessage *protocol.Message) {
e.mutexMap.Lock(protocolMessage.Key.String()) e.mutexMap.Lock(protocolMessage.Key.String())
defer e.mutexMap.Unlock(protocolMessage.Key.String()) defer e.mutexMap.Unlock(protocolMessage.Key.String())
connection, err := net.Dial("tcp", protocolMessage.EntryPublicAddress) connection, err := net.Dial("tcp", protocolMessage.EntryPublicAddress)
if err != nil { if err != nil {
slog.Error("could not connect to entry", "error", err)
return return
} }
@ -311,6 +314,7 @@ func (e *Exit) handleConnectReverse(protocolMessage *protocol.Message) {
readbuffer := make([]byte, 1) readbuffer := make([]byte, 1)
_, err = connection.Read(readbuffer) _, err = connection.Read(readbuffer)
if err != nil { if err != nil {
slog.Error("could not read from connection", "error", err)
return return
} }
if readbuffer[0] != 1 { if readbuffer[0] != 1 {
@ -322,7 +326,7 @@ func (e *Exit) handleConnectReverse(protocolMessage *protocol.Message) {
slog.Error("could not connect to backend", "error", err) slog.Error("could not connect to backend", "error", err)
return return
} }
slog.Info("connected to entry", "key", protocolMessage.Key)
go socks5.Proxy(dst, connection, nil) go socks5.Proxy(dst, connection, nil)
go socks5.Proxy(connection, dst, nil) go socks5.Proxy(connection, dst, nil)
} }
@ -344,4 +348,5 @@ func (e *Exit) handleSocks5ProxyMessage(
return return
} }
dst.WriteNostrEvent(msg) dst.WriteNostrEvent(msg)
slog.Info("wrote event to backend", "key", protocolMessage.Key)
} }