mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-08-31 15:37:14 +00:00
feat(iplimit): gate IP limit on fail2ban and reset stale limits
Per-client IP limit only enforces where fail2ban is installed, so the panel now reports enforceability and disables the field otherwise: - Add GET /panel/api/server/fail2banStatus (enabled/installed/usable/windows), cached 30s. - ClientFormModal and ClientBulkAddModal disable the IP Limit input when not usable and show a hover tooltip; Windows gets a platform-specific message instead of the bash-menu hint. - One-time migration ResetIpLimitNoFail2ban zeroes existing client limitIp (inbound settings JSON + clients table) on hosts without fail2ban, where the limit never applied. - Drop the recurring '[LimitIP] Fail2Ban is not installed' warning. - Add limitIpFail2banMissing/limitIpFail2banWindows/limitIpDisabled across all 13 locales.
This commit is contained in:
@@ -142,6 +142,10 @@ type ServerService struct {
|
||||
|
||||
versionsCacheMu sync.Mutex
|
||||
versionsCache *cachedXrayVersions
|
||||
|
||||
fail2banMu sync.Mutex
|
||||
fail2banInstalled bool
|
||||
fail2banCheckedAt time.Time
|
||||
}
|
||||
|
||||
type cachedXrayVersions struct {
|
||||
@@ -185,6 +189,53 @@ func (s *ServerService) LastStatus() *Status {
|
||||
return s.lastStatus
|
||||
}
|
||||
|
||||
// Fail2banStatus tells the frontend whether the per-client IP limit can
|
||||
// actually be enforced. Enforcement depends on fail2ban, so a limit set
|
||||
// without it would silently do nothing.
|
||||
type Fail2banStatus struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
Installed bool `json:"installed"`
|
||||
Usable bool `json:"usable"`
|
||||
Windows bool `json:"windows"`
|
||||
}
|
||||
|
||||
const fail2banInstalledCacheTTL = 30 * time.Second
|
||||
|
||||
func (s *ServerService) GetFail2banStatus() Fail2banStatus {
|
||||
enabled := isFail2banEnabled()
|
||||
|
||||
installed := false
|
||||
if enabled {
|
||||
installed = s.isFail2banInstalled()
|
||||
}
|
||||
|
||||
return Fail2banStatus{
|
||||
Enabled: enabled,
|
||||
Installed: installed,
|
||||
Usable: enabled && installed,
|
||||
Windows: runtime.GOOS == "windows",
|
||||
}
|
||||
}
|
||||
|
||||
func isFail2banEnabled() bool {
|
||||
value, ok := os.LookupEnv("XUI_ENABLE_FAIL2BAN")
|
||||
return !ok || value == "true"
|
||||
}
|
||||
|
||||
func (s *ServerService) isFail2banInstalled() bool {
|
||||
s.fail2banMu.Lock()
|
||||
defer s.fail2banMu.Unlock()
|
||||
|
||||
if !s.fail2banCheckedAt.IsZero() && time.Since(s.fail2banCheckedAt) < fail2banInstalledCacheTTL {
|
||||
return s.fail2banInstalled
|
||||
}
|
||||
|
||||
err := exec.Command("fail2ban-client", "-h").Run()
|
||||
s.fail2banInstalled = err == nil
|
||||
s.fail2banCheckedAt = time.Now()
|
||||
return s.fail2banInstalled
|
||||
}
|
||||
|
||||
// RefreshStatus collects a new system snapshot, stores it as LastStatus, and
|
||||
// appends it to the system-metrics time series. Returns the new snapshot (may
|
||||
// be nil if collection failed). Called by the background ticker; the caller is
|
||||
|
||||
Reference in New Issue
Block a user