1Panel/backend/utils/firewall/client.go

40 lines
989 B
Go
Raw Normal View History

package firewall
import (
2023-03-30 18:03:21 +08:00
"os"
"github.com/1Panel-dev/1Panel/backend/buserr"
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/utils/firewall/client"
)
type FirewallClient interface {
2023-03-30 18:03:21 +08:00
Name() string // ufw firewalld
Start() error
Stop() error
Restart() error
Reload() error
2023-03-30 18:03:21 +08:00
Status() (string, error) // running not running
Version() (string, error)
2023-03-30 18:03:21 +08:00
ListPort() ([]client.FireInfo, error)
2024-06-15 22:28:03 +08:00
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
2024-06-15 22:28:03 +08:00
EnableForward() error
}
func NewFirewallClient() (FirewallClient, error) {
2023-03-30 18:03:21 +08:00
if _, err := os.Stat("/usr/sbin/firewalld"); err == nil {
return client.NewFirewalld()
}
if _, err := os.Stat("/usr/sbin/ufw"); err == nil {
return client.NewUfw()
}
return nil, buserr.New(constant.ErrFirewall)
}