2022-11-21 04:39:21 +09:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/go-errors/errors"
|
|
|
|
"github.com/samber/lo"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2022-11-23 05:33:23 +09:00
|
|
|
AdminUIDs map[int64]struct{}
|
|
|
|
TGBotToken string
|
|
|
|
TGAnnounceCommands bool
|
2022-11-21 04:39:21 +09:00
|
|
|
|
2022-11-21 16:17:02 +09:00
|
|
|
WatchedInterface string
|
|
|
|
MonthlyTrafficLimitGiB int
|
2022-11-21 04:39:21 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
var config *Config
|
|
|
|
|
|
|
|
func LoadCfg() error {
|
|
|
|
cfg := Config{}
|
|
|
|
|
|
|
|
token := os.Getenv("TG_TOKEN")
|
|
|
|
if token == "" {
|
|
|
|
return errors.New("TG_TOKEN env not set")
|
|
|
|
}
|
|
|
|
cfg.TGBotToken = token
|
|
|
|
|
|
|
|
adminUIDsEnv := os.Getenv("TG_ADMIN_UIDS")
|
2022-11-21 16:17:02 +09:00
|
|
|
adminUIDs := lo.FilterMap(strings.Split(adminUIDsEnv, ","), func(s string, _ int) (string, bool) {
|
|
|
|
trimmed := strings.TrimSpace(s)
|
|
|
|
return trimmed, trimmed != ""
|
2022-11-21 04:39:21 +09:00
|
|
|
})
|
|
|
|
cfg.AdminUIDs = make(map[int64]struct{}, len(adminUIDs))
|
|
|
|
for _, uidStr := range adminUIDs {
|
|
|
|
uid, err := strconv.ParseInt(uidStr, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("invalid admin UID: " + uidStr)
|
|
|
|
}
|
|
|
|
cfg.AdminUIDs[uid] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2022-11-23 05:33:23 +09:00
|
|
|
announceCmdsEnv := os.Getenv("TG_ANNOUNCE_CMDS")
|
|
|
|
if !lo.Contains([]string{"", "no", "false", "0"}, strings.ToLower(announceCmdsEnv)) {
|
|
|
|
cfg.TGAnnounceCommands = true
|
|
|
|
}
|
|
|
|
|
2022-11-21 16:17:02 +09:00
|
|
|
cfg.WatchedInterface = "eth0"
|
|
|
|
if iface := os.Getenv("TG_WATCHED_INTERFACE"); iface != "" {
|
|
|
|
cfg.WatchedInterface = iface
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.MonthlyTrafficLimitGiB = 1000
|
|
|
|
if trafficLimitStr := os.Getenv("TG_MONTHLY_TRAFFIC_LIMIT_GIB"); trafficLimitStr != "" {
|
|
|
|
var err error
|
|
|
|
if cfg.MonthlyTrafficLimitGiB, err = strconv.Atoi(trafficLimitStr); err != nil {
|
|
|
|
return errors.New("invalid traffic limit: " + trafficLimitStr)
|
|
|
|
}
|
2022-11-21 04:39:21 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
config = &cfg
|
|
|
|
return nil
|
|
|
|
}
|