2025-03-08 15:07:27 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
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"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
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
|
|
|
)
|
|
|
|
|
2025-03-10 19:53:10 -04:00
|
|
|
var ()
|
2025-03-08 15:07:27 -05:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Logger
|
2025-03-10 19:53:10 -04:00
|
|
|
homedir, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
logUserMsg("error: failed to get home directory\n")
|
|
|
|
logUserMsg("error: are you on a real computer?\n")
|
|
|
|
// Look up why this function might fail...
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
flagHost := flag.String("host", "localhost", "the host to listen on")
|
|
|
|
flagPort := flag.Int("port", 8000, "the port to listen on")
|
|
|
|
flagDir := flag.String("dir", filepath.Join(homedir, ".dsfxnode"), "the directory to store data in")
|
|
|
|
flagLogFile := flag.String("log", "logs", "the file to write logs to, relative to -dir")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
dataDir := *flagDir
|
|
|
|
|
|
|
|
err = os.Mkdir(dataDir, 0777)
|
|
|
|
if errors.Is(err, os.ErrExist) {
|
|
|
|
// If the directory already exists, we can ignore the error.
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
logUserMsg("error: failed to create the data directory: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
var logFile *os.File
|
|
|
|
if *flagLogFile == "stdout" {
|
|
|
|
logFile = os.Stdout
|
|
|
|
} else {
|
|
|
|
logFilePath := filepath.Join(dataDir, *flagLogFile)
|
|
|
|
logFile, err = os.OpenFile(logFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
|
|
|
|
if err != nil {
|
|
|
|
logUserMsg("warn: log file missing, making a new one: %s\n", logFilePath)
|
|
|
|
logFile, err = os.Create(logFilePath)
|
|
|
|
if err != nil {
|
|
|
|
logUserMsg("error: failed to create log file: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer logFile.Close()
|
2025-03-08 15:07:27 -05:00
|
|
|
|
|
|
|
opts := &slog.HandlerOptions{
|
|
|
|
AddSource: false,
|
|
|
|
Level: slog.LevelDebug,
|
|
|
|
}
|
2025-03-10 19:53:10 -04:00
|
|
|
logger := slog.New(slog.NewTextHandler(logFile, opts))
|
2025-03-08 15:07:27 -05:00
|
|
|
|
|
|
|
// 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
|
|
|
keyFile, err := os.Open(filepath.Join(dataDir, "key"))
|
|
|
|
if err != nil {
|
|
|
|
logUserMsg("warn: key file is missing, making a new one\n")
|
|
|
|
logUserMsg("warn: if this is your first time running dsfxctl, you can ignore this\n")
|
|
|
|
|
|
|
|
keyFile, err = os.Create(filepath.Join(dataDir, "key"))
|
|
|
|
if err != nil {
|
|
|
|
logUserMsg("error: failed to create key file: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
privkey, err := identity.Generate()
|
|
|
|
if err != nil {
|
|
|
|
logUserMsg("error: failed to generate key: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = keyFile.Write([]byte(base64.StdEncoding.EncodeToString(privkey)))
|
|
|
|
if err != nil {
|
|
|
|
logUserMsg("error: failed to write key: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer keyFile.Close()
|
|
|
|
|
2025-03-08 15:07:27 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Flags
|
|
|
|
|
2025-03-10 19:53:10 -04:00
|
|
|
masterKey, err := identity.LoadSigningKeyFromFile(filepath.Join(dataDir, "key"))
|
|
|
|
if err != nil {
|
|
|
|
logUserMsg("error: failed to load key: %v\n", err)
|
2025-03-08 15:07:27 -05:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2025-03-10 19:53:10 -04:00
|
|
|
var admins []string
|
|
|
|
adminsFile, err := os.ReadFile(filepath.Join(dataDir, "admins"))
|
2025-03-08 15:07:27 -05:00
|
|
|
if err != nil {
|
2025-03-10 19:53:10 -04:00
|
|
|
logUserMsg("warn: failed to read admins file.. no one will be able to control this server..\n")
|
|
|
|
logUserMsg(`
|
|
|
|
If you aren't sure what this means, here's a brief explanation:
|
|
|
|
The admins file is a list of public keys that are allowed to control
|
|
|
|
the server. The base are base64 encoded and separated by newlines.
|
|
|
|
The application looks for this file at <dir>/admins, where <dir> is
|
|
|
|
the directory you specified with the -dir flag when starting the
|
|
|
|
system. By default, this is ~/.dsfxnode.
|
|
|
|
|
|
|
|
When you run the dsfxctl command, it will generate a key for you and
|
|
|
|
print it to the console. You can copy this key and paste it into the
|
|
|
|
admins file to give yourself control over the server.
|
|
|
|
`)
|
|
|
|
logUserMsg("\n")
|
|
|
|
}
|
|
|
|
if adminsFile != nil {
|
|
|
|
rawAdmins := strings.Split(string(adminsFile), "\n")
|
|
|
|
for _, admin := range rawAdmins {
|
|
|
|
if admin != "" {
|
|
|
|
admins = append(admins, admin)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logUserMsg("info: loaded admins: %v\n", admins)
|
2025-03-08 15:07:27 -05:00
|
|
|
}
|
|
|
|
|
2025-03-09 12:33:27 -04:00
|
|
|
tcpAddrRaw := net.JoinHostPort(*flagHost, fmt.Sprint(*flagPort))
|
|
|
|
tcpAddr, err := net.ResolveTCPAddr("tcp", tcpAddrRaw)
|
|
|
|
if err != nil {
|
2025-03-10 19:53:10 -04:00
|
|
|
logUserMsg("warn: invalid host or port: %v\n", err)
|
2025-03-09 12:33:27 -04:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2025-03-10 10:29:54 -04:00
|
|
|
addr := network.FromTCPAddr(tcpAddr, identity.ToPublicKey(masterKey))
|
2025-03-09 12:33:27 -04:00
|
|
|
|
2025-03-08 15:07:27 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Listener
|
|
|
|
|
2025-03-09 15:52:33 -04:00
|
|
|
listener, err := network.Listen(ctx, masterKey, addr)
|
2025-03-08 15:07:27 -05:00
|
|
|
if err != nil {
|
|
|
|
logger.ErrorContext(ctx, "listener error", slog.Any("error", err))
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2025-03-10 19:53:10 -04:00
|
|
|
logUserMsg("info: listener created\n")
|
|
|
|
logUserMsg(">> Operating System: %s\n", runtime.GOOS)
|
|
|
|
logUserMsg(">> Architecture: %s\n", runtime.GOARCH)
|
|
|
|
logUserMsg(">> CPU Cores: %d\n", runtime.NumCPU())
|
|
|
|
logUserMsg(">> Public Address: %s\n", addr.String())
|
2025-03-08 15:07:27 -05:00
|
|
|
|
|
|
|
for {
|
|
|
|
conn, err := listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
logger.ErrorContext(ctx, "accept failure", slog.Any("error", err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
go handleConnection(ctx, conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleConnection(ctx context.Context, conn net.Conn) error {
|
|
|
|
defer conn.Close()
|
|
|
|
|
2025-03-09 15:52:33 -04:00
|
|
|
logger := logging.FromContext(ctx)
|
2025-03-08 15:07:27 -05:00
|
|
|
|
|
|
|
msg := make([]byte, 1024)
|
|
|
|
n, err := conn.Read(msg)
|
|
|
|
if err != nil {
|
|
|
|
logger.ErrorContext(ctx, "failed to read from connection", slog.Any("error", err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.InfoContext(ctx, "received msg", slog.Int("bytes", n))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2025-03-10 19:53:10 -04:00
|
|
|
|
|
|
|
func logUserMsg(msg string, args ...any) {
|
|
|
|
fmt.Fprintf(os.Stderr, msg, args...)
|
|
|
|
}
|