From 52c3b13cb609b7676c812523c4fe8bea2a4574cb Mon Sep 17 00:00:00 2001 From: David Dworken Date: Sun, 9 Oct 2022 12:13:05 -0700 Subject: [PATCH] Add reupload command + include source_device_id query param when submitting entries --- client/client_test.go | 5 ++++- hishtory.go | 16 ++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/client/client_test.go b/client/client_test.go index eeae246..ab4daa7 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -245,12 +245,15 @@ func testIntegrationWithNewDevice(t *testing.T, tester shellTester) { t.Fatalf("hishtory query doesn't contain cmd run on another machine! out=%#v", out) } + // Run a reupload just to test that flow + tester.RunInteractiveShell(t, "hishtory reupload") + // Finally, test the export command out = tester.RunInteractiveShell(t, `hishtory export | grep -v pipefail | grep -v '/tmp/client install'`) if strings.Contains(out, "thisisnotrecorded") { t.Fatalf("hishtory export contains a command that should not have been recorded, out=%#v", out) } - expectedOutputWithoutKey := "hishtory status\nhishtory query\nls /a\nls /bar\nls /foo\necho foo\necho bar\nhishtory enable\necho thisisrecorded\nhishtory query\nhishtory query foo\necho hello | grep complex | sed s/h/i/g; echo baz && echo \"fo 'o\"\nhishtory query complex\nhishtory query\necho mynewcommand\nhishtory query\nyes | hishtory init %s\nhishtory query\necho mynewercommand\nhishtory query\nothercomputer\nhishtory query\n" + expectedOutputWithoutKey := "hishtory status\nhishtory query\nls /a\nls /bar\nls /foo\necho foo\necho bar\nhishtory enable\necho thisisrecorded\nhishtory query\nhishtory query foo\necho hello | grep complex | sed s/h/i/g; echo baz && echo \"fo 'o\"\nhishtory query complex\nhishtory query\necho mynewcommand\nhishtory query\nyes | hishtory init %s\nhishtory query\necho mynewercommand\nhishtory query\nothercomputer\nhishtory query\nhishtory reupload\n" expectedOutput := fmt.Sprintf(expectedOutputWithoutKey, userSecret) if diff := cmp.Diff(expectedOutput, out); diff != "" { t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out) diff --git a/hishtory.go b/hishtory.go index feaa114..64c62eb 100644 --- a/hishtory.go +++ b/hishtory.go @@ -107,6 +107,18 @@ func main() { if err != nil { log.Fatalf("Failed to update hishtory: %v", err) } + case "reupload": + // Purposefully undocumented since this command is generally not necessary to run + ctx := hctx.MakeContext() + config := hctx.GetConf(ctx) + entries, err := data.Search(hctx.GetDb(ctx), "", 0) + lib.CheckFatalError(err) + for _, entry := range entries { + jsonValue, err := lib.EncryptAndMarshal(config, entry) + lib.CheckFatalError(err) + _, err = lib.ApiPost("/api/v1/submit?source_device_id="+config.DeviceId, "application/json", jsonValue) + lib.CheckFatalError(err) + } case "-h": fallthrough case "help": @@ -265,7 +277,7 @@ func maybeUploadSkippedHistoryEntries(ctx *context.Context) error { if err != nil { return err } - _, err = lib.ApiPost("/api/v1/submit", "application/json", jsonValue) + _, err = lib.ApiPost("/api/v1/submit?source_device_id="+config.DeviceId, "application/json", jsonValue) if err != nil { // Failed to upload the history entry, so we must still be offline. So just return nil and we'll try again later. return nil @@ -303,7 +315,7 @@ func saveHistoryEntry(ctx *context.Context) { // Persist it remotely jsonValue, err := lib.EncryptAndMarshal(config, entry) lib.CheckFatalError(err) - _, err = lib.ApiPost("/api/v1/submit", "application/json", jsonValue) + _, err = lib.ApiPost("/api/v1/submit?source_device_id="+config.DeviceId, "application/json", jsonValue) if err != nil { if lib.IsOfflineError(err) { hctx.GetLogger().Printf("Failed to remotely persist hishtory entry because the device is offline!")