package main import ( "context" "crypto/ecdsa" "flag" "fmt" "log/slog" "net" "os" "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 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) } masterKey, err := LoadMasterKey(*flagKey) if err != nil { logger.ErrorContext(ctx, "failed to load private key", slog.Any("error", err)) os.Exit(1) } switch flag.Arg(0) { case "test": raddr := flag.Arg(1) if raddr == "" { logger.ErrorContext(ctx, "no remote address provided") os.Exit(1) } testConnection(ctx, raddr, masterKey) case "": logger.InfoContext(ctx, "no command provided") os.Exit(1) default: logger.InfoContext(ctx, "unknown command") os.Exit(1) } } func testConnection(ctx context.Context, raddr string, clientPrivateKey *ecdsa.PrivateKey) { logger := dlog.FromContext(context.Background()) serverAddr, err := dnet.ParseAddr(raddr) if err != nil { logger.ErrorContext(ctx, "failed to parse server address", slog.Any("error", err)) return } tcpAddr := &net.TCPAddr{ IP: serverAddr.IP(), Port: serverAddr.Port(), } conn, err := dnet.Dial(ctx, nil, tcpAddr, clientPrivateKey, serverAddr.PublicKey()) if err != nil { logger.ErrorContext(ctx, "failed to connect", slog.Any("error", err)) return } defer conn.Close() }