2021-08-29 11:50:53 +00:00
|
|
|
package socket
|
|
|
|
|
|
|
|
import (
|
2021-08-29 16:54:20 +00:00
|
|
|
"fmt"
|
2021-08-29 11:50:53 +00:00
|
|
|
"github.com/kisielk/og-rek"
|
|
|
|
"github.com/nlpodyssey/gopickle/types"
|
2021-08-29 16:54:20 +00:00
|
|
|
"net"
|
|
|
|
"strings"
|
2021-08-29 11:50:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Fail2BanSocket struct {
|
|
|
|
socket net.Conn
|
|
|
|
encoder *ogórek.Encoder
|
|
|
|
}
|
|
|
|
|
2021-08-29 16:54:20 +00:00
|
|
|
type JailStats struct {
|
|
|
|
FailedCurrent int
|
|
|
|
FailedTotal int
|
|
|
|
BannedCurrent int
|
|
|
|
BannedTotal int
|
|
|
|
}
|
|
|
|
|
2021-08-30 06:36:15 +00:00
|
|
|
func ConnectToSocket(path string) (*Fail2BanSocket, error) {
|
2021-08-29 11:50:53 +00:00
|
|
|
c, err := net.Dial("unix", path)
|
|
|
|
if err != nil {
|
2021-08-30 06:36:15 +00:00
|
|
|
return nil, err
|
2021-08-29 11:50:53 +00:00
|
|
|
}
|
|
|
|
return &Fail2BanSocket{
|
|
|
|
socket: c,
|
|
|
|
encoder: ogórek.NewEncoder(c),
|
2021-08-30 06:36:15 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Fail2BanSocket) Close() error {
|
|
|
|
return s.socket.Close()
|
2021-08-29 11:50:53 +00:00
|
|
|
}
|
|
|
|
|
2021-08-30 07:19:11 +00:00
|
|
|
func (s *Fail2BanSocket) Ping() (bool, error) {
|
2021-08-29 11:50:53 +00:00
|
|
|
response, err := s.sendCommand([]string{pingCommand, "100"})
|
|
|
|
if err != nil {
|
2021-08-30 07:19:11 +00:00
|
|
|
return false, newConnectionError(pingCommand, err)
|
2021-08-29 11:50:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if t, ok := response.(*types.Tuple); ok {
|
|
|
|
if (*t)[1] == "pong" {
|
2021-08-30 07:19:11 +00:00
|
|
|
return true, nil
|
2021-08-29 11:50:53 +00:00
|
|
|
}
|
2021-08-30 07:19:11 +00:00
|
|
|
return false, fmt.Errorf("unexpected response data (expecting 'pong'): %s", (*t)[1])
|
2021-08-29 11:50:53 +00:00
|
|
|
}
|
2021-08-30 07:19:11 +00:00
|
|
|
return false, newBadFormatError(pingCommand, response)
|
2021-08-29 11:50:53 +00:00
|
|
|
}
|
2021-08-29 16:54:20 +00:00
|
|
|
|
|
|
|
func (s *Fail2BanSocket) GetJails() ([]string, error) {
|
|
|
|
response, err := s.sendCommand([]string{statusCommand})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if lvl1, ok := response.(*types.Tuple); ok {
|
|
|
|
if lvl2, ok := lvl1.Get(1).(*types.List); ok {
|
|
|
|
if lvl3, ok := lvl2.Get(1).(*types.Tuple); ok {
|
|
|
|
if lvl4, ok := lvl3.Get(1).(string); ok {
|
|
|
|
splitJails := strings.Split(lvl4, ",")
|
|
|
|
return trimSpaceForAll(splitJails), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, newBadFormatError(statusCommand, response)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Fail2BanSocket) GetJailStats(jail string) (JailStats, error) {
|
|
|
|
response, err := s.sendCommand([]string{statusCommand, jail})
|
|
|
|
if err != nil {
|
|
|
|
return JailStats{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
stats := JailStats{
|
|
|
|
FailedCurrent: -1,
|
|
|
|
FailedTotal: -1,
|
|
|
|
BannedCurrent: -1,
|
|
|
|
BannedTotal: -1,
|
|
|
|
}
|
|
|
|
|
|
|
|
if lvl1, ok := response.(*types.Tuple); ok {
|
|
|
|
if lvl2, ok := lvl1.Get(1).(*types.List); ok {
|
|
|
|
if filter, ok := lvl2.Get(0).(*types.Tuple); ok {
|
|
|
|
if filterLvl1, ok := filter.Get(1).(*types.List); ok {
|
|
|
|
if filterCurrentTuple, ok := filterLvl1.Get(0).(*types.Tuple); ok {
|
|
|
|
if filterCurrent, ok := filterCurrentTuple.Get(1).(int); ok {
|
|
|
|
stats.FailedCurrent = filterCurrent
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if filterTotalTuple, ok := filterLvl1.Get(1).(*types.Tuple); ok {
|
|
|
|
if filterTotal, ok := filterTotalTuple.Get(1).(int); ok {
|
|
|
|
stats.FailedTotal = filterTotal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if actions, ok := lvl2.Get(1).(*types.Tuple); ok {
|
|
|
|
if actionsLvl1, ok := actions.Get(1).(*types.List); ok {
|
|
|
|
if actionsCurrentTuple, ok := actionsLvl1.Get(0).(*types.Tuple); ok {
|
|
|
|
if actionsCurrent, ok := actionsCurrentTuple.Get(1).(int); ok {
|
|
|
|
stats.BannedCurrent = actionsCurrent
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if actionsTotalTuple, ok := actionsLvl1.Get(1).(*types.Tuple); ok {
|
|
|
|
if actionsTotal, ok := actionsTotalTuple.Get(1).(int); ok {
|
|
|
|
stats.BannedTotal = actionsTotal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return stats, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return stats, newBadFormatError(statusCommand, response)
|
|
|
|
}
|
|
|
|
|
2021-09-10 06:13:56 +00:00
|
|
|
func (s *Fail2BanSocket) GetServerVersion() (string, error) {
|
|
|
|
response, err := s.sendCommand([]string{versionCommand})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if lvl1, ok := response.(*types.Tuple); ok {
|
|
|
|
if versionStr, ok := lvl1.Get(1).(string); ok {
|
|
|
|
return versionStr, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", newBadFormatError(versionCommand, response)
|
|
|
|
}
|
|
|
|
|
2021-08-29 16:54:20 +00:00
|
|
|
func newBadFormatError(command string, data interface{}) error {
|
|
|
|
return fmt.Errorf("(%s) unexpected response format - cannot parse: %v", command, data)
|
|
|
|
}
|
|
|
|
|
2021-08-30 07:19:11 +00:00
|
|
|
func newConnectionError(command string, err error) error {
|
|
|
|
return fmt.Errorf("(%s) failed to send command through socket: %v", command, err)
|
|
|
|
}
|
|
|
|
|
2021-08-29 16:54:20 +00:00
|
|
|
func trimSpaceForAll(slice []string) []string {
|
|
|
|
for i := range slice {
|
|
|
|
slice[i] = strings.TrimSpace(slice[i])
|
|
|
|
}
|
|
|
|
return slice
|
|
|
|
}
|