2023-03-24 15:19:17 +00:00
|
|
|
package firewall
|
|
|
|
|
|
|
|
import (
|
2023-09-20 06:18:20 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2024-11-06 08:31:19 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
2023-03-24 15:19:17 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/firewall/client"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FirewallClient interface {
|
2023-03-30 10:03:21 +00:00
|
|
|
Name() string // ufw firewalld
|
2023-03-24 15:19:17 +00:00
|
|
|
Start() error
|
|
|
|
Stop() error
|
2023-12-20 05:46:09 +00:00
|
|
|
Restart() error
|
2023-03-24 15:19:17 +00:00
|
|
|
Reload() error
|
2023-03-30 10:03:21 +00:00
|
|
|
Status() (string, error) // running not running
|
2023-03-30 08:07:28 +00:00
|
|
|
Version() (string, error)
|
2023-03-30 10:03:21 +00:00
|
|
|
|
2023-03-24 15:19:17 +00:00
|
|
|
ListPort() ([]client.FireInfo, error)
|
2024-06-15 14:28:03 +00:00
|
|
|
ListForward() ([]client.FireInfo, error)
|
2023-03-27 11:02:36 +00:00
|
|
|
ListAddress() ([]client.FireInfo, error)
|
2023-03-24 15:19:17 +00:00
|
|
|
|
|
|
|
Port(port client.FireInfo, operation string) error
|
|
|
|
RichRules(rule client.FireInfo, operation string) error
|
|
|
|
PortForward(info client.Forward, operation string) error
|
2024-06-15 14:28:03 +00:00
|
|
|
|
|
|
|
EnableForward() error
|
2023-03-24 15:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewFirewallClient() (FirewallClient, error) {
|
2024-11-06 08:31:19 +00:00
|
|
|
firewalld := cmd.Which("firewalld")
|
|
|
|
ufw := cmd.Which("ufw")
|
2024-10-11 10:03:33 +00:00
|
|
|
|
2024-11-06 08:31:19 +00:00
|
|
|
if firewalld && ufw {
|
|
|
|
return nil, buserr.New(constant.ErrFirewallBoth)
|
2024-10-11 10:03:33 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 08:31:19 +00:00
|
|
|
if firewalld {
|
2023-03-30 10:03:21 +00:00
|
|
|
return client.NewFirewalld()
|
|
|
|
}
|
2024-11-06 08:31:19 +00:00
|
|
|
if ufw {
|
2023-03-30 10:03:21 +00:00
|
|
|
return client.NewUfw()
|
|
|
|
}
|
2024-11-06 08:31:19 +00:00
|
|
|
return nil, buserr.New(constant.ErrFirewallNone)
|
2023-03-24 15:19:17 +00:00
|
|
|
}
|