2026-06-01 18:26:54 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
2026-08-24 12:51:34 +08:00
|
|
|
"context"
|
2026-09-11 15:16:44 +08:00
|
|
|
"encoding/json"
|
2026-08-27 14:00:43 +08:00
|
|
|
"errors"
|
2026-06-01 18:26:54 +08:00
|
|
|
"fmt"
|
2026-09-15 23:55:01 +08:00
|
|
|
"slices"
|
2026-09-21 15:09:13 +08:00
|
|
|
"strings"
|
2026-09-11 15:16:44 +08:00
|
|
|
"sync"
|
2026-06-01 18:26:54 +08:00
|
|
|
|
2026-08-24 12:51:34 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/agent/app/dto"
|
2026-10-08 18:19:30 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/agent/app/task"
|
2026-09-21 15:09:13 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/agent/buserr"
|
2026-06-01 18:26:54 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/agent/constant"
|
2026-09-11 15:16:44 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/agent/global"
|
2026-08-24 12:51:34 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/agent/utils/cmd"
|
2026-06-01 18:26:54 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/agent/utils/firewall"
|
2026-09-21 15:09:13 +08:00
|
|
|
dockerfirewall "github.com/1Panel-dev/1Panel/agent/utils/firewall/docker_guard"
|
2026-08-24 12:51:34 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/agent/utils/firewall/filter"
|
2026-10-08 18:19:30 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/agent/utils/firewall/forwarding"
|
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/utils/firewall/iptables_helper"
|
2026-08-24 12:51:34 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/agent/utils/firewall/lifecycle"
|
2026-10-08 18:19:30 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/agent/utils/firewall/nftables_helper"
|
2026-09-11 15:16:44 +08:00
|
|
|
"gorm.io/gorm"
|
2026-06-01 18:26:54 +08:00
|
|
|
)
|
|
|
|
|
|
2026-08-24 12:51:34 +08:00
|
|
|
type IFirewallSettingService interface {
|
2026-09-16 22:09:49 +08:00
|
|
|
CreatePortWhitelist(context.Context, dto.FirewallPortWhitelistCreate) error
|
|
|
|
|
UpdatePortWhitelist(context.Context, dto.FirewallPortWhitelistUpdate) error
|
|
|
|
|
DeletePortWhitelist(context.Context, dto.FirewallPortWhitelistDelete) error
|
2026-08-24 12:51:34 +08:00
|
|
|
Load(context.Context) (dto.FirewallSettings, error)
|
|
|
|
|
Operate(context.Context, dto.FirewallBackendOperation) error
|
2026-10-08 18:19:30 +08:00
|
|
|
OperateFamily(dto.FirewallFamilyOperation) (dto.FilterChainOperationResponse, error)
|
|
|
|
|
OperateIPv6(dto.FirewallIPv6Operation) (dto.FilterChainOperationResponse, error)
|
2026-06-01 18:26:54 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-24 12:51:34 +08:00
|
|
|
type FirewallSettingService struct{}
|
|
|
|
|
|
2026-09-16 22:09:49 +08:00
|
|
|
var firewallWhitelistMu sync.Mutex
|
2026-09-11 15:16:44 +08:00
|
|
|
|
2026-10-08 18:19:30 +08:00
|
|
|
func (s *FirewallSettingService) CreatePortWhitelist(ctx context.Context, request dto.FirewallPortWhitelistCreate) (result error) {
|
2026-09-21 15:09:13 +08:00
|
|
|
firewallWhitelistMu.Lock()
|
|
|
|
|
firewallRuleMutationMu.Lock()
|
2026-10-08 18:19:30 +08:00
|
|
|
defer func() {
|
|
|
|
|
firewallRuleMutationMu.Unlock()
|
|
|
|
|
firewallWhitelistMu.Unlock()
|
|
|
|
|
if result == nil {
|
|
|
|
|
result = newFirewallService().SyncPortWhitelist(ctx)
|
2026-09-21 15:09:13 +08:00
|
|
|
}
|
2026-10-08 18:19:30 +08:00
|
|
|
}()
|
|
|
|
|
current, err := loadFirewallPortWhiteList()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
current = append(current, request.Rule)
|
|
|
|
|
current, err = firewall.ValidatePortWhitelist(current)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if err := validateFirewallWhitelistFamilies(current); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
value, err := json.Marshal(current)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return settingRepo.UpdateOrCreate(constant.FirewallPortWhiteList, string(value))
|
2026-09-15 23:55:01 +08:00
|
|
|
}
|
|
|
|
|
|
2026-10-08 18:19:30 +08:00
|
|
|
func (s *FirewallSettingService) UpdatePortWhitelist(ctx context.Context, request dto.FirewallPortWhitelistUpdate) (result error) {
|
2026-09-21 15:09:13 +08:00
|
|
|
firewallWhitelistMu.Lock()
|
|
|
|
|
firewallRuleMutationMu.Lock()
|
2026-10-08 18:19:30 +08:00
|
|
|
defer func() {
|
|
|
|
|
firewallRuleMutationMu.Unlock()
|
|
|
|
|
firewallWhitelistMu.Unlock()
|
|
|
|
|
if result == nil {
|
|
|
|
|
result = newFirewallService().SyncPortWhitelist(ctx)
|
2026-09-21 15:09:13 +08:00
|
|
|
}
|
2026-10-08 18:19:30 +08:00
|
|
|
}()
|
|
|
|
|
current, err := loadFirewallPortWhiteList()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
index, err := findPortWhitelistRule(current, request.OldRule)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
current[index] = request.Rule
|
|
|
|
|
current, err = firewall.ValidatePortWhitelist(current)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if err := validateFirewallWhitelistFamilies(current); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
value, err := json.Marshal(current)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return settingRepo.UpdateOrCreate(constant.FirewallPortWhiteList, string(value))
|
2026-09-15 23:55:01 +08:00
|
|
|
}
|
|
|
|
|
|
2026-09-16 22:09:49 +08:00
|
|
|
func (s *FirewallSettingService) DeletePortWhitelist(ctx context.Context, request dto.FirewallPortWhitelistDelete) error {
|
|
|
|
|
firewallWhitelistMu.Lock()
|
2026-09-11 15:16:44 +08:00
|
|
|
firewallRuleMutationMu.Lock()
|
2026-10-08 18:19:30 +08:00
|
|
|
defer func() {
|
|
|
|
|
firewallRuleMutationMu.Unlock()
|
|
|
|
|
firewallWhitelistMu.Unlock()
|
|
|
|
|
}()
|
|
|
|
|
current, err := loadFirewallPortWhiteList()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if request.Rule == nil {
|
|
|
|
|
return filter.ErrInvalidRule
|
|
|
|
|
}
|
|
|
|
|
index, err := findPortWhitelistRule(current, *request.Rule)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
current = slices.Delete(current, index, index+1)
|
|
|
|
|
current, err = firewall.ValidatePortWhitelist(current)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if err := validateFirewallWhitelistFamilies(current); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
value, err := json.Marshal(current)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return settingRepo.UpdateOrCreate(constant.FirewallPortWhiteList, string(value))
|
2026-09-11 15:16:44 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-24 12:51:34 +08:00
|
|
|
func (s *FirewallSettingService) Load(ctx context.Context) (dto.FirewallSettings, error) {
|
2026-10-08 18:19:30 +08:00
|
|
|
families, err := loadFirewallFamilies()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return dto.FirewallSettings{}, err
|
|
|
|
|
}
|
|
|
|
|
result := dto.FirewallSettings{PingStatus: firewall.LoadPingStatus(), IPv6Enabled: slices.Contains(families, constant.FirewallFamilyIPv6)}
|
2026-08-24 12:51:34 +08:00
|
|
|
|
|
|
|
|
installed := make(map[string]bool)
|
|
|
|
|
for _, name := range lifecycle.InstalledProviders() {
|
|
|
|
|
installed[name] = true
|
|
|
|
|
}
|
2026-09-21 15:09:13 +08:00
|
|
|
systemBackend, _ := settingRepo.GetValueByKey(constant.FirewallSystemBackendKey)
|
|
|
|
|
result.System.Selected = strings.TrimSpace(systemBackend)
|
2026-08-27 22:24:15 +08:00
|
|
|
if result.System.Selected == "" {
|
2026-09-21 15:09:13 +08:00
|
|
|
if client, err := lifecycle.NewClient(""); err == nil {
|
2026-08-27 22:24:15 +08:00
|
|
|
result.System.Selected = client.Name()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result.System.Current = result.System.Selected
|
2026-08-24 12:51:34 +08:00
|
|
|
for _, name := range []string{
|
|
|
|
|
constant.FirewallProviderFirewalld,
|
|
|
|
|
constant.FirewallProviderUFW,
|
|
|
|
|
constant.FirewallProviderIptables,
|
|
|
|
|
constant.FirewallProviderNftables,
|
|
|
|
|
} {
|
|
|
|
|
option := dto.FirewallBackendOption{Name: name, Installed: installed[name], Supported: true}
|
2026-08-27 22:24:15 +08:00
|
|
|
if option.Installed && name == result.System.Selected {
|
2026-09-21 15:09:13 +08:00
|
|
|
client, err := lifecycle.NewClient(name)
|
2026-08-24 12:51:34 +08:00
|
|
|
if err != nil {
|
|
|
|
|
option.Message = err.Error()
|
2026-09-21 15:09:13 +08:00
|
|
|
} else if name == constant.FirewallProviderIptables || name == constant.FirewallProviderNftables {
|
2026-10-08 18:19:30 +08:00
|
|
|
overview, err := loadSystemFirewallOverview(name, "base", families)
|
2026-08-24 17:40:36 +08:00
|
|
|
if err != nil {
|
|
|
|
|
option.Message = err.Error()
|
|
|
|
|
}
|
2026-09-21 15:09:13 +08:00
|
|
|
option.Initialized, option.Bound = overview.IsInit, overview.IsBind
|
|
|
|
|
option.IPv4, option.IPv6 = overview.IPv4, overview.IPv6
|
2026-08-24 17:40:36 +08:00
|
|
|
} else if option.Active, err = client.Status(); err != nil {
|
|
|
|
|
option.Message = err.Error()
|
2026-08-24 12:51:34 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-08-27 22:24:15 +08:00
|
|
|
if name == result.System.Selected && name == constant.FirewallProviderIptables {
|
2026-08-24 12:51:34 +08:00
|
|
|
if commands, err := lifecycle.ResolveIptablesCommands(); err == nil {
|
|
|
|
|
option.Implementation = commands.IPv4
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result.System.Options = append(result.System.Options, option)
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-21 15:09:13 +08:00
|
|
|
forwardingBackend, _ := settingRepo.GetValueByKey(constant.FirewallForwardingBackendKey)
|
|
|
|
|
result.Forwarding.Selected = strings.TrimSpace(forwardingBackend)
|
|
|
|
|
if result.Forwarding.Selected == "" {
|
|
|
|
|
result.Forwarding.Selected = constant.FirewallProviderIptables
|
|
|
|
|
}
|
2026-08-27 22:24:15 +08:00
|
|
|
result.Forwarding.Current = result.Forwarding.Selected
|
2026-08-24 12:51:34 +08:00
|
|
|
for _, name := range []string{constant.FirewallProviderIptables, constant.FirewallProviderNftables} {
|
|
|
|
|
option := dto.FirewallBackendOption{Name: name, Installed: installed[name], Supported: true}
|
2026-08-27 22:24:15 +08:00
|
|
|
if option.Installed && name == result.Forwarding.Selected {
|
2026-10-08 18:19:30 +08:00
|
|
|
manager, err := newForwardingAdapterFor(ctx, name)
|
2026-08-24 12:51:34 +08:00
|
|
|
if err != nil {
|
|
|
|
|
option.Message = err.Error()
|
|
|
|
|
} else {
|
2026-10-08 18:19:30 +08:00
|
|
|
status, statusErr := loadForwardingFirewallOverview(manager, families)
|
2026-09-21 15:09:13 +08:00
|
|
|
option.IPv4, option.IPv6 = status.IPv4, status.IPv6
|
|
|
|
|
if statusErr != nil {
|
|
|
|
|
option.Message = statusErr.Error()
|
|
|
|
|
} else {
|
|
|
|
|
option.Initialized, option.Bound = status.IsInit, status.IsBind
|
2026-08-24 17:40:36 +08:00
|
|
|
}
|
2026-10-08 18:19:30 +08:00
|
|
|
if result.IPv6Enabled && name == constant.FirewallProviderIptables && !option.IPv6.Available {
|
2026-09-21 15:09:13 +08:00
|
|
|
if commands, err := lifecycle.ResolveIptablesCommands(); err == nil && !commands.IPv6Available() {
|
|
|
|
|
option.IPv6.Reason = dockerfirewall.ReasonCommandMissing
|
2026-08-24 17:40:36 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-08-24 12:51:34 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-08-27 22:24:15 +08:00
|
|
|
if name == result.Forwarding.Selected && name == constant.FirewallProviderIptables {
|
2026-08-24 12:51:34 +08:00
|
|
|
if commands, err := lifecycle.ResolveIptablesCommands(); err == nil {
|
|
|
|
|
option.Implementation = commands.IPv4
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result.Forwarding.Options = append(result.Forwarding.Options, option)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dockerInstalled := cmd.Which("docker")
|
2026-08-27 22:24:15 +08:00
|
|
|
dockerVersion := ""
|
|
|
|
|
if dockerInstalled {
|
|
|
|
|
dockerVersion = loadDockerEngineVersion(ctx)
|
|
|
|
|
}
|
2026-09-21 15:09:13 +08:00
|
|
|
dockerBackend, _ := settingRepo.GetValueByKey(constant.FirewallDockerBackendKey)
|
|
|
|
|
dockerBackend = strings.ToLower(strings.TrimSpace(dockerBackend))
|
|
|
|
|
if dockerBackend == constant.FirewallProviderIptables || dockerBackend == constant.FirewallProviderNftables {
|
|
|
|
|
result.Docker.Selected = dockerBackend
|
|
|
|
|
}
|
2026-08-24 12:51:34 +08:00
|
|
|
result.Docker.Current = result.Docker.Selected
|
|
|
|
|
for _, name := range []string{constant.FirewallProviderIptables, constant.FirewallProviderNftables} {
|
|
|
|
|
option := dto.FirewallBackendOption{
|
2026-08-27 10:31:39 +08:00
|
|
|
Name: name, Installed: installed[name], Supported: dockerInstalled,
|
|
|
|
|
Active: dockerInstalled && installed[name] && result.Docker.Selected == name,
|
2026-08-24 12:51:34 +08:00
|
|
|
}
|
2026-08-27 22:24:15 +08:00
|
|
|
if name == constant.FirewallProviderNftables && dockerInstalled && !dockerNftablesSupported(dockerVersion) {
|
|
|
|
|
option.Supported = false
|
|
|
|
|
option.SupportReason = "docker_version_unsupported"
|
|
|
|
|
option.Active = false
|
|
|
|
|
}
|
|
|
|
|
if option.Active {
|
2026-10-08 18:19:30 +08:00
|
|
|
guard := newDockerFirewallRuntime(ctx, name)
|
|
|
|
|
for _, family := range families {
|
|
|
|
|
status := guard.Status(family)
|
|
|
|
|
option.Initialized = option.Initialized || status.Initialized
|
|
|
|
|
option.Bound = option.Bound || status.Bound
|
|
|
|
|
info := dto.FirewallBackendFamilyStatus{
|
|
|
|
|
Available: status.Reason != dockerfirewall.ReasonCommandMissing,
|
|
|
|
|
Initialized: status.Initialized, Bound: status.Bound, Reason: status.Reason,
|
|
|
|
|
}
|
|
|
|
|
if family == constant.FirewallFamilyIPv4 {
|
|
|
|
|
option.IPv4 = info
|
|
|
|
|
} else {
|
|
|
|
|
option.IPv6 = info
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-08-27 22:24:15 +08:00
|
|
|
}
|
2026-08-24 12:51:34 +08:00
|
|
|
result.Docker.Options = append(result.Docker.Options, option)
|
|
|
|
|
}
|
2026-10-08 18:19:30 +08:00
|
|
|
result.PortWhitelist, err = loadPortWhitelistSetting()
|
2026-09-16 15:34:10 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return result, err
|
|
|
|
|
}
|
|
|
|
|
result.PanelPort = LoadPanelPort()
|
|
|
|
|
sshPort, sshErr := loadSSHWhitelistPortFrom(sshPath)
|
|
|
|
|
if sshErr != nil {
|
|
|
|
|
global.LOG.Warnf("load SSH port for firewall settings: %v", sshErr)
|
|
|
|
|
} else {
|
|
|
|
|
result.SSHPort = sshPort
|
|
|
|
|
}
|
2026-09-15 23:55:01 +08:00
|
|
|
return result, err
|
2026-06-01 18:26:54 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-24 12:51:34 +08:00
|
|
|
func (s *FirewallSettingService) Operate(ctx context.Context, request dto.FirewallBackendOperation) error {
|
2026-10-08 18:19:30 +08:00
|
|
|
if request.Operation == "cleanup" {
|
|
|
|
|
_, err := newFirewallService().Reset(ctx, dto.FirewallRuleReset{Subsystem: request.Subsystem, Provider: filter.Provider(request.Backend)})
|
|
|
|
|
return err
|
|
|
|
|
}
|
2026-09-15 10:04:53 +08:00
|
|
|
if err := lockFirewallLifecycleIdle(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer firewallLifecycleTaskMu.Unlock()
|
2026-08-24 12:51:34 +08:00
|
|
|
if request.Subsystem != "system" && request.Backend != constant.FirewallProviderIptables && request.Backend != constant.FirewallProviderNftables {
|
|
|
|
|
return fmt.Errorf("%s only supports iptables or nftables", request.Subsystem)
|
|
|
|
|
}
|
2026-09-21 15:09:13 +08:00
|
|
|
if request.Subsystem == "system" && (request.Backend != constant.FirewallProviderIptables && request.Backend != constant.FirewallProviderNftables) && request.Operation != "select" {
|
2026-08-24 17:40:36 +08:00
|
|
|
return fmt.Errorf("%s does not support initialization or cleanup", request.Backend)
|
|
|
|
|
}
|
2026-08-24 12:51:34 +08:00
|
|
|
switch request.Subsystem {
|
|
|
|
|
case "system":
|
2026-09-07 18:05:31 +08:00
|
|
|
if err := s.operateSystem(request); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if request.Operation == "initialize" {
|
|
|
|
|
service := newFirewallService()
|
2026-09-15 23:55:01 +08:00
|
|
|
whitelistErr := service.SyncPortWhitelist(ctx)
|
2026-10-08 18:19:30 +08:00
|
|
|
return whitelistErr
|
2026-09-07 18:05:31 +08:00
|
|
|
}
|
|
|
|
|
return nil
|
2026-08-24 12:51:34 +08:00
|
|
|
case "forwarding":
|
2026-10-08 18:19:30 +08:00
|
|
|
return s.operateForwarding(ctx, request)
|
2026-08-24 12:51:34 +08:00
|
|
|
case "docker":
|
|
|
|
|
return s.operateDocker(ctx, request)
|
|
|
|
|
default:
|
|
|
|
|
return fmt.Errorf("unsupported firewall subsystem %q", request.Subsystem)
|
2026-06-01 18:26:54 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-08 18:19:30 +08:00
|
|
|
func (s *FirewallSettingService) OperateFamily(request dto.FirewallFamilyOperation) (dto.FilterChainOperationResponse, error) {
|
|
|
|
|
if request.Family != constant.FirewallFamilyIPv4 && request.Family != constant.FirewallFamilyIPv6 {
|
|
|
|
|
return dto.FilterChainOperationResponse{}, filter.ErrInvalidScope
|
|
|
|
|
}
|
|
|
|
|
if request.Backend != constant.FirewallProviderIptables && request.Backend != constant.FirewallProviderNftables {
|
|
|
|
|
return dto.FilterChainOperationResponse{}, filter.ErrUnsupportedScope
|
|
|
|
|
}
|
|
|
|
|
if request.Operation != "initialize" && request.Operation != "repair" && request.Operation != "bind" {
|
|
|
|
|
return dto.FilterChainOperationResponse{}, filter.ErrRuleOperation
|
|
|
|
|
}
|
|
|
|
|
subsystem := ""
|
|
|
|
|
switch request.Subsystem {
|
|
|
|
|
case "system":
|
|
|
|
|
subsystem = firewallTaskHost
|
|
|
|
|
case "forwarding":
|
|
|
|
|
subsystem = firewallTaskForwarding
|
|
|
|
|
case "docker":
|
|
|
|
|
subsystem = firewallTaskDocker
|
|
|
|
|
default:
|
|
|
|
|
return dto.FilterChainOperationResponse{}, filter.ErrInvalidScope
|
|
|
|
|
}
|
|
|
|
|
if err := task.CheckScopeTaskIsExecuting(task.TaskScopeFirewall, 0); err != nil {
|
|
|
|
|
return dto.FilterChainOperationResponse{}, err
|
|
|
|
|
}
|
|
|
|
|
return queueFirewallRuleTask(subsystem, task.TaskExec, "", nil, func(t *task.Task) error {
|
|
|
|
|
if err := lockFirewallLifecycleIdle(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer firewallLifecycleTaskMu.Unlock()
|
|
|
|
|
t.Logf("backend=%s family=%s operation=%s", request.Backend, request.Family, request.Operation)
|
|
|
|
|
families, err := loadFirewallFamilies()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if !slices.Contains(families, request.Family) {
|
|
|
|
|
return fmt.Errorf("IPv6 firewall support is disabled")
|
|
|
|
|
}
|
|
|
|
|
initialize := request.Operation != "bind"
|
|
|
|
|
switch request.Subsystem {
|
|
|
|
|
case "system":
|
|
|
|
|
firewallWhitelistMu.Lock()
|
|
|
|
|
defer firewallWhitelistMu.Unlock()
|
|
|
|
|
if err := newFirewallService().checkSelectedProvider(t.TaskCtx, filter.Provider(request.Backend)); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
ports, err := loadFirewallPortWhiteList()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
required, err := firewall.RequiredPortWhitelist(ports)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
firewallRuleMutationMu.Lock()
|
|
|
|
|
if request.Backend == constant.FirewallProviderIptables {
|
|
|
|
|
err = iptables_helper.OperateFamily(request.Family, initialize, required)
|
|
|
|
|
} else {
|
|
|
|
|
err = nftables_helper.OperateFamily(filter.Family(request.Family), initialize, required)
|
|
|
|
|
}
|
|
|
|
|
firewallRuleMutationMu.Unlock()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if initialize {
|
|
|
|
|
if err := newFirewallService().applyPortWhitelist(t.TaskCtx, ports, nil, filter.Family(request.Family)); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return settingRepo.UpdateOrCreate("IptablesStatus", constant.StatusEnable)
|
|
|
|
|
case "forwarding":
|
|
|
|
|
forwardingMutationMu.Lock()
|
|
|
|
|
defer forwardingMutationMu.Unlock()
|
|
|
|
|
manager, err := newForwardingAdapter(t.TaskCtx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if manager.Name() != request.Backend {
|
|
|
|
|
return filter.ErrProviderUnavailable
|
|
|
|
|
}
|
|
|
|
|
if err := manager.OperateFamily(request.Family, initialize); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
rules, err := manager.List()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if err := persistForwardingRules(manager, rules); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return settingRepo.UpdateOrCreate(constant.FirewallForwardingInitializedKey, constant.StatusEnable)
|
|
|
|
|
default:
|
|
|
|
|
dockerPortGuardServiceMu.Lock()
|
|
|
|
|
defer dockerPortGuardServiceMu.Unlock()
|
|
|
|
|
runtime, backend, err := newDockerPortGuardService().runtimeForDocker(t.TaskCtx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if backend != request.Backend {
|
|
|
|
|
return filter.ErrProviderUnavailable
|
|
|
|
|
}
|
|
|
|
|
if err := runtime.OperateFamily(request.Family, initialize); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
inventory, err := runtime.ListPolicies()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if err := persistDockerRules(backend, inventory); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return settingRepo.UpdateOrCreate(constant.FirewallDockerPortGuardStatusKey, constant.StatusEnable)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *FirewallSettingService) OperateIPv6(request dto.FirewallIPv6Operation) (dto.FilterChainOperationResponse, error) {
|
|
|
|
|
if request.Status != constant.StatusEnable && request.Status != constant.StatusDisable {
|
|
|
|
|
return dto.FilterChainOperationResponse{}, filter.ErrInvalidRule
|
|
|
|
|
}
|
|
|
|
|
if err := task.CheckScopeTaskIsExecuting(task.TaskScopeFirewall, 0); err != nil {
|
|
|
|
|
return dto.FilterChainOperationResponse{}, err
|
|
|
|
|
}
|
|
|
|
|
return queueFirewallRuleTask(firewallTaskHost, task.TaskExec, "", nil, func(t *task.Task) error {
|
|
|
|
|
if err := lockFirewallLifecycleIdle(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer firewallLifecycleTaskMu.Unlock()
|
|
|
|
|
firewallWhitelistMu.Lock()
|
|
|
|
|
defer firewallWhitelistMu.Unlock()
|
|
|
|
|
firewallRuleMutationMu.Lock()
|
|
|
|
|
defer firewallRuleMutationMu.Unlock()
|
|
|
|
|
forwardingMutationMu.Lock()
|
|
|
|
|
defer forwardingMutationMu.Unlock()
|
|
|
|
|
dockerPortGuardServiceMu.Lock()
|
|
|
|
|
defer dockerPortGuardServiceMu.Unlock()
|
|
|
|
|
if request.Status == constant.StatusEnable {
|
|
|
|
|
return settingRepo.UpdateOrCreate(constant.FirewallIPv6SupportKey, request.Status)
|
|
|
|
|
}
|
|
|
|
|
ports, err := loadFirewallPortWhiteList()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
value, err := json.Marshal(ipv4PortWhitelist(ports))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
installed := lifecycle.InstalledProviders()
|
|
|
|
|
for _, selection := range []struct{ subsystem, key string }{
|
|
|
|
|
{"system", constant.FirewallSystemBackendKey},
|
|
|
|
|
{"forwarding", constant.FirewallForwardingBackendKey},
|
|
|
|
|
{"docker", constant.FirewallDockerBackendKey},
|
|
|
|
|
} {
|
|
|
|
|
backend, err := settingRepo.GetValueByKey(selection.key)
|
|
|
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
backend = strings.ToLower(strings.TrimSpace(backend))
|
|
|
|
|
if backend == "" {
|
|
|
|
|
if selection.subsystem == "system" {
|
|
|
|
|
if len(installed) == 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
client, err := NewSelectedSystemFirewallClient()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
backend = client.Name()
|
|
|
|
|
} else {
|
|
|
|
|
backend = constant.FirewallProviderIptables
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !slices.Contains(installed, backend) || (backend != constant.FirewallProviderIptables && backend != constant.FirewallProviderNftables) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
t.Logf("disable IPv6 firewall bindings: subsystem=%s backend=%s", selection.subsystem, backend)
|
|
|
|
|
switch selection.subsystem {
|
|
|
|
|
case "system":
|
|
|
|
|
if backend == constant.FirewallProviderIptables {
|
|
|
|
|
err = iptables_helper.UnbindIPv6BaseChains()
|
|
|
|
|
} else {
|
|
|
|
|
err = nftables_helper.SetTableDormant(t.TaskCtx, "ip6", nftables_helper.TableName)
|
|
|
|
|
if err == nil {
|
|
|
|
|
err = nftables_helper.PersistRuleset(t.TaskCtx)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
case "forwarding":
|
|
|
|
|
var manager forwarding.Adapter
|
|
|
|
|
manager, err = newForwardingAdapterFor(t.TaskCtx, backend)
|
|
|
|
|
if err == nil {
|
|
|
|
|
err = manager.UnbindFamily(constant.FirewallFamilyIPv6)
|
|
|
|
|
}
|
|
|
|
|
case "docker":
|
|
|
|
|
err = newDockerFirewallRuntime(t.TaskCtx, backend).Unbind(constant.FirewallFamilyIPv6)
|
|
|
|
|
}
|
|
|
|
|
if err != nil && !errors.Is(err, filter.ErrFamilyUnavailable) {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if err := t.TaskCtx.Err(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return settingRepo.UpdateValues(map[string]string{constant.FirewallIPv6SupportKey: request.Status, constant.FirewallPortWhiteList: string(value)})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-21 15:09:13 +08:00
|
|
|
func NewIFirewallSettingService() IFirewallSettingService {
|
|
|
|
|
return &FirewallSettingService{}
|
2026-08-25 18:50:27 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-24 12:51:34 +08:00
|
|
|
func (s *FirewallSettingService) operateSystem(request dto.FirewallBackendOperation) error {
|
2026-09-07 18:05:31 +08:00
|
|
|
firewallRuleMutationMu.Lock()
|
|
|
|
|
defer firewallRuleMutationMu.Unlock()
|
2026-09-21 15:09:13 +08:00
|
|
|
if _, err := lifecycle.NewClient(request.Backend); err != nil {
|
2026-08-24 12:51:34 +08:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
previous, _ := settingRepo.GetValueByKey(constant.FirewallSystemBackendKey)
|
|
|
|
|
if previous == "" {
|
2026-09-21 15:09:13 +08:00
|
|
|
if client, err := lifecycle.NewClient(""); err == nil {
|
2026-08-24 12:51:34 +08:00
|
|
|
previous = client.Name()
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-08-27 10:31:39 +08:00
|
|
|
if request.Operation == "select" && previous != "" && previous != request.Backend {
|
|
|
|
|
initialized, err := systemFirewallBackendInitialized(previous)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if initialized {
|
2026-09-21 15:09:13 +08:00
|
|
|
return buserr.WithMap("ErrFirewallBackendCleanupRequired", map[string]interface{}{"current": previous, "target": request.Backend}, nil)
|
2026-08-27 10:31:39 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-08-24 12:51:34 +08:00
|
|
|
if err := settingRepo.UpdateOrCreate(constant.FirewallSystemBackendKey, request.Backend); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
rollback := func(err error) error {
|
|
|
|
|
if err == nil {
|
2026-06-09 11:27:38 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
2026-08-24 12:51:34 +08:00
|
|
|
_ = settingRepo.UpdateOrCreate(constant.FirewallSystemBackendKey, previous)
|
|
|
|
|
return err
|
2026-06-09 11:27:38 +08:00
|
|
|
}
|
2026-08-24 12:51:34 +08:00
|
|
|
if request.Operation == "select" {
|
2026-06-09 11:27:38 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
2026-09-07 18:05:31 +08:00
|
|
|
initErr := newFirewallService().operateFilterChainBaseLocked(request.Backend, dto.FilterChainOperation{
|
2026-08-24 17:40:36 +08:00
|
|
|
Name: constant.FirewallBasicChain, Operate: string(firewall.BaseOperationInit),
|
|
|
|
|
})
|
2026-08-24 12:51:34 +08:00
|
|
|
if initErr != nil {
|
|
|
|
|
return rollback(initErr)
|
2026-06-01 18:26:54 +08:00
|
|
|
}
|
2026-08-24 17:40:36 +08:00
|
|
|
return settingRepo.UpdateOrCreate(constant.FirewallFilterInitializedKey, constant.StatusEnable)
|
2026-06-01 18:26:54 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-27 10:31:39 +08:00
|
|
|
func systemFirewallBackendInitialized(backend string) (bool, error) {
|
2026-09-21 15:09:13 +08:00
|
|
|
client, err := lifecycle.NewClient(backend)
|
2026-08-27 22:24:15 +08:00
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, lifecycle.ErrNotInstalled) {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
2026-09-21 15:09:13 +08:00
|
|
|
if backend == constant.FirewallProviderIptables || backend == constant.FirewallProviderNftables {
|
2026-08-27 10:31:39 +08:00
|
|
|
for _, family := range []string{constant.FirewallFamilyIPv4, constant.FirewallFamilyIPv6} {
|
|
|
|
|
initialized, _, err := loadSystemFirewallFamilyStatus(backend, family)
|
2026-09-07 18:05:31 +08:00
|
|
|
if family == constant.FirewallFamilyIPv6 && errors.Is(err, filter.ErrFamilyUnavailable) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2026-08-27 10:31:39 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
if initialized {
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
return client.Status()
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-08 18:19:30 +08:00
|
|
|
func (s *FirewallSettingService) operateForwarding(ctx context.Context, request dto.FirewallBackendOperation) error {
|
|
|
|
|
if _, err := newForwardingAdapterFor(ctx, request.Backend); err != nil {
|
2026-08-24 12:51:34 +08:00
|
|
|
return err
|
|
|
|
|
}
|
2026-08-25 18:50:27 +08:00
|
|
|
previous, _ := settingRepo.GetValueByKey(constant.FirewallForwardingBackendKey)
|
|
|
|
|
if request.Operation == "select" {
|
|
|
|
|
current := previous
|
|
|
|
|
if current == "" {
|
2026-10-08 18:19:30 +08:00
|
|
|
detected, err := newForwardingAdapter(ctx)
|
2026-08-25 18:50:27 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
current = detected.Name()
|
|
|
|
|
}
|
2026-10-08 18:19:30 +08:00
|
|
|
initialized, err := forwardingBackendInitialized(ctx, current)
|
2026-08-25 18:50:27 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if current != request.Backend && initialized {
|
2026-09-21 15:09:13 +08:00
|
|
|
return buserr.WithMap("ErrFirewallBackendCleanupRequired", map[string]interface{}{"current": current, "target": request.Backend}, nil)
|
2026-08-25 18:50:27 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-08-24 12:51:34 +08:00
|
|
|
if err := settingRepo.UpdateOrCreate(constant.FirewallForwardingBackendKey, request.Backend); err != nil {
|
|
|
|
|
return err
|
2026-06-01 18:26:54 +08:00
|
|
|
}
|
2026-08-24 12:51:34 +08:00
|
|
|
if request.Operation == "initialize" {
|
2026-10-08 18:19:30 +08:00
|
|
|
return newForwardingService().Enable(ctx)
|
2026-08-24 12:51:34 +08:00
|
|
|
}
|
|
|
|
|
return nil
|
2026-06-01 18:26:54 +08:00
|
|
|
}
|
2026-08-25 18:50:27 +08:00
|
|
|
|
2026-10-08 18:19:30 +08:00
|
|
|
func forwardingBackendInitialized(ctx context.Context, backend string) (bool, error) {
|
|
|
|
|
manager, err := newForwardingAdapterFor(ctx, backend)
|
2026-08-25 18:50:27 +08:00
|
|
|
if err != nil {
|
2026-08-27 14:00:43 +08:00
|
|
|
if errors.Is(err, lifecycle.ErrNotInstalled) {
|
2026-08-25 18:50:27 +08:00
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
for _, family := range []string{constant.FirewallFamilyIPv4, constant.FirewallFamilyIPv6} {
|
|
|
|
|
initialized, _, err := manager.FamilyStatus(family)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
if initialized {
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-21 15:09:13 +08:00
|
|
|
func (s *FirewallSettingService) operateDocker(ctx context.Context, request dto.FirewallBackendOperation) error {
|
2026-10-08 18:19:30 +08:00
|
|
|
|
2026-09-21 15:09:13 +08:00
|
|
|
previous, _ := settingRepo.GetValueByKey(constant.FirewallDockerBackendKey)
|
|
|
|
|
if request.Operation == "select" {
|
|
|
|
|
current := previous
|
|
|
|
|
if current == "" {
|
|
|
|
|
current = constant.FirewallProviderNftables
|
|
|
|
|
if request.Backend == constant.FirewallProviderNftables {
|
|
|
|
|
current = constant.FirewallProviderIptables
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-08 18:19:30 +08:00
|
|
|
initialized, err := dockerGuardBackendInitialized(ctx, current)
|
2026-09-21 15:09:13 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if current != request.Backend && initialized {
|
|
|
|
|
return buserr.WithMap("ErrFirewallBackendCleanupRequired", map[string]interface{}{"current": current, "target": request.Backend}, nil)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if err := settingRepo.UpdateOrCreate(constant.FirewallDockerBackendKey, request.Backend); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if request.Operation == "select" {
|
|
|
|
|
if err := (&DockerService{}).UpdateFirewallBackend(request.Backend); err != nil {
|
|
|
|
|
_ = settingRepo.UpdateOrCreate(constant.FirewallDockerBackendKey, previous)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if request.Operation == "initialize" {
|
|
|
|
|
if err := newDockerPortGuardService().Operate(ctx, dto.DockerPortGuardOperation{Operation: "initialize"}); err != nil {
|
|
|
|
|
_ = settingRepo.UpdateOrCreate(constant.FirewallDockerBackendKey, previous)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-08 18:19:30 +08:00
|
|
|
func dockerGuardBackendInitialized(ctx context.Context, backend string) (bool, error) {
|
|
|
|
|
guard := newDockerFirewallRuntime(ctx, backend)
|
2026-09-21 15:09:13 +08:00
|
|
|
for _, family := range []string{dockerfirewall.FamilyIPv4, dockerfirewall.FamilyIPv6} {
|
|
|
|
|
initialized, err := guard.Initialized(family)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
if initialized {
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false, nil
|
2026-08-25 18:50:27 +08:00
|
|
|
}
|