neko/server/internal/config/session.go

160 lines
6.2 KiB
Go
Raw Normal View History

package config
import (
2021-03-18 02:09:10 +13:00
"time"
2024-07-19 07:48:09 +12:00
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
2020-11-02 06:39:12 +13:00
type Session struct {
2023-03-28 05:33:51 +13:00
File string
PrivateMode bool
2024-04-21 00:27:15 +12:00
LockedLogins bool
LockedControls bool
2024-04-22 06:10:16 +12:00
ControlProtection bool
2021-08-24 04:41:19 +12:00
ImplicitHosting bool
2021-11-02 05:49:02 +13:00
InactiveCursors bool
2021-08-24 04:41:19 +12:00
MercifulReconnect bool
APIToken string
2021-03-18 02:09:10 +13:00
2021-04-25 06:53:37 +12:00
CookieEnabled bool
2021-03-18 02:09:10 +13:00
CookieName string
2022-01-17 09:03:25 +13:00
CookieExpiration time.Duration
2021-03-18 02:09:10 +13:00
CookieSecure bool
}
2020-11-02 06:39:12 +13:00
func (Session) Init(cmd *cobra.Command) error {
2023-03-28 05:33:51 +13:00
cmd.PersistentFlags().String("session.file", "", "if sessions should be stored in a file, otherwise they will be stored only in memory")
if err := viper.BindPFlag("session.file", cmd.PersistentFlags().Lookup("session.file")); err != nil {
return err
}
cmd.PersistentFlags().Bool("session.private_mode", false, "whether private mode should be enabled initially")
if err := viper.BindPFlag("session.private_mode", cmd.PersistentFlags().Lookup("session.private_mode")); err != nil {
return err
}
2024-04-21 00:27:15 +12:00
cmd.PersistentFlags().Bool("session.locked_logins", false, "whether logins should be locked for users initially")
if err := viper.BindPFlag("session.locked_logins", cmd.PersistentFlags().Lookup("session.locked_logins")); err != nil {
return err
}
cmd.PersistentFlags().Bool("session.locked_controls", false, "whether controls should be locked for users initially")
if err := viper.BindPFlag("session.locked_controls", cmd.PersistentFlags().Lookup("session.locked_controls")); err != nil {
return err
}
2024-04-22 06:10:16 +12:00
cmd.PersistentFlags().Bool("session.control_protection", false, "users can gain control only if at least one admin is in the room")
if err := viper.BindPFlag("session.control_protection", cmd.PersistentFlags().Lookup("session.control_protection")); err != nil {
return err
}
2021-03-17 03:24:58 +13:00
cmd.PersistentFlags().Bool("session.implicit_hosting", true, "allow implicit control switching")
if err := viper.BindPFlag("session.implicit_hosting", cmd.PersistentFlags().Lookup("session.implicit_hosting")); err != nil {
2020-12-02 23:24:20 +13:00
return err
}
2021-11-02 09:35:15 +13:00
cmd.PersistentFlags().Bool("session.inactive_cursors", false, "show inactive cursors on the screen")
2021-11-02 05:49:02 +13:00
if err := viper.BindPFlag("session.inactive_cursors", cmd.PersistentFlags().Lookup("session.inactive_cursors")); err != nil {
return err
}
2021-08-24 04:41:19 +12:00
cmd.PersistentFlags().Bool("session.merciful_reconnect", true, "allow reconnecting to websocket even if previous connection was not closed")
if err := viper.BindPFlag("session.merciful_reconnect", cmd.PersistentFlags().Lookup("session.merciful_reconnect")); err != nil {
return err
}
2021-03-17 03:24:58 +13:00
cmd.PersistentFlags().String("session.api_token", "", "API token for interacting with external services")
if err := viper.BindPFlag("session.api_token", cmd.PersistentFlags().Lookup("session.api_token")); err != nil {
2021-03-16 01:34:14 +13:00
return err
}
2021-03-18 02:09:10 +13:00
// cookie
2021-04-25 06:53:37 +12:00
cmd.PersistentFlags().Bool("session.cookie.enabled", true, "whether cookies authentication should be enabled")
if err := viper.BindPFlag("session.cookie.enabled", cmd.PersistentFlags().Lookup("session.cookie.enabled")); err != nil {
return err
}
2021-03-18 02:09:10 +13:00
cmd.PersistentFlags().String("session.cookie.name", "NEKO_SESSION", "name of the cookie that holds token")
if err := viper.BindPFlag("session.cookie.name", cmd.PersistentFlags().Lookup("session.cookie.name")); err != nil {
return err
}
cmd.PersistentFlags().Int("session.cookie.expiration", 365*24, "expiration of the cookie in hours")
if err := viper.BindPFlag("session.cookie.expiration", cmd.PersistentFlags().Lookup("session.cookie.expiration")); err != nil {
return err
}
cmd.PersistentFlags().Bool("session.cookie.secure", true, "use secure cookies")
if err := viper.BindPFlag("session.cookie.secure", cmd.PersistentFlags().Lookup("session.cookie.secure")); err != nil {
return err
}
return nil
}
2024-07-19 07:48:09 +12:00
func (Session) InitV2(cmd *cobra.Command) error {
2024-07-19 07:54:53 +12:00
cmd.PersistentFlags().StringSlice("locks", []string{}, "V2: resources, that will be locked when starting (control, login)")
2024-07-19 07:48:09 +12:00
if err := viper.BindPFlag("locks", cmd.PersistentFlags().Lookup("locks")); err != nil {
return err
}
2024-07-19 07:54:53 +12:00
cmd.PersistentFlags().Bool("control_protection", false, "V2: control protection means, users can gain control only if at least one admin is in the room")
2024-07-19 07:48:09 +12:00
if err := viper.BindPFlag("control_protection", cmd.PersistentFlags().Lookup("control_protection")); err != nil {
return err
}
2024-07-19 07:54:53 +12:00
cmd.PersistentFlags().Bool("implicit_control", false, "V2: if enabled members can gain control implicitly")
2024-07-19 07:48:09 +12:00
if err := viper.BindPFlag("implicit_control", cmd.PersistentFlags().Lookup("implicit_control")); err != nil {
return err
}
return nil
}
2020-11-02 06:39:12 +13:00
func (s *Session) Set() {
2023-03-28 05:33:51 +13:00
s.File = viper.GetString("session.file")
s.PrivateMode = viper.GetBool("session.private_mode")
2024-04-21 00:27:15 +12:00
s.LockedLogins = viper.GetBool("session.locked_logins")
s.LockedControls = viper.GetBool("session.locked_controls")
2024-04-22 06:10:16 +12:00
s.ControlProtection = viper.GetBool("session.control_protection")
2021-03-17 03:24:58 +13:00
s.ImplicitHosting = viper.GetBool("session.implicit_hosting")
2021-11-02 05:49:02 +13:00
s.InactiveCursors = viper.GetBool("session.inactive_cursors")
2021-08-24 04:41:19 +12:00
s.MercifulReconnect = viper.GetBool("session.merciful_reconnect")
2021-03-17 03:24:58 +13:00
s.APIToken = viper.GetString("session.api_token")
2021-03-18 02:09:10 +13:00
2021-04-25 06:53:37 +12:00
s.CookieEnabled = viper.GetBool("session.cookie.enabled")
2021-03-18 02:09:10 +13:00
s.CookieName = viper.GetString("session.cookie.name")
2022-01-17 09:03:25 +13:00
s.CookieExpiration = time.Duration(viper.GetInt("session.cookie.expiration")) * time.Hour
2021-03-18 02:09:10 +13:00
s.CookieSecure = viper.GetBool("session.cookie.secure")
}
2024-07-19 07:48:09 +12:00
func (s *Session) SetV2() {
if viper.IsSet("locks") {
locks := viper.GetStringSlice("locks")
for _, lock := range locks {
switch lock {
// TODO: file_transfer
case "control":
s.LockedControls = true
case "login":
s.LockedLogins = true
}
}
log.Warn().Msg("you are using v2 configuration 'NEKO_LOCKS' which is deprecated, please use 'NEKO_SESSION_LOCKED_CONTROLS' and 'NEKO_SESSION_LOCKED_LOGINS' instead")
}
if viper.IsSet("implicit_control") {
s.ImplicitHosting = viper.GetBool("implicit_control")
log.Warn().Msg("you are using v2 configuration 'NEKO_IMPLICIT_CONTROL' which is deprecated, please use 'NEKO_SESSION_IMPLICIT_HOSTING' instead")
}
if viper.IsSet("control_protection") {
s.ControlProtection = viper.GetBool("control_protection")
log.Warn().Msg("you are using v2 configuration 'NEKO_CONTROL_PROTECTION' which is deprecated, please use 'NEKO_SESSION_CONTROL_PROTECTION' instead")
}
}