2020-02-18 13:10:23 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-07-22 15:03:01 +00:00
|
|
|
"net"
|
2020-02-18 13:10:23 +00:00
|
|
|
"os"
|
2021-10-07 18:43:10 +00:00
|
|
|
"strconv"
|
2023-01-26 15:57:07 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2020-04-01 12:57:27 +00:00
|
|
|
|
2020-10-25 17:10:50 +00:00
|
|
|
"git.sr.ht/~emersion/go-scfg"
|
2020-02-18 13:10:23 +00:00
|
|
|
)
|
|
|
|
|
2023-02-06 12:16:55 +00:00
|
|
|
var DefaultPath string
|
|
|
|
|
2020-07-22 15:03:01 +00:00
|
|
|
type IPSet []*net.IPNet
|
|
|
|
|
|
|
|
func (set IPSet) Contains(ip net.IP) bool {
|
|
|
|
for _, n := range set {
|
|
|
|
if n.Contains(ip) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// loopbackIPs contains the loopback networks 127.0.0.0/8 and ::1/128.
|
|
|
|
var loopbackIPs = IPSet{
|
|
|
|
&net.IPNet{
|
|
|
|
IP: net.IP{127, 0, 0, 0},
|
|
|
|
Mask: net.CIDRMask(8, 32),
|
|
|
|
},
|
|
|
|
&net.IPNet{
|
|
|
|
IP: net.IPv6loopback,
|
|
|
|
Mask: net.CIDRMask(128, 128),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-01-26 15:57:07 +00:00
|
|
|
func parseDuration(s string) (time.Duration, error) {
|
|
|
|
if !strings.HasSuffix(s, "d") {
|
|
|
|
return 0, fmt.Errorf("missing 'd' suffix in duration")
|
|
|
|
}
|
|
|
|
s = strings.TrimSuffix(s, "d")
|
|
|
|
v, err := strconv.ParseFloat(s, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("invalid duration: %v", err)
|
|
|
|
}
|
|
|
|
return time.Duration(v * 24 * float64(time.Hour)), nil
|
|
|
|
}
|
|
|
|
|
2020-02-18 13:10:23 +00:00
|
|
|
type TLS struct {
|
|
|
|
CertPath, KeyPath string
|
|
|
|
}
|
|
|
|
|
2022-09-11 11:50:34 +00:00
|
|
|
type DB struct {
|
|
|
|
Driver, Source string
|
|
|
|
}
|
|
|
|
|
|
|
|
type MsgStore struct {
|
|
|
|
Driver, Source string
|
|
|
|
}
|
|
|
|
|
2022-09-11 13:45:28 +00:00
|
|
|
type Auth struct {
|
|
|
|
Driver, Source string
|
|
|
|
}
|
|
|
|
|
2020-02-18 13:10:23 +00:00
|
|
|
type Server struct {
|
2021-10-07 18:43:10 +00:00
|
|
|
Listen []string
|
|
|
|
TLS *TLS
|
2021-11-02 21:38:07 +00:00
|
|
|
Hostname string
|
|
|
|
Title string
|
2021-10-13 08:58:34 +00:00
|
|
|
MOTDPath string
|
2021-10-07 18:43:10 +00:00
|
|
|
|
2022-09-11 11:50:34 +00:00
|
|
|
DB DB
|
|
|
|
MsgStore MsgStore
|
2022-09-11 13:45:28 +00:00
|
|
|
Auth Auth
|
2021-10-07 18:43:10 +00:00
|
|
|
|
2020-07-22 15:03:01 +00:00
|
|
|
HTTPOrigins []string
|
|
|
|
AcceptProxyIPs IPSet
|
2021-10-07 18:43:10 +00:00
|
|
|
|
2023-01-26 15:57:07 +00:00
|
|
|
MaxUserNetworks int
|
|
|
|
UpstreamUserIPs []*net.IPNet
|
|
|
|
DisableInactiveUsersDelay time.Duration
|
2023-01-26 18:51:35 +00:00
|
|
|
EnableUsersOnAuth bool
|
2020-02-18 13:10:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Defaults() *Server {
|
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
hostname = "localhost"
|
|
|
|
}
|
|
|
|
return &Server{
|
2022-09-11 11:50:34 +00:00
|
|
|
Hostname: hostname,
|
|
|
|
DB: DB{
|
|
|
|
Driver: "sqlite3",
|
|
|
|
Source: "soju.db",
|
|
|
|
},
|
|
|
|
MsgStore: MsgStore{
|
|
|
|
Driver: "memory",
|
|
|
|
},
|
2022-09-11 13:45:28 +00:00
|
|
|
Auth: Auth{
|
|
|
|
Driver: "internal",
|
|
|
|
},
|
2021-10-07 18:43:10 +00:00
|
|
|
MaxUserNetworks: -1,
|
2020-02-18 13:10:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Load(path string) (*Server, error) {
|
2020-10-25 17:10:50 +00:00
|
|
|
cfg, err := scfg.Load(path)
|
2020-02-18 13:10:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-25 17:10:50 +00:00
|
|
|
return parse(cfg)
|
2020-02-18 13:10:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 17:10:50 +00:00
|
|
|
func parse(cfg scfg.Block) (*Server, error) {
|
2020-02-18 13:10:23 +00:00
|
|
|
srv := Defaults()
|
2020-10-25 17:10:50 +00:00
|
|
|
for _, d := range cfg {
|
2020-02-18 13:10:23 +00:00
|
|
|
switch d.Name {
|
|
|
|
case "listen":
|
2020-06-04 18:10:17 +00:00
|
|
|
var uri string
|
2020-10-25 17:10:50 +00:00
|
|
|
if err := d.ParseParams(&uri); err != nil {
|
2020-02-18 13:10:23 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-04 18:10:17 +00:00
|
|
|
srv.Listen = append(srv.Listen, uri)
|
2020-02-18 13:10:23 +00:00
|
|
|
case "hostname":
|
2020-10-25 17:10:50 +00:00
|
|
|
if err := d.ParseParams(&srv.Hostname); err != nil {
|
2020-02-18 13:10:23 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-02 21:38:07 +00:00
|
|
|
case "title":
|
|
|
|
if err := d.ParseParams(&srv.Title); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case "motd":
|
|
|
|
if err := d.ParseParams(&srv.MOTDPath); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-18 13:10:23 +00:00
|
|
|
case "tls":
|
|
|
|
tls := &TLS{}
|
2020-10-25 17:10:50 +00:00
|
|
|
if err := d.ParseParams(&tls.CertPath, &tls.KeyPath); err != nil {
|
2020-02-18 13:10:23 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
srv.TLS = tls
|
2021-04-21 16:15:04 +00:00
|
|
|
case "db":
|
2022-09-11 11:50:34 +00:00
|
|
|
if err := d.ParseParams(&srv.DB.Driver, &srv.DB.Source); err != nil {
|
2020-03-04 17:22:58 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-09 14:41:41 +00:00
|
|
|
case "message-store", "log":
|
2022-09-11 11:50:34 +00:00
|
|
|
if err := d.ParseParams(&srv.MsgStore.Driver); err != nil {
|
2020-03-25 22:51:28 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-11 11:50:34 +00:00
|
|
|
switch srv.MsgStore.Driver {
|
2022-05-09 14:59:27 +00:00
|
|
|
case "memory":
|
2022-09-11 11:50:34 +00:00
|
|
|
srv.MsgStore.Source = ""
|
2022-05-09 14:59:27 +00:00
|
|
|
case "fs":
|
2022-09-11 11:50:34 +00:00
|
|
|
if err := d.ParseParams(nil, &srv.MsgStore.Source); err != nil {
|
2022-05-09 14:59:27 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
default:
|
2022-09-11 11:50:34 +00:00
|
|
|
return nil, fmt.Errorf("directive %q: unknown driver %q", d.Name, srv.MsgStore.Driver)
|
2021-04-21 16:15:04 +00:00
|
|
|
}
|
2022-09-11 13:45:28 +00:00
|
|
|
case "auth":
|
|
|
|
if err := d.ParseParams(&srv.Auth.Driver); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch srv.Auth.Driver {
|
2023-01-30 22:32:07 +00:00
|
|
|
case "internal", "pam":
|
2022-09-11 13:45:28 +00:00
|
|
|
srv.Auth.Source = ""
|
2022-09-11 13:46:27 +00:00
|
|
|
case "oauth2":
|
|
|
|
if err := d.ParseParams(nil, &srv.Auth.Source); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-11 13:45:28 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("directive %q: unknown driver %q", d.Name, srv.Auth.Driver)
|
|
|
|
}
|
2020-04-23 20:25:43 +00:00
|
|
|
case "http-origin":
|
2020-07-22 16:07:55 +00:00
|
|
|
srv.HTTPOrigins = d.Params
|
2020-07-22 15:03:01 +00:00
|
|
|
case "accept-proxy-ip":
|
|
|
|
srv.AcceptProxyIPs = nil
|
|
|
|
for _, s := range d.Params {
|
2020-10-25 17:22:12 +00:00
|
|
|
if s == "localhost" {
|
|
|
|
srv.AcceptProxyIPs = append(srv.AcceptProxyIPs, loopbackIPs...)
|
|
|
|
continue
|
|
|
|
}
|
2020-07-22 15:03:01 +00:00
|
|
|
_, n, err := net.ParseCIDR(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("directive %q: failed to parse CIDR: %v", d.Name, err)
|
|
|
|
}
|
|
|
|
srv.AcceptProxyIPs = append(srv.AcceptProxyIPs, n)
|
|
|
|
}
|
2021-10-07 18:43:10 +00:00
|
|
|
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)
|
|
|
|
}
|
2021-10-21 17:14:39 +00:00
|
|
|
case "upstream-user-ip":
|
|
|
|
if len(srv.UpstreamUserIPs) > 0 {
|
|
|
|
return nil, fmt.Errorf("directive %q: can only be specified once", d.Name)
|
|
|
|
}
|
|
|
|
var hasIPv4, hasIPv6 bool
|
|
|
|
for _, s := range d.Params {
|
|
|
|
_, n, err := net.ParseCIDR(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("directive %q: failed to parse CIDR: %v", d.Name, err)
|
|
|
|
}
|
|
|
|
if n.IP.To4() == nil {
|
|
|
|
if hasIPv6 {
|
|
|
|
return nil, fmt.Errorf("directive %q: found two IPv6 CIDRs", d.Name)
|
|
|
|
}
|
|
|
|
hasIPv6 = true
|
|
|
|
} else {
|
|
|
|
if hasIPv4 {
|
|
|
|
return nil, fmt.Errorf("directive %q: found two IPv4 CIDRs", d.Name)
|
|
|
|
}
|
|
|
|
hasIPv4 = true
|
|
|
|
}
|
|
|
|
srv.UpstreamUserIPs = append(srv.UpstreamUserIPs, n)
|
|
|
|
}
|
2023-01-26 15:57:07 +00:00
|
|
|
case "disable-inactive-user":
|
|
|
|
var durStr string
|
|
|
|
if err := d.ParseParams(&durStr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dur, err := parseDuration(durStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("directive %q: %v", d.Name, err)
|
|
|
|
} else if dur < 0 {
|
|
|
|
return nil, fmt.Errorf("directive %q: duration must be positive", d.Name)
|
|
|
|
}
|
|
|
|
srv.DisableInactiveUsersDelay = dur
|
2023-01-26 18:51:35 +00:00
|
|
|
case "enable-user-on-auth":
|
|
|
|
var s string
|
|
|
|
if err := d.ParseParams(&s); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
b, err := strconv.ParseBool(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("directive %q: %v", d.Name, err)
|
|
|
|
}
|
|
|
|
srv.EnableUsersOnAuth = b
|
2020-02-18 13:10:23 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown directive %q", d.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return srv, nil
|
|
|
|
}
|