Files
1Panel/agent/utils/firewall/forwarding/adapter.go
HynoR f35b0deb29 refactor(firewall): extract port forwarding subsystem (#13347)
Port forwarding no longer shares the filter client. FilterClient keeps only
filter capabilities, and forwarding gets its own adapter, service and boot
replay:

- utils/firewall/forwarding holds the provider adapters. firewalld uses native
  forward-port, ufw and iptables share the NAT implementation moved out of
  client/iptables/forward.go.
- service/forwarding.go owns base info, search, operate, enable and replay.
  The API keeps its routes and dispatches on name/type/operate.
- init/firewall replays forwarding through that service instead of loading NAT
  rule files inline.

Also adds 1PANEL_FORWARD to the IptablesOp name enum: the frontend already
sends {"name":"1PANEL_FORWARD","operate":"init-forward"} and the validator
rejected it with 400 before reaching the service. Besides that, the only
observable difference is that a forward-tab search no longer triggers the
port/address record cleanup goroutine on the side.
2026-07-30 14:07:41 +08:00

47 lines
1.0 KiB
Go

package forwarding
import (
"errors"
)
const (
ChainPreRouting = "1PANEL_PREROUTING"
ChainPostRouting = "1PANEL_POSTROUTING"
ChainForward = "1PANEL_FORWARD"
ForwardFile = "1panel_forward.rules"
PreRoutingFile = "1panel_forward_pre.rules"
PostRoutingFile = "1panel_forward_post.rules"
)
type Rule struct {
Num string
Protocol string
Port string
TargetIP string
TargetPort string
Interface string
}
// Adapter is the complete provider-specific forwarding surface. Filter
// clients intentionally do not implement any of these methods.
type Adapter interface {
Name() string
List() ([]Rule, error)
Operate(rule Rule, operation string) error
Enable() error
InitStatus() (bool, bool)
Replay() error
}
func NewAdapter(provider string) (Adapter, error) {
switch provider {
case "firewalld":
return newFirewalldAdapter(), nil
case "ufw", "iptables":
return newLegacyNATAdapter(provider), nil
default:
return nil, errors.New("unsupported forwarding provider: " + provider)
}
}