ergo/irc/kline.go

241 lines
5.1 KiB
Go
Raw Normal View History

2017-03-27 14:15:02 +02:00
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
2017-01-11 13:38:16 +01:00
// released under the MIT license
package irc
import (
"encoding/json"
2019-01-22 11:01:01 +01:00
"fmt"
"strings"
"sync"
2019-01-22 11:01:01 +01:00
"time"
2017-01-11 13:38:16 +01:00
2017-06-15 18:14:19 +02:00
"github.com/goshuirc/irc-go/ircmatch"
2017-01-11 13:38:16 +01:00
"github.com/tidwall/buntdb"
)
const (
2019-01-22 11:01:01 +01:00
keyKlineEntry = "bans.klinev2 %s"
2017-01-11 13:38:16 +01:00
)
// KLineInfo contains the address itself and expiration time for a given network.
type KLineInfo struct {
// Mask that is blocked.
Mask string
// Matcher, to facilitate fast matching.
Matcher ircmatch.Matcher
// Info contains information on the ban.
Info IPBanInfo
}
// KLineManager manages and klines.
type KLineManager struct {
2019-01-22 11:01:01 +01:00
sync.RWMutex // tier 1
persistenceMutex sync.Mutex // tier 2
2017-01-11 13:38:16 +01:00
// kline'd entries
2019-01-22 11:01:01 +01:00
entries map[string]KLineInfo
expirationTimers map[string]*time.Timer
server *Server
2017-01-11 13:38:16 +01:00
}
// NewKLineManager returns a new KLineManager.
2019-01-22 11:01:01 +01:00
func NewKLineManager(s *Server) *KLineManager {
2017-01-11 13:38:16 +01:00
var km KLineManager
2019-01-22 11:01:01 +01:00
km.entries = make(map[string]KLineInfo)
km.expirationTimers = make(map[string]*time.Timer)
km.server = s
km.loadFromDatastore()
2017-01-11 13:38:16 +01:00
return &km
}
// AllBans returns all bans (for use with APIs, etc).
func (km *KLineManager) AllBans() map[string]IPBanInfo {
allb := make(map[string]IPBanInfo)
km.RLock()
defer km.RUnlock()
2017-01-11 13:38:16 +01:00
for name, info := range km.entries {
allb[name] = info.Info
}
return allb
}
// AddMask adds to the blocked list.
2019-01-22 11:01:01 +01:00
func (km *KLineManager) AddMask(mask string, duration time.Duration, reason, operReason, operName string) error {
km.persistenceMutex.Lock()
defer km.persistenceMutex.Unlock()
info := IPBanInfo{
Reason: reason,
OperReason: operReason,
OperName: operName,
TimeCreated: time.Now(),
Duration: duration,
}
km.addMaskInternal(mask, info)
return km.persistKLine(mask, info)
}
func (km *KLineManager) addMaskInternal(mask string, info IPBanInfo) {
2017-01-11 13:38:16 +01:00
kln := KLineInfo{
Mask: mask,
Matcher: ircmatch.MakeMatch(mask),
2019-01-22 11:01:01 +01:00
Info: info,
2017-01-11 13:38:16 +01:00
}
2019-01-22 11:01:01 +01:00
var timeLeft time.Duration
if info.Duration > 0 {
timeLeft = info.timeLeft()
if timeLeft <= 0 {
return
}
}
km.Lock()
2019-01-22 11:01:01 +01:00
defer km.Unlock()
km.entries[mask] = kln
km.cancelTimer(mask)
if info.Duration == 0 {
return
}
// set up new expiration timer
timeCreated := info.TimeCreated
processExpiration := func() {
km.Lock()
defer km.Unlock()
maskBan, ok := km.entries[mask]
if ok && maskBan.Info.TimeCreated.Equal(timeCreated) {
delete(km.entries, mask)
delete(km.expirationTimers, mask)
2019-01-22 11:01:01 +01:00
}
}
km.expirationTimers[mask] = time.AfterFunc(timeLeft, processExpiration)
2017-01-11 13:38:16 +01:00
}
2019-01-22 11:01:01 +01:00
func (km *KLineManager) cancelTimer(id string) {
oldTimer := km.expirationTimers[id]
if oldTimer != nil {
oldTimer.Stop()
delete(km.expirationTimers, id)
}
2017-01-11 13:38:16 +01:00
}
2019-01-22 11:01:01 +01:00
func (km *KLineManager) persistKLine(mask string, info IPBanInfo) error {
// save in datastore
klineKey := fmt.Sprintf(keyKlineEntry, mask)
// assemble json from ban info
b, err := json.Marshal(info)
if err != nil {
return err
}
bstr := string(b)
var setOptions *buntdb.SetOptions
if info.Duration != 0 {
setOptions = &buntdb.SetOptions{Expires: true, TTL: info.Duration}
}
err = km.server.store.Update(func(tx *buntdb.Tx) error {
_, _, err := tx.Set(klineKey, bstr, setOptions)
return err
})
return err
}
func (km *KLineManager) unpersistKLine(mask string) error {
// save in datastore
klineKey := fmt.Sprintf(keyKlineEntry, mask)
return km.server.store.Update(func(tx *buntdb.Tx) error {
_, err := tx.Delete(klineKey)
return err
})
}
// RemoveMask removes a mask from the blocked list.
func (km *KLineManager) RemoveMask(mask string) error {
km.persistenceMutex.Lock()
defer km.persistenceMutex.Unlock()
present := func() bool {
km.Lock()
defer km.Unlock()
_, ok := km.entries[mask]
if ok {
delete(km.entries, mask)
}
2019-01-22 11:01:01 +01:00
km.cancelTimer(mask)
return ok
}()
2019-01-22 11:01:01 +01:00
if !present {
return errNoExistingBan
}
return km.unpersistKLine(mask)
}
// CheckMasks returns whether or not the hostmask(s) are banned, and how long they are banned for.
func (km *KLineManager) CheckMasks(masks ...string) (isBanned bool, info IPBanInfo) {
km.RLock()
defer km.RUnlock()
2017-01-11 13:38:16 +01:00
for _, entryInfo := range km.entries {
for _, mask := range masks {
if entryInfo.Matcher.Match(mask) {
2019-01-22 11:01:01 +01:00
return true, entryInfo.Info
2017-01-11 13:38:16 +01:00
}
}
}
// no matches!
2019-01-22 11:01:01 +01:00
isBanned = false
return
2017-01-11 13:38:16 +01:00
}
2019-01-22 11:01:01 +01:00
func (km *KLineManager) loadFromDatastore() {
2017-01-11 13:38:16 +01:00
// load from datastore
2019-01-22 11:01:01 +01:00
klinePrefix := fmt.Sprintf(keyKlineEntry, "")
km.server.store.View(func(tx *buntdb.Tx) error {
tx.AscendGreaterOrEqual("", klinePrefix, func(key, value string) bool {
if !strings.HasPrefix(key, klinePrefix) {
return false
}
2017-01-11 13:38:16 +01:00
// get address name
2019-01-22 11:01:01 +01:00
mask := strings.TrimPrefix(key, klinePrefix)
2017-01-11 13:38:16 +01:00
// load ban info
var info IPBanInfo
2019-01-22 11:01:01 +01:00
err := json.Unmarshal([]byte(value), &info)
if err != nil {
km.server.logger.Error("internal", "couldn't unmarshal kline", err.Error())
return true
}
2017-01-11 13:38:16 +01:00
2017-11-19 01:32:32 +01:00
// add oper name if it doesn't exist already
if info.OperName == "" {
2019-01-22 11:01:01 +01:00
info.OperName = km.server.name
2017-11-19 01:32:32 +01:00
}
2017-01-11 13:38:16 +01:00
// add to the server
2019-01-22 11:01:01 +01:00
km.addMaskInternal(mask, info)
2017-01-11 13:38:16 +01:00
2019-01-22 11:01:01 +01:00
return true
2017-01-11 13:38:16 +01:00
})
return nil
})
2019-01-22 11:01:01 +01:00
}
func (s *Server) loadKLines() {
s.klines = NewKLineManager(s)
2017-01-11 13:38:16 +01:00
}