2022-04-05 07:07:01 +02:00
package lib
2022-01-09 06:59:28 +01:00
import (
2022-09-17 20:21:42 +02:00
"bufio"
2022-03-30 06:56:28 +02:00
"bytes"
2022-09-21 07:28:40 +02:00
"context"
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"
2022-01-09 20:00:53 +01:00
"log"
2022-06-05 07:06:50 +02:00
"math/rand"
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-09-17 20:21:42 +02:00
"path/filepath"
2022-09-02 08:22:53 +02:00
"regexp"
2022-04-12 07:36:52 +02:00
"runtime"
2022-01-09 06:59:28 +01:00
"strconv"
"strings"
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-07 07:44:10 +02:00
"gorm.io/gorm"
2022-04-06 08:31:24 +02:00
2022-11-01 18:23:35 +01:00
"github.com/araddon/dateparse"
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-09-21 07:28:40 +02:00
"github.com/ddworken/hishtory/client/hctx"
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-18 04:54:17 +02:00
//go:embed config.zsh
var ConfigZshContents string
2022-10-19 04:55:41 +02:00
//go:embed config.fish
var ConfigFishContents string
2022-04-17 05:50:02 +02:00
var Version string = "Unknown"
2022-11-15 04:26:56 +01:00
var GitCommit string = "Unknown"
2022-04-17 05:50:02 +02:00
2022-09-17 20:21:42 +02:00
// 256KB ought to be enough for any reasonable cmd
var maxSupportedLineLengthForImport = 256_000
2022-09-22 05:19:11 +02:00
func getCwd ( ctx * context . Context ) ( string , string , error ) {
2023-02-20 21:42:23 +01:00
cwd , err := getCwdWithoutSubstitution ( )
2022-01-09 06:59:28 +01:00
if err != nil {
2022-09-08 08:20:31 +02:00
return "" , "" , fmt . Errorf ( "failed to get cwd for last command: %v" , err )
2022-01-09 06:59:28 +01:00
}
2022-09-22 05:19:11 +02:00
homedir := hctx . GetHome ( ctx )
2022-03-30 06:56:28 +02:00
if cwd == homedir {
2022-09-08 08:20:31 +02:00
return "~/" , homedir , nil
2022-03-30 06:56:28 +02:00
}
2022-01-09 06:59:28 +01:00
if strings . HasPrefix ( cwd , homedir ) {
2022-09-08 08:20:31 +02:00
return strings . Replace ( cwd , homedir , "~" , 1 ) , homedir , nil
2022-01-09 06:59:28 +01:00
}
2022-09-08 08:20:31 +02:00
return cwd , homedir , nil
2022-01-09 06:59:28 +01:00
}
2023-02-20 21:42:23 +01:00
func getCwdWithoutSubstitution ( ) ( string , error ) {
cwd , err := os . Getwd ( )
if err == nil {
return cwd , nil
}
// Fall back to the syscall to see if that works, as an attempt to
// fix github.com/ddworken/hishtory/issues/69
if syscall . ImplementsGetwd && false {
cwd , err = syscall . Getwd ( )
if err == nil {
return cwd , nil
}
}
return "" , err
}
2022-09-21 07:28:40 +02:00
func BuildHistoryEntry ( ctx * context . Context , args [ ] string ) ( * data . HistoryEntry , error ) {
2022-05-28 08:05:28 +02:00
if len ( args ) < 6 {
2022-11-10 01:14:44 +01:00
hctx . GetLogger ( ) . Warnf ( "BuildHistoryEntry called with args=%#v, which has too few entries! This can happen in specific edge cases for newly opened terminals and is likely not a problem." , args )
2022-05-28 08:05:28 +02:00
return nil , nil
}
2022-04-18 04:54:17 +02:00
shell := args [ 2 ]
2022-05-28 08:05:28 +02:00
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
2022-09-08 08:20:31 +02:00
// cwd and homedir
2022-09-22 05:19:11 +02:00
cwd , homedir , err := getCwd ( ctx )
2022-01-09 06:59:28 +01:00
if err != nil {
return nil , fmt . Errorf ( "failed to build history entry: %v" , err )
}
entry . CurrentWorkingDirectory = cwd
2022-09-08 08:20:31 +02:00
entry . HomeDirectory = homedir
2022-01-09 06:59:28 +01:00
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 )
}
2022-09-21 07:28:40 +02:00
shouldBeSkipped , err := shouldSkipHiddenCommand ( ctx , args [ 4 ] )
2022-04-18 04:54:17 +02:00
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-09-08 08:20:31 +02:00
cmd , err = maybeSkipBashHistTimePrefix ( cmd )
if err != nil {
return nil , err
}
entry . Command = cmd
2022-10-19 04:55:41 +02:00
} else if shell == "zsh" || shell == "fish" {
2022-04-18 06:04:44 +02:00
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-10-24 00:40:30 +02:00
if strings . TrimSpace ( entry . Command ) == "" {
// Skip recording empty commands where the user just hits enter in their terminal
return nil , nil
}
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
2022-09-21 07:28:40 +02:00
config := hctx . GetConf ( ctx )
2022-05-02 04:37:26 +02:00
entry . DeviceId = config . DeviceId
2022-10-24 07:01:53 +02:00
// custom columns
cc , err := buildCustomColumns ( ctx )
if err != nil {
return nil , err
}
entry . CustomColumns = cc
2022-01-09 06:59:28 +01:00
return & entry , nil
}
2022-10-24 07:01:53 +02:00
func buildCustomColumns ( ctx * context . Context ) ( data . CustomColumns , error ) {
ccs := data . CustomColumns { }
config := hctx . GetConf ( ctx )
for _ , cc := range config . CustomColumns {
cmd := exec . Command ( "bash" , "-c" , cc . ColumnCommand )
var stdout bytes . Buffer
cmd . Stdout = & stdout
var stderr bytes . Buffer
cmd . Stderr = & stderr
err := cmd . Start ( )
if err != nil {
return nil , fmt . Errorf ( "failed to execute custom command named %v (stdout=%#v, stderr=%#v)" , cc . ColumnName , stdout . String ( ) , stderr . String ( ) )
}
err = cmd . Wait ( )
if err != nil {
2022-11-01 22:33:27 +01:00
// Log a warning, but don't crash. This way commands can exit with a different status and still work.
2022-11-10 01:14:44 +01:00
hctx . GetLogger ( ) . Warnf ( "failed to execute custom command named %v (stdout=%#v, stderr=%#v)" , cc . ColumnName , stdout . String ( ) , stderr . String ( ) )
2022-10-24 07:01:53 +02:00
}
ccv := data . CustomColumn {
Name : cc . ColumnName ,
Val : strings . TrimSpace ( stdout . String ( ) ) ,
}
ccs = append ( ccs , ccv )
}
return ccs , nil
}
2022-11-13 01:30:59 +01:00
func stripZshWeirdness ( cmd string ) string {
// Zsh has this weird behavior where sometimes commands are saved in the hishtory file
// with a weird prefix. I've never been able to figure out why this happens, but we
// can at least strip it.
2022-09-23 03:09:51 +02:00
firstCommandBugRegex := regexp . MustCompile ( ` : \d+:\d;(.*) ` )
matches := firstCommandBugRegex . FindStringSubmatch ( cmd )
2022-11-13 01:30:59 +01:00
if len ( matches ) == 2 {
return matches [ 1 ]
}
return cmd
2022-09-23 03:09:51 +02:00
}
2022-09-28 07:30:35 +02:00
func isBashWeirdness ( cmd string ) bool {
// Bash has this weird behavior where the it has entries like `#1664342754` in the
// history file. We want to skip these.
2022-11-13 01:30:59 +01:00
firstCommandBugRegex := regexp . MustCompile ( ` ^#\d+\s+$ ` )
2022-11-13 03:46:14 +01:00
return firstCommandBugRegex . MatchString ( cmd )
2022-09-28 07:30:35 +02:00
}
2022-09-02 08:22:53 +02:00
func buildRegexFromTimeFormat ( timeFormat string ) string {
expectedRegex := ""
lastCharWasPercent := false
for _ , char := range timeFormat {
if lastCharWasPercent {
if char == '%' {
expectedRegex += regexp . QuoteMeta ( string ( char ) )
lastCharWasPercent = false
continue
} else if char == 't' {
expectedRegex += "\t"
} else if char == 'F' {
expectedRegex += buildRegexFromTimeFormat ( "%Y-%m-%d" )
} else if char == 'Y' {
expectedRegex += "[0-9]{4}"
} else if char == 'G' {
expectedRegex += "[0-9]{4}"
} else if char == 'g' {
expectedRegex += "[0-9]{2}"
} else if char == 'C' {
expectedRegex += "[0-9]{2}"
2022-09-08 08:32:25 +02:00
} else if char == 'u' || char == 'w' {
expectedRegex += "[0-9]"
2022-09-02 08:22:53 +02:00
} else if char == 'm' {
expectedRegex += "[0-9]{2}"
} else if char == 'd' {
expectedRegex += "[0-9]{2}"
} else if char == 'D' {
expectedRegex += buildRegexFromTimeFormat ( "%m/%d/%y" )
} else if char == 'T' {
expectedRegex += buildRegexFromTimeFormat ( "%H:%M:%S" )
2022-09-08 08:32:25 +02:00
} else if char == 'H' || char == 'I' || char == 'U' || char == 'V' || char == 'W' || char == 'y' || char == 'Y' {
2022-09-02 08:22:53 +02:00
expectedRegex += "[0-9]{2}"
} else if char == 'M' {
expectedRegex += "[0-9]{2}"
} else if char == 'j' {
expectedRegex += "[0-9]{3}"
2022-09-08 08:32:25 +02:00
} else if char == 'S' || char == 'm' {
2022-09-02 08:22:53 +02:00
expectedRegex += "[0-9]{2}"
} else if char == 'c' {
// Note: Specific to the POSIX locale
expectedRegex += buildRegexFromTimeFormat ( "%a %b %e %H:%M:%S %Y" )
} else if char == 'a' {
// Note: Specific to the POSIX locale
expectedRegex += "(Sun|Mon|Tue|Wed|Thu|Fri|Sat)"
} else if char == 'b' || char == 'h' {
// Note: Specific to the POSIX locale
expectedRegex += "(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)"
2022-09-08 08:32:25 +02:00
} else if char == 'e' || char == 'k' || char == 'l' {
2022-09-02 08:22:53 +02:00
expectedRegex += "[0-9 ]{2}"
2022-09-08 08:32:25 +02:00
} else if char == 'n' {
expectedRegex += "\n"
} else if char == 'p' {
expectedRegex += "(AM|PM)"
} else if char == 'P' {
expectedRegex += "(am|pm)"
} else if char == 's' {
expectedRegex += "\\d+"
} else if char == 'z' {
expectedRegex += "[+-][0-9]{4}"
} else if char == 'r' {
expectedRegex += buildRegexFromTimeFormat ( "%I:%M:%S %p" )
} else if char == 'R' {
expectedRegex += buildRegexFromTimeFormat ( "%H:%M" )
} else if char == 'x' {
expectedRegex += buildRegexFromTimeFormat ( "%m/%d/%y" )
} else if char == 'X' {
expectedRegex += buildRegexFromTimeFormat ( "%H:%M:%S" )
2022-09-02 08:22:53 +02:00
} else {
panic ( fmt . Sprintf ( "buildRegexFromTimeFormat doesn't support %%%v, please open a bug against github.com/ddworken/hishtory" , string ( char ) ) )
}
} else if char != '%' {
expectedRegex += regexp . QuoteMeta ( string ( char ) )
}
lastCharWasPercent = false
if char == '%' {
lastCharWasPercent = true
}
}
return expectedRegex
}
2022-09-08 08:20:31 +02:00
func maybeSkipBashHistTimePrefix ( cmdLine string ) ( string , error ) {
2022-06-13 06:28:19 +02:00
format := os . Getenv ( "HISTTIMEFORMAT" )
if format == "" {
2022-09-08 08:20:31 +02:00
return cmdLine , nil
2022-06-13 06:28:19 +02:00
}
2022-09-02 08:22:53 +02:00
re , err := regexp . Compile ( "^" + buildRegexFromTimeFormat ( format ) )
if err != nil {
2022-09-08 08:20:31 +02:00
return "" , fmt . Errorf ( "failed to parse regex for HISTTIMEFORMAT variable: %v" , err )
2022-06-13 06:28:19 +02:00
}
2022-09-08 08:20:31 +02:00
return re . ReplaceAllLiteralString ( cmdLine , "" ) , nil
2022-06-13 06:28:19 +02:00
}
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-10-24 00:40:30 +02:00
split := strings . SplitN ( strings . TrimSpace ( history ) , " " , 2 )
if len ( split ) <= 1 {
2022-11-13 15:42:37 +01:00
return "" , fmt . Errorf ( "got unexpected bash history line: %#v, please open a bug at github.com/ddworken/hishtory" , history )
2022-10-24 00:40:30 +02:00
}
split = strings . SplitN ( split [ 1 ] , " " , 2 )
if len ( split ) <= 1 {
2022-11-13 15:42:37 +01:00
return "" , fmt . Errorf ( "got unexpected bash history line: %#v, please open a bug at github.com/ddworken/hishtory" , history )
2022-10-24 00:40:30 +02:00
}
return split [ 1 ] , nil
2022-04-15 09:04:49 +02:00
}
2022-09-21 07:28:40 +02:00
func shouldSkipHiddenCommand ( ctx * context . Context , historyLine string ) ( bool , error ) {
config := hctx . GetConf ( ctx )
2022-04-15 09:04:49 +02:00
if config . LastSavedHistoryLine == historyLine {
return true , nil
}
config . LastSavedHistoryLine = historyLine
2022-09-21 07:28:40 +02:00
err := hctx . SetConfig ( config )
2022-04-15 09:04:49 +02:00
if err != nil {
return false , err
}
return false , nil
2022-01-09 06:59:28 +01:00
}
2022-11-15 04:26:56 +01:00
func Setup ( userSecret string , isOffline bool ) error {
if userSecret == "" {
userSecret = uuid . Must ( uuid . NewRandom ( ) ) . String ( )
2022-01-09 06:59:28 +01:00
}
fmt . Println ( "Setting secret hishtory key to " + string ( userSecret ) )
2022-04-07 07:44:10 +02:00
// Create and set the config
2022-09-21 07:28:40 +02:00
var config hctx . ClientConfig
2022-01-09 20:00:53 +01:00
config . UserSecret = userSecret
config . IsEnabled = true
2022-04-04 06:00:46 +02:00
config . DeviceId = uuid . Must ( uuid . NewRandom ( ) ) . String ( )
2022-10-24 01:51:39 +02:00
config . ControlRSearchEnabled = true
2022-11-03 21:16:45 +01:00
config . IsOffline = isOffline
2022-09-21 07:28:40 +02:00
err := hctx . SetConfig ( config )
2022-04-04 06:27:32 +02:00
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-09-21 08:30:57 +02:00
db , err := hctx . OpenLocalSqliteDb ( )
if err != nil {
return err
}
2022-04-07 07:43:07 +02:00
db . Exec ( "DELETE FROM history_entries" )
// Bootstrap from remote date
2022-11-03 21:16:45 +01:00
if config . IsOffline {
return nil
}
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 )
2022-09-08 08:20:31 +02:00
tx = tx . Where ( "home_directory = ?" , entry . HomeDirectory )
2022-04-06 08:31:24 +02:00
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 {
2022-09-05 00:40:30 +02:00
db . Create ( entry )
2022-11-16 08:20:19 +01:00
// TODO: check the error here and bubble it up
2022-04-06 08:31:24 +02:00
}
}
2022-10-27 07:11:07 +02:00
func getCustomColumnValue ( ctx * context . Context , header string , entry data . HistoryEntry ) ( string , error ) {
2022-10-26 09:35:36 +02:00
for _ , c := range entry . CustomColumns {
if strings . EqualFold ( c . Name , header ) {
return c . Val , nil
}
}
2022-10-26 09:44:26 +02:00
config := hctx . GetConf ( ctx )
for _ , c := range config . CustomColumns {
if strings . EqualFold ( c . ColumnName , header ) {
return "" , nil
}
}
2022-10-26 09:35:36 +02:00
return "" , fmt . Errorf ( "failed to find a column matching the column name %#v (is there a typo?)" , header )
}
2022-10-27 07:11:07 +02:00
func buildTableRow ( ctx * context . Context , columnNames [ ] string , entry data . HistoryEntry ) ( [ ] string , error ) {
row := make ( [ ] string , 0 )
for _ , header := range columnNames {
switch header {
case "Hostname" :
row = append ( row , entry . Hostname )
case "CWD" :
row = append ( row , entry . CurrentWorkingDirectory )
case "Timestamp" :
2022-11-12 02:17:54 +01:00
row = append ( row , entry . StartTime . Format ( hctx . GetConf ( ctx ) . TimestampFormat ) )
2022-10-27 07:11:07 +02:00
case "Runtime" :
row = append ( row , entry . EndTime . Sub ( entry . StartTime ) . Round ( time . Millisecond ) . String ( ) )
case "Exit Code" :
row = append ( row , fmt . Sprintf ( "%d" , entry . ExitCode ) )
case "Command" :
row = append ( row , entry . Command )
default :
customColumnValue , err := getCustomColumnValue ( ctx , header , entry )
if err != nil {
return nil , err
}
row = append ( row , customColumnValue )
}
}
return row , nil
}
func stringArrayToAnyArray ( arr [ ] string ) [ ] any {
ret := make ( [ ] any , 0 )
for _ , item := range arr {
ret = append ( ret , item )
}
return ret
}
2022-11-04 04:36:36 +01:00
func DisplayResults ( ctx * context . Context , results [ ] * data . HistoryEntry , numResults int ) error {
2022-10-24 05:54:46 +02:00
config := hctx . GetConf ( ctx )
2022-01-09 06:59:28 +01:00
headerFmt := color . New ( color . FgGreen , color . Underline ) . SprintfFunc ( )
2022-10-24 05:54:46 +02:00
columns := make ( [ ] any , 0 )
for _ , c := range config . DisplayedColumns {
columns = append ( columns , c )
}
tbl := table . New ( columns ... )
2022-01-09 06:59:28 +01:00
tbl . WithHeaderFormatter ( headerFmt )
2022-11-04 04:36:36 +01:00
lastCommand := ""
numRows := 0
for _ , entry := range results {
2022-11-11 16:54:00 +01:00
if entry != nil && strings . TrimSpace ( entry . Command ) == strings . TrimSpace ( lastCommand ) && config . FilterDuplicateCommands {
2022-11-04 04:36:36 +01:00
continue
}
row , err := buildTableRow ( ctx , config . DisplayedColumns , * entry )
2022-10-27 07:11:07 +02:00
if err != nil {
return err
2022-10-24 05:54:46 +02:00
}
2022-10-28 06:20:33 +02:00
tbl . AddRow ( stringArrayToAnyArray ( row ) ... )
2022-11-04 04:36:36 +01:00
numRows += 1
lastCommand = entry . Command
if numRows >= numResults {
break
}
2022-01-09 06:59:28 +01:00
}
tbl . Print ( )
2022-10-24 05:54:46 +02:00
return nil
2022-01-09 06:59:28 +01:00
}
2022-01-09 20:00:53 +01:00
2022-09-21 07:28:40 +02:00
func IsEnabled ( ctx * context . Context ) ( bool , error ) {
return hctx . GetConf ( ctx ) . IsEnabled , nil
2022-01-09 20:00:53 +01:00
}
func CheckFatalError ( err error ) {
if err != nil {
2022-04-12 07:36:52 +02:00
_ , filename , line , _ := runtime . Caller ( 1 )
2022-11-27 17:54:34 +01:00
log . Fatalf ( "hishtory v0.%s fatal error at %s:%d: %v" , Version , filename , line , err )
2022-01-09 20:00:53 +01:00
}
}
2022-01-10 00:48:20 +01:00
2022-11-13 01:30:59 +01:00
func ImportHistory ( ctx * context . Context , shouldReadStdin , force bool ) ( int , error ) {
2022-09-21 07:28:40 +02:00
config := hctx . GetConf ( ctx )
2022-11-13 01:30:59 +01:00
if config . HaveCompletedInitialImport && ! force {
2022-09-17 20:21:42 +02:00
// Don't run an import if we already have run one. This avoids importing the same entry multiple times.
2022-09-17 20:49:31 +02:00
return 0 , nil
2022-09-17 20:21:42 +02:00
}
2022-09-22 05:19:11 +02:00
homedir := hctx . GetHome ( ctx )
2022-11-13 01:39:21 +01:00
bashHistPath := filepath . Join ( homedir , ".bash_history" )
historyEntries , err := readFileToArray ( bashHistPath )
2022-09-17 20:21:42 +02:00
if err != nil {
2022-09-17 20:49:31 +02:00
return 0 , fmt . Errorf ( "failed to parse bash history: %v" , err )
2022-09-17 20:21:42 +02:00
}
2022-11-13 01:39:21 +01:00
zshHistPath := filepath . Join ( homedir , ".zsh_history" )
extraEntries , err := readFileToArray ( zshHistPath )
2022-09-17 20:21:42 +02:00
if err != nil {
2022-09-17 20:49:31 +02:00
return 0 , fmt . Errorf ( "failed to parse zsh history: %v" , err )
2022-09-17 20:21:42 +02:00
}
historyEntries = append ( historyEntries , extraEntries ... )
2022-11-04 06:32:55 +01:00
extraEntries , err = parseFishHistory ( homedir )
if err != nil {
return 0 , fmt . Errorf ( "failed to parse fish history: %v" , err )
}
historyEntries = append ( historyEntries , extraEntries ... )
2022-11-13 01:39:21 +01:00
if histfile := os . Getenv ( "HISTFILE" ) ; histfile != "" && histfile != zshHistPath && histfile != bashHistPath {
extraEntries , err := readFileToArray ( histfile )
if err != nil {
return 0 , fmt . Errorf ( "failed to parse histfile: %v" , err )
}
historyEntries = append ( historyEntries , extraEntries ... )
}
2022-10-16 18:51:52 +02:00
if shouldReadStdin {
extraEntries , err = readStdin ( )
if err != nil {
return 0 , fmt . Errorf ( "failed to read stdin: %v" , err )
}
historyEntries = append ( historyEntries , extraEntries ... )
}
2022-09-21 07:28:40 +02:00
db := hctx . GetDb ( ctx )
2022-09-17 20:21:42 +02:00
currentUser , err := user . Current ( )
if err != nil {
2022-09-17 20:49:31 +02:00
return 0 , err
2022-09-17 20:21:42 +02:00
}
hostname , err := os . Hostname ( )
if err != nil {
2022-09-17 20:49:31 +02:00
return 0 , err
2022-09-17 20:21:42 +02:00
}
for _ , cmd := range historyEntries {
2022-11-13 01:30:59 +01:00
cmd := stripZshWeirdness ( cmd )
if isBashWeirdness ( cmd ) || strings . HasPrefix ( cmd , " " ) {
2022-09-23 03:09:51 +02:00
// Skip it
continue
}
2022-09-21 08:30:57 +02:00
entry := data . HistoryEntry {
2022-09-17 20:21:42 +02:00
LocalUsername : currentUser . Name ,
Hostname : hostname ,
Command : cmd ,
CurrentWorkingDirectory : "Unknown" ,
HomeDirectory : homedir ,
2022-09-23 03:09:51 +02:00
ExitCode : 0 ,
2022-09-21 08:30:57 +02:00
StartTime : time . Now ( ) ,
EndTime : time . Now ( ) ,
2022-09-17 20:21:42 +02:00
DeviceId : config . DeviceId ,
2022-09-21 08:30:57 +02:00
}
err = ReliableDbCreate ( db , entry )
2022-09-17 20:21:42 +02:00
if err != nil {
2022-09-21 08:30:57 +02:00
return 0 , fmt . Errorf ( "failed to insert imported history entry: %v" , err )
2022-09-17 20:21:42 +02:00
}
}
2022-10-10 02:19:15 +02:00
err = Reupload ( ctx )
if err != nil {
return 0 , fmt . Errorf ( "failed to upload hishtory import: %v" , err )
}
2022-09-17 20:21:42 +02:00
config . HaveCompletedInitialImport = true
2022-09-21 07:28:40 +02:00
err = hctx . SetConfig ( config )
2022-09-17 20:21:42 +02:00
if err != nil {
2022-09-17 20:49:31 +02:00
return 0 , fmt . Errorf ( "failed to mark initial import as completed, this may lead to duplicate history entries: %v" , err )
2022-09-17 20:21:42 +02:00
}
2022-11-11 23:14:11 +01:00
// Trigger a checkpoint so that these bulk entries are added from the WAL to the main DB
db . Exec ( "PRAGMA wal_checkpoint" )
2022-09-17 20:49:31 +02:00
return len ( historyEntries ) , nil
2022-09-17 20:21:42 +02:00
}
2022-10-01 08:50:25 +02:00
func readStdin ( ) ( [ ] string , error ) {
ret := make ( [ ] string , 0 )
in := bufio . NewReader ( os . Stdin )
for {
s , err := in . ReadString ( '\n' )
if err != nil {
if err != io . EOF {
return nil , err
}
break
}
s = strings . TrimSpace ( s )
if s != "" {
ret = append ( ret , s )
}
}
return ret , nil
}
2022-11-04 06:32:55 +01:00
func parseFishHistory ( homedir string ) ( [ ] string , error ) {
lines , err := readFileToArray ( filepath . Join ( homedir , ".local/share/fish/fish_history" ) )
if err != nil {
return nil , err
}
ret := make ( [ ] string , 0 )
for _ , line := range lines {
line = strings . TrimSpace ( line )
if strings . HasPrefix ( line , "- cmd: " ) {
ret = append ( ret , strings . SplitN ( line , ": " , 2 ) [ 1 ] )
}
}
return ret , nil
}
2022-09-17 20:21:42 +02:00
func readFileToArray ( path string ) ( [ ] string , error ) {
2022-09-17 21:22:29 +02:00
if _ , err := os . Stat ( path ) ; errors . Is ( err , os . ErrNotExist ) {
return [ ] string { } , nil
}
2022-09-17 20:21:42 +02:00
file , err := os . Open ( path )
if err != nil {
return nil , err
}
defer file . Close ( )
scanner := bufio . NewScanner ( file )
buf := make ( [ ] byte , maxSupportedLineLengthForImport )
scanner . Buffer ( buf , maxSupportedLineLengthForImport )
lines := make ( [ ] string , 0 )
for scanner . Scan ( ) {
lines = append ( lines , scanner . Text ( ) )
}
if err := scanner . Err ( ) ; err != nil {
return nil , err
}
return lines , nil
}
2022-06-05 07:27:04 +02:00
func GetDownloadData ( ) ( shared . UpdateInfo , error ) {
2022-04-17 05:50:02 +02:00
respBody , err := ApiGet ( "/api/v1/download" )
if err != nil {
2022-06-05 07:27:04 +02:00
return shared . UpdateInfo { } , fmt . Errorf ( "failed to download update info: %v" , err )
2022-04-17 05:50:02 +02:00
}
var downloadData shared . UpdateInfo
err = json . Unmarshal ( respBody , & downloadData )
if err != nil {
2022-06-05 07:27:04 +02:00
return shared . UpdateInfo { } , fmt . Errorf ( "failed to parse update info: %v" , err )
}
return downloadData , nil
}
2022-11-04 02:04:16 +01:00
func getTmpClientPath ( ) string {
tmpDir := "/tmp/"
if os . Getenv ( "TMPDIR" ) != "" {
tmpDir = os . Getenv ( "TMPDIR" )
}
return path . Join ( tmpDir , "hishtory-client" )
}
2022-09-22 05:20:43 +02:00
func Update ( ctx * context . Context ) error {
2022-06-05 07:27:04 +02:00
// Download the binary
downloadData , err := GetDownloadData ( )
if err != nil {
return err
2022-04-17 05:50:02 +02:00
}
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-10-03 05:14:54 +02:00
var slsaError error
2022-05-27 08:45:08 +02:00
if runtime . GOOS == "darwin" {
2022-11-04 02:04:16 +01:00
slsaError = verifyBinaryMac ( ctx , getTmpClientPath ( ) , downloadData )
2022-05-27 08:45:08 +02:00
} else {
2022-11-04 02:04:16 +01:00
slsaError = verifyBinary ( ctx , getTmpClientPath ( ) , getTmpClientPath ( ) + ".intoto.jsonl" , downloadData . Version )
2022-05-27 08:45:08 +02:00
}
2022-10-03 05:14:54 +02:00
if slsaError != nil {
err = handleSlsaFailure ( slsaError )
if err != nil {
return err
}
2022-04-17 01:02:07 +02:00
}
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" {
2022-09-22 05:20:43 +02:00
homedir := hctx . GetHome ( ctx )
2022-12-17 07:22:57 +01:00
err = syscall . Unlink ( path . Join ( homedir , data . GetHishtoryPath ( ) , "hishtory" ) )
2022-04-21 06:57:37 +02:00
if err != nil {
2022-12-17 07:22:57 +01:00
return fmt . Errorf ( "failed to unlink %s for update: %v" , path . Join ( homedir , data . GetHishtoryPath ( ) , "hishtory" ) , err )
2022-04-21 06:57:37 +02:00
}
2022-04-07 03:18:46 +02:00
}
2022-04-17 05:50:02 +02:00
2022-04-17 01:02:07 +02:00
// Install the new one
2022-11-04 02:04:16 +01:00
cmd := exec . Command ( "chmod" , "+x" , getTmpClientPath ( ) )
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-10-24 04:29:29 +02:00
return fmt . Errorf ( "failed to chmod +x the update (stdout=%#v, stderr=%#v): %v" , stdout . String ( ) , stderr . String ( ) , err )
2022-04-17 05:50:02 +02:00
}
2022-11-04 02:04:16 +01:00
cmd = exec . Command ( getTmpClientPath ( ) , "install" )
2022-10-24 04:29:29 +02:00
cmd . Stdout = os . Stdout
2022-04-22 07:52:38 +02:00
stderr = bytes . Buffer { }
2022-10-24 04:29:29 +02:00
cmd . Stdin = os . Stdin
2022-04-07 03:18:46 +02:00
err = cmd . Run ( )
if err != nil {
2022-11-04 02:04:16 +01:00
return fmt . Errorf ( "failed to install update (stderr=%#v), is %s in a noexec directory? (if so, set the TMPDIR environment variable): %v" , stderr . String ( ) , getTmpClientPath ( ) , 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-09-22 05:22:34 +02:00
func verifyBinaryMac ( ctx * context . Context , binaryPath string , downloadData shared . UpdateInfo ) error {
2022-05-27 08:45:08 +02:00
// On Mac, binary verification is a bit more complicated since mac binaries are code
// signed. To verify a signed binary, we:
// 1. Download the unsigned binary
// 2. Strip the real signature from the signed binary and the ad-hoc signature from the unsigned binary
// 3. Assert that those binaries match
// 4. Use SLSA to verify the unsigned binary (pre-strip)
// Yes, this is complicated. But AFAICT, it is the only solution here.
// Step 1: Download the "unsigned" binary that actually has an ad-hoc signature from the
// go compiler.
unsignedBinaryPath := binaryPath + "-unsigned"
var err error = nil
if runtime . GOOS == "darwin" && runtime . GOARCH == "amd64" {
err = downloadFile ( unsignedBinaryPath , downloadData . DarwinAmd64UnsignedUrl )
} else if runtime . GOOS == "darwin" && runtime . GOARCH == "arm64" {
err = downloadFile ( unsignedBinaryPath , downloadData . DarwinArm64UnsignedUrl )
} else {
2022-06-05 07:29:07 +02:00
err = fmt . Errorf ( "verifyBinaryMac() called for the unhandled branch GOOS=%s, GOARCH=%s" , runtime . GOOS , runtime . GOARCH )
2022-05-27 08:45:08 +02:00
}
if err != nil {
return err
}
// Step 2: Create the .nosig files that have no signatures whatsoever
noSigSuffix := ".nosig"
err = stripCodeSignature ( binaryPath , binaryPath + noSigSuffix )
if err != nil {
return err
}
err = stripCodeSignature ( unsignedBinaryPath , unsignedBinaryPath + noSigSuffix )
if err != nil {
return err
}
// Step 3: Compare the binaries
err = assertIdenticalBinaries ( binaryPath + noSigSuffix , unsignedBinaryPath + noSigSuffix )
if err != nil {
return err
}
// Step 4: Use SLSA to verify the unsigned binary
2022-11-04 02:04:16 +01:00
return verifyBinary ( ctx , unsignedBinaryPath , getTmpClientPath ( ) + ".intoto.jsonl" , downloadData . Version )
2022-05-27 08:45:08 +02:00
}
func assertIdenticalBinaries ( bin1Path , bin2Path string ) error {
bin1 , err := os . ReadFile ( bin1Path )
if err != nil {
return err
}
bin2 , err := os . ReadFile ( bin2Path )
if err != nil {
return err
}
if len ( bin1 ) != len ( bin2 ) {
return fmt . Errorf ( "unsigned binaries have different lengths (len(%s)=%d, len(%s)=%d)" , bin1Path , len ( bin1 ) , bin2Path , len ( bin2 ) )
}
differences := make ( [ ] string , 0 )
2022-05-28 07:41:52 +02:00
for i := range bin1 {
2022-05-27 08:45:08 +02:00
b1 := bin1 [ i ]
b2 := bin2 [ i ]
if b1 != b2 {
differences = append ( differences , fmt . Sprintf ( "diff at index %d: %s[%d]=%x, %s[%d]=%x" , i , bin1Path , i , b1 , bin2Path , i , b2 ) )
}
}
for _ , d := range differences {
2022-11-10 01:14:44 +01:00
hctx . GetLogger ( ) . Infof ( "comparing binaries: %#v\n" , d )
2022-05-27 08:45:08 +02:00
}
2022-05-28 07:41:52 +02:00
if len ( differences ) > 5 {
2022-05-27 08:45:08 +02:00
return fmt . Errorf ( "found %d differences in the binary" , len ( differences ) )
}
return nil
}
func stripCodeSignature ( inPath , outPath string ) error {
_ , err := exec . LookPath ( "codesign_allocate" )
if err != nil {
return fmt . Errorf ( "your system is missing the codesign_allocate tool, so we can't verify the SLSA attestation (you can bypass this by setting `export HISHTORY_DISABLE_SLSA_ATTESTATION=true` in your shell)" )
}
cmd := exec . Command ( "codesign_allocate" , "-i" , inPath , "-o" , outPath , "-r" )
var stdout bytes . Buffer
cmd . Stdout = & stdout
var stderr bytes . Buffer
cmd . Stderr = & stderr
err = cmd . Run ( )
if err != nil {
2022-09-17 20:21:42 +02:00
return fmt . Errorf ( "failed to use codesign_allocate to strip signatures on binary=%v (stdout=%#v, stderr%#v): %v" , inPath , stdout . String ( ) , stderr . String ( ) , err )
2022-05-27 08:45:08 +02:00
}
return nil
}
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
2022-12-12 05:39:45 +01:00
} else if runtime . GOOS == "linux" && runtime . GOARCH == "arm64" {
clientUrl = updateInfo . LinuxArm64Url
clientProvenanceUrl = updateInfo . LinuxArm64AttestationUrl
2023-02-19 07:00:39 +01:00
} else if runtime . GOOS == "linux" && runtime . GOARCH == "arm" {
clientUrl = updateInfo . LinuxArm7Url
clientProvenanceUrl = updateInfo . LinuxArm7AttestationUrl
2022-04-17 21:30:46 +02:00
} 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 )
}
2022-11-04 02:04:16 +01:00
err := downloadFile ( getTmpClientPath ( ) , clientUrl )
2022-04-17 21:30:46 +02:00
if err != nil {
return err
}
2022-11-04 02:04:16 +01:00
err = downloadFile ( getTmpClientPath ( ) + ".intoto.jsonl" , clientProvenanceUrl )
2022-04-17 21:30:46 +02:00
if err != nil {
return err
}
return nil
}
2022-04-17 05:50:02 +02:00
func downloadFile ( filename , url string ) error {
2022-09-17 20:21:42 +02:00
// Download the data
2022-04-17 05:50:02 +02:00
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
2022-09-17 20:21:42 +02:00
// Delete the file if it already exists. This is necessary due to https://openradar.appspot.com/FB8735191
if _ , err := os . Stat ( filename ) ; err == nil {
err = os . Remove ( filename )
if err != nil {
return fmt . Errorf ( "failed to delete file %v when trying to download a new version" , filename )
}
}
// Create the file
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-10-01 18:50:06 +02:00
func httpClient ( ) * http . Client {
return & http . Client { }
}
2022-04-16 09:44:47 +02:00
func ApiGet ( path string ) ( [ ] byte , error ) {
2022-09-05 03:37:46 +02:00
if os . Getenv ( "HISHTORY_SIMULATE_NETWORK_ERROR" ) != "" {
return nil , fmt . Errorf ( "simulated network error: dial tcp: lookup api.hishtory.dev" )
}
2022-04-16 09:44:47 +02:00
start := time . Now ( )
2022-10-01 18:50:06 +02:00
req , err := http . NewRequest ( "GET" , getServerHostname ( ) + path , nil )
if err != nil {
return nil , fmt . Errorf ( "failed to create GET: %v" , err )
}
req . Header . Set ( "X-Hishtory-Version" , "v0." + Version )
resp , err := httpClient ( ) . Do ( req )
2022-04-16 09:44:47 +02:00
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
}
2022-11-27 20:59:06 +01:00
respBody , err := io . ReadAll ( resp . Body )
2022-04-16 09:44:47 +02:00
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-11-10 01:14:44 +01:00
hctx . GetLogger ( ) . Infof ( "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 ) {
2022-09-05 03:37:46 +02:00
if os . Getenv ( "HISHTORY_SIMULATE_NETWORK_ERROR" ) != "" {
return nil , fmt . Errorf ( "simulated network error: dial tcp: lookup api.hishtory.dev" )
}
2022-04-16 09:44:47 +02:00
start := time . Now ( )
2022-10-01 18:50:06 +02:00
req , err := http . NewRequest ( "POST" , getServerHostname ( ) + path , bytes . NewBuffer ( data ) )
if err != nil {
return nil , fmt . Errorf ( "failed to create POST: %v" , err )
}
req . Header . Set ( "Content-Type" , contentType )
req . Header . Set ( "X-Hishtory-Version" , "v0." + Version )
resp , err := httpClient ( ) . Do ( req )
2022-04-16 09:44:47 +02:00
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 )
}
2022-11-27 20:59:06 +01:00
respBody , err := io . ReadAll ( resp . Body )
2022-04-16 09:44:47 +02:00
if err != nil {
return nil , fmt . Errorf ( "failed to read response body from POST %s: %v" , path , err )
}
duration := time . Since ( start )
2022-11-10 01:14:44 +01:00
hctx . GetLogger ( ) . Infof ( "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-05-02 04:37:26 +02:00
func IsOfflineError ( err error ) bool {
2022-09-18 18:42:24 +02:00
if err == nil {
return false
}
2022-09-30 07:43:03 +02:00
return strings . Contains ( err . Error ( ) , "dial tcp: lookup api.hishtory.dev" ) ||
strings . Contains ( err . Error ( ) , "connect: network is unreachable" ) ||
strings . Contains ( err . Error ( ) , "read: connection reset by peer" ) ||
2022-10-15 19:12:18 +02:00
strings . Contains ( err . Error ( ) , ": EOF" ) ||
strings . Contains ( err . Error ( ) , ": status_code=502" ) ||
2022-10-18 20:52:52 +02:00
strings . Contains ( err . Error ( ) , ": status_code=503" ) ||
2022-11-15 01:29:55 +01:00
strings . Contains ( err . Error ( ) , ": i/o timeout" ) ||
2022-12-10 18:43:02 +01:00
strings . Contains ( err . Error ( ) , "connect: operation timed out" ) ||
strings . Contains ( err . Error ( ) , "net/http: TLS handshake timeout" )
2022-05-02 04:37:26 +02:00
}
2022-06-05 07:06:50 +02:00
func ReliableDbCreate ( db * gorm . DB , entry interface { } ) error {
var err error = nil
i := 0
for i = 0 ; i < 10 ; i ++ {
result := db . Create ( entry )
err = result . Error
if err != nil {
errMsg := err . Error ( )
if errMsg == "database is locked (5) (SQLITE_BUSY)" || errMsg == "database is locked (261)" {
time . Sleep ( time . Duration ( i * rand . Intn ( 100 ) ) * time . Millisecond )
continue
}
2022-09-21 08:30:57 +02:00
if strings . Contains ( errMsg , "UNIQUE constraint failed" ) {
2022-06-05 07:06:50 +02:00
if i == 0 {
return err
} else {
return nil
}
}
2022-11-03 21:16:45 +01:00
return fmt . Errorf ( "unrecoverable sqlite error: %v" , err )
2022-06-05 07:06:50 +02:00
}
if err != nil && err . Error ( ) != "database is locked (5) (SQLITE_BUSY)" {
2022-11-03 21:16:45 +01:00
return fmt . Errorf ( "unrecoverable sqlite error: %v" , err )
2022-06-05 07:06:50 +02:00
}
}
return fmt . Errorf ( "failed to create DB entry even with %d retries: %v" , i , err )
}
2022-09-05 03:37:46 +02:00
2022-10-10 02:10:11 +02:00
func EncryptAndMarshal ( config hctx . ClientConfig , entries [ ] * data . HistoryEntry ) ( [ ] byte , error ) {
var encEntries [ ] shared . EncHistoryEntry
for _ , entry := range entries {
encEntry , err := data . EncryptHistoryEntry ( config . UserSecret , * entry )
if err != nil {
return nil , fmt . Errorf ( "failed to encrypt history entry" )
}
encEntry . DeviceId = config . DeviceId
encEntries = append ( encEntries , encEntry )
2022-09-05 03:37:46 +02:00
}
2022-10-10 02:10:11 +02:00
jsonValue , err := json . Marshal ( encEntries )
2022-09-05 03:37:46 +02:00
if err != nil {
return jsonValue , fmt . Errorf ( "failed to marshal encrypted history entry: %v" , err )
}
return jsonValue , nil
}
2022-09-20 07:49:48 +02:00
2022-10-10 02:10:11 +02:00
func Reupload ( ctx * context . Context ) error {
config := hctx . GetConf ( ctx )
2022-11-03 21:16:45 +01:00
if config . IsOffline {
return nil
}
2022-11-01 18:23:35 +01:00
entries , err := Search ( ctx , hctx . GetDb ( ctx ) , "" , 0 )
2022-10-10 02:10:11 +02:00
if err != nil {
return fmt . Errorf ( "failed to reupload due to failed search: %v" , err )
}
2022-11-26 19:31:43 +01:00
for _ , chunk := range shared . Chunks ( entries , 100 ) {
2022-10-11 07:04:59 +02:00
jsonValue , err := EncryptAndMarshal ( config , chunk )
if err != nil {
return fmt . Errorf ( "failed to reupload due to failed encryption: %v" , err )
}
_ , err = ApiPost ( "/api/v1/submit?source_device_id=" + config . DeviceId , "application/json" , jsonValue )
if err != nil {
return fmt . Errorf ( "failed to reupload due to failed POST: %v" , err )
}
2022-10-10 02:10:11 +02:00
}
return nil
}
2022-10-11 07:04:59 +02:00
2022-10-16 18:22:34 +02:00
func RetrieveAdditionalEntriesFromRemote ( ctx * context . Context ) error {
db := hctx . GetDb ( ctx )
config := hctx . GetConf ( ctx )
2022-11-03 21:16:45 +01:00
if config . IsOffline {
return nil
}
2022-10-16 18:22:34 +02:00
respBody , err := ApiGet ( "/api/v1/query?device_id=" + config . DeviceId + "&user_id=" + data . UserId ( config . UserSecret ) )
if IsOfflineError ( err ) {
return nil
}
if err != nil {
return err
}
var retrievedEntries [ ] * shared . EncHistoryEntry
err = json . Unmarshal ( respBody , & retrievedEntries )
if err != nil {
return fmt . Errorf ( "failed to load JSON response: %v" , err )
}
for _ , entry := range retrievedEntries {
decEntry , err := data . DecryptHistoryEntry ( config . UserSecret , * entry )
if err != nil {
return fmt . Errorf ( "failed to decrypt history entry from server: %v" , err )
}
AddToDbIfNew ( db , decEntry )
}
return ProcessDeletionRequests ( ctx )
}
func ProcessDeletionRequests ( ctx * context . Context ) error {
config := hctx . GetConf ( ctx )
2022-11-03 21:16:45 +01:00
if config . IsOffline {
return nil
}
2022-10-16 18:22:34 +02:00
resp , err := ApiGet ( "/api/v1/get-deletion-requests?user_id=" + data . UserId ( config . UserSecret ) + "&device_id=" + config . DeviceId )
if IsOfflineError ( err ) {
return nil
}
if err != nil {
return err
}
var deletionRequests [ ] * shared . DeletionRequest
err = json . Unmarshal ( resp , & deletionRequests )
if err != nil {
return err
}
db := hctx . GetDb ( ctx )
for _ , request := range deletionRequests {
for _ , entry := range request . Messages . Ids {
res := db . Where ( "device_id = ? AND end_time = ?" , entry . DeviceId , entry . Date ) . Delete ( & data . HistoryEntry { } )
if res . Error != nil {
return fmt . Errorf ( "DB error: %v" , res . Error )
}
}
}
return nil
}
2022-11-15 04:26:56 +01:00
func GetBanner ( ctx * context . Context ) ( [ ] byte , error ) {
2022-10-16 18:22:34 +02:00
config := hctx . GetConf ( ctx )
2022-11-03 21:16:45 +01:00
if config . IsOffline {
return [ ] byte { } , nil
}
2022-11-15 04:26:56 +01:00
url := "/api/v1/banner?commit_hash=" + GitCommit + "&user_id=" + data . UserId ( config . UserSecret ) + "&device_id=" + config . DeviceId + "&version=" + Version + "&forced_banner=" + os . Getenv ( "FORCED_BANNER" )
2022-10-16 18:22:34 +02:00
return ApiGet ( url )
}
2022-10-24 02:35:02 +02:00
2022-11-01 18:23:35 +01:00
func parseTimeGenerously ( input string ) ( time . Time , error ) {
input = strings . ReplaceAll ( input , "_" , " " )
return dateparse . ParseLocal ( input )
}
func MakeWhereQueryFromSearch ( ctx * context . Context , db * gorm . DB , query string ) ( * gorm . DB , error ) {
tokens , err := tokenize ( query )
if err != nil {
return nil , fmt . Errorf ( "failed to tokenize query: %v" , err )
}
tx := db . Model ( & data . HistoryEntry { } ) . Where ( "true" )
for _ , token := range tokens {
if strings . HasPrefix ( token , "-" ) {
2023-02-19 06:46:51 +01:00
if token == "-" {
// The entire token is a -, just ignore this token. Otherwise we end up
// interpreting "-" as exluding literally all results which is pretty useless.
continue
}
2023-02-04 08:55:55 +01:00
if containsUnescaped ( token , ":" ) {
2022-11-01 18:23:35 +01:00
query , v1 , v2 , err := parseAtomizedToken ( ctx , token [ 1 : ] )
if err != nil {
return nil , err
}
tx = tx . Where ( "NOT " + query , v1 , v2 )
} else {
query , v1 , v2 , v3 , err := parseNonAtomizedToken ( token [ 1 : ] )
if err != nil {
return nil , err
}
tx = tx . Where ( "NOT " + query , v1 , v2 , v3 )
}
2023-02-04 08:55:55 +01:00
} else if containsUnescaped ( token , ":" ) {
2022-11-01 18:23:35 +01:00
query , v1 , v2 , err := parseAtomizedToken ( ctx , token )
if err != nil {
return nil , err
}
tx = tx . Where ( query , v1 , v2 )
} else {
query , v1 , v2 , v3 , err := parseNonAtomizedToken ( token )
if err != nil {
return nil , err
}
tx = tx . Where ( query , v1 , v2 , v3 )
}
}
return tx , nil
}
func Search ( ctx * context . Context , db * gorm . DB , query string , limit int ) ( [ ] * data . HistoryEntry , error ) {
if ctx == nil && query != "" {
return nil , fmt . Errorf ( "lib.Search called with a nil context and a non-empty query (this should never happen)" )
}
tx , err := MakeWhereQueryFromSearch ( ctx , db , query )
if err != nil {
return nil , err
}
tx = tx . Order ( "end_time DESC" )
if limit > 0 {
tx = tx . Limit ( limit )
}
var historyEntries [ ] * data . HistoryEntry
result := tx . Find ( & historyEntries )
if result . Error != nil {
return nil , fmt . Errorf ( "DB query error: %v" , result . Error )
}
return historyEntries , nil
}
func parseNonAtomizedToken ( token string ) ( string , interface { } , interface { } , interface { } , error ) {
2023-02-14 07:26:02 +01:00
wildcardedToken := "%" + stripBackslash ( token ) + "%"
2022-11-01 18:23:35 +01:00
return "(command LIKE ? OR hostname LIKE ? OR current_working_directory LIKE ?)" , wildcardedToken , wildcardedToken , wildcardedToken , nil
}
func parseAtomizedToken ( ctx * context . Context , token string ) ( string , interface { } , interface { } , error ) {
2023-02-04 08:55:55 +01:00
splitToken := splitEscaped ( token , ':' , 2 )
2023-02-14 07:26:02 +01:00
field := stripBackslash ( splitToken [ 0 ] )
val := stripBackslash ( splitToken [ 1 ] )
2022-11-01 18:23:35 +01:00
switch field {
case "user" :
return "(local_username = ?)" , val , nil , nil
case "host" :
fallthrough
case "hostname" :
return "(instr(hostname, ?) > 0)" , val , nil , nil
case "cwd" :
return "(instr(current_working_directory, ?) > 0 OR instr(REPLACE(current_working_directory, '~/', home_directory), ?) > 0)" , strings . TrimSuffix ( val , "/" ) , strings . TrimSuffix ( val , "/" ) , nil
case "exit_code" :
return "(exit_code = ?)" , val , nil , nil
case "before" :
t , err := parseTimeGenerously ( val )
if err != nil {
return "" , nil , nil , fmt . Errorf ( "failed to parse before:%s as a timestamp: %v" , val , err )
}
return "(CAST(strftime(\"%s\",start_time) AS INTEGER) < ?)" , t . Unix ( ) , nil , nil
case "after" :
t , err := parseTimeGenerously ( val )
if err != nil {
return "" , nil , nil , fmt . Errorf ( "failed to parse after:%s as a timestamp: %v" , val , err )
}
return "(CAST(strftime(\"%s\",start_time) AS INTEGER) > ?)" , t . Unix ( ) , nil , nil
default :
knownCustomColumns := make ( [ ] string , 0 )
// Get custom columns that are defined on this machine
conf := hctx . GetConf ( ctx )
for _ , c := range conf . CustomColumns {
knownCustomColumns = append ( knownCustomColumns , c . ColumnName )
}
// Also get all ones that are in the DB
names , err := getAllCustomColumnNames ( ctx )
if err != nil {
return "" , nil , nil , fmt . Errorf ( "failed to get custom column names from the DB: %v" , err )
}
knownCustomColumns = append ( knownCustomColumns , names ... )
// Check if the atom is for a custom column that exists and if it isn't, return an error
isCustomColumn := false
for _ , ccName := range knownCustomColumns {
if ccName == field {
isCustomColumn = true
}
}
if ! isCustomColumn {
2022-11-27 17:54:34 +01:00
return "" , nil , nil , fmt . Errorf ( "search query contains unknown search atom '%s' that doesn't match any column names" , field )
2022-11-01 18:23:35 +01:00
}
// Build the where clause for the custom column
return "EXISTS (SELECT 1 FROM json_each(custom_columns) WHERE json_extract(value, '$.name') = ? and instr(json_extract(value, '$.value'), ?) > 0)" , field , val , nil
}
}
func getAllCustomColumnNames ( ctx * context . Context ) ( [ ] string , error ) {
db := hctx . GetDb ( ctx )
query := `
SELECT DISTINCT json_extract ( value , ' $ . name ' ) as cc_name
FROM history_entries
JOIN json_each ( custom_columns )
WHERE value IS NOT NULL
LIMIT 10 `
rows , err := db . Raw ( query ) . Rows ( )
if err != nil {
return nil , err
}
ccNames := make ( [ ] string , 0 )
for rows . Next ( ) {
var ccName string
err = rows . Scan ( & ccName )
if err != nil {
return nil , err
}
ccNames = append ( ccNames , ccName )
}
return ccNames , nil
}
func tokenize ( query string ) ( [ ] string , error ) {
if query == "" {
return [ ] string { } , nil
}
2023-02-04 08:55:55 +01:00
return splitEscaped ( query , ' ' , - 1 ) , nil
}
2023-02-04 18:58:27 +01:00
func splitEscaped ( query string , separator rune , maxSplit int ) [ ] string {
var token [ ] rune
2023-02-04 08:55:55 +01:00
var tokens [ ] string
2023-02-04 18:58:27 +01:00
splits := 1
runeQuery := [ ] rune ( query )
for i := 0 ; i < len ( runeQuery ) ; i ++ {
if ( maxSplit < 0 || splits < maxSplit ) && runeQuery [ i ] == separator {
2023-02-04 08:55:55 +01:00
tokens = append ( tokens , string ( token ) )
token = token [ : 0 ]
splits ++
2023-02-04 18:58:27 +01:00
} else if runeQuery [ i ] == '\\' && i + 1 < len ( runeQuery ) {
token = append ( token , runeQuery [ i ] , runeQuery [ i + 1 ] )
2023-02-04 08:55:55 +01:00
i ++
} else {
2023-02-04 18:58:27 +01:00
token = append ( token , runeQuery [ i ] )
2023-02-04 08:55:55 +01:00
}
}
tokens = append ( tokens , string ( token ) )
return tokens
}
func containsUnescaped ( query string , token string ) bool {
2023-02-04 18:58:27 +01:00
runeQuery := [ ] rune ( query )
for i := 0 ; i < len ( runeQuery ) ; i ++ {
if runeQuery [ i ] == '\\' && i + 1 < len ( runeQuery ) {
2023-02-04 08:55:55 +01:00
i ++
2023-02-04 18:58:27 +01:00
} else if string ( runeQuery [ i : i + len ( token ) ] ) == token {
2023-02-04 08:55:55 +01:00
return true
}
}
return false
}
2023-02-14 07:26:02 +01:00
func stripBackslash ( query string ) string {
2023-02-04 18:58:27 +01:00
var newQuery [ ] rune
2023-02-14 17:47:38 +01:00
for _ , char := range query {
if char != '\\' {
newQuery = append ( newQuery , char )
2023-02-04 08:55:55 +01:00
}
}
return string ( newQuery )
2022-11-01 18:23:35 +01:00
}
2022-11-03 03:41:49 +01:00
2022-11-15 04:26:56 +01:00
func GetDumpRequests ( config hctx . ClientConfig ) ( [ ] * shared . DumpRequest , error ) {
if config . IsOffline {
return make ( [ ] * shared . DumpRequest , 0 ) , nil
}
resp , err := ApiGet ( "/api/v1/get-dump-requests?user_id=" + data . UserId ( config . UserSecret ) + "&device_id=" + config . DeviceId )
if IsOfflineError ( err ) {
return [ ] * shared . DumpRequest { } , nil
}
if err != nil {
return nil , err
}
var dumpRequests [ ] * shared . DumpRequest
err = json . Unmarshal ( resp , & dumpRequests )
return dumpRequests , err
}
2022-12-18 09:19:52 +01:00
func SendDeletionRequest ( deletionRequest shared . DeletionRequest ) error {
data , err := json . Marshal ( deletionRequest )
if err != nil {
return err
}
_ , err = ApiPost ( "/api/v1/add-deletion-request" , "application/json" , data )
if err != nil {
return fmt . Errorf ( "failed to send deletion request to backend service, this may cause commands to not get deleted on other instances of hishtory: %v" , err )
}
return nil
}