Close #2
This commit is contained in:
		
							
								
								
									
										28
									
								
								conf.go
									
									
									
									
									
								
							
							
						
						
									
										28
									
								
								conf.go
									
									
									
									
									
								
							@ -6,6 +6,7 @@ import (
 | 
				
			|||||||
	"log"
 | 
						"log"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"regexp"
 | 
						"regexp"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"gopkg.in/yaml.v3"
 | 
						"gopkg.in/yaml.v3"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
@ -21,30 +22,51 @@ type Stream struct {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
type Filter struct {
 | 
					type Filter struct {
 | 
				
			||||||
	Regex         []string
 | 
						Regex         []string
 | 
				
			||||||
	compiledRegex []regexp.Regexp
 | 
						compiledRegex []regexp.Regexp // Computed after YAML parsing
 | 
				
			||||||
	Retry         uint
 | 
						Retry         uint
 | 
				
			||||||
	RetryPeriod   string `yaml:"retry-period"`
 | 
						RetryPeriod   string `yaml:"retry-period"`
 | 
				
			||||||
 | 
						retryDuration time.Duration // Computed after YAML parsing
 | 
				
			||||||
	Actions       map[string]*Action
 | 
						Actions       map[string]*Action
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Action struct {
 | 
					type Action struct {
 | 
				
			||||||
	name, filterName, streamName string
 | 
						name, filterName, streamName string // Computed after YAML parsing
 | 
				
			||||||
	Cmd                          []string
 | 
						Cmd                          []string
 | 
				
			||||||
	After                        string `yaml:",omitempty"`
 | 
						After                        string `yaml:",omitempty"`
 | 
				
			||||||
 | 
						afterDuration                time.Duration // Computed after YAML parsing
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (c *Conf) setup() {
 | 
					func (c *Conf) setup() {
 | 
				
			||||||
	for streamName, stream := range c.Streams {
 | 
						for streamName, stream := range c.Streams {
 | 
				
			||||||
		for filterName, filter := range stream.Filters {
 | 
							for filterName, filter := range stream.Filters {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// Parse Duration
 | 
				
			||||||
 | 
								retryDuration, err := time.ParseDuration(filter.RetryPeriod)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									log.Fatalln("Failed to parse time in configuration file:", err)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								filter.retryDuration = retryDuration
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			// Compute Regexes
 | 
								// Compute Regexes
 | 
				
			||||||
			for _, regex := range filter.Regex {
 | 
								for _, regex := range filter.Regex {
 | 
				
			||||||
				filter.compiledRegex = append(filter.compiledRegex, *regexp.MustCompile(regex))
 | 
									filter.compiledRegex = append(filter.compiledRegex, *regexp.MustCompile(regex))
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			// Give all relevant infos to Actions
 | 
					
 | 
				
			||||||
			for actionName, action := range filter.Actions {
 | 
								for actionName, action := range filter.Actions {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									// Give all relevant infos to Actions
 | 
				
			||||||
				action.name = actionName
 | 
									action.name = actionName
 | 
				
			||||||
				action.filterName = filterName
 | 
									action.filterName = filterName
 | 
				
			||||||
				action.streamName = streamName
 | 
									action.streamName = streamName
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									// Parse Duration
 | 
				
			||||||
 | 
									if action.After != "" {
 | 
				
			||||||
 | 
										afterDuration, err := time.ParseDuration(action.After)
 | 
				
			||||||
 | 
										if err != nil {
 | 
				
			||||||
 | 
											log.Fatalln("Failed to parse time in configuration file:", err)
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
										action.afterDuration = afterDuration
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										6
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								main.go
									
									
									
									
									
								
							@ -3,6 +3,7 @@ package main
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"bufio"
 | 
						"bufio"
 | 
				
			||||||
	"log"
 | 
						"log"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
	"os/exec"
 | 
						"os/exec"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -49,7 +50,10 @@ func (f *Filter) launch(line *string) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (a *Action) launch(line *string) {
 | 
					func (a *Action) launch(line *string) {
 | 
				
			||||||
	log.Printf("INFO %s.%s.%s: line {%s} → run {%s}\n", a.streamName, a.filterName, a.name, *line, a.Cmd)
 | 
						if a.afterDuration != 0 {
 | 
				
			||||||
 | 
							time.Sleep(a.afterDuration)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						log.Printf("INFO %s.%s.%s: line {%s} → run %s\n", a.streamName, a.filterName, a.name, *line, a.Cmd)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	cmd := exec.Command(a.Cmd[0], a.Cmd[1:]...)
 | 
						cmd := exec.Command(a.Cmd[0], a.Cmd[1:]...)
 | 
				
			||||||
	if ret := cmd.Run(); ret != nil {
 | 
						if ret := cmd.Run(); ret != nil {
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										11
									
								
								reaction.yml
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								reaction.yml
									
									
									
									
									
								
							@ -21,14 +21,3 @@ streams:
 | 
				
			|||||||
          sleepdamn:
 | 
					          sleepdamn:
 | 
				
			||||||
            cmd: [ "echo", "sleepDAMN" ]
 | 
					            cmd: [ "echo", "sleepDAMN" ]
 | 
				
			||||||
            after: 2s
 | 
					            after: 2s
 | 
				
			||||||
 | 
					 | 
				
			||||||
  # - cmd: journalctl -fu phpfpm-nextcloud.service
 | 
					 | 
				
			||||||
  #   filters:
 | 
					 | 
				
			||||||
  #     - regex:
 | 
					 | 
				
			||||||
  #         - '"message":"Login failed: .\+ (Remote IP: <ip>)"'
 | 
					 | 
				
			||||||
  #       retry: 3
 | 
					 | 
				
			||||||
  #       retry-period: 1h
 | 
					 | 
				
			||||||
  #       actions:
 | 
					 | 
				
			||||||
  #         - cmd: *iptablesban
 | 
					 | 
				
			||||||
  #         - cmd: *iptablesunban 
 | 
					 | 
				
			||||||
  #           after: 1h
 | 
					 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user