2025-03-08 15:07:27 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log/slog"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
|
2025-03-09 12:33:27 -04:00
|
|
|
"koti.casa/numenor-labs/dsfx/shared/dcrypto"
|
2025-03-08 15:07:27 -05:00
|
|
|
"koti.casa/numenor-labs/dsfx/shared/dlog"
|
|
|
|
"koti.casa/numenor-labs/dsfx/shared/dnet"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Logger
|
|
|
|
|
|
|
|
opts := &slog.HandlerOptions{
|
|
|
|
AddSource: false,
|
|
|
|
Level: slog.LevelDebug,
|
|
|
|
}
|
|
|
|
logger := slog.New(slog.NewTextHandler(os.Stdout, opts))
|
|
|
|
|
|
|
|
// Everything in the application will attempt to use the logger in stored in
|
|
|
|
// the context, but we also set the default with slog as a fallback. In cases
|
|
|
|
// where the context is not available, or the context is not a child of the
|
|
|
|
// context with the logger, the default logger will be used.
|
|
|
|
slog.SetDefault(logger)
|
|
|
|
ctx = dlog.WithContext(ctx, logger)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Commands
|
|
|
|
|
|
|
|
flag.Usage = func() {
|
|
|
|
fmt.Fprintf(os.Stderr, "Usage: %s [command] [args]\n", os.Args[0])
|
|
|
|
fmt.Fprintf(os.Stderr, "Commands:\n")
|
|
|
|
fmt.Fprintf(os.Stderr, " test <remote_addr> Test the connection to the server\n")
|
|
|
|
fmt.Fprintf(os.Stderr, "Flags:\n")
|
|
|
|
flag.PrintDefaults()
|
|
|
|
}
|
|
|
|
|
|
|
|
flagKey := flag.String("key", "", "the path to the private key file")
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if *flagKey == "" {
|
|
|
|
logger.ErrorContext(ctx, "private key path is required")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2025-03-09 12:33:27 -04:00
|
|
|
identity, err := dcrypto.LoadSigningKeyFromFile(*flagKey)
|
2025-03-08 15:07:27 -05:00
|
|
|
if err != nil {
|
|
|
|
logger.ErrorContext(ctx, "failed to load private key", slog.Any("error", err))
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2025-03-09 12:33:27 -04:00
|
|
|
laddr := dnet.NewAddr(
|
|
|
|
net.ParseIP("0.0.0.0"),
|
|
|
|
0, // port 0 means any available port
|
|
|
|
&identity.PublicKey,
|
|
|
|
)
|
|
|
|
|
|
|
|
logger.DebugContext(ctx, "using addr", slog.String("address", laddr.String()))
|
|
|
|
|
2025-03-08 15:07:27 -05:00
|
|
|
switch flag.Arg(0) {
|
|
|
|
case "test":
|
2025-03-09 12:33:27 -04:00
|
|
|
raddrRaw := flag.Arg(1)
|
|
|
|
if raddrRaw == "" {
|
2025-03-08 15:07:27 -05:00
|
|
|
logger.ErrorContext(ctx, "no remote address provided")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2025-03-09 12:33:27 -04:00
|
|
|
testConnection(ctx, identity, laddr, raddrRaw)
|
2025-03-08 15:07:27 -05:00
|
|
|
case "":
|
|
|
|
logger.InfoContext(ctx, "no command provided")
|
|
|
|
os.Exit(1)
|
|
|
|
default:
|
|
|
|
logger.InfoContext(ctx, "unknown command")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-09 12:33:27 -04:00
|
|
|
func testConnection(ctx context.Context, identity *ecdsa.PrivateKey, laddr *dnet.Addr, raddrRaw string) {
|
2025-03-08 15:07:27 -05:00
|
|
|
logger := dlog.FromContext(context.Background())
|
|
|
|
|
2025-03-09 12:33:27 -04:00
|
|
|
raddr, err := dnet.ParseAddr(raddrRaw)
|
2025-03-08 15:07:27 -05:00
|
|
|
if err != nil {
|
|
|
|
logger.ErrorContext(ctx, "failed to parse server address", slog.Any("error", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-03-09 12:33:27 -04:00
|
|
|
conn, err := dnet.Dial(ctx, identity, laddr, raddr)
|
2025-03-08 15:07:27 -05:00
|
|
|
if err != nil {
|
|
|
|
logger.ErrorContext(ctx, "failed to connect", slog.Any("error", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
}
|