This commit is contained in:
dd dd 2024-07-28 19:56:42 +02:00
parent eade2322ac
commit d31b3144d4
2 changed files with 30 additions and 27 deletions

View File

@ -6,15 +6,15 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var httpsPort int32
var httpTarget string
const ( const (
usagePort = "set the https reverse proxy port" usagePort = "set the https reverse proxy port"
usageTarget = "set https reverse proxy target (your local service)" usageTarget = "set https reverse proxy target (your local service)"
) )
func main() { func main() {
var httpsPort int32
var httpTarget string
rootCmd := &cobra.Command{Use: "exit", Run: startExitNode} rootCmd := &cobra.Command{Use: "exit", Run: startExitNode}
rootCmd.Flags().Int32VarP(&httpsPort, "port", "p", 0, usagePort) rootCmd.Flags().Int32VarP(&httpsPort, "port", "p", 0, usagePort)
rootCmd.Flags().StringVarP(&httpTarget, "target", "t", "", usageTarget) rootCmd.Flags().StringVarP(&httpTarget, "target", "t", "", usageTarget)
@ -25,9 +25,19 @@ func main() {
} }
// updateConfigFlag updates the configuration with the provided flags. // updateConfigFlag updates the configuration with the provided flags.
func updateConfigFlag(cfg *config.ExitConfig) { func updateConfigFlag(cmd *cobra.Command, cfg *config.ExitConfig) error {
httpsPort, err := cmd.Flags().GetInt32("port")
if err != nil {
return err
}
httpTarget, err := cmd.Flags().GetString("target")
if err != nil {
return err
}
cfg.HttpsPort = httpsPort cfg.HttpsPort = httpsPort
cfg.HttpsTarget = httpTarget cfg.HttpsTarget = httpTarget
return nil
} }
func startExitNode(cmd *cobra.Command, args []string) { func startExitNode(cmd *cobra.Command, args []string) {
@ -37,7 +47,7 @@ func startExitNode(cmd *cobra.Command, args []string) {
if err != nil { if err != nil {
panic(err) panic(err)
} }
updateConfigFlag(cfg) updateConfigFlag(cmd, cfg)
ctx := cmd.Context() ctx := cmd.Context()
exitNode := exit.NewExit(ctx, cfg) exitNode := exit.NewExit(ctx, cfg)
exitNode.ListenAndServe(ctx) exitNode.ListenAndServe(ctx)

View File

@ -1,10 +1,13 @@
package exit package exit
import ( import (
"crypto/tls"
"encoding/base32" "encoding/base32"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"log/slog"
"net"
"strings"
"github.com/asmogo/nws/config" "github.com/asmogo/nws/config"
"github.com/asmogo/nws/netstr" "github.com/asmogo/nws/netstr"
"github.com/asmogo/nws/protocol" "github.com/asmogo/nws/protocol"
@ -16,9 +19,6 @@ import (
"github.com/nbd-wtf/go-nostr/nip19" "github.com/nbd-wtf/go-nostr/nip19"
"github.com/puzpuzpuz/xsync/v3" "github.com/puzpuzpuz/xsync/v3"
"golang.org/x/net/context" "golang.org/x/net/context"
"log/slog"
"net"
"strings"
) )
const ( const (
@ -194,7 +194,8 @@ func (e *Exit) handleSubscription(ctx context.Context, pubKey string, since nost
Since: &since, Since: &since,
Tags: nostr.TagMap{ Tags: nostr.TagMap{
"p": []string{pubKey}, "p": []string{pubKey},
}}, },
},
}) })
e.incomingChannel = incomingEventChannel e.incomingChannel = incomingEventChannel
return nil return nil
@ -234,12 +235,11 @@ func (e *Exit) processMessage(ctx context.Context, msg nostr.IncomingEvent) {
slog.Error("could not unmarshal message") slog.Error("could not unmarshal message")
return return
} }
// todo -- remove isTLS from both connection handlers (use generic handler)
switch protocolMessage.Type { switch protocolMessage.Type {
case protocol.MessageConnect: case protocol.MessageConnect:
e.handleConnect(ctx, msg, protocolMessage, false) e.handleConnect(ctx, msg, protocolMessage)
case protocol.MessageConnectReverse: case protocol.MessageConnectReverse:
e.handleConnectReverse(ctx, protocolMessage, false) e.handleConnectReverse(protocolMessage)
case protocol.MessageTypeSocks5: case protocol.MessageTypeSocks5:
e.handleSocks5ProxyMessage(msg, protocolMessage) e.handleSocks5ProxyMessage(msg, protocolMessage)
} }
@ -252,7 +252,10 @@ func (e *Exit) processMessage(ctx context.Context, msg nostr.IncomingEvent) {
// If the connection cannot be established, it logs an error and returns. // If the connection cannot be established, it logs an error and returns.
// It then stores the connection in the nostrConnectionMap and creates two goroutines // It then stores the connection in the nostrConnectionMap and creates two goroutines
// to proxy the data between the connection and the backend. // to proxy the data between the connection and the backend.
func (e *Exit) handleConnect(ctx context.Context, msg nostr.IncomingEvent, protocolMessage *protocol.Message, isTLS bool) { func (e *Exit) handleConnect(
ctx context.Context,
msg nostr.IncomingEvent,
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())
receiver, err := nip19.EncodeProfile(msg.PubKey, []string{msg.Relay.String()}) receiver, err := nip19.EncodeProfile(msg.PubKey, []string{msg.Relay.String()})
@ -266,12 +269,7 @@ func (e *Exit) handleConnect(ctx context.Context, msg nostr.IncomingEvent, proto
netstr.WithUUID(protocolMessage.Key), netstr.WithUUID(protocolMessage.Key),
) )
var dst net.Conn var dst net.Conn
if isTLS { dst, err = net.Dial("tcp", e.config.BackendHost)
conf := tls.Config{InsecureSkipVerify: true}
dst, err = tls.Dial("tcp", e.config.BackendHost, &conf)
} else {
dst, err = net.Dial("tcp", e.config.BackendHost)
}
if err != nil { if err != nil {
slog.Error("could not connect to backend", "error", err) slog.Error("could not connect to backend", "error", err)
return return
@ -283,7 +281,7 @@ func (e *Exit) handleConnect(ctx context.Context, msg nostr.IncomingEvent, proto
go socks5.Proxy(connection, dst, nil) go socks5.Proxy(connection, dst, nil)
} }
func (e *Exit) handleConnectReverse(ctx context.Context, protocolMessage *protocol.Message, isTLS bool) { 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.Destination) connection, err := net.Dial("tcp", protocolMessage.Destination)
@ -291,12 +289,7 @@ func (e *Exit) handleConnectReverse(ctx context.Context, protocolMessage *protoc
return return
} }
var dst net.Conn var dst net.Conn
if isTLS { dst, err = net.Dial("tcp", e.config.BackendHost)
conf := tls.Config{InsecureSkipVerify: true}
dst, err = tls.Dial("tcp", e.config.BackendHost, &conf)
} else {
dst, err = net.Dial("tcp", e.config.BackendHost)
}
if err != nil { if err != nil {
slog.Error("could not connect to backend", "error", err) slog.Error("could not connect to backend", "error", err)
return return