2023-01-03 09:18:30 +00:00
|
|
|
// Copyright (C) 2019-2023 Nicola Murino
|
2022-07-17 18:16:00 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2023-01-03 09:18:30 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-07-17 18:16:00 +00:00
|
|
|
|
2022-03-13 12:45:07 +00:00
|
|
|
package dataprovider
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-05-09 09:19:29 +00:00
|
|
|
"sync/atomic"
|
2022-03-13 12:45:07 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/robfig/cron/v3"
|
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/logger"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/metric"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/util"
|
2022-03-13 12:45:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-02-09 08:33:33 +00:00
|
|
|
scheduler *cron.Cron
|
|
|
|
lastUserCacheUpdate atomic.Int64
|
|
|
|
lastIPListsCacheUpdate atomic.Int64
|
2022-07-11 06:17:36 +00:00
|
|
|
// used for bolt and memory providers, so we avoid iterating all users/rules
|
2022-05-09 09:19:29 +00:00
|
|
|
// to find recently modified ones
|
2022-08-30 13:47:41 +00:00
|
|
|
lastUserUpdate atomic.Int64
|
|
|
|
lastRuleUpdate atomic.Int64
|
2022-03-13 12:45:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func stopScheduler() {
|
|
|
|
if scheduler != nil {
|
|
|
|
scheduler.Stop()
|
|
|
|
scheduler = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func startScheduler() error {
|
|
|
|
stopScheduler()
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
scheduler = cron.New(cron.WithLocation(time.UTC))
|
2022-09-25 17:48:55 +00:00
|
|
|
_, err := scheduler.AddFunc("@every 55s", checkDataprovider)
|
2022-03-13 12:45:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to schedule dataprovider availability check: %w", err)
|
|
|
|
}
|
|
|
|
err = addScheduledCacheUpdates()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-25 17:48:55 +00:00
|
|
|
if currentNode != nil {
|
|
|
|
_, err = scheduler.AddFunc("@every 30m", func() {
|
|
|
|
err := provider.cleanupNodes()
|
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelError, "unable to cleanup nodes: %v", err)
|
|
|
|
} else {
|
|
|
|
providerLog(logger.LevelDebug, "cleanup nodes ok")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to schedule nodes cleanup: %w", err)
|
|
|
|
}
|
2022-03-13 12:45:07 +00:00
|
|
|
scheduler.Start()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func addScheduledCacheUpdates() error {
|
2022-08-30 13:47:41 +00:00
|
|
|
lastUserCacheUpdate.Store(util.GetTimeAsMsSinceEpoch(time.Now()))
|
2023-02-09 08:33:33 +00:00
|
|
|
lastIPListsCacheUpdate.Store(util.GetTimeAsMsSinceEpoch(time.Now()))
|
2022-03-13 12:45:07 +00:00
|
|
|
_, err := scheduler.AddFunc("@every 10m", checkCacheUpdates)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to schedule cache updates: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkDataprovider() {
|
2022-09-25 17:48:55 +00:00
|
|
|
if currentNode != nil {
|
|
|
|
if err := provider.updateNodeTimestamp(); err != nil {
|
|
|
|
providerLog(logger.LevelError, "unable to update node timestamp: %v", err)
|
|
|
|
} else {
|
|
|
|
providerLog(logger.LevelDebug, "node timestamp updated")
|
|
|
|
}
|
|
|
|
}
|
2022-03-13 12:45:07 +00:00
|
|
|
err := provider.checkAvailability()
|
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelError, "check availability error: %v", err)
|
|
|
|
}
|
|
|
|
metric.UpdateDataProviderAvailability(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkCacheUpdates() {
|
2023-02-09 08:33:33 +00:00
|
|
|
checkUserCache()
|
|
|
|
checkIPListEntryCache()
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkUserCache() {
|
|
|
|
lastCheck := lastUserCacheUpdate.Load()
|
|
|
|
providerLog(logger.LevelDebug, "start user cache check, update time %v", util.GetTimeFromMsecSinceEpoch(lastCheck))
|
2022-03-13 12:45:07 +00:00
|
|
|
checkTime := util.GetTimeAsMsSinceEpoch(time.Now())
|
2023-02-09 08:33:33 +00:00
|
|
|
if config.IsShared == 1 {
|
|
|
|
lastCheck -= 5000
|
|
|
|
}
|
|
|
|
users, err := provider.getRecentlyUpdatedUsers(lastCheck)
|
2022-03-13 12:45:07 +00:00
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelError, "unable to get recently updated users: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2023-02-09 08:33:33 +00:00
|
|
|
for idx := range users {
|
|
|
|
user := users[idx]
|
2022-07-17 16:48:41 +00:00
|
|
|
providerLog(logger.LevelDebug, "invalidate caches for user %q", user.Username)
|
|
|
|
if user.DeletedAt > 0 {
|
|
|
|
deletedAt := util.GetTimeFromMsecSinceEpoch(user.DeletedAt)
|
|
|
|
if deletedAt.Add(30 * time.Minute).Before(time.Now()) {
|
|
|
|
providerLog(logger.LevelDebug, "removing user %q deleted at %s", user.Username, deletedAt)
|
|
|
|
go provider.deleteUser(user, false) //nolint:errcheck
|
|
|
|
}
|
|
|
|
webDAVUsersCache.remove(user.Username)
|
|
|
|
delayedQuotaUpdater.resetUserQuota(user.Username)
|
|
|
|
} else {
|
|
|
|
webDAVUsersCache.swap(&user)
|
|
|
|
}
|
2022-03-13 12:45:07 +00:00
|
|
|
cachedPasswords.Remove(user.Username)
|
|
|
|
}
|
2022-08-30 13:47:41 +00:00
|
|
|
lastUserCacheUpdate.Store(checkTime)
|
|
|
|
providerLog(logger.LevelDebug, "end user cache check, new update time %v", util.GetTimeFromMsecSinceEpoch(lastUserCacheUpdate.Load()))
|
2022-03-13 12:45:07 +00:00
|
|
|
}
|
2022-05-09 09:19:29 +00:00
|
|
|
|
2023-02-09 08:33:33 +00:00
|
|
|
func checkIPListEntryCache() {
|
|
|
|
if config.IsShared != 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
hasMemoryLists := false
|
|
|
|
for _, l := range inMemoryLists {
|
|
|
|
if l.isInMemory.Load() {
|
|
|
|
hasMemoryLists = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !hasMemoryLists {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
providerLog(logger.LevelDebug, "start IP list cache check, update time %v", util.GetTimeFromMsecSinceEpoch(lastIPListsCacheUpdate.Load()))
|
|
|
|
checkTime := util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
entries, err := provider.getRecentlyUpdatedIPListEntries(lastIPListsCacheUpdate.Load() - 5000)
|
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelError, "unable to get recently updated IP list entries: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for idx := range entries {
|
|
|
|
e := entries[idx]
|
|
|
|
providerLog(logger.LevelDebug, "update cache for IP list entry %q", e.getName())
|
|
|
|
if e.DeletedAt > 0 {
|
|
|
|
deletedAt := util.GetTimeFromMsecSinceEpoch(e.DeletedAt)
|
|
|
|
if deletedAt.Add(30 * time.Minute).Before(time.Now()) {
|
|
|
|
providerLog(logger.LevelDebug, "removing IP list entry %q deleted at %s", e.getName(), deletedAt)
|
|
|
|
go provider.deleteIPListEntry(e, false) //nolint:errcheck
|
|
|
|
}
|
|
|
|
for _, l := range inMemoryLists {
|
|
|
|
l.removeEntry(&e)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, l := range inMemoryLists {
|
|
|
|
l.updateEntry(&e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lastIPListsCacheUpdate.Store(checkTime)
|
|
|
|
providerLog(logger.LevelDebug, "end IP list entries cache check, new update time %v", util.GetTimeFromMsecSinceEpoch(lastIPListsCacheUpdate.Load()))
|
|
|
|
}
|
|
|
|
|
2022-05-09 09:19:29 +00:00
|
|
|
func setLastUserUpdate() {
|
2022-08-30 13:47:41 +00:00
|
|
|
lastUserUpdate.Store(util.GetTimeAsMsSinceEpoch(time.Now()))
|
2022-05-09 09:19:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getLastUserUpdate() int64 {
|
2022-08-30 13:47:41 +00:00
|
|
|
return lastUserUpdate.Load()
|
2022-05-09 09:19:29 +00:00
|
|
|
}
|
2022-07-11 06:17:36 +00:00
|
|
|
|
|
|
|
func setLastRuleUpdate() {
|
2022-08-30 13:47:41 +00:00
|
|
|
lastRuleUpdate.Store(util.GetTimeAsMsSinceEpoch(time.Now()))
|
2022-07-11 06:17:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getLastRuleUpdate() int64 {
|
2022-08-30 13:47:41 +00:00
|
|
|
return lastRuleUpdate.Load()
|
2022-07-11 06:17:36 +00:00
|
|
|
}
|