@@ -8,17 +8,57 @@ import (
88 "log"
99 "net/http"
1010 "os"
11+ "sync"
12+ "time"
1113
1214 tgbotapi "github.com/OvyFlash/telegram-bot-api"
1315 "github.com/joho/godotenv"
1416)
1517
18+ // 403 error tracking
19+ var (
20+ forbidden403Count int
21+ forbidden403Mutex sync.Mutex
22+ forbidden403MaxCount = 5
23+ forbidden403Window = 5 * time .Minute
24+ forbidden403Times []time.Time
25+ )
26+
27+ var Err403TooMany = fmt .Errorf ("слишком много ошибок 403 при доступе к базе данных" )
28+
29+ func track403Error () {
30+ forbidden403Mutex .Lock ()
31+ defer forbidden403Mutex .Unlock ()
32+
33+ now := time .Now ()
34+ cutoff := now .Add (- forbidden403Window )
35+ var recent []time.Time
36+ for _ , t := range forbidden403Times {
37+ if t .After (cutoff ) {
38+ recent = append (recent , t )
39+ }
40+ }
41+ recent = append (recent , now )
42+ forbidden403Times = recent
43+
44+ log .Printf ("Ошибка 403 при доступе к базе данных. Количество за последние %v: %d/%d" ,
45+ forbidden403Window , len (forbidden403Times ), forbidden403MaxCount )
46+
47+ if len (forbidden403Times ) >= forbidden403MaxCount {
48+ log .Printf ("КРИТИЧЕСКАЯ ОШИБКА: Слишком много ошибок 403 (%d за %v). Завершение программы." ,
49+ len (forbidden403Times ), forbidden403Window )
50+ os .Exit (3 )
51+ }
52+ }
53+
1654// PocketBase global credentials
17- var pocketBaseUrl string
18- var email string
19- var password string
20- var authToken string
21- var api_endpint string
55+ var (
56+ pocketBaseUrl string
57+ email string
58+ password string
59+ authToken string
60+ api_endpint string
61+ )
2262
2363// just for sending search requests to pocketbase
2464func sendAuthorizedRequest (method , url string , payload []byte ) ([]byte , error ) {
@@ -44,6 +84,12 @@ func sendAuthorizedRequest(method, url string, payload []byte) ([]byte, error) {
4484 return nil , err
4585 }
4686
87+ // Проверка на 403 ошибку
88+ if resp .StatusCode == http .StatusForbidden {
89+ track403Error ()
90+ return nil , fmt .Errorf ("ошибка 403 Forbidden при доступе к базе данных: %s" , string (body ))
91+ }
92+
4793 return body , nil
4894}
4995
@@ -53,7 +99,7 @@ type FileResponse struct {
5399}
54100
55101func getTelegramFile (bot * tgbotapi.BotAPI , fileID string ) (string , error ) {
56- //file, err := bot.GetFile(tgbotapi.FileConfig{FileID: fileID})
102+ // file, err := bot.GetFile(tgbotapi.FileConfig{FileID: fileID})
57103 CallUrl := fmt .Sprintf ("%s/bot%s/getFile?file_id=%s" , api_endpint , bot .Token , fileID )
58104 resp , err := http .Get (CallUrl )
59105 if err != nil {
@@ -77,7 +123,7 @@ func getTelegramFile(bot *tgbotapi.BotAPI, fileID string) (string, error) {
77123 if ! ok || filePath == "" {
78124 return "" , fmt .Errorf ("не найден путь в ответе сервера" )
79125 }
80- //serverPath := fmt.Sprintf("/storage/%s/%s", bot.Token, filePath.(string))
126+ // serverPath := fmt.Sprintf("/storage/%s/%s", bot.Token, filePath.(string))
81127 return filePath .(string ), nil
82128 } else {
83129 return "" , fmt .Errorf ("ошибка получения пути: %v" , resp .StatusCode )
0 commit comments