2023-03-27 19:02:36 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2023-03-28 15:17:13 +08:00
|
|
|
"strings"
|
|
|
|
|
2023-03-27 19:02:36 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/firewall"
|
|
|
|
fireClient "github.com/1Panel-dev/1Panel/backend/utils/firewall/client"
|
|
|
|
"github.com/jinzhu/copier"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FirewallService struct{}
|
|
|
|
|
|
|
|
type IFirewallService interface {
|
|
|
|
SearchWithPage(search dto.RuleSearch) (int64, interface{}, error)
|
2023-03-28 15:17:13 +08:00
|
|
|
OperatePortRule(req dto.PortRuleOperate, reload bool) error
|
|
|
|
OperateAddressRule(req dto.AddrRuleOperate, reload bool) error
|
|
|
|
UpdatePortRule(req dto.PortRuleUpdate) error
|
|
|
|
UpdateAddrRule(req dto.AddrRuleUpdate) error
|
|
|
|
BacthOperateRule(req dto.BatchRuleOperate) error
|
2023-03-27 19:02:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIFirewallService() IFirewallService {
|
|
|
|
return &FirewallService{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *FirewallService) SearchWithPage(req dto.RuleSearch) (int64, interface{}, error) {
|
|
|
|
var (
|
|
|
|
datas []fireClient.FireInfo
|
|
|
|
backDatas []fireClient.FireInfo
|
|
|
|
)
|
|
|
|
client, err := firewall.NewFirewallClient()
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
if req.Type == "port" {
|
|
|
|
ports, err := client.ListPort()
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
2023-03-28 15:17:13 +08:00
|
|
|
if len(req.Info) != 0 {
|
|
|
|
for _, port := range ports {
|
|
|
|
if strings.Contains(port.Port, req.Info) {
|
|
|
|
datas = append(datas, port)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
datas = ports
|
|
|
|
}
|
2023-03-27 19:02:36 +08:00
|
|
|
} else {
|
2023-03-28 15:17:13 +08:00
|
|
|
addrs, err := client.ListAddress()
|
2023-03-27 19:02:36 +08:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
2023-03-28 15:17:13 +08:00
|
|
|
if len(req.Info) != 0 {
|
|
|
|
for _, addr := range addrs {
|
|
|
|
if strings.Contains(addr.Address, req.Info) {
|
|
|
|
datas = append(datas, addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
datas = addrs
|
|
|
|
}
|
2023-03-27 19:02:36 +08:00
|
|
|
}
|
|
|
|
total, start, end := len(datas), (req.Page-1)*req.PageSize, req.Page*req.PageSize
|
|
|
|
if start > total {
|
|
|
|
backDatas = make([]fireClient.FireInfo, 0)
|
|
|
|
} else {
|
|
|
|
if end >= total {
|
|
|
|
end = total
|
|
|
|
}
|
|
|
|
backDatas = datas[start:end]
|
|
|
|
}
|
|
|
|
|
|
|
|
return int64(total), backDatas, nil
|
|
|
|
}
|
|
|
|
|
2023-03-28 15:17:13 +08:00
|
|
|
func (u *FirewallService) OperatePortRule(req dto.PortRuleOperate, reload bool) error {
|
2023-03-27 19:02:36 +08:00
|
|
|
client, err := firewall.NewFirewallClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-28 15:17:13 +08:00
|
|
|
if client.Name() == "ufw" {
|
|
|
|
req.Port = strings.ReplaceAll(req.Port, "-", ":")
|
|
|
|
if req.Operation == "remove" && req.Protocol == "tcp/udp" {
|
|
|
|
req.Protocol = ""
|
|
|
|
return u.operatePort(client, req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-27 23:29:46 +08:00
|
|
|
if req.Protocol == "tcp/udp" {
|
|
|
|
req.Protocol = "tcp"
|
2023-03-28 15:17:13 +08:00
|
|
|
if err := u.operatePort(client, req); err != nil {
|
2023-03-27 23:29:46 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.Protocol = "udp"
|
2023-03-27 19:02:36 +08:00
|
|
|
}
|
2023-03-28 15:17:13 +08:00
|
|
|
if err := u.operatePort(client, req); err != nil {
|
2023-03-27 23:29:46 +08:00
|
|
|
return err
|
2023-03-27 19:02:36 +08:00
|
|
|
}
|
2023-03-28 15:17:13 +08:00
|
|
|
if reload {
|
|
|
|
return client.Reload()
|
|
|
|
}
|
|
|
|
return nil
|
2023-03-27 19:02:36 +08:00
|
|
|
}
|
|
|
|
|
2023-03-28 15:17:13 +08:00
|
|
|
func (u *FirewallService) OperateAddressRule(req dto.AddrRuleOperate, reload bool) error {
|
2023-03-27 19:02:36 +08:00
|
|
|
client, err := firewall.NewFirewallClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var fireInfo fireClient.FireInfo
|
|
|
|
if err := copier.Copy(&fireInfo, &req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-28 15:17:13 +08:00
|
|
|
|
|
|
|
addressList := strings.Split(req.Address, ",")
|
|
|
|
for _, addr := range addressList {
|
|
|
|
if len(addr) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fireInfo.Address = addr
|
|
|
|
if err := client.RichRules(fireInfo, req.Operation); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if reload {
|
|
|
|
return client.Reload()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *FirewallService) UpdatePortRule(req dto.PortRuleUpdate) error {
|
|
|
|
client, err := firewall.NewFirewallClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := u.OperatePortRule(req.OldRule, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := u.OperatePortRule(req.NewRule, false); err != nil {
|
2023-03-27 23:29:46 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return client.Reload()
|
|
|
|
}
|
|
|
|
|
2023-03-28 15:17:13 +08:00
|
|
|
func (u *FirewallService) UpdateAddrRule(req dto.AddrRuleUpdate) error {
|
|
|
|
client, err := firewall.NewFirewallClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := u.OperateAddressRule(req.OldRule, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := u.OperateAddressRule(req.NewRule, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return client.Reload()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *FirewallService) BacthOperateRule(req dto.BatchRuleOperate) error {
|
|
|
|
client, err := firewall.NewFirewallClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if req.Type == "port" {
|
|
|
|
for _, rule := range req.Rules {
|
|
|
|
if err := u.OperatePortRule(rule, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return client.Reload()
|
|
|
|
}
|
|
|
|
for _, rule := range req.Rules {
|
|
|
|
itemRule := dto.AddrRuleOperate{Operation: rule.Operation, Address: rule.Address, Strategy: rule.Strategy}
|
|
|
|
if err := u.OperateAddressRule(itemRule, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return client.Reload()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *FirewallService) operatePort(client firewall.FirewallClient, req dto.PortRuleOperate) error {
|
2023-03-27 23:29:46 +08:00
|
|
|
var fireInfo fireClient.FireInfo
|
|
|
|
if err := copier.Copy(&fireInfo, &req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-28 15:17:13 +08:00
|
|
|
if client.Name() == "ufw" {
|
|
|
|
if len(fireInfo.Address) != 0 && fireInfo.Address != "Anywhere" {
|
|
|
|
return client.RichRules(fireInfo, req.Operation)
|
|
|
|
}
|
|
|
|
return client.Port(fireInfo, req.Operation)
|
|
|
|
}
|
|
|
|
|
2023-03-27 23:29:46 +08:00
|
|
|
if len(fireInfo.Address) != 0 || fireInfo.Strategy == "drop" {
|
|
|
|
return client.RichRules(fireInfo, req.Operation)
|
|
|
|
}
|
|
|
|
return client.Port(fireInfo, req.Operation)
|
2023-03-27 19:02:36 +08:00
|
|
|
}
|