2022-04-05 07:07:01 +02:00
|
|
|
package lib
|
2022-01-09 06:59:28 +01:00
|
|
|
|
|
|
|
import (
|
2022-01-10 01:39:13 +01:00
|
|
|
_ "embed"
|
|
|
|
|
2022-03-30 06:56:28 +02:00
|
|
|
"bytes"
|
2022-01-09 20:00:53 +01:00
|
|
|
"encoding/json"
|
2022-01-09 06:59:28 +01:00
|
|
|
"fmt"
|
2022-01-10 00:48:20 +01:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2022-01-09 20:00:53 +01:00
|
|
|
"log"
|
2022-01-09 06:59:28 +01:00
|
|
|
"os"
|
2022-01-10 00:48:20 +01:00
|
|
|
"os/exec"
|
2022-01-09 06:59:28 +01:00
|
|
|
"os/user"
|
|
|
|
"path"
|
2022-01-10 00:48:20 +01:00
|
|
|
"path/filepath"
|
2022-01-09 06:59:28 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2022-04-04 06:27:32 +02:00
|
|
|
"net/http"
|
|
|
|
|
2022-01-09 06:59:28 +01:00
|
|
|
"github.com/fatih/color"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/rodaine/table"
|
|
|
|
|
2022-04-05 07:07:01 +02:00
|
|
|
"github.com/ddworken/hishtory/shared"
|
2022-01-09 06:59:28 +01:00
|
|
|
)
|
|
|
|
|
2022-01-10 01:39:13 +01:00
|
|
|
var (
|
|
|
|
//go:embed config.sh
|
|
|
|
CONFIG_SH_CONTENTS string
|
|
|
|
)
|
|
|
|
|
2022-01-09 06:59:28 +01:00
|
|
|
func getCwd() (string, error) {
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to get cwd for last command: %v", err)
|
|
|
|
}
|
|
|
|
homedir, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to get user's home directory: %v", err)
|
|
|
|
}
|
2022-03-30 06:56:28 +02:00
|
|
|
if cwd == homedir {
|
|
|
|
return "~/", nil
|
|
|
|
}
|
2022-01-09 06:59:28 +01:00
|
|
|
if strings.HasPrefix(cwd, homedir) {
|
|
|
|
return strings.Replace(cwd, homedir, "~", 1), nil
|
|
|
|
}
|
|
|
|
return cwd, nil
|
|
|
|
}
|
|
|
|
|
2022-04-05 07:07:01 +02:00
|
|
|
func BuildHistoryEntry(args []string) (*shared.HistoryEntry, error) {
|
|
|
|
var entry shared.HistoryEntry
|
2022-01-09 06:59:28 +01:00
|
|
|
|
|
|
|
// exitCode
|
|
|
|
exitCode, err := strconv.Atoi(args[2])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to build history entry: %v", err)
|
|
|
|
}
|
|
|
|
entry.ExitCode = exitCode
|
|
|
|
|
|
|
|
// user
|
|
|
|
user, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to build history entry: %v", err)
|
|
|
|
}
|
|
|
|
entry.LocalUsername = user.Username
|
|
|
|
|
|
|
|
// cwd
|
|
|
|
cwd, err := getCwd()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to build history entry: %v", err)
|
|
|
|
}
|
|
|
|
entry.CurrentWorkingDirectory = cwd
|
|
|
|
|
2022-01-10 01:39:13 +01:00
|
|
|
// start time
|
|
|
|
nanos, err := strconv.ParseInt(args[4], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse start time %s as int: %v", args[4], err)
|
|
|
|
}
|
|
|
|
entry.StartTime = time.Unix(0, nanos)
|
2022-01-09 06:59:28 +01:00
|
|
|
|
|
|
|
// end time
|
|
|
|
entry.EndTime = time.Now()
|
|
|
|
|
|
|
|
// command
|
|
|
|
cmd, err := getLastCommand(args[3])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to build history entry: %v", err)
|
|
|
|
}
|
|
|
|
entry.Command = cmd
|
|
|
|
|
|
|
|
// hostname
|
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to build history entry: %v", err)
|
|
|
|
}
|
|
|
|
entry.Hostname = hostname
|
2022-04-05 07:07:01 +02:00
|
|
|
|
2022-01-09 06:59:28 +01:00
|
|
|
return &entry, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getLastCommand(history string) (string, error) {
|
|
|
|
return strings.TrimSpace(strings.SplitN(strings.TrimSpace(history), " ", 2)[1]), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetUserSecret() (string, error) {
|
2022-01-09 20:00:53 +01:00
|
|
|
config, err := GetConfig()
|
2022-01-09 06:59:28 +01:00
|
|
|
if err != nil {
|
2022-01-09 20:00:53 +01:00
|
|
|
return "", err
|
2022-01-09 06:59:28 +01:00
|
|
|
}
|
2022-01-09 20:00:53 +01:00
|
|
|
return config.UserSecret, nil
|
2022-01-09 06:59:28 +01:00
|
|
|
}
|
|
|
|
|
2022-04-04 06:00:46 +02:00
|
|
|
func Setup(args []string) error {
|
2022-01-09 06:59:28 +01:00
|
|
|
userSecret := uuid.Must(uuid.NewRandom()).String()
|
|
|
|
if len(args) > 2 && args[2] != "" {
|
|
|
|
userSecret = args[2]
|
|
|
|
}
|
|
|
|
fmt.Println("Setting secret hishtory key to " + string(userSecret))
|
|
|
|
|
2022-01-09 20:00:53 +01:00
|
|
|
var config ClientConfig
|
|
|
|
config.UserSecret = userSecret
|
|
|
|
config.IsEnabled = true
|
2022-04-04 06:00:46 +02:00
|
|
|
config.DeviceId = uuid.Must(uuid.NewRandom()).String()
|
2022-04-04 06:27:32 +02:00
|
|
|
|
|
|
|
err := SetConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to persist config to disk: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-04-05 07:07:01 +02:00
|
|
|
_, err = http.Get(GetServerHostname()+"/api/v1/eregister?user_id=" + shared.UserId(userSecret) + "&device_id=" + config.DeviceId)
|
2022-04-04 06:27:32 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to register device with backend: %v", err)
|
|
|
|
}
|
2022-04-05 07:07:01 +02:00
|
|
|
return nil
|
2022-01-09 06:59:28 +01:00
|
|
|
}
|
|
|
|
|
2022-04-05 07:07:01 +02:00
|
|
|
func DisplayResults(results []*shared.HistoryEntry, displayHostname bool) {
|
2022-01-09 06:59:28 +01:00
|
|
|
headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc()
|
2022-01-10 01:39:13 +01:00
|
|
|
tbl := table.New("CWD", "Timestamp", "Runtime", "Exit Code", "Command")
|
2022-01-09 23:34:59 +01:00
|
|
|
if displayHostname {
|
2022-01-10 01:39:13 +01:00
|
|
|
tbl = table.New("Hostname", "CWD", "Timestamp", "Runtime", "Exit Code", "Command")
|
2022-01-09 23:34:59 +01:00
|
|
|
}
|
2022-01-09 06:59:28 +01:00
|
|
|
tbl.WithHeaderFormatter(headerFmt)
|
|
|
|
|
|
|
|
for _, result := range results {
|
2022-01-10 01:39:13 +01:00
|
|
|
timestamp := result.StartTime.Format("Jan 2 2006 15:04:05 MST")
|
|
|
|
duration := result.EndTime.Sub(result.StartTime).Round(time.Millisecond).String()
|
2022-01-09 23:34:59 +01:00
|
|
|
if displayHostname {
|
2022-01-10 01:39:13 +01:00
|
|
|
tbl.AddRow(result.Hostname, result.CurrentWorkingDirectory, timestamp, duration, result.ExitCode, result.Command)
|
2022-01-09 23:34:59 +01:00
|
|
|
} else {
|
2022-01-10 01:39:13 +01:00
|
|
|
tbl.AddRow(result.CurrentWorkingDirectory, timestamp, duration, result.ExitCode, result.Command)
|
2022-01-09 23:34:59 +01:00
|
|
|
}
|
2022-01-09 06:59:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
tbl.Print()
|
|
|
|
}
|
2022-01-09 20:00:53 +01:00
|
|
|
|
|
|
|
type ClientConfig struct {
|
|
|
|
UserSecret string `json:"user_secret"`
|
|
|
|
IsEnabled bool `json:"is_enabled"`
|
2022-04-04 06:00:46 +02:00
|
|
|
DeviceId string `json:"device_id"`
|
2022-01-09 20:00:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetConfig() (ClientConfig, error) {
|
|
|
|
homedir, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
return ClientConfig{}, fmt.Errorf("failed to retrieve homedir: %v", err)
|
|
|
|
}
|
2022-04-05 07:07:01 +02:00
|
|
|
data, err := os.ReadFile(path.Join(homedir, shared.HISHTORY_PATH, shared.CONFIG_PATH))
|
2022-01-09 20:00:53 +01:00
|
|
|
if err != nil {
|
|
|
|
return ClientConfig{}, fmt.Errorf("failed to read config file: %v", err)
|
|
|
|
}
|
|
|
|
var config ClientConfig
|
|
|
|
err = json.Unmarshal(data, &config)
|
|
|
|
if err != nil {
|
|
|
|
return ClientConfig{}, fmt.Errorf("failed to parse config file: %v", err)
|
|
|
|
}
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetConfig(config ClientConfig) error {
|
|
|
|
serializedConfig, err := json.Marshal(config)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to serialize config: %v", err)
|
|
|
|
}
|
|
|
|
homedir, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to retrieve homedir: %v", err)
|
|
|
|
}
|
2022-04-05 07:07:01 +02:00
|
|
|
clientDir := path.Join(homedir, shared.HISHTORY_PATH)
|
2022-04-03 07:27:20 +02:00
|
|
|
err = os.MkdirAll(clientDir, 0744)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create ~/.hishtory/ folder: %v", err)
|
|
|
|
}
|
2022-04-05 07:07:01 +02:00
|
|
|
err = os.WriteFile(path.Join(homedir, shared.HISHTORY_PATH, shared.CONFIG_PATH), serializedConfig, 0600)
|
2022-01-09 20:00:53 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to write config: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsEnabled() (bool, error) {
|
|
|
|
config, err := GetConfig()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return config.IsEnabled, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Enable() error {
|
|
|
|
config, err := GetConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
config.IsEnabled = true
|
|
|
|
return SetConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Disable() error {
|
|
|
|
config, err := GetConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
config.IsEnabled = false
|
|
|
|
return SetConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func CheckFatalError(err error) {
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("hishtory fatal error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2022-01-10 00:48:20 +01:00
|
|
|
|
|
|
|
func Install() error {
|
|
|
|
homedir, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get user's home directory: %v", err)
|
|
|
|
}
|
2022-04-05 07:07:01 +02:00
|
|
|
clientDir := path.Join(homedir, shared.HISHTORY_PATH)
|
2022-04-03 07:27:20 +02:00
|
|
|
err = os.MkdirAll(clientDir, 0744)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create folder for hishtory binary: %v", err)
|
|
|
|
}
|
2022-01-10 00:48:20 +01:00
|
|
|
path, err := installBinary(homedir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-10 01:39:13 +01:00
|
|
|
err = configureBashrc(homedir, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = GetConfig()
|
|
|
|
if err != nil {
|
|
|
|
// No config, so set up a new installation
|
2022-04-04 06:00:46 +02:00
|
|
|
return Setup(os.Args)
|
2022-01-10 01:39:13 +01:00
|
|
|
}
|
|
|
|
return nil
|
2022-01-10 00:48:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func configureBashrc(homedir, binaryPath string) error {
|
2022-03-30 06:56:28 +02:00
|
|
|
// Create the file we're going to source in our bashrc. Do this no matter what in case there are updates to it.
|
|
|
|
bashConfigPath := path.Join(filepath.Dir(binaryPath), "config.sh")
|
|
|
|
err := ioutil.WriteFile(bashConfigPath, []byte(CONFIG_SH_CONTENTS), 0644)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to write config.sh file: %v", err)
|
|
|
|
}
|
2022-01-10 01:39:13 +01:00
|
|
|
// Check if we need to configure the bashrc
|
2022-01-10 00:48:20 +01:00
|
|
|
bashrc, err := ioutil.ReadFile(path.Join(homedir, ".bashrc"))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to read bashrc: %v", err)
|
|
|
|
}
|
2022-01-10 01:39:13 +01:00
|
|
|
if strings.Contains(string(bashrc), "# Hishtory Config:") {
|
2022-01-10 00:48:20 +01:00
|
|
|
return nil
|
|
|
|
}
|
2022-01-10 01:39:13 +01:00
|
|
|
// Add to bashrc
|
2022-01-10 00:48:20 +01:00
|
|
|
f, err := os.OpenFile(path.Join(homedir, ".bashrc"), os.O_APPEND|os.O_WRONLY, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to append to bashrc: %v", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2022-01-10 01:39:13 +01:00
|
|
|
_, err = f.WriteString("\n# Hishtory Config:\nexport PATH=\"$PATH:" + filepath.Dir(binaryPath) + "\"\nsource " + bashConfigPath + "\n")
|
2022-01-10 00:48:20 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to append to bashrc: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func installBinary(homedir string) (string, error) {
|
|
|
|
clientPath, err := exec.LookPath("hishtory")
|
|
|
|
if err != nil {
|
2022-04-05 07:07:01 +02:00
|
|
|
clientPath = path.Join(homedir, shared.HISHTORY_PATH, "hishtory")
|
2022-01-10 00:48:20 +01:00
|
|
|
}
|
|
|
|
err = copyFile(os.Args[0], clientPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to copy hishtory binary to $PATH: %v", err)
|
|
|
|
}
|
|
|
|
err = os.Chmod(clientPath, 0700)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to set permissions on hishtory binary: %v", err)
|
|
|
|
}
|
|
|
|
return clientPath, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyFile(src, dst string) error {
|
|
|
|
sourceFileStat, err := os.Stat(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !sourceFileStat.Mode().IsRegular() {
|
|
|
|
return fmt.Errorf("%s is not a regular file", src)
|
|
|
|
}
|
|
|
|
|
|
|
|
source, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer source.Close()
|
|
|
|
|
|
|
|
destination, err := os.Create(dst)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer destination.Close()
|
|
|
|
_, err = io.Copy(destination, source)
|
|
|
|
return err
|
|
|
|
}
|
2022-03-30 06:56:28 +02:00
|
|
|
|
|
|
|
func Update(url string) error {
|
|
|
|
var stdout bytes.Buffer
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
cmd := exec.Command("bash", "-c", "curl -o /tmp/hishtory-client "+url+"; chmod +x /tmp/hishtory-client; /tmp/hishtory-client install")
|
|
|
|
cmd.Stdout = &stdout
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to update: %v, stdout=%+v, stderr=%+v", err, stdout.String(), stderr.String())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-04-05 07:07:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
func GetServerHostname() string {
|
|
|
|
if server := os.Getenv("HISHTORY_SERVER"); server != "" {
|
|
|
|
return server
|
|
|
|
}
|
|
|
|
return "http://localhost:8080"
|
|
|
|
}
|