2023-03-24 23:19:17 +08:00
|
|
|
package firewall
|
|
|
|
|
|
|
|
import (
|
2023-03-30 18:03:21 +08:00
|
|
|
"os"
|
|
|
|
|
2023-09-20 14:18:20 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2023-03-24 23:19:17 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/firewall/client"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FirewallClient interface {
|
2023-03-30 18:03:21 +08:00
|
|
|
Name() string // ufw firewalld
|
2023-03-24 23:19:17 +08:00
|
|
|
Start() error
|
|
|
|
Stop() error
|
2023-12-20 13:46:09 +08:00
|
|
|
Restart() error
|
2023-03-24 23:19:17 +08:00
|
|
|
Reload() error
|
2023-03-30 18:03:21 +08:00
|
|
|
Status() (string, error) // running not running
|
2023-03-30 16:07:28 +08:00
|
|
|
Version() (string, error)
|
2023-03-30 18:03:21 +08:00
|
|
|
|
2023-03-24 23:19:17 +08:00
|
|
|
ListPort() ([]client.FireInfo, error)
|
2023-03-27 19:02:36 +08:00
|
|
|
ListAddress() ([]client.FireInfo, error)
|
2023-03-24 23:19:17 +08:00
|
|
|
|
|
|
|
Port(port client.FireInfo, operation string) error
|
|
|
|
RichRules(rule client.FireInfo, operation string) error
|
|
|
|
PortForward(info client.Forward, operation string) 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()
|
|
|
|
}
|
2023-09-20 14:18:20 +08:00
|
|
|
return nil, buserr.New(constant.ErrFirewall)
|
2023-03-24 23:19:17 +08:00
|
|
|
}
|