2022-04-05 07:07:01 +02:00
|
|
|
package lib
|
2022-01-09 06:59:28 +01:00
|
|
|
|
|
|
|
import (
|
2022-03-30 06:56:28 +02:00
|
|
|
"bytes"
|
2022-04-28 18:51:01 +02:00
|
|
|
"encoding/hex"
|
2022-01-09 20:00:53 +01:00
|
|
|
"encoding/json"
|
2022-04-20 06:05:54 +02:00
|
|
|
"errors"
|
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-04-07 03:18:46 +02:00
|
|
|
"net/http"
|
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-04-12 07:36:52 +02:00
|
|
|
"runtime"
|
2022-01-09 06:59:28 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2022-04-16 09:44:47 +02:00
|
|
|
"sync"
|
2022-04-07 03:18:46 +02:00
|
|
|
"syscall"
|
2022-01-09 06:59:28 +01:00
|
|
|
"time"
|
2022-04-04 06:27:32 +02:00
|
|
|
|
2022-04-12 07:36:52 +02:00
|
|
|
_ "embed" // for embedding config.sh
|
|
|
|
|
2022-04-26 07:03:31 +02:00
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
|
2022-04-21 02:28:19 +02:00
|
|
|
"github.com/glebarez/sqlite" // an alternate non-cgo-requiring sqlite driver
|
2022-04-07 07:44:10 +02:00
|
|
|
"gorm.io/gorm"
|
2022-04-15 05:18:49 +02:00
|
|
|
"gorm.io/gorm/logger"
|
2022-04-06 08:31:24 +02:00
|
|
|
|
2022-01-09 06:59:28 +01:00
|
|
|
"github.com/fatih/color"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/rodaine/table"
|
|
|
|
|
2022-04-08 05:59:40 +02:00
|
|
|
"github.com/ddworken/hishtory/client/data"
|
2022-04-05 07:07:01 +02:00
|
|
|
"github.com/ddworken/hishtory/shared"
|
2022-01-09 06:59:28 +01:00
|
|
|
)
|
|
|
|
|
2022-04-08 06:40:22 +02:00
|
|
|
//go:embed config.sh
|
|
|
|
var ConfigShContents string
|
2022-01-10 01:39:13 +01:00
|
|
|
|
2022-04-15 05:18:49 +02:00
|
|
|
//go:embed test_config.sh
|
|
|
|
var TestConfigShContents string
|
|
|
|
|
2022-04-18 04:54:17 +02:00
|
|
|
//go:embed config.zsh
|
|
|
|
var ConfigZshContents string
|
|
|
|
|
2022-04-22 07:25:24 +02:00
|
|
|
//go:embed test_config.zsh
|
|
|
|
var TestConfigZshContents string
|
|
|
|
|
2022-04-17 05:50:02 +02:00
|
|
|
var Version string = "Unknown"
|
|
|
|
|
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-08 05:59:40 +02:00
|
|
|
func BuildHistoryEntry(args []string) (*data.HistoryEntry, error) {
|
2022-04-18 04:54:17 +02:00
|
|
|
shell := args[2]
|
2022-04-08 05:59:40 +02:00
|
|
|
var entry data.HistoryEntry
|
2022-01-09 06:59:28 +01:00
|
|
|
|
|
|
|
// exitCode
|
2022-04-18 04:54:17 +02:00
|
|
|
exitCode, err := strconv.Atoi(args[3])
|
2022-01-09 06:59:28 +01:00
|
|
|
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
|
2022-04-19 07:36:57 +02:00
|
|
|
seconds, err := parseCrossPlatformInt(args[5])
|
2022-01-10 01:39:13 +01:00
|
|
|
if err != nil {
|
2022-04-18 04:54:17 +02:00
|
|
|
return nil, fmt.Errorf("failed to parse start time %s as int: %v", args[5], err)
|
2022-01-10 01:39:13 +01:00
|
|
|
}
|
2022-04-19 07:36:57 +02:00
|
|
|
entry.StartTime = time.Unix(seconds, 0)
|
2022-01-09 06:59:28 +01:00
|
|
|
|
|
|
|
// end time
|
|
|
|
entry.EndTime = time.Now()
|
|
|
|
|
|
|
|
// command
|
2022-04-18 04:54:17 +02:00
|
|
|
if shell == "bash" {
|
|
|
|
cmd, err := getLastCommand(args[4])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to build history entry: %v", err)
|
|
|
|
}
|
|
|
|
shouldBeSkipped, err := shouldSkipHiddenCommand(args[4])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to check if command was hidden: %v", err)
|
|
|
|
}
|
2022-04-19 06:41:49 +02:00
|
|
|
if shouldBeSkipped || strings.HasPrefix(cmd, " ") {
|
2022-04-18 06:04:44 +02:00
|
|
|
// Don't save commands that start with a space
|
2022-04-18 04:54:17 +02:00
|
|
|
return nil, nil
|
|
|
|
}
|
2022-04-18 06:04:44 +02:00
|
|
|
entry.Command = cmd
|
|
|
|
} else if shell == "zsh" {
|
|
|
|
cmd := strings.TrimSuffix(strings.TrimSuffix(args[4], "\n"), " ")
|
2022-04-18 04:54:17 +02:00
|
|
|
if strings.HasPrefix(cmd, " ") {
|
|
|
|
// Don't save commands that start with a space
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
entry.Command = cmd
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("tried to save a hishtory entry from an unsupported shell=%#v", shell)
|
2022-04-15 09:04:49 +02:00
|
|
|
}
|
2022-01-09 06:59:28 +01:00
|
|
|
|
|
|
|
// hostname
|
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to build history entry: %v", err)
|
|
|
|
}
|
|
|
|
entry.Hostname = hostname
|
2022-04-06 08:31:24 +02:00
|
|
|
|
2022-05-02 04:37:26 +02:00
|
|
|
// device ID
|
|
|
|
config, err := GetConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get device ID when building history entry: %v", err)
|
|
|
|
}
|
|
|
|
entry.DeviceId = config.DeviceId
|
|
|
|
|
2022-01-09 06:59:28 +01:00
|
|
|
return &entry, nil
|
|
|
|
}
|
|
|
|
|
2022-04-17 20:09:30 +02:00
|
|
|
func parseCrossPlatformInt(data string) (int64, error) {
|
|
|
|
data = strings.TrimSuffix(data, "N")
|
|
|
|
return strconv.ParseInt(data, 10, 64)
|
|
|
|
}
|
|
|
|
|
2022-01-09 06:59:28 +01:00
|
|
|
func getLastCommand(history string) (string, error) {
|
2022-04-19 07:19:51 +02:00
|
|
|
return strings.SplitN(strings.SplitN(strings.TrimSpace(history), " ", 2)[1], " ", 2)[1], nil
|
2022-04-15 09:04:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func shouldSkipHiddenCommand(historyLine string) (bool, error) {
|
|
|
|
config, err := GetConfig()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if config.LastSavedHistoryLine == historyLine {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
config.LastSavedHistoryLine = historyLine
|
|
|
|
err = SetConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return false, nil
|
2022-01-09 06:59:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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-04-07 07:44:10 +02:00
|
|
|
// Create and set the config
|
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-07 07:44:10 +02:00
|
|
|
// Drop all existing data
|
2022-04-07 07:43:07 +02:00
|
|
|
db, err := OpenLocalSqliteDb()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to open DB: %v", err)
|
|
|
|
}
|
|
|
|
db.Exec("DELETE FROM history_entries")
|
|
|
|
|
|
|
|
// Bootstrap from remote date
|
2022-04-28 19:56:59 +02:00
|
|
|
_, err = ApiGet("/api/v1/register?user_id=" + data.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-06 08:31:24 +02:00
|
|
|
|
2022-04-28 19:56:59 +02:00
|
|
|
respBody, err := ApiGet("/api/v1/bootstrap?user_id=" + data.UserId(userSecret) + "&device_id=" + config.DeviceId)
|
2022-04-06 08:31:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to bootstrap device from the backend: %v", err)
|
|
|
|
}
|
|
|
|
var retrievedEntries []*shared.EncHistoryEntry
|
2022-04-08 05:59:40 +02:00
|
|
|
err = json.Unmarshal(respBody, &retrievedEntries)
|
2022-04-06 08:31:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to load JSON response: %v", err)
|
|
|
|
}
|
|
|
|
for _, entry := range retrievedEntries {
|
2022-04-08 05:59:40 +02:00
|
|
|
decEntry, err := data.DecryptHistoryEntry(userSecret, *entry)
|
2022-04-06 08:31:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to decrypt history entry from server: %v", err)
|
|
|
|
}
|
|
|
|
AddToDbIfNew(db, decEntry)
|
|
|
|
}
|
|
|
|
|
2022-04-07 03:18:46 +02:00
|
|
|
return nil
|
2022-01-09 06:59:28 +01:00
|
|
|
}
|
|
|
|
|
2022-04-08 05:59:40 +02:00
|
|
|
func AddToDbIfNew(db *gorm.DB, entry data.HistoryEntry) {
|
2022-04-06 08:31:24 +02:00
|
|
|
tx := db.Where("local_username = ?", entry.LocalUsername)
|
|
|
|
tx = tx.Where("hostname = ?", entry.Hostname)
|
|
|
|
tx = tx.Where("command = ?", entry.Command)
|
|
|
|
tx = tx.Where("current_working_directory = ?", entry.CurrentWorkingDirectory)
|
|
|
|
tx = tx.Where("exit_code = ?", entry.ExitCode)
|
|
|
|
tx = tx.Where("start_time = ?", entry.StartTime)
|
|
|
|
tx = tx.Where("end_time = ?", entry.EndTime)
|
2022-04-08 05:59:40 +02:00
|
|
|
var results []data.HistoryEntry
|
2022-04-06 08:31:24 +02:00
|
|
|
tx.Limit(1).Find(&results)
|
|
|
|
if len(results) == 0 {
|
|
|
|
db.Create(entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-09 23:52:10 +02:00
|
|
|
func DisplayResults(results []*data.HistoryEntry) {
|
2022-01-09 06:59:28 +01:00
|
|
|
headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc()
|
2022-04-09 23:52:10 +02:00
|
|
|
tbl := table.New("Hostname", "CWD", "Timestamp", "Runtime", "Exit Code", "Command")
|
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-04-09 23:52:10 +02:00
|
|
|
tbl.AddRow(result.Hostname, result.CurrentWorkingDirectory, timestamp, duration, result.ExitCode, result.Command)
|
2022-01-09 06:59:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
tbl.Print()
|
|
|
|
}
|
2022-01-09 20:00:53 +01:00
|
|
|
|
|
|
|
type ClientConfig struct {
|
2022-04-15 09:04:49 +02:00
|
|
|
UserSecret string `json:"user_secret"`
|
|
|
|
IsEnabled bool `json:"is_enabled"`
|
|
|
|
DeviceId string `json:"device_id"`
|
|
|
|
LastSavedHistoryLine string `json:"last_saved_history_line"`
|
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 {
|
2022-04-15 05:18:49 +02:00
|
|
|
files, err := ioutil.ReadDir(path.Join(homedir, shared.HISHTORY_PATH))
|
|
|
|
if err != nil {
|
|
|
|
return ClientConfig{}, fmt.Errorf("failed to read config file (and failed to list too): %v", err)
|
|
|
|
}
|
|
|
|
filenames := ""
|
|
|
|
for _, file := range files {
|
|
|
|
filenames += file.Name()
|
|
|
|
filenames += ", "
|
|
|
|
}
|
|
|
|
return ClientConfig{}, fmt.Errorf("failed to read config file (files in ~/.hishtory/: %s): %v", filenames, err)
|
2022-01-09 20:00:53 +01:00
|
|
|
}
|
|
|
|
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-08 06:40:22 +02:00
|
|
|
err = os.MkdirAll(clientDir, 0o744)
|
2022-04-03 07:27:20 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create ~/.hishtory/ folder: %v", err)
|
|
|
|
}
|
2022-04-08 06:40:22 +02:00
|
|
|
err = os.WriteFile(path.Join(homedir, shared.HISHTORY_PATH, shared.CONFIG_PATH), serializedConfig, 0o600)
|
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 {
|
2022-04-12 07:36:52 +02:00
|
|
|
_, filename, line, _ := runtime.Caller(1)
|
|
|
|
log.Fatalf("hishtory fatal error at %s:%d: %v", filename, line, err)
|
2022-01-09 20:00:53 +01:00
|
|
|
}
|
|
|
|
}
|
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-08 06:40:22 +02:00
|
|
|
err = os.MkdirAll(clientDir, 0o744)
|
2022-04-03 07:27:20 +02:00
|
|
|
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
|
|
|
|
}
|
2022-04-18 04:54:17 +02:00
|
|
|
err = configureZshrc(homedir, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-10 01:39:13 +01:00
|
|
|
_, 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
|
|
|
}
|
|
|
|
|
2022-04-18 04:54:17 +02:00
|
|
|
func configureZshrc(homedir, binaryPath string) error {
|
|
|
|
// Create the file we're going to source in our zshrc. Do this no matter what in case there are updates to it.
|
|
|
|
zshConfigPath := path.Join(homedir, shared.HISHTORY_PATH, "config.zsh")
|
2022-04-22 07:25:24 +02:00
|
|
|
configContents := ConfigZshContents
|
|
|
|
if os.Getenv("HISHTORY_TEST") != "" {
|
|
|
|
configContents = TestConfigZshContents
|
|
|
|
}
|
|
|
|
err := ioutil.WriteFile(zshConfigPath, []byte(configContents), 0o644)
|
2022-04-18 04:54:17 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to write config.zsh file: %v", err)
|
|
|
|
}
|
|
|
|
// Check if we need to configure the zshrc
|
2022-04-20 06:05:54 +02:00
|
|
|
zshIsConfigured, err := isZshConfigured(homedir)
|
2022-04-18 04:54:17 +02:00
|
|
|
if err != nil {
|
2022-04-20 06:05:54 +02:00
|
|
|
return fmt.Errorf("failed to check ~/.zshrc: %v", err)
|
2022-04-18 04:54:17 +02:00
|
|
|
}
|
2022-04-20 06:05:54 +02:00
|
|
|
if zshIsConfigured {
|
2022-04-18 04:54:17 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Add to zshrc
|
2022-04-20 06:05:54 +02:00
|
|
|
f, err := os.OpenFile(path.Join(homedir, ".zshrc"), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0o644)
|
2022-04-18 04:54:17 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to append to zshrc: %v", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
_, err = f.WriteString("\n# Hishtory Config:\nexport PATH=\"$PATH:" + path.Join(homedir, shared.HISHTORY_PATH) + "\"\nsource " + zshConfigPath + "\n")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to append to zshrc: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 06:05:54 +02:00
|
|
|
func isZshConfigured(homedir string) (bool, error) {
|
|
|
|
_, err := os.Stat(path.Join(homedir, ".zshrc"))
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
bashrc, err := ioutil.ReadFile(path.Join(homedir, ".zshrc"))
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed to read zshrc: %v", err)
|
|
|
|
}
|
|
|
|
return strings.Contains(string(bashrc), "# Hishtory Config:"), 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.
|
2022-04-18 04:54:17 +02:00
|
|
|
bashConfigPath := path.Join(homedir, shared.HISHTORY_PATH, "config.sh")
|
2022-04-15 05:18:49 +02:00
|
|
|
configContents := ConfigShContents
|
|
|
|
if os.Getenv("HISHTORY_TEST") != "" {
|
|
|
|
configContents = TestConfigShContents
|
|
|
|
}
|
|
|
|
err := ioutil.WriteFile(bashConfigPath, []byte(configContents), 0o644)
|
2022-03-30 06:56:28 +02:00
|
|
|
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-04-20 06:05:54 +02:00
|
|
|
bashIsConfigured, err := isBashConfigured(homedir)
|
2022-01-10 00:48:20 +01:00
|
|
|
if err != nil {
|
2022-04-20 06:05:54 +02:00
|
|
|
return fmt.Errorf("failed to check ~/.bashrc: %v", err)
|
2022-01-10 00:48:20 +01:00
|
|
|
}
|
2022-04-20 06:05:54 +02:00
|
|
|
if bashIsConfigured {
|
2022-01-10 00:48:20 +01:00
|
|
|
return nil
|
|
|
|
}
|
2022-01-10 01:39:13 +01:00
|
|
|
// Add to bashrc
|
2022-04-20 06:05:54 +02:00
|
|
|
f, err := os.OpenFile(path.Join(homedir, ".bashrc"), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0o644)
|
2022-01-10 00:48:20 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to append to bashrc: %v", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2022-04-18 04:54:17 +02:00
|
|
|
_, err = f.WriteString("\n# Hishtory Config:\nexport PATH=\"$PATH:" + path.Join(homedir, shared.HISHTORY_PATH) + "\"\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
|
|
|
|
}
|
|
|
|
|
2022-04-20 06:05:54 +02:00
|
|
|
func isBashConfigured(homedir string) (bool, error) {
|
|
|
|
_, err := os.Stat(path.Join(homedir, ".bashrc"))
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
bashrc, err := ioutil.ReadFile(path.Join(homedir, ".bashrc"))
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed to read bashrc: %v", err)
|
|
|
|
}
|
|
|
|
return strings.Contains(string(bashrc), "# Hishtory Config:"), nil
|
|
|
|
}
|
|
|
|
|
2022-01-10 00:48:20 +01:00
|
|
|
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
|
|
|
}
|
2022-04-14 06:30:27 +02:00
|
|
|
if _, err := os.Stat(clientPath); err == nil {
|
|
|
|
err = syscall.Unlink(clientPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to unlink %s for install: %v", clientPath, err)
|
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
}
|
2022-04-08 06:40:22 +02:00
|
|
|
err = os.Chmod(clientPath, 0o700)
|
2022-01-10 00:48:20 +01:00
|
|
|
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
|
|
|
|
2022-04-17 01:02:07 +02:00
|
|
|
func Update() error {
|
|
|
|
// Download the binary
|
2022-04-17 05:50:02 +02:00
|
|
|
respBody, err := ApiGet("/api/v1/download")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to download update info: %v", err)
|
|
|
|
}
|
|
|
|
var downloadData shared.UpdateInfo
|
|
|
|
err = json.Unmarshal(respBody, &downloadData)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse update info: %v", err)
|
|
|
|
}
|
2022-04-17 06:39:40 +02:00
|
|
|
if downloadData.Version == "v0."+Version {
|
2022-04-17 05:50:02 +02:00
|
|
|
fmt.Printf("Latest version (v0.%s) is already installed\n", Version)
|
2022-04-17 06:54:34 +02:00
|
|
|
return nil
|
2022-04-17 05:50:02 +02:00
|
|
|
}
|
2022-04-17 21:30:46 +02:00
|
|
|
err = downloadFiles(downloadData)
|
2022-04-17 05:50:02 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-04-07 03:18:46 +02:00
|
|
|
}
|
2022-04-17 01:02:07 +02:00
|
|
|
|
|
|
|
// Verify the SLSA attestation
|
2022-04-17 21:30:46 +02:00
|
|
|
err = verifyBinary("/tmp/hishtory-client", "/tmp/hishtory-client.intoto.jsonl", downloadData.Version+"-"+runtime.GOOS+"-"+runtime.GOARCH)
|
2022-04-17 01:02:07 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to verify SLSA provenance of the updated binary, aborting update: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-04-17 05:50:02 +02:00
|
|
|
// Unlink the existing binary so we can overwrite it even though it is still running
|
2022-04-21 06:57:37 +02:00
|
|
|
if runtime.GOOS == "linux" {
|
|
|
|
homedir, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get user's home directory: %v", err)
|
|
|
|
}
|
|
|
|
err = syscall.Unlink(path.Join(homedir, shared.HISHTORY_PATH, "hishtory"))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to unlink %s for update: %v", path.Join(homedir, shared.HISHTORY_PATH, "hishtory"), err)
|
|
|
|
}
|
2022-04-07 03:18:46 +02:00
|
|
|
}
|
2022-04-17 05:50:02 +02:00
|
|
|
|
2022-04-26 06:42:28 +02:00
|
|
|
// On MacOS, set the xattrs containing the signatures. These are generated by an action and pushed to a github release that we download and set
|
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
err := setCodesigningXattrs(downloadData, "/tmp/hishtory-client")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to set codesigning xattrs: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-17 01:02:07 +02:00
|
|
|
// Install the new one
|
2022-04-17 05:50:02 +02:00
|
|
|
cmd := exec.Command("chmod", "+x", "/tmp/hishtory-client")
|
2022-04-21 06:57:37 +02:00
|
|
|
var stdout bytes.Buffer
|
|
|
|
cmd.Stdout = &stdout
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
cmd.Stderr = &stderr
|
2022-04-17 05:50:02 +02:00
|
|
|
err = cmd.Run()
|
|
|
|
if err != nil {
|
2022-04-21 06:57:37 +02:00
|
|
|
return fmt.Errorf("failed to chmod +x the update (out=%#v, err=%#v): %v", stdout.String(), stderr.String(), err)
|
2022-04-17 05:50:02 +02:00
|
|
|
}
|
2022-04-07 03:18:46 +02:00
|
|
|
cmd = exec.Command("/tmp/hishtory-client", "install")
|
2022-04-22 07:52:38 +02:00
|
|
|
stdout = bytes.Buffer{}
|
|
|
|
stderr = bytes.Buffer{}
|
2022-04-07 03:18:46 +02:00
|
|
|
err = cmd.Run()
|
|
|
|
if err != nil {
|
2022-04-22 07:52:38 +02:00
|
|
|
return fmt.Errorf("failed to install update (out=%#v, err=%#v): %v", stdout.String(), stderr.String(), err)
|
2022-03-30 06:56:28 +02:00
|
|
|
}
|
2022-04-17 05:50:02 +02:00
|
|
|
fmt.Printf("Successfully updated hishtory from v0.%s to %s\n", Version, downloadData.Version)
|
2022-03-30 06:56:28 +02:00
|
|
|
return nil
|
|
|
|
}
|
2022-04-05 07:07:01 +02:00
|
|
|
|
2022-04-17 21:30:46 +02:00
|
|
|
func downloadFiles(updateInfo shared.UpdateInfo) error {
|
|
|
|
clientUrl := ""
|
|
|
|
clientProvenanceUrl := ""
|
|
|
|
if runtime.GOOS == "linux" && runtime.GOARCH == "amd64" {
|
|
|
|
clientUrl = updateInfo.LinuxAmd64Url
|
|
|
|
clientProvenanceUrl = updateInfo.LinuxAmd64AttestationUrl
|
|
|
|
} else if runtime.GOOS == "darwin" && runtime.GOARCH == "amd64" {
|
|
|
|
clientUrl = updateInfo.DarwinAmd64Url
|
|
|
|
clientProvenanceUrl = updateInfo.DarwinAmd64AttestationUrl
|
|
|
|
} else if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" {
|
|
|
|
clientUrl = updateInfo.DarwinArm64Url
|
|
|
|
clientProvenanceUrl = updateInfo.DarwinArm64AttestationUrl
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("no update info found for GOOS=%s, GOARCH=%s", runtime.GOOS, runtime.GOARCH)
|
|
|
|
}
|
|
|
|
err := downloadFile("/tmp/hishtory-client", clientUrl)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = downloadFile("/tmp/hishtory-client.intoto.jsonl", clientProvenanceUrl)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-17 05:50:02 +02:00
|
|
|
func downloadFile(filename, url string) error {
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to download file at %s to %s: %v", url, filename, err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2022-04-19 06:28:41 +02:00
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return fmt.Errorf("failed to download file at %s due to resp_code=%d", url, resp.StatusCode)
|
|
|
|
}
|
2022-04-17 05:50:02 +02:00
|
|
|
|
|
|
|
out, err := os.Create(filename)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to save file to %s: %v", filename, err)
|
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(out, resp.Body)
|
2022-04-19 06:28:41 +02:00
|
|
|
|
2022-04-17 05:50:02 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-16 09:44:47 +02:00
|
|
|
func getServerHostname() string {
|
2022-04-05 07:07:01 +02:00
|
|
|
if server := os.Getenv("HISHTORY_SERVER"); server != "" {
|
|
|
|
return server
|
|
|
|
}
|
2022-04-06 08:31:24 +02:00
|
|
|
return "https://api.hishtory.dev"
|
2022-04-07 03:18:46 +02:00
|
|
|
}
|
2022-04-07 07:43:07 +02:00
|
|
|
|
2022-04-16 09:44:47 +02:00
|
|
|
var (
|
|
|
|
hishtoryLogger *log.Logger
|
|
|
|
getLoggerOnce sync.Once
|
|
|
|
)
|
|
|
|
|
2022-04-28 18:51:01 +02:00
|
|
|
func GetLogger() *log.Logger {
|
2022-04-16 09:44:47 +02:00
|
|
|
getLoggerOnce.Do(func() {
|
|
|
|
homedir, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("failed to get user's home directory: %v", err))
|
|
|
|
}
|
|
|
|
f, err := os.OpenFile(path.Join(homedir, shared.HISHTORY_PATH, "hishtory.log"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0o660)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("failed to open hishtory.log: %v", err))
|
|
|
|
}
|
|
|
|
// Purposefully not closing the file. Yes, this is a dangling file handle. But hishtory is short lived so this is okay.
|
|
|
|
hishtoryLogger = log.New(f, "\n", log.LstdFlags|log.Lshortfile)
|
|
|
|
})
|
|
|
|
return hishtoryLogger
|
|
|
|
}
|
|
|
|
|
2022-04-07 07:43:07 +02:00
|
|
|
func OpenLocalSqliteDb() (*gorm.DB, error) {
|
|
|
|
homedir, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get user's home directory: %v", err)
|
|
|
|
}
|
2022-04-08 06:40:22 +02:00
|
|
|
err = os.MkdirAll(path.Join(homedir, shared.HISHTORY_PATH), 0o744)
|
2022-04-07 07:43:07 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create ~/.hishtory dir: %v", err)
|
|
|
|
}
|
2022-04-28 18:51:01 +02:00
|
|
|
hishtoryLogger := GetLogger()
|
2022-04-15 05:18:49 +02:00
|
|
|
newLogger := logger.New(
|
2022-04-16 09:44:47 +02:00
|
|
|
hishtoryLogger,
|
2022-04-15 05:18:49 +02:00
|
|
|
logger.Config{
|
2022-04-16 09:44:47 +02:00
|
|
|
SlowThreshold: 100 * time.Millisecond,
|
|
|
|
LogLevel: logger.Warn,
|
|
|
|
IgnoreRecordNotFoundError: false,
|
2022-04-15 05:18:49 +02:00
|
|
|
Colorful: false,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
db, err := gorm.Open(sqlite.Open(path.Join(homedir, shared.HISHTORY_PATH, shared.DB_PATH)), &gorm.Config{SkipDefaultTransaction: true, Logger: newLogger})
|
2022-04-07 07:43:07 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to connect to the DB: %v", err)
|
|
|
|
}
|
|
|
|
tx, err := db.DB()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = tx.Ping()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-08 05:59:40 +02:00
|
|
|
db.AutoMigrate(&data.HistoryEntry{})
|
2022-04-21 07:13:10 +02:00
|
|
|
db.Exec("PRAGMA journal_mode = WAL")
|
2022-04-07 07:43:07 +02:00
|
|
|
return db, nil
|
|
|
|
}
|
2022-04-16 09:44:47 +02:00
|
|
|
|
|
|
|
func ApiGet(path string) ([]byte, error) {
|
|
|
|
start := time.Now()
|
|
|
|
resp, err := http.Get(getServerHostname() + path)
|
|
|
|
if err != nil {
|
2022-05-02 04:37:26 +02:00
|
|
|
return nil, fmt.Errorf("failed to GET %s%s: %v", getServerHostname(), path, err)
|
2022-04-16 09:44:47 +02:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != 200 {
|
2022-05-02 04:37:26 +02:00
|
|
|
return nil, fmt.Errorf("failed to GET %s%s: status_code=%d", getServerHostname(), path, resp.StatusCode)
|
2022-04-16 09:44:47 +02:00
|
|
|
}
|
|
|
|
respBody, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2022-05-02 04:37:26 +02:00
|
|
|
return nil, fmt.Errorf("failed to read response body from GET %s%s: %v", getServerHostname(), path, err)
|
2022-04-16 09:44:47 +02:00
|
|
|
}
|
|
|
|
duration := time.Since(start)
|
2022-04-28 18:51:01 +02:00
|
|
|
GetLogger().Printf("ApiGet(%#v): %s\n", path, duration.String())
|
2022-04-16 09:44:47 +02:00
|
|
|
return respBody, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ApiPost(path, contentType string, data []byte) ([]byte, error) {
|
|
|
|
start := time.Now()
|
|
|
|
resp, err := http.Post(getServerHostname()+path, contentType, bytes.NewBuffer(data))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to POST %s: %v", path, err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return nil, fmt.Errorf("failed to POST %s: status_code=%d", path, resp.StatusCode)
|
|
|
|
}
|
|
|
|
respBody, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to read response body from POST %s: %v", path, err)
|
|
|
|
}
|
|
|
|
duration := time.Since(start)
|
2022-04-28 18:51:01 +02:00
|
|
|
GetLogger().Printf("ApiPost(%#v): %s\n", path, duration.String())
|
2022-04-16 09:44:47 +02:00
|
|
|
return respBody, nil
|
|
|
|
}
|
2022-04-26 06:42:28 +02:00
|
|
|
|
2022-04-26 07:03:31 +02:00
|
|
|
type darwinCodeSignature struct {
|
|
|
|
Cd string `json:"cd"`
|
|
|
|
Cr string `json:"cr"`
|
|
|
|
Cr1 string `json:"cr1"`
|
|
|
|
Cs string `json:"cs"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseXattr(xattrDump string) (darwinCodeSignature, error) {
|
|
|
|
var xattr darwinCodeSignature
|
|
|
|
err := json.Unmarshal([]byte(xattrDump), &xattr)
|
|
|
|
if err != nil {
|
|
|
|
return xattr, fmt.Errorf("failed to parse xattr: %v", err)
|
2022-04-26 06:42:28 +02:00
|
|
|
}
|
2022-04-26 07:03:31 +02:00
|
|
|
return xattr, nil
|
2022-04-26 06:42:28 +02:00
|
|
|
}
|
|
|
|
|
2022-04-28 18:51:01 +02:00
|
|
|
func parseHex(input string) []byte {
|
|
|
|
input = strings.ReplaceAll(input, " ", "")
|
|
|
|
input = strings.ReplaceAll(input, "\n", "")
|
|
|
|
data, err := hex.DecodeString(input)
|
|
|
|
if err != nil {
|
|
|
|
panic("TODO: wire this up")
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
2022-04-26 06:42:28 +02:00
|
|
|
func setXattr(filename, xattrDump string) {
|
2022-04-26 07:03:31 +02:00
|
|
|
x, err := parseXattr(xattrDump)
|
2022-04-26 06:42:28 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("failed to parse xattr file: %v", err))
|
|
|
|
}
|
2022-04-28 18:51:01 +02:00
|
|
|
err = unix.Setxattr(filename, "com.apple.cs.CodeDirectory", parseHex(x.Cd), 0)
|
2022-04-26 07:03:31 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("failed to set xattr com.apple.cs.CodeDirectory on file %#v: %v", filename, err))
|
|
|
|
}
|
2022-04-28 18:51:01 +02:00
|
|
|
err = unix.Setxattr(filename, "com.apple.cs.CodeRequirements", parseHex(x.Cr), 0)
|
2022-04-26 07:03:31 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("failed to set xattr com.apple.cs.CodeRequirements on file %#v: %v", filename, err))
|
|
|
|
}
|
2022-04-28 18:51:01 +02:00
|
|
|
err = unix.Setxattr(filename, "com.apple.cs.CodeRequirements-1", parseHex(x.Cr1), 0)
|
2022-04-26 07:03:31 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("failed to set xattr com.apple.cs.CodeRequirements-1 on file %#v: %v", filename, err))
|
|
|
|
}
|
2022-04-28 18:51:01 +02:00
|
|
|
err = unix.Setxattr(filename, "com.apple.cs.CodeSignature", parseHex(x.Cs), 0)
|
2022-04-26 07:03:31 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("failed to set xattr com.apple.cs.CodeSignature on file %#v: %v", filename, err))
|
2022-04-26 06:42:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setCodesigningXattrs(downloadInfo shared.UpdateInfo, filename string) error {
|
|
|
|
if runtime.GOOS != "darwin" {
|
|
|
|
return fmt.Errorf("setCodesigningXattrs is only supported on macOS")
|
|
|
|
}
|
|
|
|
url := ""
|
|
|
|
if runtime.GOARCH == "arm64" {
|
|
|
|
url = downloadInfo.DarwinArm64Xattr
|
|
|
|
} else if runtime.GOARCH == "amd64" {
|
|
|
|
url = downloadInfo.DarwinAmd64Xattr
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("setCodesigningXattrs only supports arm64 and amd64: %#v", runtime.GOARCH)
|
|
|
|
}
|
2022-04-26 07:21:17 +02:00
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to GET %s: %v", url, err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return fmt.Errorf("failed to GET %s: status_code=%d", url, resp.StatusCode)
|
|
|
|
}
|
|
|
|
xattrDump, err := ioutil.ReadAll(resp.Body)
|
2022-04-26 06:42:28 +02:00
|
|
|
if err != nil {
|
2022-04-26 07:21:17 +02:00
|
|
|
return fmt.Errorf("failed to read response body from GET %s: %v", url, err)
|
2022-04-26 06:42:28 +02:00
|
|
|
}
|
|
|
|
setXattr(filename, string(xattrDump))
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-02 04:37:26 +02:00
|
|
|
|
|
|
|
func IsOfflineError(err error) bool {
|
|
|
|
return strings.Contains(err.Error(), "dial tcp: lookup api.hishtory.dev") || strings.Contains(err.Error(), "read: connection reset by peer")
|
|
|
|
}
|