Configs refactor. New WIP config for activity watch like reaction server
This commit is contained in:
		@ -27,7 +27,7 @@ streams:
 | 
				
			|||||||
  ssh:
 | 
					  ssh:
 | 
				
			||||||
    # note that if the command is not in environment's `PATH`
 | 
					    # note that if the command is not in environment's `PATH`
 | 
				
			||||||
    # its full path must be given.
 | 
					    # its full path must be given.
 | 
				
			||||||
    cmd: [ "journalctl" "-fu" "sshd.service" ]
 | 
					    cmd: [ "journalctl" "-n0" "-fu" "sshd.service" ]
 | 
				
			||||||
    # filters run actions when they match regexes on a stream
 | 
					    # filters run actions when they match regexes on a stream
 | 
				
			||||||
    filters:
 | 
					    filters:
 | 
				
			||||||
      # filters have a user-defined name
 | 
					      # filters have a user-defined name
 | 
				
			||||||
@ -97,7 +97,7 @@ cat FILE | ` + bold + `reaction test-regex` + reset + ` REGEX # test REGEX again
 | 
				
			|||||||
`)
 | 
					`)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//go:embed reaction.yml
 | 
					//go:embed example.yml
 | 
				
			||||||
var exampleConf string
 | 
					var exampleConf string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func Main() {
 | 
					func Main() {
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										104
									
								
								config/activitywatch.jsonnet
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										104
									
								
								config/activitywatch.jsonnet
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,104 @@
 | 
				
			|||||||
 | 
					local directory = '~/.local/share/watch';
 | 
				
			||||||
 | 
					// Those strings will be substitued in each shell() call
 | 
				
			||||||
 | 
					local substitutions = [
 | 
				
			||||||
 | 
					  ['OUTFILE', directory + '/logs-$(date %+F)'],
 | 
				
			||||||
 | 
					  ['TMUXFILE', directory + '/tmux'],
 | 
				
			||||||
 | 
					  ['DATE', '"$(date "+%F %T")"'],
 | 
				
			||||||
 | 
					];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Substitue each substitutions' item in string
 | 
				
			||||||
 | 
					local sub(str) = std.foldl(
 | 
				
			||||||
 | 
					  (function(changedstr, kv) std.strReplace(changedstr, kv[0], kv[1])),
 | 
				
			||||||
 | 
					  substitutions,
 | 
				
			||||||
 | 
					  str
 | 
				
			||||||
 | 
					);
 | 
				
			||||||
 | 
					local shell(prg) = [
 | 
				
			||||||
 | 
					  'sh',
 | 
				
			||||||
 | 
					  '-c',
 | 
				
			||||||
 | 
					  sub(prg),
 | 
				
			||||||
 | 
					];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  // Startup is currently not implemented
 | 
				
			||||||
 | 
					  startup: shell(|||
 | 
				
			||||||
 | 
					    mkdir -p "$(dirname OUTFILE)"
 | 
				
			||||||
 | 
					    echo DATE start >> OUTFILE
 | 
				
			||||||
 | 
					    # tmux set-hook -g pane-focus-in[50] new-session -d 'echo tmux >> TMUXFILE'
 | 
				
			||||||
 | 
					  |||),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  // Stop is currently not implemented
 | 
				
			||||||
 | 
					  stop: shell(|||
 | 
				
			||||||
 | 
					    tmux set-hook -ug pane-focus-in[50]
 | 
				
			||||||
 | 
					    echo DATE stop >> OUTFILE
 | 
				
			||||||
 | 
					  |||),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  patterns: {
 | 
				
			||||||
 | 
					    all: { regex: '.*' },
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  streams: {
 | 
				
			||||||
 | 
					    // Be notified about each window focus change
 | 
				
			||||||
 | 
					    // FIXME DOESN'T WORK
 | 
				
			||||||
 | 
					    sway: {
 | 
				
			||||||
 | 
					      cmd: shell(|||
 | 
				
			||||||
 | 
					        swaymsg -rm -t subscribe "['window']" | jq -r 'select(.change == "focus") | .container | if has("app_id") and .app_id != null then .app_id else .window_properties.class end'
 | 
				
			||||||
 | 
					      |||),
 | 
				
			||||||
 | 
					      filters: {
 | 
				
			||||||
 | 
					        send: {
 | 
				
			||||||
 | 
					          regex: ['^<all>$'],
 | 
				
			||||||
 | 
					          actions: {
 | 
				
			||||||
 | 
					            send: { cmd: shell('echo DATE focus <all> >> OUTFILE') },
 | 
				
			||||||
 | 
					          },
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					      },
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Be notified when user is away
 | 
				
			||||||
 | 
					    swayidle: {
 | 
				
			||||||
 | 
					      cmd: ['swayidle', 'timeout', '60', 'echo sleep', 'resume', 'echo resume'],
 | 
				
			||||||
 | 
					      filters: {
 | 
				
			||||||
 | 
					        send: {
 | 
				
			||||||
 | 
					          regex: ['^<all>$'],
 | 
				
			||||||
 | 
					          actions: {
 | 
				
			||||||
 | 
					            send: { cmd: shell('echo DATE <all> >> OUTFILE') },
 | 
				
			||||||
 | 
					          },
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					      },
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Be notified about tmux activity
 | 
				
			||||||
 | 
					    // Limitation: can't handle multiple concurrently attached sessions
 | 
				
			||||||
 | 
					    // tmux: {
 | 
				
			||||||
 | 
					    //   cmd: shell(|||
 | 
				
			||||||
 | 
					    //     LAST_TIME="0"
 | 
				
			||||||
 | 
					    //     LAST_ACTIVITY=""
 | 
				
			||||||
 | 
					    //     while true;
 | 
				
			||||||
 | 
					    //     do
 | 
				
			||||||
 | 
					    //       NEW_TIME=$(tmux display -p '#{session_activity}')
 | 
				
			||||||
 | 
					    //       if [ -n "$NEW_TIME" ] && [ "$NEW_TIME" -gt "$LAST_TIME" ]
 | 
				
			||||||
 | 
					    //       then
 | 
				
			||||||
 | 
					    //         LAST_TIME="$NEW_TIME"
 | 
				
			||||||
 | 
					    //         NEW_ACTIVITY="$(tmux display -p '#{pane_current_command} #{pane_current_path}')"
 | 
				
			||||||
 | 
					    //         if [ -n "$NEW_ACTIVITY" ] && [ "$NEW_ACTIVITY" != "$LAST_ACTIVITY" ]
 | 
				
			||||||
 | 
					    //         then
 | 
				
			||||||
 | 
					    //           LAST_ACTIVITY="$NEW_ACTIVITY"
 | 
				
			||||||
 | 
					    //           echo "tmux $NEW_ACTIVITY"
 | 
				
			||||||
 | 
					    //         fi
 | 
				
			||||||
 | 
					    //       fi
 | 
				
			||||||
 | 
					    //       sleep 10
 | 
				
			||||||
 | 
					    //     done
 | 
				
			||||||
 | 
					    //   |||),
 | 
				
			||||||
 | 
					    //   filters: {
 | 
				
			||||||
 | 
					    //     send: {
 | 
				
			||||||
 | 
					    //       regex: ['^tmux <all>$'],
 | 
				
			||||||
 | 
					    //       actions: {
 | 
				
			||||||
 | 
					    //         send: { cmd: shell('echo DATE tmux <all> >> OUTFILE') },
 | 
				
			||||||
 | 
					    //       },
 | 
				
			||||||
 | 
					    //     },
 | 
				
			||||||
 | 
					    //   },
 | 
				
			||||||
 | 
					    // },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Be notified about firefox activity
 | 
				
			||||||
 | 
					    // TODO
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -1,6 +1,7 @@
 | 
				
			|||||||
// This file is using JSONNET, a complete configuration language based on JSON
 | 
					// This file is using JSONNET, a complete configuration language based on JSON
 | 
				
			||||||
// See https://jsonnet.org
 | 
					// See https://jsonnet.org
 | 
				
			||||||
// JSONNET is a superset of JSON, so one can write plain JSON files if wanted.
 | 
					// JSONNET is a superset of JSON, so one can write plain JSON files if wanted.
 | 
				
			||||||
 | 
					// Note that YAML is also supported.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// variables defined for later use.
 | 
					// variables defined for later use.
 | 
				
			||||||
local iptablesban = ['ip46tables', '-w', '-A', 'reaction', '1', '-s', '<ip>', '-j', 'DROP'];
 | 
					local iptablesban = ['ip46tables', '-w', '-A', 'reaction', '1', '-s', '<ip>', '-j', 'DROP'];
 | 
				
			||||||
@ -29,7 +30,7 @@ local iptablesunban = ['ip46tables', '-w', '-D', 'reaction', '1', '-s', '<ip>',
 | 
				
			|||||||
    ssh: {
 | 
					    ssh: {
 | 
				
			||||||
      // note that if the command is not in environment's `PATH`
 | 
					      // note that if the command is not in environment's `PATH`
 | 
				
			||||||
      // its full path must be given.
 | 
					      // its full path must be given.
 | 
				
			||||||
      cmd: ['journalctl', '-fu', 'sshd.service'],
 | 
					      cmd: ['journalctl', '-n0', '-fu', 'sshd.service'],
 | 
				
			||||||
      // filters run actions when they match regexes on a stream
 | 
					      // filters run actions when they match regexes on a stream
 | 
				
			||||||
      filters: {
 | 
					      filters: {
 | 
				
			||||||
        // filters have a user-defined name
 | 
					        // filters have a user-defined name
 | 
				
			||||||
							
								
								
									
										1
									
								
								config/example.yml
									
									
									
									
									
										Symbolic link
									
								
							
							
						
						
									
										1
									
								
								config/example.yml
									
									
									
									
									
										Symbolic link
									
								
							@ -0,0 +1 @@
 | 
				
			|||||||
 | 
					../app/example.yml
 | 
				
			||||||
							
								
								
									
										70
									
								
								config/heavy-load.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										70
									
								
								config/heavy-load.yml
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,70 @@
 | 
				
			|||||||
 | 
					---
 | 
				
			||||||
 | 
					patterns:
 | 
				
			||||||
 | 
					  num:
 | 
				
			||||||
 | 
					    regex: '[0-9]+'
 | 
				
			||||||
 | 
					  ip:
 | 
				
			||||||
 | 
					    regex: '(?:(?:[0-9]{1,3}\.){3}[0-9]{1,3})|(?:[0-9a-fA-F:]{2,90})'
 | 
				
			||||||
 | 
					    ignore:
 | 
				
			||||||
 | 
					      - 1.0.0.1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					streams:
 | 
				
			||||||
 | 
					  tailDown1:
 | 
				
			||||||
 | 
					    cmd: [ "sh", "-c", "sleep 2; seq 100010 | while read i; do echo found $(($i % 100)); done" ]
 | 
				
			||||||
 | 
					    filters:
 | 
				
			||||||
 | 
					      findIP:
 | 
				
			||||||
 | 
					        regex:
 | 
				
			||||||
 | 
					          - '^found <num>$'
 | 
				
			||||||
 | 
					        retry: 50
 | 
				
			||||||
 | 
					        retry-period: 1m
 | 
				
			||||||
 | 
					        actions:
 | 
				
			||||||
 | 
					          damn:
 | 
				
			||||||
 | 
					            cmd: [ "echo", "<num>" ]
 | 
				
			||||||
 | 
					          undamn:
 | 
				
			||||||
 | 
					            cmd: [ "echo", "undamn", "<num>" ]
 | 
				
			||||||
 | 
					            after: 1m
 | 
				
			||||||
 | 
					            onexit: false
 | 
				
			||||||
 | 
					  tailDown2:
 | 
				
			||||||
 | 
					    cmd: [ "sh", "-c", "sleep 2; seq 100010 | while read i; do echo prout $(($i % 100)); done" ]
 | 
				
			||||||
 | 
					    filters:
 | 
				
			||||||
 | 
					      findIP:
 | 
				
			||||||
 | 
					        regex:
 | 
				
			||||||
 | 
					          - '^prout <num>$'
 | 
				
			||||||
 | 
					        retry: 50
 | 
				
			||||||
 | 
					        retry-period: 1m
 | 
				
			||||||
 | 
					        actions:
 | 
				
			||||||
 | 
					          damn:
 | 
				
			||||||
 | 
					            cmd: [ "echo", "<num>" ]
 | 
				
			||||||
 | 
					          undamn:
 | 
				
			||||||
 | 
					            cmd: [ "echo", "undamn", "<num>" ]
 | 
				
			||||||
 | 
					            after: 1m
 | 
				
			||||||
 | 
					            onexit: false
 | 
				
			||||||
 | 
					  tailDown3:
 | 
				
			||||||
 | 
					    cmd: [ "sh", "-c", "sleep 2; seq 100010 | while read i; do echo nanana $(($i % 100)); done" ]
 | 
				
			||||||
 | 
					    filters:
 | 
				
			||||||
 | 
					      findIP:
 | 
				
			||||||
 | 
					        regex:
 | 
				
			||||||
 | 
					          - '^nanana <num>$'
 | 
				
			||||||
 | 
					        retry: 50
 | 
				
			||||||
 | 
					        retry-period: 2m
 | 
				
			||||||
 | 
					        actions:
 | 
				
			||||||
 | 
					          damn:
 | 
				
			||||||
 | 
					            cmd: [ "true" ]
 | 
				
			||||||
 | 
					          undamn:
 | 
				
			||||||
 | 
					            cmd: [ "true" ]
 | 
				
			||||||
 | 
					            after: 1m
 | 
				
			||||||
 | 
					            onexit: false
 | 
				
			||||||
 | 
					  tailDown4:
 | 
				
			||||||
 | 
					    cmd: [ "sh", "-c", "sleep 2; seq 100010 | while read i; do echo nanana $(($i % 100)); done" ]
 | 
				
			||||||
 | 
					    filters:
 | 
				
			||||||
 | 
					      findIP:
 | 
				
			||||||
 | 
					        regex:
 | 
				
			||||||
 | 
					          - '^nomatch <num>$'
 | 
				
			||||||
 | 
					        retry: 50
 | 
				
			||||||
 | 
					        retry-period: 2m
 | 
				
			||||||
 | 
					        actions:
 | 
				
			||||||
 | 
					          damn:
 | 
				
			||||||
 | 
					            cmd: [ "echo", "<num>" ]
 | 
				
			||||||
 | 
					          undamn:
 | 
				
			||||||
 | 
					            cmd: [ "echo", "undamn", "<num>" ]
 | 
				
			||||||
 | 
					            after: 1m
 | 
				
			||||||
 | 
					            onexit: false
 | 
				
			||||||
@ -1 +0,0 @@
 | 
				
			|||||||
../app/reaction.yml
 | 
					 | 
				
			||||||
		Reference in New Issue
	
	Block a user