blocky/cmd/blocking.go

134 lines
3.2 KiB
Go
Raw Normal View History

2020-04-08 23:03:07 +02:00
package cmd
import (
"context"
2020-04-08 23:03:07 +02:00
"fmt"
"net/http"
"strings"
2020-04-08 23:03:07 +02:00
2021-08-25 22:06:34 +02:00
"github.com/0xERR0R/blocky/api"
"github.com/0xERR0R/blocky/log"
2020-04-08 23:03:07 +02:00
"github.com/spf13/cobra"
)
2021-02-08 21:57:59 +01:00
func newBlockingCommand() *cobra.Command {
c := &cobra.Command{
Use: "blocking",
Aliases: []string{"block"},
Short: "Control status of blocking resolver",
}
c.AddCommand(&cobra.Command{
2020-04-08 23:03:07 +02:00
Use: "enable",
Args: cobra.NoArgs,
Aliases: []string{"on"},
Short: "Enable blocking",
RunE: enableBlocking,
2020-04-08 23:03:07 +02:00
})
disableCommand := &cobra.Command{
Use: "disable",
Aliases: []string{"off"},
Args: cobra.NoArgs,
Short: "Disable blocking for certain duration",
RunE: disableBlocking,
2020-04-08 23:03:07 +02:00
}
disableCommand.Flags().DurationP("duration", "d", 0, "duration in min")
disableCommand.Flags().StringArrayP("groups", "g", []string{}, "blocking groups to disable")
2021-02-08 21:57:59 +01:00
c.AddCommand(disableCommand)
2020-04-08 23:03:07 +02:00
2021-02-08 21:57:59 +01:00
c.AddCommand(&cobra.Command{
2020-04-08 23:03:07 +02:00
Use: "status",
Args: cobra.NoArgs,
Short: "Print the status of blocking resolver",
RunE: statusBlocking,
2020-04-08 23:03:07 +02:00
})
2021-02-08 21:57:59 +01:00
return c
2020-04-08 23:03:07 +02:00
}
func enableBlocking(_ *cobra.Command, _ []string) error {
client, err := api.NewClientWithResponses(apiURL())
if err != nil {
return fmt.Errorf("can't create client: %w", err)
}
resp, err := client.EnableBlockingWithResponse(context.Background())
2020-04-08 23:03:07 +02:00
if err != nil {
return fmt.Errorf("can't execute %w", err)
2020-04-08 23:03:07 +02:00
}
if resp.StatusCode() == http.StatusOK {
log.Log().Info("OK")
2020-04-08 23:03:07 +02:00
} else {
return fmt.Errorf("response NOK, Status: %s", resp.Status())
2020-04-08 23:03:07 +02:00
}
return nil
2020-04-08 23:03:07 +02:00
}
func disableBlocking(cmd *cobra.Command, _ []string) error {
2020-04-08 23:03:07 +02:00
duration, _ := cmd.Flags().GetDuration("duration")
groups, _ := cmd.Flags().GetStringArray("groups")
2020-04-08 23:03:07 +02:00
durationString := duration.String()
groupsString := strings.Join(groups, ",")
client, err := api.NewClientWithResponses(apiURL())
if err != nil {
return fmt.Errorf("can't create client: %w", err)
}
resp, err := client.DisableBlockingWithResponse(context.Background(), &api.DisableBlockingParams{
Duration: &durationString,
Groups: &groupsString,
})
2020-04-08 23:03:07 +02:00
if err != nil {
return fmt.Errorf("can't execute %w", err)
2020-04-08 23:03:07 +02:00
}
if resp.StatusCode() == http.StatusOK {
log.Log().Info("OK")
2020-04-08 23:03:07 +02:00
} else {
return fmt.Errorf("response NOK, Status: %s", resp.Status())
2020-04-08 23:03:07 +02:00
}
return nil
2020-04-08 23:03:07 +02:00
}
func statusBlocking(_ *cobra.Command, _ []string) error {
client, err := api.NewClientWithResponses(apiURL())
2020-04-08 23:03:07 +02:00
if err != nil {
return fmt.Errorf("can't create client: %w", err)
2020-04-08 23:03:07 +02:00
}
resp, err := client.BlockingStatusWithResponse(context.Background())
if err != nil {
return fmt.Errorf("can't execute %w", err)
2020-04-08 23:03:07 +02:00
}
if resp.StatusCode() != http.StatusOK {
return fmt.Errorf("response NOK, Status: %s", resp.Status())
}
2020-04-08 23:03:07 +02:00
if err != nil {
return fmt.Errorf("can't parse response %w", err)
}
2020-04-08 23:03:07 +02:00
if resp.JSON200.Enabled {
log.Log().Info("blocking enabled")
2020-04-08 23:03:07 +02:00
} else {
var groupNames string
if resp.JSON200.DisabledGroups != nil {
groupNames = strings.Join(*resp.JSON200.DisabledGroups, "; ")
}
if resp.JSON200.AutoEnableInSec == nil || *resp.JSON200.AutoEnableInSec == 0 {
log.Log().Infof("blocking disabled for groups: %s", groupNames)
2020-04-08 23:03:07 +02:00
} else {
log.Log().Infof("blocking disabled for groups: '%s', for %d seconds",
groupNames, *resp.JSON200.AutoEnableInSec)
2020-04-08 23:03:07 +02:00
}
}
return nil
2020-04-08 23:03:07 +02:00
}