dsfx/cmd/dsfxctl/main.go

143 lines
3.8 KiB
Go
Raw Normal View History

2025-03-08 15:07:27 -05:00
package main
import (
"context"
2025-03-10 10:29:54 -04:00
"crypto/ed25519"
2025-03-10 19:53:10 -04:00
"encoding/base64"
"errors"
2025-03-08 15:07:27 -05:00
"flag"
"fmt"
"log/slog"
"net"
"os"
2025-03-10 19:53:10 -04:00
"path/filepath"
2025-03-08 15:07:27 -05:00
2025-03-09 15:52:33 -04:00
"koti.casa/numenor-labs/dsfx/pkg/crypto/identity"
"koti.casa/numenor-labs/dsfx/pkg/logging"
"koti.casa/numenor-labs/dsfx/pkg/network"
2025-03-08 15:07:27 -05:00
)
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)
2025-03-09 15:52:33 -04:00
ctx = logging.WithContext(ctx, logger)
2025-03-08 15:07:27 -05:00
2025-03-10 19:53:10 -04:00
homedir, err := os.UserHomeDir()
if err != nil {
logger.ErrorContext(ctx, "failed to get home directory", slog.Any("error", err))
os.Exit(1)
}
flagDir := flag.String("dir", filepath.Join(homedir, ".dsfxctl"), "data directory")
err = os.Mkdir(*flagDir, 0777)
if errors.Is(err, os.ErrExist) {
// If the directory already exists, we can ignore the error.
err = nil
}
if err != nil {
logger.ErrorContext(ctx, "failed to create directory", slog.Any("error", err))
os.Exit(1)
}
keyFile, err := os.Open(filepath.Join(*flagDir, "key"))
if err != nil {
logger.WarnContext(ctx, "key file is missing, reinitializing")
logger.WarnContext(ctx, "if this is your first time running dsfxctl, you can ignore this")
}
if keyFile == nil {
logger.InfoContext(ctx, "generating new key")
keyFile, err = os.Create(filepath.Join(*flagDir, "key"))
if err != nil {
logger.ErrorContext(ctx, "failed to create key file", slog.Any("error", err))
os.Exit(1)
}
privkey, err := identity.Generate()
if err != nil {
logger.ErrorContext(ctx, "failed to generate key", slog.Any("error", err))
os.Exit(1)
}
_, err = keyFile.Write([]byte(base64.StdEncoding.EncodeToString(privkey)))
if err != nil {
logger.ErrorContext(ctx, "failed to write key", slog.Any("error", err))
os.Exit(1)
}
}
defer keyFile.Close()
2025-03-08 15:07:27 -05:00
// ---------------------------------------------------------------------------
// Commands
2025-03-10 19:53:10 -04:00
flag.Parse()
2025-03-08 15:07:27 -05:00
flag.Usage = func() {
2025-03-09 15:52:33 -04:00
fmt.Fprintf(os.Stderr, "Usage: dsfxctl [command] [args]\n")
2025-03-08 15:07:27 -05:00
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()
}
2025-03-10 19:53:10 -04:00
id, err := identity.LoadSigningKeyFromFile(filepath.Join(*flagDir, "key"))
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 15:52:33 -04:00
laddr := network.NewAddr(
2025-03-09 12:33:27 -04:00
net.ParseIP("0.0.0.0"),
0, // port 0 means any available port
2025-03-10 10:29:54 -04:00
identity.ToPublicKey(id),
2025-03-09 12:33:27 -04:00
)
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-10 10:29:54 -04:00
testConnection(ctx, id, 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-10 10:29:54 -04:00
func testConnection(ctx context.Context, id ed25519.PrivateKey, laddr *network.Addr, raddrRaw string) {
2025-03-09 15:52:33 -04:00
logger := logging.FromContext(context.Background())
2025-03-08 15:07:27 -05:00
2025-03-09 15:52:33 -04:00
raddr, err := network.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-10 10:29:54 -04:00
conn, err := network.Dial(ctx, id, 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()
}