add scaffolding for configuration file

This commit is contained in:
Steven Polley 2024-04-17 19:12:01 -06:00
parent a0d118b987
commit e95b4972da
4 changed files with 111 additions and 19 deletions

33
hypd/cmd/defaultconfig.go Normal file
View File

@ -0,0 +1,33 @@
/*
Copyright © 2024 Steven Polley <himself@stevenpolley.net>
*/
package cmd
import (
"encoding/json"
"fmt"
"deadbeef.codes/steven/hyp/hypd/configuration"
"github.com/spf13/cobra"
)
// defaultconfigCmd represents the defaultconfig command
var defaultconfigCmd = &cobra.Command{
Use: "defaultconfig",
Short: "Prints the default configuration to stdout",
Long: `The default configuration is used if one is not set. The default configuration
can be used as a reference to build your own. `,
Run: func(cmd *cobra.Command, args []string) {
config := configuration.DefaultConfig()
b, err := json.MarshalIndent(config, "", " ")
if err != nil {
panic(fmt.Errorf("failed to marshal default configuration to json (this should never happen): %v", err))
}
fmt.Println(string(b))
},
}
func init() {
generateCmd.AddCommand(defaultconfigCmd)
}

View File

@ -6,6 +6,7 @@ package cmd
import (
"fmt"
"deadbeef.codes/steven/hyp/hypd/configuration"
"deadbeef.codes/steven/hyp/hypd/server"
"github.com/spf13/cobra"
)
@ -30,7 +31,16 @@ Example Usage:
`,
Run: func(cmd *cobra.Command, args []string) {
err := server.PacketServer(args[0])
configFile, err := cmd.Flags().GetString("configfile")
if err != nil {
panic(fmt.Errorf("failed to get configfile flag: %w", err))
}
hypdConfiguration, err := configuration.LoadConfiguration(configFile)
if err != nil {
panic(fmt.Errorf("failed to start packet server: %w", err))
}
err = server.PacketServer(args[0], hypdConfiguration)
if err != nil {
panic(fmt.Errorf("failed to start packet server: %w", err))
}
@ -40,22 +50,6 @@ Example Usage:
func init() {
rootCmd.AddCommand(serverCmd)
/*
viper.SetConfigName("hypconfig")
viper.SetConfigType("yaml")
viper.AddConfigPath("/etc/hyp/")
viper.AddConfigPath("$HOME/.hyp")
viper.AddConfigPath(".")
viper.SetDefault("RefreshInterval", 7200)
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Config file not found
// TBD: Implement
} else {
// Config file was found, but another error was produced
panic(fmt.Errorf("failed reading existing config file: %w", err))
}
}*/
serverCmd.PersistentFlags().String("configfile", "", "Path to the file containing the hypd configuration.")
}

View File

@ -0,0 +1,64 @@
package configuration
import (
"encoding/json"
"fmt"
"os"
)
type HypdConfiguration struct {
PreSharedKeyDirectory string `json:"preSharedKeyDirectory"` // hypd will load all *.secret files from this directory
SuccessAction string `json:"successAction"` // The action to take
TimeoutSeconds int `json:"timeoutSeconds"` // If > 0, once a knock sequence has been successful this value will count down and when it reaches 0, it will perform the TimeoutAction on the client.
TimeoutAction string `json:"timeoutAction"` // The action to take after TimeoutSeconds has elapsed. only applicable if TimeoutSeconds is > 0
}
// LoadConfiguration opens and parses the configuration file into a HypdConfiguration struct
// If a configFilePath is not specified, it will search in common locations
func LoadConfiguration(configFilePath string) (*HypdConfiguration, error) {
if configFilePath == "" {
commonLocations := []string{"hypdconfig.json",
"~/.config/hyp/hypdConfig.json",
"/etc/hyp/hypdConfig.json",
"/usr/local/etc/hyp/hypdConfig.json",
}
for _, loc := range commonLocations {
if _, err := os.Stat(loc); err == nil {
configFilePath = loc
break
}
}
}
// if it's still not found after checking common locations, load default config
if configFilePath == "" {
return DefaultConfig(), nil
}
// Otherwise if a config is specified, try to load it and error if it fails.
// I think it's better to error here if a config was intended and failed
// rather than failing back to default
b, err := os.ReadFile(configFilePath)
if err != nil {
return nil, fmt.Errorf("failed to read config file '%s': %w", configFilePath, err)
}
var hypdConfiguration *HypdConfiguration
err = json.Unmarshal(b, hypdConfiguration)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal config file json to HypdConfiguration (is the config file malformed?): %w", err)
}
return nil, nil
}
func DefaultConfig() *HypdConfiguration {
return &HypdConfiguration{
PreSharedKeyDirectory: "./secrets/",
SuccessAction: "iptables -A INPUT -p tcp -s %s --dport 22 -j ACCEPT",
TimeoutSeconds: 1440,
TimeoutAction: "iptables -D INPUT -p tcp -s %s --dport 22 -j ACCEPT",
}
}

View File

@ -15,6 +15,7 @@ import (
"os/exec"
"time"
"deadbeef.codes/steven/hyp/hypd/configuration"
"deadbeef.codes/steven/hyp/otphyp"
"github.com/cilium/ebpf/link"
"github.com/cilium/ebpf/ringbuf"
@ -48,7 +49,7 @@ var (
// PacketServer is the main function when operating in server mode
// it sets up the pcap on the capture device and starts a goroutine
// to rotate the knock sequence
func PacketServer(captureDevice string) error {
func PacketServer(captureDevice string, config *configuration.HypdConfiguration) error {
iface, err := net.InterfaceByName(captureDevice)
if err != nil {