mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-02 07:24:48 +03:00
5d2d068484
The external traffic webhook was sent from inside the 5-second accounting job: its cadence could not be changed, and a slow receiver held up accounting. Add an interval setting (5 to 3600 seconds, default 5). The accounting poll only sums each tick's deltas per client email and per inbound or outbound tag; once the interval has passed, the batch is posted from its own goroutine. Accounting, quota enforcement and online updates keep their 5-second cadence. Delivery semantics are unchanged: each batch is posted once. A failure drops it, warns once with the reason (never the URI) and reports when deliveries resume. Destinations are checked at dial time and redirects are refused. A settings client that omits the interval keeps the stored value. Fixes #7028
60 lines
2.0 KiB
Go
60 lines
2.0 KiB
Go
package service
|
|
|
|
import (
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
)
|
|
|
|
const (
|
|
externalTrafficInformIntervalKey = "externalTrafficInformInterval"
|
|
minExternalTrafficInformInterval = 5
|
|
maxExternalTrafficInformInterval = 3600
|
|
)
|
|
|
|
// ExternalTrafficInformConfig is one committed snapshot of the webhook settings.
|
|
type ExternalTrafficInformConfig struct {
|
|
Enabled bool
|
|
URI string
|
|
Interval time.Duration
|
|
// SkipReason says why an enabled webhook cannot be sent; it never quotes the stored URI.
|
|
SkipReason string
|
|
}
|
|
|
|
func (s *SettingService) GetExternalTrafficInformConfig() (ExternalTrafficInformConfig, error) {
|
|
keys := []string{"externalTrafficInformEnable", "externalTrafficInformURI", externalTrafficInformIntervalKey}
|
|
var rows []model.Setting
|
|
if err := database.GetDB().Where("key IN ?", keys).Find(&rows).Error; err != nil {
|
|
return ExternalTrafficInformConfig{}, err
|
|
}
|
|
stored := make(map[string]string, len(rows))
|
|
for _, row := range rows {
|
|
stored[row.Key] = row.Value
|
|
}
|
|
effective := func(key string) string { return effectiveSettingValue(key, stored[key]) }
|
|
if effective("externalTrafficInformEnable") != "true" {
|
|
return ExternalTrafficInformConfig{}, nil
|
|
}
|
|
seconds, err := strconv.Atoi(effective(externalTrafficInformIntervalKey))
|
|
if err != nil || !validExternalTrafficInformInterval(seconds) {
|
|
seconds = minExternalTrafficInformInterval
|
|
}
|
|
cfg := ExternalTrafficInformConfig{Enabled: true, Interval: time.Duration(seconds) * time.Second}
|
|
// A url.Parse error quotes the URI, credentials included, so it is not passed on.
|
|
switch uri, err := SanitizeHTTPURL(stored["externalTrafficInformURI"]); {
|
|
case err != nil:
|
|
cfg.SkipReason = "ExternalTrafficInformURI is not a valid http(s) URL"
|
|
case uri == "":
|
|
cfg.SkipReason = "ExternalTrafficInformURI is empty"
|
|
default:
|
|
cfg.URI = uri
|
|
}
|
|
return cfg, nil
|
|
}
|
|
|
|
func validExternalTrafficInformInterval(seconds int) bool {
|
|
return seconds >= minExternalTrafficInformInterval && seconds <= maxExternalTrafficInformInterval
|
|
}
|