From 61a2113d9d86d4c257853cedf27c5da9203985d3 Mon Sep 17 00:00:00 2001 From: Bill Mill Date: Tue, 21 Jun 2022 16:23:57 -0400 Subject: [PATCH] separate out command binaries --- Makefile | 10 +-- client/main.go | 165 +--------------------------------------- cmd/springer/main.go | 171 ++++++++++++++++++++++++++++++++++++++++++ cmd/springerd/main.go | 67 +++++++++++++++++ modd.conf | 2 +- server/main.go | 84 ++++----------------- 6 files changed, 261 insertions(+), 238 deletions(-) create mode 100644 cmd/springer/main.go create mode 100644 cmd/springerd/main.go diff --git a/Makefile b/Makefile index 9ffca75..a3f4573 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,8 @@ -bin/springerd: server/main.go - go build -o bin/springerd ./server +bin/springerd: cmd/springerd/main.go server/main.go + go build -o bin/springerd ./cmd/springer bin/springer: client/main.go - go build -o bin/springer ./client + go build -o bin/springer ./cmd/springerd # Use zig as the cc to cross-compile mattn/sqlite for linux # @@ -12,5 +12,5 @@ bin/springer: client/main.go # # to run locally: # docker run -it -v $(pwd)/bin:/app ubuntu /app/springerd-linux -bin/springerd-linux: server/main.go - CGO_ENABLED=1 GOOS=linux GOARCH=amd64 CC="zig cc -target x86_64-linux" CXX="zig cc -target x86_64-linux" go build -o bin/springerd-linux ./server +bin/springerd-linux: cmd/springerd/main.go server/main.go + CGO_ENABLED=1 GOOS=linux GOARCH=amd64 CC="zig cc -target x86_64-linux" CXX="zig cc -target x86_64-linux" go build -o bin/springerd-linux ./cmd/springerd diff --git a/client/main.go b/client/main.go index ae7eaff..e30cfcb 100644 --- a/client/main.go +++ b/client/main.go @@ -1,25 +1,16 @@ -// TODO: -// * Board UI? -package main +package client import ( "bytes" "crypto/ed25519" "encoding/hex" - "errors" - "flag" "fmt" - "io/ioutil" - "net/http" - "os" - "os/user" - "path/filepath" "runtime" "sync" "time" ) -func validKey() (ed25519.PublicKey, ed25519.PrivateKey) { +func ValidKey() (ed25519.PublicKey, ed25519.PrivateKey) { // A conforming key's final seven hex characters must be 83e followed by // four characters that, interpreted as MMYY, express a valid month and // year in the range 01/00 .. 12/99. Formally, the key must match this @@ -75,155 +66,3 @@ func validKey() (ed25519.PublicKey, ed25519.PrivateKey) { return publicKey, privateKey } - -func fileExists(name string) bool { - if _, err := os.Stat(name); errors.Is(err, os.ErrNotExist) { - return false - } - return true -} - -func getKeys(folder string) (ed25519.PublicKey, ed25519.PrivateKey) { - // get the expected public key file and private key file paths - var pubfile, privfile string - if len(folder) == 0 { - user, err := user.Current() - if err != nil { - panic(err) - } - - configPath := os.Getenv("XDG_CONFIG_HOME") - if configPath == "" { - configPath = filepath.Join(user.HomeDir, ".config", "spring83") - } - - if err = os.MkdirAll(configPath, os.ModePerm); err != nil { - panic(err) - } - - pubfile = filepath.Join(configPath, "key.pub") - privfile = filepath.Join(configPath, "key.priv") - } else { - pubfile = filepath.Join(folder, "key.pub") - privfile = filepath.Join(folder, "key.priv") - } - - // try to load the public and private key files as ed25519 keys - var pubkey ed25519.PublicKey - var privkey ed25519.PrivateKey - var err error - if fileExists(pubfile) && fileExists(privfile) { - pubkey, err = ioutil.ReadFile(pubfile) - if err != nil { - panic(err) - } - - privkey, err = ioutil.ReadFile(privfile) - if err != nil { - panic(err) - } - } else { - fmt.Println("Generating valid key. This will take a minute") - pubkey, privkey = validKey() - - os.WriteFile(pubfile, pubkey, 0666) - os.WriteFile(privfile, privkey, 0600) - } - - return pubkey, privkey -} - -func getBody(inputFile string) []byte { - var body []byte - var err error - if inputFile == "-" { - body, err = ioutil.ReadAll(os.Stdin) - if err != nil { - panic(err) - } - } else { - body, err = ioutil.ReadFile(inputFile) - if err != nil { - panic(err) - } - } - - // Prepend a time element. Maybe we should check to see if it's already - // been provided? - timeElt := []byte(fmt.Sprintf("