Skip to content

Commit

Permalink
Merge pull request #34 from asmogo/exit_log
Browse files Browse the repository at this point in the history
Add logging for error handling and connection status
  • Loading branch information
asmogo authored Aug 7, 2024
2 parents 25a5486 + 05d6a6e commit 41d7a45
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
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

Expand All @@ -45,7 +45,7 @@ This will start an example environment, including:
- Exit node
- Exit node with HTTPS reverse proxy
- [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:

Expand Down
11 changes: 8 additions & 3 deletions exit/exit.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,12 @@ func (e *Exit) ListenAndServe(ctx context.Context) {
func (e *Exit) processMessage(ctx context.Context, msg nostr.IncomingEvent) {
sharedKey, err := nip04.ComputeSharedSecret(msg.PubKey, e.config.NostrPrivateKey)
if err != nil {
slog.Error("could not compute shared key", "error", err)
return
}
decodedMessage, err := nip04.Decrypt(msg.Content, sharedKey)
if err != nil {
slog.Error("could not decrypt message", "error", err)
return
}
protocolMessage, err := protocol.UnmarshalJSON([]byte(decodedMessage))
Expand All @@ -242,6 +244,7 @@ func (e *Exit) processMessage(ctx context.Context, msg nostr.IncomingEvent) {
}
destination, err := protocol.Parse(protocolMessage.Destination)
if err != nil {
slog.Error("could not parse destination", "error", err)
return
}
if destination.TLD == "nostr" {
Expand Down Expand Up @@ -289,17 +292,17 @@ func (e *Exit) handleConnect(
}

e.nostrConnectionMap.Store(protocolMessage.Key.String(), connection)

slog.Info("connected to backend", "key", protocolMessage.Key)
go socks5.Proxy(dst, connection, nil)
go socks5.Proxy(connection, dst, nil)
}

func (e *Exit) handleConnectReverse(protocolMessage *protocol.Message) {

e.mutexMap.Lock(protocolMessage.Key.String())
defer e.mutexMap.Unlock(protocolMessage.Key.String())
connection, err := net.Dial("tcp", protocolMessage.EntryPublicAddress)
if err != nil {
slog.Error("could not connect to entry", "error", err)
return
}

Expand All @@ -311,6 +314,7 @@ func (e *Exit) handleConnectReverse(protocolMessage *protocol.Message) {
readbuffer := make([]byte, 1)
_, err = connection.Read(readbuffer)
if err != nil {
slog.Error("could not read from connection", "error", err)
return
}
if readbuffer[0] != 1 {
Expand All @@ -322,7 +326,7 @@ func (e *Exit) handleConnectReverse(protocolMessage *protocol.Message) {
slog.Error("could not connect to backend", "error", err)
return
}

slog.Info("connected to entry", "key", protocolMessage.Key)
go socks5.Proxy(dst, connection, nil)
go socks5.Proxy(connection, dst, nil)
}
Expand All @@ -344,4 +348,5 @@ func (e *Exit) handleSocks5ProxyMessage(
return
}
dst.WriteNostrEvent(msg)
slog.Info("wrote event to backend", "key", protocolMessage.Key)
}

0 comments on commit 41d7a45

Please sign in to comment.