2025-04-16 16:18:40 -04:00
|
|
|
/*
|
|
|
|
Copyright © 2025 Jake jake.young.dev@gmail.com
|
|
|
|
*/
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
2025-04-17 15:40:54 +00:00
|
|
|
"code.jakeyoungdev.com/jake/mctl/cryptography"
|
2025-04-16 16:18:40 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"golang.org/x/term"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2025-04-17 15:40:54 +00:00
|
|
|
cfgserver string
|
|
|
|
cfgport int
|
2025-04-16 16:18:40 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// configCmd represents the config command
|
|
|
|
var configCmd = &cobra.Command{
|
2025-04-17 15:40:54 +00:00
|
|
|
Use: "config",
|
|
|
|
Example: "mctl config -s x.x.x.x -p 61695",
|
|
|
|
Short: "Create and populate config file",
|
2025-04-16 16:18:40 -04:00
|
|
|
Long: `Creates the .mctl file in the user home directory
|
|
|
|
populating it with the server address, rcon password, and
|
|
|
|
rcon port to be pulled when using Login command`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
//read in password using term to keep it secure/hidden from bash history
|
|
|
|
fmt.Printf("Password: ")
|
|
|
|
ps, err := term.ReadPassword(int(os.Stdin.Fd()))
|
|
|
|
cobra.CheckErr(err)
|
|
|
|
|
|
|
|
//generate and apply random nonce
|
|
|
|
nonce := make([]byte, 12)
|
|
|
|
_, err = io.ReadFull(rand.Reader, nonce)
|
|
|
|
cobra.CheckErr(err)
|
2025-04-17 15:40:54 +00:00
|
|
|
viper.Set("nonce", string(nonce))
|
2025-04-16 16:18:40 -04:00
|
|
|
|
2025-04-17 15:40:54 +00:00
|
|
|
//encrypt password
|
|
|
|
ciphert, err := cryptography.EncryptPassword(ps)
|
|
|
|
cobra.CheckErr(err)
|
2025-04-16 16:18:40 -04:00
|
|
|
|
|
|
|
//update config file with new values
|
2025-04-17 15:40:54 +00:00
|
|
|
viper.Set("server", cfgserver)
|
2025-04-16 16:18:40 -04:00
|
|
|
viper.Set("password", string(ciphert))
|
2025-04-17 15:40:54 +00:00
|
|
|
viper.Set("port", cfgport)
|
2025-04-24 12:23:07 -04:00
|
|
|
err = viper.WriteConfig()
|
|
|
|
cobra.CheckErr(err)
|
2025-04-16 16:18:40 -04:00
|
|
|
fmt.Println()
|
|
|
|
fmt.Println("Config file updated!")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
initConfig()
|
2025-04-17 15:40:54 +00:00
|
|
|
configCmd.Flags().StringVarP(&cfgserver, "server", "s", "", "server address")
|
2025-04-24 12:23:07 -04:00
|
|
|
err := configCmd.MarkFlagRequired("server")
|
|
|
|
cobra.CheckErr(err)
|
2025-04-17 15:40:54 +00:00
|
|
|
configCmd.Flags().IntVarP(&cfgport, "port", "p", 0, "server rcon port")
|
2025-04-24 12:23:07 -04:00
|
|
|
err = configCmd.MarkFlagRequired("port")
|
|
|
|
cobra.CheckErr(err)
|
2025-04-16 16:18:40 -04:00
|
|
|
rootCmd.AddCommand(configCmd)
|
|
|
|
}
|
|
|
|
|
2025-04-17 15:40:54 +00:00
|
|
|
// init config sets viper config and checks for config file, creating it if it doesn't exist
|
2025-04-16 16:18:40 -04:00
|
|
|
func initConfig() {
|
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
cobra.CheckErr(err)
|
|
|
|
|
|
|
|
viper.AddConfigPath(home)
|
|
|
|
viper.SetConfigType("yaml")
|
|
|
|
viper.SetConfigName(".mctl")
|
|
|
|
viper.AutomaticEnv()
|
2025-04-24 12:23:07 -04:00
|
|
|
err = viper.ReadInConfig()
|
|
|
|
cobra.CheckErr(err)
|
2025-04-16 16:18:40 -04:00
|
|
|
|
|
|
|
if err := viper.ReadInConfig(); err != nil {
|
|
|
|
//file does not exist, create it
|
2025-04-17 15:40:54 +00:00
|
|
|
viper.Set("server", cfgserver)
|
2025-04-16 16:18:40 -04:00
|
|
|
viper.Set("password", "")
|
2025-04-17 15:40:54 +00:00
|
|
|
viper.Set("port", cfgport)
|
2025-04-16 16:18:40 -04:00
|
|
|
viper.Set("nonce", "")
|
|
|
|
|
2025-04-16 16:29:51 -04:00
|
|
|
//generate psuedo-random key
|
2025-04-16 16:25:45 -04:00
|
|
|
uu := make([]byte, 32)
|
|
|
|
_, err := rand.Read(uu)
|
|
|
|
cobra.CheckErr(err)
|
2025-04-16 16:18:40 -04:00
|
|
|
|
2025-04-17 21:25:54 +00:00
|
|
|
//create custom command map
|
|
|
|
cmdMap := make(map[string]any, 0)
|
|
|
|
|
|
|
|
//write config
|
|
|
|
viper.Set("customcmd", cmdMap)
|
2025-04-16 16:25:45 -04:00
|
|
|
viper.Set("device", string(uu))
|
2025-04-24 12:23:07 -04:00
|
|
|
cobra.CheckErr(viper.SafeWriteConfig())
|
2025-04-16 16:18:40 -04:00
|
|
|
}
|
|
|
|
}
|