3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-24 12:59:33 +01:00
ergo/irc/dline.go

281 lines
6.5 KiB
Go
Raw Permalink Normal View History

2017-03-27 14:15:02 +02:00
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
2016-11-04 03:42:58 +01:00
// released under the MIT license
package irc
2016-11-04 12:14:52 +01:00
import (
2019-01-22 11:01:01 +01:00
"encoding/json"
2016-11-04 12:14:52 +01:00
"fmt"
2019-01-22 11:01:01 +01:00
"strings"
"sync"
2016-11-04 12:14:52 +01:00
"time"
2021-05-25 06:34:38 +02:00
"github.com/ergochat/ergo/irc/flatip"
2016-11-04 12:14:52 +01:00
"github.com/tidwall/buntdb"
)
const (
2019-01-22 11:01:01 +01:00
keyDlineEntry = "bans.dlinev2 %s"
2016-11-04 12:14:52 +01:00
)
2016-11-04 03:42:58 +01:00
// IPBanInfo holds info about an IP/net ban.
type IPBanInfo struct {
2021-01-22 15:38:40 +01:00
// RequireSASL indicates a "soft" ban; connections are allowed but they must SASL
RequireSASL bool
2016-11-04 03:42:58 +01:00
// Reason is the ban reason.
Reason string `json:"reason"`
2016-11-04 03:42:58 +01:00
// OperReason is an oper ban reason.
2016-11-04 12:14:52 +01:00
OperReason string `json:"oper_reason"`
2017-11-19 01:27:40 +01:00
// OperName is the oper who set the ban.
OperName string `json:"oper_name"`
2019-01-22 11:01:01 +01:00
// time of ban creation
TimeCreated time.Time
// duration of the ban; 0 means "permanent"
Duration time.Duration
}
func (info IPBanInfo) timeLeft() time.Duration {
return time.Until(info.TimeCreated.Add(info.Duration))
2019-01-22 11:01:01 +01:00
}
func (info IPBanInfo) TimeLeft() string {
if info.Duration == 0 {
return "indefinite"
} else {
return info.timeLeft().Truncate(time.Second).String()
}
2016-11-04 03:42:58 +01:00
}
// BanMessage returns the ban message.
func (info IPBanInfo) BanMessage(message string) string {
2021-01-19 14:49:45 +01:00
reason := info.Reason
if reason == "" {
reason = "No reason given"
}
message = fmt.Sprintf(message, reason)
2019-01-22 11:01:01 +01:00
if info.Duration != 0 {
message += fmt.Sprintf(" [%s]", info.TimeLeft())
}
return message
}
2016-11-04 03:42:58 +01:00
// DLineManager manages and dlines.
type DLineManager struct {
2019-01-22 11:01:01 +01:00
sync.RWMutex // tier 1
persistenceMutex sync.Mutex // tier 2
// networks that are dlined:
2020-12-08 03:21:10 +01:00
networks map[flatip.IPNet]IPBanInfo
2019-01-22 11:01:01 +01:00
// this keeps track of expiration timers for temporary bans
2020-12-08 03:21:10 +01:00
expirationTimers map[flatip.IPNet]*time.Timer
2019-01-22 11:01:01 +01:00
server *Server
2016-11-04 03:42:58 +01:00
}
// NewDLineManager returns a new DLineManager.
2019-01-22 11:01:01 +01:00
func NewDLineManager(server *Server) *DLineManager {
2016-11-04 12:14:52 +01:00
var dm DLineManager
2020-12-08 03:21:10 +01:00
dm.networks = make(map[flatip.IPNet]IPBanInfo)
dm.expirationTimers = make(map[flatip.IPNet]*time.Timer)
2019-01-22 11:01:01 +01:00
dm.server = server
dm.loadFromDatastore()
2016-11-04 03:42:58 +01:00
return &dm
}
2016-11-06 02:05:29 +01:00
// AllBans returns all bans (for use with APIs, etc).
func (dm *DLineManager) AllBans() map[string]IPBanInfo {
allb := make(map[string]IPBanInfo)
dm.RLock()
defer dm.RUnlock()
2019-01-22 11:01:01 +01:00
for key, info := range dm.networks {
2021-01-19 14:49:45 +01:00
allb[key.HumanReadableString()] = info
2016-11-06 02:05:29 +01:00
}
return allb
}
2016-11-04 03:42:58 +01:00
// AddNetwork adds a network to the blocked list.
2021-01-22 15:38:40 +01:00
func (dm *DLineManager) AddNetwork(network flatip.IPNet, duration time.Duration, requireSASL bool, reason, operReason, operName string) error {
2019-01-22 11:01:01 +01:00
dm.persistenceMutex.Lock()
defer dm.persistenceMutex.Unlock()
// assemble ban info
info := IPBanInfo{
2021-01-22 15:38:40 +01:00
RequireSASL: requireSASL,
2019-01-22 11:01:01 +01:00
Reason: reason,
OperReason: operReason,
OperName: operName,
TimeCreated: time.Now().UTC(),
2019-01-22 11:01:01 +01:00
Duration: duration,
2016-11-04 03:42:58 +01:00
}
2019-01-22 11:01:01 +01:00
id := dm.addNetworkInternal(network, info)
return dm.persistDline(id, info)
2016-11-04 03:42:58 +01:00
}
2021-01-19 14:49:45 +01:00
func (dm *DLineManager) addNetworkInternal(flatnet flatip.IPNet, info IPBanInfo) (id flatip.IPNet) {
2020-12-08 03:21:10 +01:00
id = flatnet
2019-01-22 11:01:01 +01:00
var timeLeft time.Duration
if info.Duration != 0 {
timeLeft = info.timeLeft()
if timeLeft <= 0 {
return
}
2016-11-04 03:42:58 +01:00
}
2019-01-22 11:01:01 +01:00
dm.Lock()
2019-01-22 11:01:01 +01:00
defer dm.Unlock()
2020-12-08 03:21:10 +01:00
dm.networks[flatnet] = info
2019-01-22 11:01:01 +01:00
2020-12-08 03:21:10 +01:00
dm.cancelTimer(flatnet)
2019-01-22 11:01:01 +01:00
if info.Duration == 0 {
return
}
// set up new expiration timer
timeCreated := info.TimeCreated
processExpiration := func() {
dm.Lock()
defer dm.Unlock()
2020-12-08 03:21:10 +01:00
banInfo, ok := dm.networks[flatnet]
if ok && banInfo.TimeCreated.Equal(timeCreated) {
delete(dm.networks, flatnet)
2019-01-22 11:01:01 +01:00
// TODO(slingamn) here's where we'd remove it from the radix tree
2020-12-08 03:21:10 +01:00
delete(dm.expirationTimers, flatnet)
2019-01-22 11:01:01 +01:00
}
}
2020-12-08 03:21:10 +01:00
dm.expirationTimers[flatnet] = time.AfterFunc(timeLeft, processExpiration)
2019-01-22 11:01:01 +01:00
return
2016-11-04 03:42:58 +01:00
}
2020-12-08 03:21:10 +01:00
func (dm *DLineManager) cancelTimer(flatnet flatip.IPNet) {
oldTimer := dm.expirationTimers[flatnet]
2019-01-22 11:01:01 +01:00
if oldTimer != nil {
oldTimer.Stop()
2020-12-08 03:21:10 +01:00
delete(dm.expirationTimers, flatnet)
2019-01-22 11:01:01 +01:00
}
2016-11-04 03:42:58 +01:00
}
2020-12-08 03:21:10 +01:00
func (dm *DLineManager) persistDline(id flatip.IPNet, info IPBanInfo) error {
2019-01-22 11:01:01 +01:00
// save in datastore
2020-12-08 03:21:10 +01:00
dlineKey := fmt.Sprintf(keyDlineEntry, id.String())
2019-01-22 11:01:01 +01:00
// assemble json from ban info
b, err := json.Marshal(info)
if err != nil {
dm.server.logger.Error("internal", "couldn't marshal d-line", err.Error())
return err
}
bstr := string(b)
var setOptions *buntdb.SetOptions
if info.Duration != 0 {
setOptions = &buntdb.SetOptions{Expires: true, TTL: info.Duration}
2016-11-04 03:42:58 +01:00
}
2019-01-22 11:01:01 +01:00
err = dm.server.store.Update(func(tx *buntdb.Tx) error {
_, _, err := tx.Set(dlineKey, bstr, setOptions)
return err
})
if err != nil {
dm.server.logger.Error("internal", "couldn't store d-line", err.Error())
}
return err
}
2020-12-08 03:21:10 +01:00
func (dm *DLineManager) unpersistDline(id flatip.IPNet) error {
dlineKey := fmt.Sprintf(keyDlineEntry, id.String())
2019-01-22 11:01:01 +01:00
return dm.server.store.Update(func(tx *buntdb.Tx) error {
_, err := tx.Delete(dlineKey)
return err
})
}
// RemoveNetwork removes a network from the blocked list.
2021-01-19 14:49:45 +01:00
func (dm *DLineManager) RemoveNetwork(network flatip.IPNet) error {
2019-01-22 11:01:01 +01:00
dm.persistenceMutex.Lock()
defer dm.persistenceMutex.Unlock()
2021-01-19 14:49:45 +01:00
id := network
2019-01-22 11:01:01 +01:00
present := func() bool {
dm.Lock()
defer dm.Unlock()
_, ok := dm.networks[id]
delete(dm.networks, id)
dm.cancelTimer(id)
return ok
}()
2016-11-04 03:42:58 +01:00
2019-01-22 11:01:01 +01:00
if !present {
return errNoExistingBan
}
return dm.unpersistDline(id)
}
// CheckIP returns whether or not an IP address was banned, and how long it is banned for.
func (dm *DLineManager) CheckIP(addr flatip.IP) (isBanned bool, info IPBanInfo) {
dm.RLock()
defer dm.RUnlock()
2016-11-04 03:42:58 +01:00
2019-01-22 11:01:01 +01:00
// check networks
// TODO(slingamn) use a radix tree as the data plane for this
2020-12-08 03:21:10 +01:00
for flatnet, info := range dm.networks {
if flatnet.Contains(addr) {
return true, info
}
2016-11-04 03:42:58 +01:00
}
// no matches!
2019-01-22 11:01:01 +01:00
return
2016-11-04 03:42:58 +01:00
}
2016-11-04 12:14:52 +01:00
2019-01-22 11:01:01 +01:00
func (dm *DLineManager) loadFromDatastore() {
dlinePrefix := fmt.Sprintf(keyDlineEntry, "")
dm.server.store.View(func(tx *buntdb.Tx) error {
tx.AscendGreaterOrEqual("", dlinePrefix, func(key, value string) bool {
if !strings.HasPrefix(key, dlinePrefix) {
return false
}
2016-11-04 12:14:52 +01:00
// get address name
2019-01-22 11:01:01 +01:00
key = strings.TrimPrefix(key, dlinePrefix)
2016-11-04 12:14:52 +01:00
// load addr/net
2021-01-19 14:49:45 +01:00
hostNet, err := flatip.ParseToNormalizedNet(key)
2016-11-04 12:14:52 +01:00
if err != nil {
2019-01-22 11:01:01 +01:00
dm.server.logger.Error("internal", "bad dline cidr", err.Error())
return true
2016-11-04 12:14:52 +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 {
dm.server.logger.Error("internal", "bad dline data", err.Error())
return true
}
2016-11-04 12:14:52 +01:00
2017-11-19 01:27:40 +01:00
// set opername if it isn't already set
if info.OperName == "" {
2019-01-22 11:01:01 +01:00
info.OperName = dm.server.name
2017-11-19 01:27:40 +01:00
}
2016-11-04 12:14:52 +01:00
// add to the server
2019-01-22 11:01:01 +01:00
dm.addNetworkInternal(hostNet, info)
2016-11-04 12:14:52 +01:00
2019-01-22 11:01:01 +01:00
return true
2016-11-04 12:14:52 +01:00
})
return nil
})
}
2019-01-22 11:01:01 +01:00
func (s *Server) loadDLines() {
s.dlines = NewDLineManager(s)
}