Make session length configurable

This commit is contained in:
Bolke de Bruin 2022-10-22 10:17:43 +02:00
parent 2abf83f0be
commit 43eb2d5f47
3 changed files with 12 additions and 4 deletions

View file

@ -45,6 +45,7 @@ type ServerConfig struct {
SessionKey string `koanf:"sessionkey"` SessionKey string `koanf:"sessionkey"`
SessionEncryptionKey string `koanf:"sessionencryptionkey"` SessionEncryptionKey string `koanf:"sessionencryptionkey"`
SessionStore string `koanf:"sessionstore"` SessionStore string `koanf:"sessionstore"`
MaxSessionLength int `koanf:"maxsessionlength"`
SendBuf int `koanf:"sendbuf"` SendBuf int `koanf:"sendbuf"`
ReceiveBuf int `koanf:"receivebuf"` ReceiveBuf int `koanf:"receivebuf"`
Tls string `koanf:"tls"` Tls string `koanf:"tls"`

View file

@ -94,7 +94,11 @@ func main() {
security.Hosts = conf.Server.Hosts security.Hosts = conf.Server.Hosts
// init session store // init session store
web.InitStore([]byte(conf.Server.SessionKey), []byte(conf.Server.SessionEncryptionKey), conf.Server.SessionStore) web.InitStore([]byte(conf.Server.SessionKey),
[]byte(conf.Server.SessionEncryptionKey),
conf.Server.SessionStore,
conf.Server.MaxSessionLength,
)
// configure web backend // configure web backend
w := &web.Config{ w := &web.Config{

View file

@ -17,7 +17,7 @@ const (
var sessionStore sessions.Store var sessionStore sessions.Store
func InitStore(sessionKey []byte, encryptionKey []byte, storeType string) { func InitStore(sessionKey []byte, encryptionKey []byte, storeType string, maxLength int) {
if len(sessionKey) < 32 { if len(sessionKey) < 32 {
log.Fatal("Session key too small") log.Fatal("Session key too small")
} }
@ -30,8 +30,11 @@ func InitStore(sessionKey []byte, encryptionKey []byte, storeType string) {
fs := sessions.NewFilesystemStore(os.TempDir(), sessionKey, encryptionKey) fs := sessions.NewFilesystemStore(os.TempDir(), sessionKey, encryptionKey)
// set max length // set max length
log.Printf("Setting maximum session storage to %d bytes", maxSessionLength) if maxLength == 0 {
fs.MaxLength(maxSessionLength) maxLength = maxSessionLength
}
log.Printf("Setting maximum session storage to %d bytes", maxLength)
fs.MaxLength(maxLength)
sessionStore = fs sessionStore = fs
} else { } else {