2023-03-24 23:19:17 +08:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2023-08-07 22:46:27 +08:00
|
|
|
"sync"
|
2023-03-24 23:19:17 +08:00
|
|
|
|
2023-07-17 16:34:29 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2023-03-30 18:03:21 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
2023-03-24 23:19:17 +08:00
|
|
|
)
|
|
|
|
|
2023-03-30 18:03:21 +08:00
|
|
|
type Firewall struct{}
|
2023-03-24 23:19:17 +08:00
|
|
|
|
|
|
|
func NewFirewalld() (*Firewall, error) {
|
2023-03-30 18:03:21 +08:00
|
|
|
return &Firewall{}, nil
|
2023-03-24 23:19:17 +08:00
|
|
|
}
|
|
|
|
|
2023-03-28 15:17:13 +08:00
|
|
|
func (f *Firewall) Name() string {
|
|
|
|
return "firewalld"
|
|
|
|
}
|
|
|
|
|
2023-03-24 23:19:17 +08:00
|
|
|
func (f *Firewall) Status() (string, error) {
|
2023-03-30 18:03:21 +08:00
|
|
|
stdout, _ := cmd.Exec("firewall-cmd --state")
|
2023-04-01 00:51:25 +08:00
|
|
|
if stdout == "running\n" {
|
|
|
|
return "running", nil
|
|
|
|
}
|
|
|
|
return "not running", nil
|
2023-03-30 16:07:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Firewall) Version() (string, error) {
|
2023-03-30 18:03:21 +08:00
|
|
|
stdout, err := cmd.Exec("firewall-cmd --version")
|
2023-03-24 23:19:17 +08:00
|
|
|
if err != nil {
|
2023-03-30 16:07:28 +08:00
|
|
|
return "", fmt.Errorf("load the firewall version failed, err: %s", stdout)
|
2023-03-24 23:19:17 +08:00
|
|
|
}
|
2023-03-30 16:07:28 +08:00
|
|
|
return strings.ReplaceAll(stdout, "\n ", ""), nil
|
2023-03-24 23:19:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Firewall) Start() error {
|
2023-03-30 18:03:21 +08:00
|
|
|
stdout, err := cmd.Exec("systemctl start firewalld")
|
2023-03-24 23:19:17 +08:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("enable the firewall failed, err: %s", stdout)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Firewall) Stop() error {
|
2023-03-30 18:03:21 +08:00
|
|
|
stdout, err := cmd.Exec("systemctl stop firewalld")
|
2023-03-24 23:19:17 +08:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("stop the firewall failed, err: %s", stdout)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-20 13:46:09 +08:00
|
|
|
func (f *Firewall) Restart() error {
|
|
|
|
stdout, err := cmd.Exec("systemctl restart firewalld")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("restart the firewall failed, err: %s", stdout)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-24 23:19:17 +08:00
|
|
|
func (f *Firewall) Reload() error {
|
2023-03-30 18:03:21 +08:00
|
|
|
stdout, err := cmd.Exec("firewall-cmd --reload")
|
2023-03-24 23:19:17 +08:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("reload firewall failed, err: %s", stdout)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Firewall) ListPort() ([]FireInfo, error) {
|
2023-08-07 22:46:27 +08:00
|
|
|
var wg sync.WaitGroup
|
2023-03-24 23:19:17 +08:00
|
|
|
var datas []FireInfo
|
2023-08-07 22:46:27 +08:00
|
|
|
wg.Add(2)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
stdout, err := cmd.Exec("firewall-cmd --zone=public --list-ports")
|
|
|
|
if err != nil {
|
|
|
|
return
|
2023-04-01 00:51:25 +08:00
|
|
|
}
|
2023-08-07 22:46:27 +08:00
|
|
|
ports := strings.Split(strings.ReplaceAll(stdout, "\n", ""), " ")
|
|
|
|
for _, port := range ports {
|
|
|
|
if len(port) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var itemPort FireInfo
|
|
|
|
if strings.Contains(port, "/") {
|
|
|
|
itemPort.Port = strings.Split(port, "/")[0]
|
|
|
|
itemPort.Protocol = strings.Split(port, "/")[1]
|
|
|
|
}
|
|
|
|
itemPort.Strategy = "accept"
|
|
|
|
datas = append(datas, itemPort)
|
2023-03-24 23:19:17 +08:00
|
|
|
}
|
2023-08-07 22:46:27 +08:00
|
|
|
}()
|
2023-03-27 19:02:36 +08:00
|
|
|
|
2023-08-07 22:46:27 +08:00
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
stdout1, err := cmd.Exec("firewall-cmd --zone=public --list-rich-rules")
|
|
|
|
if err != nil {
|
|
|
|
return
|
2023-03-27 19:02:36 +08:00
|
|
|
}
|
2023-08-07 22:46:27 +08:00
|
|
|
rules := strings.Split(stdout1, "\n")
|
|
|
|
for _, rule := range rules {
|
|
|
|
if len(rule) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
itemRule := f.loadInfo(rule)
|
|
|
|
if len(itemRule.Port) != 0 && itemRule.Family == "ipv4" {
|
2024-01-25 18:18:47 +08:00
|
|
|
itemRule.Family = ""
|
2023-08-07 22:46:27 +08:00
|
|
|
datas = append(datas, itemRule)
|
|
|
|
}
|
2023-03-27 19:02:36 +08:00
|
|
|
}
|
2023-08-07 22:46:27 +08:00
|
|
|
}()
|
|
|
|
wg.Wait()
|
2023-03-24 23:19:17 +08:00
|
|
|
return datas, nil
|
|
|
|
}
|
|
|
|
|
2023-03-27 19:02:36 +08:00
|
|
|
func (f *Firewall) ListAddress() ([]FireInfo, error) {
|
2023-03-30 18:03:21 +08:00
|
|
|
stdout, err := cmd.Exec("firewall-cmd --zone=public --list-rich-rules")
|
2023-03-24 23:19:17 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var datas []FireInfo
|
|
|
|
rules := strings.Split(stdout, "\n")
|
|
|
|
for _, rule := range rules {
|
|
|
|
if len(rule) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2023-03-27 19:02:36 +08:00
|
|
|
itemRule := f.loadInfo(rule)
|
2023-04-01 00:51:25 +08:00
|
|
|
if len(itemRule.Port) == 0 && len(itemRule.Address) != 0 {
|
2023-03-27 19:02:36 +08:00
|
|
|
datas = append(datas, itemRule)
|
2023-03-24 23:19:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return datas, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Firewall) Port(port FireInfo, operation string) error {
|
2023-07-17 16:34:29 +08:00
|
|
|
if cmd.CheckIllegal(operation, port.Protocol, port.Port) {
|
|
|
|
return buserr.New(constant.ErrCmdIllegal)
|
|
|
|
}
|
|
|
|
|
2023-03-30 18:03:21 +08:00
|
|
|
stdout, err := cmd.Execf("firewall-cmd --zone=public --%s-port=%s/%s --permanent", operation, port.Port, port.Protocol)
|
2023-03-24 23:19:17 +08:00
|
|
|
if err != nil {
|
2023-11-03 14:38:13 +08:00
|
|
|
return fmt.Errorf("%s (port: %s/%s strategy: %s) failed, err: %s", operation, port.Port, port.Protocol, port.Strategy, stdout)
|
2023-03-24 23:19:17 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Firewall) RichRules(rule FireInfo, operation string) error {
|
2023-07-17 16:34:29 +08:00
|
|
|
if cmd.CheckIllegal(operation, rule.Address, rule.Protocol, rule.Port, rule.Strategy) {
|
|
|
|
return buserr.New(constant.ErrCmdIllegal)
|
|
|
|
}
|
2023-11-03 11:14:43 +08:00
|
|
|
ruleStr := "rule family=ipv4 "
|
|
|
|
if len(rule.Address) != 0 {
|
|
|
|
ruleStr += fmt.Sprintf("source address=%s ", rule.Address)
|
|
|
|
}
|
|
|
|
if len(rule.Port) != 0 {
|
|
|
|
ruleStr += fmt.Sprintf("port port=%s ", rule.Port)
|
|
|
|
}
|
|
|
|
if len(rule.Protocol) != 0 {
|
|
|
|
ruleStr += fmt.Sprintf("protocol=%s ", rule.Protocol)
|
2023-03-24 23:19:17 +08:00
|
|
|
}
|
2023-11-03 11:14:43 +08:00
|
|
|
ruleStr += rule.Strategy
|
2023-03-30 18:03:21 +08:00
|
|
|
stdout, err := cmd.Execf("firewall-cmd --zone=public --%s-rich-rule '%s' --permanent", operation, ruleStr)
|
2023-03-24 23:19:17 +08:00
|
|
|
if err != nil {
|
2023-11-03 14:38:13 +08:00
|
|
|
return fmt.Errorf("%s rich rules (%s) failed, err: %s", operation, ruleStr, stdout)
|
2023-03-24 23:19:17 +08:00
|
|
|
}
|
2023-11-03 11:14:43 +08:00
|
|
|
if len(rule.Address) == 0 {
|
|
|
|
stdout1, err := cmd.Execf("firewall-cmd --zone=public --%s-rich-rule '%s' --permanent", operation, strings.ReplaceAll(ruleStr, "family=ipv4 ", "family=ipv6 "))
|
|
|
|
if err != nil {
|
2023-11-03 14:38:13 +08:00
|
|
|
return fmt.Errorf("%s rich rules (%s) failed, err: %s", operation, strings.ReplaceAll(ruleStr, "family=ipv4 ", "family=ipv6 "), stdout1)
|
2023-11-03 11:14:43 +08:00
|
|
|
}
|
|
|
|
}
|
2023-03-24 23:19:17 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Firewall) PortForward(info Forward, operation string) error {
|
|
|
|
ruleStr := fmt.Sprintf("firewall-cmd --%s-forward-port=port=%s:proto=%s:toport=%s --permanent", operation, info.Port, info.Protocol, info.Target)
|
|
|
|
if len(info.Address) != 0 {
|
|
|
|
ruleStr = fmt.Sprintf("firewall-cmd --%s-forward-port=port=%s:proto=%s:toaddr=%s:toport=%s --permanent", operation, info.Port, info.Protocol, info.Address, info.Target)
|
|
|
|
}
|
|
|
|
|
2023-03-30 18:03:21 +08:00
|
|
|
stdout, err := cmd.Exec(ruleStr)
|
2023-03-24 23:19:17 +08:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s port forward failed, err: %s", operation, stdout)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-03-27 19:02:36 +08:00
|
|
|
|
|
|
|
func (f *Firewall) loadInfo(line string) FireInfo {
|
|
|
|
var itemRule FireInfo
|
|
|
|
ruleInfo := strings.Split(strings.ReplaceAll(line, "\"", ""), " ")
|
|
|
|
for _, item := range ruleInfo {
|
|
|
|
switch {
|
|
|
|
case strings.Contains(item, "family="):
|
|
|
|
itemRule.Family = strings.ReplaceAll(item, "family=", "")
|
2023-04-01 00:51:25 +08:00
|
|
|
case strings.Contains(item, "ipset="):
|
|
|
|
itemRule.Address = strings.ReplaceAll(item, "ipset=", "")
|
2023-03-27 19:02:36 +08:00
|
|
|
case strings.Contains(item, "address="):
|
|
|
|
itemRule.Address = strings.ReplaceAll(item, "address=", "")
|
|
|
|
case strings.Contains(item, "port="):
|
|
|
|
itemRule.Port = strings.ReplaceAll(item, "port=", "")
|
|
|
|
case strings.Contains(item, "protocol="):
|
|
|
|
itemRule.Protocol = strings.ReplaceAll(item, "protocol=", "")
|
2023-04-01 00:51:25 +08:00
|
|
|
case item == "accept" || item == "drop" || item == "reject":
|
2023-03-27 19:02:36 +08:00
|
|
|
itemRule.Strategy = item
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return itemRule
|
|
|
|
}
|