2024-07-23 14:48:37 +08:00
|
|
|
package firewall
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/buserr"
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/constant"
|
2024-12-17 15:59:21 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/agent/utils/cmd"
|
2024-07-23 14:48:37 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/agent/utils/firewall/client"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FirewallClient interface {
|
|
|
|
Name() string // ufw firewalld
|
|
|
|
Start() error
|
|
|
|
Stop() error
|
|
|
|
Restart() error
|
|
|
|
Reload() error
|
2024-12-18 18:06:15 +08:00
|
|
|
Status() (bool, error) // running not running
|
2024-07-23 14:48:37 +08:00
|
|
|
Version() (string, error)
|
|
|
|
|
|
|
|
ListPort() ([]client.FireInfo, error)
|
|
|
|
ListForward() ([]client.FireInfo, error)
|
|
|
|
ListAddress() ([]client.FireInfo, error)
|
|
|
|
|
|
|
|
Port(port client.FireInfo, operation string) error
|
|
|
|
RichRules(rule client.FireInfo, operation string) error
|
|
|
|
PortForward(info client.Forward, operation string) error
|
|
|
|
|
|
|
|
EnableForward() error
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFirewallClient() (FirewallClient, error) {
|
2024-12-17 15:59:21 +08:00
|
|
|
firewalld := cmd.Which("firewalld")
|
|
|
|
ufw := cmd.Which("ufw")
|
|
|
|
|
|
|
|
if firewalld && ufw {
|
|
|
|
return nil, buserr.New(constant.ErrFirewallBoth)
|
|
|
|
}
|
|
|
|
|
|
|
|
if firewalld {
|
2024-07-23 14:48:37 +08:00
|
|
|
return client.NewFirewalld()
|
|
|
|
}
|
2024-12-17 15:59:21 +08:00
|
|
|
if ufw {
|
2024-07-23 14:48:37 +08:00
|
|
|
return client.NewUfw()
|
|
|
|
}
|
2024-12-17 15:59:21 +08:00
|
|
|
return nil, buserr.New(constant.ErrFirewallNone)
|
2024-07-23 14:48:37 +08:00
|
|
|
}
|