Add max-user-networks config option

This commit is contained in:
Simon Ser 2021-10-07 20:43:10 +02:00
parent 9f021ba9a9
commit 94dbfff11d
5 changed files with 47 additions and 22 deletions

View File

@ -88,6 +88,7 @@ func main() {
srv.LogPath = cfg.LogPath
srv.HTTPOrigins = cfg.HTTPOrigins
srv.AcceptProxyIPs = cfg.AcceptProxyIPs
srv.MaxUserNetworks = cfg.MaxUserNetworks
srv.Debug = debug
for _, listen := range cfg.Listen {

View File

@ -4,6 +4,7 @@ import (
"fmt"
"net"
"os"
"strconv"
"git.sr.ht/~emersion/go-scfg"
)
@ -36,14 +37,18 @@ type TLS struct {
}
type Server struct {
Listen []string
Hostname string
TLS *TLS
SQLDriver string
SQLSource string
LogPath string
Listen []string
Hostname string
TLS *TLS
SQLDriver string
SQLSource string
LogPath string
HTTPOrigins []string
AcceptProxyIPs IPSet
MaxUserNetworks int
}
func Defaults() *Server {
@ -52,9 +57,10 @@ func Defaults() *Server {
hostname = "localhost"
}
return &Server{
Hostname: hostname,
SQLDriver: "sqlite3",
SQLSource: "soju.db",
Hostname: hostname,
SQLDriver: "sqlite3",
SQLSource: "soju.db",
MaxUserNetworks: -1,
}
}
@ -113,6 +119,15 @@ func parse(cfg scfg.Block) (*Server, error) {
}
srv.AcceptProxyIPs = append(srv.AcceptProxyIPs, n)
}
case "max-user-networks":
var max string
if err := d.ParseParams(&max); err != nil {
return nil, err
}
var err error
if srv.MaxUserNetworks, err = strconv.Atoi(max); err != nil {
return nil, fmt.Errorf("directive %q: %v", d.Name, err)
}
default:
return nil, fmt.Errorf("unknown directive %q", d.Name)
}

View File

@ -129,6 +129,9 @@ The following directives are supported:
By default, all IPs are rejected.
*max-user-networks* <limit>
Maximum number of networks per user. By default, there is no limit.
# IRC SERVICE
soju exposes an IRC service called *BouncerServ* to manage the bouncer.

View File

@ -46,14 +46,15 @@ func (l *prefixLogger) Printf(format string, v ...interface{}) {
}
type Server struct {
Hostname string
Logger Logger
HistoryLimit int
LogPath string
Debug bool
HTTPOrigins []string
AcceptProxyIPs config.IPSet
Identd *Identd // can be nil
Hostname string
Logger Logger
HistoryLimit int
LogPath string
Debug bool
HTTPOrigins []string
AcceptProxyIPs config.IPSet
MaxUserNetworks int
Identd *Identd // can be nil
db Database
stopWG sync.WaitGroup
@ -66,11 +67,12 @@ type Server struct {
func NewServer(db Database) *Server {
return &Server{
Logger: log.New(log.Writer(), "", log.LstdFlags),
HistoryLimit: 1000,
db: db,
listeners: make(map[net.Listener]struct{}),
users: make(map[string]*user),
Logger: log.New(log.Writer(), "", log.LstdFlags),
HistoryLimit: 1000,
MaxUserNetworks: -1,
db: db,
listeners: make(map[net.Listener]struct{}),
users: make(map[string]*user),
}
}

View File

@ -748,6 +748,10 @@ func (u *user) createNetwork(record *Network) (*network, error) {
return nil, err
}
if u.srv.MaxUserNetworks >= 0 && len(u.networks) >= u.srv.MaxUserNetworks {
return nil, fmt.Errorf("maximum number of networks reached")
}
network := newNetwork(u, record, nil)
err := u.srv.db.StoreNetwork(u.ID, &network.Network)
if err != nil {