mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 12:17:24 +00:00
Move request limiter to middleware.
This commit is contained in:
@@ -13,7 +13,6 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v3/log"
|
||||
@@ -25,23 +24,6 @@ const (
|
||||
storageKeyUnavailable = "storage_key_unavailable" // Placeholder for unavailable storage key
|
||||
)
|
||||
|
||||
var (
|
||||
ipDownloads = sync.Map{}
|
||||
)
|
||||
|
||||
func init() {
|
||||
go func() {
|
||||
for {
|
||||
// Clean up old IP download records every 24 hours
|
||||
time.Sleep(24 * time.Hour)
|
||||
ipDownloads.Range(func(key, value interface{}) bool {
|
||||
ipDownloads.Delete(key)
|
||||
return true
|
||||
})
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func getUploadingSize() int64 {
|
||||
return dao.GetStatistic("uploading_size")
|
||||
}
|
||||
@@ -405,7 +387,7 @@ func GetFile(fid string) (*model.FileView, error) {
|
||||
}
|
||||
|
||||
// DownloadFile handles the file download request. Return a presigned URL or a direct file path.
|
||||
func DownloadFile(ip, fid, cfToken string) (string, string, error) {
|
||||
func DownloadFile(fid, cfToken string) (string, string, error) {
|
||||
passed, err := verifyCfToken(cfToken)
|
||||
if err != nil {
|
||||
log.Error("failed to verify cf token: ", err)
|
||||
@@ -415,17 +397,6 @@ func DownloadFile(ip, fid, cfToken string) (string, string, error) {
|
||||
log.Info("cf token verification failed")
|
||||
return "", "", model.NewRequestError("cf token verification failed")
|
||||
}
|
||||
log.Info("File download request from: " + ip)
|
||||
downloads, _ := ipDownloads.Load(ip)
|
||||
if downloads == nil {
|
||||
ipDownloads.Store(ip, 1)
|
||||
} else {
|
||||
count := downloads.(int)
|
||||
if count >= config.MaxDownloadsPerDayForSingleIP() {
|
||||
return "", "", model.NewRequestError("Too many requests, please try again later")
|
||||
}
|
||||
ipDownloads.Store(ip, count+1)
|
||||
}
|
||||
file, err := dao.GetFile(fid)
|
||||
if err != nil {
|
||||
log.Error("failed to get file: ", err)
|
||||
|
Reference in New Issue
Block a user