Skip to content

Commit

Permalink
Merge pull request #10 from TibebeJS/develop
Browse files Browse the repository at this point in the history
initial work on comments to pass linter and for go-docs ref.
  • Loading branch information
TibebeJS authored Oct 30, 2021
2 parents 10c4a49 + 0322c33 commit fb4dc50
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 27 deletions.
24 changes: 24 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"github.com/spf13/viper"
)

// Configurations - struct
type Configurations struct {
Targets []TargetConfigurations
Notifications NotificationConfigurations
}

// TargetConfigurations - Each Target configuration
type TargetConfigurations struct {
Name string
Ip string
Expand All @@ -20,53 +22,65 @@ type TargetConfigurations struct {
Rules []RuleConfiguration
}

// RuleConfiguration - Rule configuration
type RuleConfiguration struct {
Failures string
Notify []interface{}
}

// PortConfigurations - Port configuration
type PortConfigurations struct {
Port uint64
Notify []interface{}
}

// TelegramConfigurations - Telegram configuration
type TelegramConfigurations struct {
Bots []TelegramBotConfiguration
Chats []TelegramChatConfiguration
TelegramBotsMap map[string]TelegramBotConfiguration
TelegramChatsMap map[string]TelegramChatConfiguration
}

// TelegramBotConfiguration - Telegram Bot configuration
type TelegramBotConfiguration struct {
Name string
Token string
}

// TelegramChatConfiguration - Telegram Chat configuration
type TelegramChatConfiguration struct {
Name string
ChatId int64
}

// SlackConfigurations - Slack configuration
type SlackConfigurations struct {
Apps []SlackAppConfiguration
Channels []SlackChannelConfiguration
SlackAppsMap map[string]SlackAppConfiguration
SlackChannelsMap map[string]SlackChannelConfiguration
}

// SlackAppConfiguration - Slack App configuration
type SlackAppConfiguration struct {
Name string
Token string
}

// SlackChannelConfiguration - Slack Channel configuration
type SlackChannelConfiguration struct {
Name string
ChannelId string
}

// EmailConfig - Email configuration
type EmailConfig struct {
SmtpConfigsMap map[string]SmtpConfiguration
Smtp []SmtpConfiguration
}

// SmtpConfiguration - Smtp configuration
type SmtpConfiguration struct {
Name string
Sender string
Expand All @@ -75,50 +89,58 @@ type SmtpConfiguration struct {
Port uint64
}

// SmtpAuthConfiguration - Smtp authentication configuration
type SmtpAuthConfiguration struct {
Username string
Password string
}

// EmailRecipientConfiguration - Smtp email recipient configuration
type EmailRecipientConfiguration struct {
To string
Subject string
}

// NotificationConfigurations - Notification configuration
type NotificationConfigurations struct {
Telegram TelegramConfigurations
Slack SlackConfigurations
Webhook []WebHookConfigurations
Email EmailConfig
}

// WebHookConfigurations - Webhook configuration
type WebHookConfigurations struct {
Endpoint string
Name string
Auth WebHookAuthConfigurations
}

// WebHookAuthConfigurations - Webhook authentication configuration
type WebHookAuthConfigurations struct {
Endpoint string
Email string
Password string
Field string
}

// TelegramNotificationConfig - Telegram notification configuration
type TelegramNotificationConfig struct {
Via string
Chat string
From string
Template string
}

// SlackNotificationConfig - Slack notification configuration
type SlackNotificationConfig struct {
Via string
Channel string
From string
Template string
}

// EmailNotificationConfig - Email notfication configuration
type EmailNotificationConfig struct {
Via string
To string
Expand All @@ -127,8 +149,10 @@ type EmailNotificationConfig struct {
Subject string
}

// NotificationStrategyConfig - Struct to choose notification strategy with
type NotificationStrategyConfig struct{ Via string }

// LoadConfig - Function to load configuration
func LoadConfig(configPath string) Configurations {
viper.SetConfigName(configPath)

Expand Down
61 changes: 42 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ import (
"github.com/spf13/cobra"
)

type PortConfigurationsStrategyCheck struct {
type portConfigurationsStrategyCheck struct {
Strategy string
}

// HealthCheckStrategyChooser - struct to resolve a scanning strategy by it's name
type HealthCheckStrategyChooser struct{}

// Parse - Function to dynamically choose scanning algorithm
func (runner *HealthCheckStrategyChooser) Parse(configuration c.TargetConfigurations) (s.Strategy, error) {

var portConfigurationsStrategyCheck PortConfigurationsStrategyCheck
mapstructure.Decode(configuration, &portConfigurationsStrategyCheck)
var portConfigurationsStrategyCheck portConfigurationsStrategyCheck
utils.Check(mapstructure.Decode(configuration, &portConfigurationsStrategyCheck))

switch portConfigurationsStrategyCheck.Strategy {
case "ping":
Expand All @@ -42,6 +44,7 @@ func (runner *HealthCheckStrategyChooser) Parse(configuration c.TargetConfigurat

}

// RunHealthCheck - Function that gets executed inside the cron job (scheduled) - runs ones for each target
func RunHealthCheck(targetConfig c.TargetConfigurations, notificationConfigs c.NotificationConfigurations) func() {
return func() {

Expand All @@ -58,12 +61,12 @@ func RunHealthCheck(targetConfig c.TargetConfigurations, notificationConfigs c.N
for _, notificationReceiver := range portToScan.Notify {

var notificationStrategyConfig c.NotificationStrategyConfig
mapstructure.Decode(notificationReceiver, &notificationStrategyConfig)
utils.Check(mapstructure.Decode(notificationReceiver, &notificationStrategyConfig))

switch notificationStrategyConfig.Via {
case "telegram":
var telegramNotificationConfig c.TelegramNotificationConfig
mapstructure.Decode(notificationReceiver, &telegramNotificationConfig)
utils.Check(mapstructure.Decode(notificationReceiver, &telegramNotificationConfig))
bot, ok := notificationConfigs.Telegram.TelegramBotsMap[telegramNotificationConfig.From]
if !ok {
fmt.Println("Error: Bot not found in config.", telegramNotificationConfig.From)
Expand All @@ -74,12 +77,15 @@ func RunHealthCheck(targetConfig c.TargetConfigurations, notificationConfigs c.N
}

notifier := n.NewTelegramNotifier(bot, chat)
notifier.NotifySpecificPortHealthCheckResult(healthCheckResult.Results[i], telegramNotificationConfig.Template)
err = notifier.NotifySpecificPortHealthCheckResult(healthCheckResult.Results[i], telegramNotificationConfig.Template)
if err != nil {
fmt.Println(err)
}
// fmt.Println("telegram notification", telegramNotificationConfig.)

case "slack":
var slackNotificationConfig c.SlackNotificationConfig
mapstructure.Decode(notificationReceiver, &slackNotificationConfig)
utils.Check(mapstructure.Decode(notificationReceiver, &slackNotificationConfig))
app, ok := notificationConfigs.Slack.SlackAppsMap[slackNotificationConfig.From]
if !ok {
fmt.Println("Error: Slack App not found in config.", slackNotificationConfig.From)
Expand All @@ -90,12 +96,15 @@ func RunHealthCheck(targetConfig c.TargetConfigurations, notificationConfigs c.N
}

notifier := n.NewSlackNotifier(app, channel)
notifier.NotifySpecificPortHealthCheckResult(healthCheckResult.Results[i], slackNotificationConfig.Template)
err = notifier.NotifySpecificPortHealthCheckResult(healthCheckResult.Results[i], slackNotificationConfig.Template)
if err != nil {
fmt.Println(err)
}
// fmt.Println("telegram notification", telegramNotificationConfig.)

case "email":
var emailNotificationConfig c.EmailNotificationConfig
mapstructure.Decode(notificationReceiver, &emailNotificationConfig)
utils.Check(mapstructure.Decode(notificationReceiver, &emailNotificationConfig))
email, ok := notificationConfigs.Email.SmtpConfigsMap[emailNotificationConfig.From]
if !ok {
fmt.Println("Error: Bot not found in config.", emailNotificationConfig.From)
Expand All @@ -105,7 +114,10 @@ func RunHealthCheck(targetConfig c.TargetConfigurations, notificationConfigs c.N
To: emailNotificationConfig.To,
Subject: emailNotificationConfig.Subject,
})
notifier.NotifySpecificPortHealthCheckResult(healthCheckResult.Results[i], emailNotificationConfig.Template)
err = notifier.NotifySpecificPortHealthCheckResult(healthCheckResult.Results[i], emailNotificationConfig.Template)
if err != nil {
fmt.Println(err)
}
// fmt.Println("telegram notification", telegramNotificationConfig.)

default:
Expand Down Expand Up @@ -150,12 +162,12 @@ func RunHealthCheck(targetConfig c.TargetConfigurations, notificationConfigs c.N
for _, notificationReceiver := range rule.Notify {

var notificationStrategyConfig c.NotificationStrategyConfig
mapstructure.Decode(notificationReceiver, &notificationStrategyConfig)
utils.Check(mapstructure.Decode(notificationReceiver, &notificationStrategyConfig))

switch notificationStrategyConfig.Via {
case "telegram":
var telegramNotificationConfig c.TelegramNotificationConfig
mapstructure.Decode(notificationReceiver, &telegramNotificationConfig)
utils.Check(mapstructure.Decode(notificationReceiver, &telegramNotificationConfig))
bot, ok := notificationConfigs.Telegram.TelegramBotsMap[telegramNotificationConfig.From]
if !ok {
fmt.Println("Error: Bot not found in config.", telegramNotificationConfig.From)
Expand All @@ -166,11 +178,14 @@ func RunHealthCheck(targetConfig c.TargetConfigurations, notificationConfigs c.N
}

notifier := n.NewTelegramNotifier(bot, chat)
notifier.NotifyHealthCheckResult(healthCheckResult, telegramNotificationConfig.Template)
err = notifier.NotifyHealthCheckResult(healthCheckResult, telegramNotificationConfig.Template)
if err != nil {
fmt.Println(err)
}
// fmt.Println("telegram notification", telegramNotificationConfig.)
case "slack":
var slackNotificationConfig c.SlackNotificationConfig
mapstructure.Decode(notificationReceiver, &slackNotificationConfig)
utils.Check(mapstructure.Decode(notificationReceiver, &slackNotificationConfig))
app, ok := notificationConfigs.Slack.SlackAppsMap[slackNotificationConfig.From]
if !ok {
fmt.Println("Error: Slack App not found in config.", slackNotificationConfig.From)
Expand All @@ -181,12 +196,15 @@ func RunHealthCheck(targetConfig c.TargetConfigurations, notificationConfigs c.N
}

notifier := n.NewSlackNotifier(app, channel)
notifier.NotifyHealthCheckResult(healthCheckResult, slackNotificationConfig.Template)
err = notifier.NotifyHealthCheckResult(healthCheckResult, slackNotificationConfig.Template)
if err != nil {
fmt.Println(err)
}
// fmt.Println("telegram notification", telegramNotificationConfig.)

case "email":
var emailNotificationConfig c.EmailNotificationConfig
mapstructure.Decode(notificationReceiver, &emailNotificationConfig)
utils.Check(mapstructure.Decode(notificationReceiver, &emailNotificationConfig))
email, ok := notificationConfigs.Email.SmtpConfigsMap[emailNotificationConfig.From]
if !ok {
fmt.Println("Error: Bot not found in config.", emailNotificationConfig.From)
Expand All @@ -196,7 +214,10 @@ func RunHealthCheck(targetConfig c.TargetConfigurations, notificationConfigs c.N
To: emailNotificationConfig.To,
Subject: emailNotificationConfig.Subject,
})
notifier.NotifyHealthCheckResult(healthCheckResult, emailNotificationConfig.Template)
err = notifier.NotifyHealthCheckResult(healthCheckResult, emailNotificationConfig.Template)
if err != nil {
fmt.Println(err)
}
default:
fmt.Println("unknown strategy")

Expand Down Expand Up @@ -271,7 +292,9 @@ func main() {

go RunHealthCheck(target, configuration.Notifications)()

c.AddFunc(target.Cron, RunHealthCheck(target, configuration.Notifications))
err := c.AddFunc(target.Cron, RunHealthCheck(target, configuration.Notifications))

utils.Check(err)

go c.Start()

Expand All @@ -286,6 +309,6 @@ func main() {

rootCmd.Flags().StringVar(&configFilePath, "config", "", "config file (default is path/config.yaml)")

rootCmd.Execute()
utils.Check(rootCmd.Execute())

}
4 changes: 4 additions & 0 deletions notifiers/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@ import (
"github.com/TibebeJS/go-alive/utils"
)

// EmailNotifier - Email Notifier
type EmailNotifier struct {
smtpConfig c.SmtpConfiguration
recipientConfig c.EmailRecipientConfiguration
}

// NewEmailNotifier - Email Notifier constructor
func NewEmailNotifier(smtpConfig c.SmtpConfiguration, recipientConfig c.EmailRecipientConfiguration) *EmailNotifier {
return &EmailNotifier{
smtpConfig: smtpConfig,
recipientConfig: recipientConfig,
}
}

// NotifySpecificPortHealthCheckResult - Sends email for each specific port scan
func (t *EmailNotifier) NotifySpecificPortHealthCheckResult(result s.SpecificPortHealthCheckResult, templateString string) error {
fmt.Println("sending an email from", t.smtpConfig.Sender, "to", t.recipientConfig.To)

Expand Down Expand Up @@ -65,6 +68,7 @@ Is Reachable: {{.IsReachable}}
return nil
}

// NotifyHealthCheckResult - Sends email of target scan result
func (t *EmailNotifier) NotifyHealthCheckResult(result s.HealthCheckResult, templateString string) error {
fmt.Println("sending an email from", t.smtpConfig.Sender, "to", t.recipientConfig.To)

Expand Down
1 change: 1 addition & 0 deletions notifiers/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package notifiers

import s "github.com/TibebeJS/go-alive/strategies"

// Notifier - Interface for each notification medium
type Notifier interface {
NotifySpecificPortHealthCheckResult(result s.SpecificPortHealthCheckResult, templateString string) error
NotifyHealthCheckResult(result s.HealthCheckResult, templateString string) error
Expand Down
Loading

0 comments on commit fb4dc50

Please sign in to comment.