2023-03-27 19:02:36 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2023-03-30 16:07:28 +08:00
|
|
|
"fmt"
|
2023-04-11 16:58:33 +08:00
|
|
|
"os"
|
2023-04-01 00:51:25 +08:00
|
|
|
"strconv"
|
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"
|
2023-04-11 16:58:33 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2023-04-01 00:51:25 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/common"
|
2023-03-27 19:02:36 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/firewall"
|
|
|
|
fireClient "github.com/1Panel-dev/1Panel/backend/utils/firewall/client"
|
|
|
|
"github.com/jinzhu/copier"
|
|
|
|
)
|
|
|
|
|
2023-04-11 16:58:33 +08:00
|
|
|
const confPath = "/etc/sysctl.conf"
|
|
|
|
|
2023-03-27 19:02:36 +08:00
|
|
|
type FirewallService struct{}
|
|
|
|
|
|
|
|
type IFirewallService interface {
|
2023-03-30 16:07:28 +08:00
|
|
|
LoadBaseInfo() (dto.FirewallBaseInfo, error)
|
2023-03-27 19:02:36 +08:00
|
|
|
SearchWithPage(search dto.RuleSearch) (int64, interface{}, error)
|
2023-03-30 16:07:28 +08:00
|
|
|
OperateFirewall(operation string) 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{}
|
|
|
|
}
|
|
|
|
|
2023-03-30 16:07:28 +08:00
|
|
|
func (u *FirewallService) LoadBaseInfo() (dto.FirewallBaseInfo, error) {
|
|
|
|
var baseInfo dto.FirewallBaseInfo
|
2023-04-14 17:14:57 +08:00
|
|
|
baseInfo.PingStatus = u.pingStatus()
|
|
|
|
baseInfo.Status = "not running"
|
|
|
|
baseInfo.Version = "-"
|
|
|
|
baseInfo.Name = "-"
|
2023-03-30 16:07:28 +08:00
|
|
|
client, err := firewall.NewFirewallClient()
|
|
|
|
if err != nil {
|
2023-04-06 11:12:15 +08:00
|
|
|
if err.Error() == "no such type" {
|
2023-04-14 17:14:57 +08:00
|
|
|
return baseInfo, nil
|
2023-04-06 11:12:15 +08:00
|
|
|
}
|
2023-03-30 16:07:28 +08:00
|
|
|
return baseInfo, err
|
|
|
|
}
|
|
|
|
baseInfo.Name = client.Name()
|
|
|
|
baseInfo.Status, err = client.Status()
|
|
|
|
if err != nil {
|
|
|
|
return baseInfo, err
|
|
|
|
}
|
|
|
|
if baseInfo.Status == "not running" {
|
|
|
|
return baseInfo, err
|
|
|
|
}
|
|
|
|
baseInfo.Version, err = client.Version()
|
|
|
|
if err != nil {
|
|
|
|
return baseInfo, err
|
|
|
|
}
|
|
|
|
return baseInfo, nil
|
|
|
|
}
|
|
|
|
|
2023-03-27 19:02:36 +08:00
|
|
|
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]
|
|
|
|
}
|
|
|
|
|
2023-04-01 00:51:25 +08:00
|
|
|
if req.Type == "port" {
|
|
|
|
apps := u.loadPortByApp()
|
|
|
|
for i := 0; i < len(backDatas); i++ {
|
2023-04-12 11:22:29 +08:00
|
|
|
port, _ := strconv.Atoi(backDatas[i].Port)
|
|
|
|
backDatas[i].IsUsed = common.ScanPort(port)
|
2023-04-01 00:51:25 +08:00
|
|
|
if backDatas[i].Protocol == "udp" {
|
2023-04-12 11:22:29 +08:00
|
|
|
backDatas[i].IsUsed = common.ScanUDPPort(port)
|
2023-04-01 00:51:25 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, app := range apps {
|
|
|
|
if app.HttpPort == backDatas[i].Port || app.HttpsPort == backDatas[i].Port {
|
|
|
|
backDatas[i].APPName = app.AppName
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-27 19:02:36 +08:00
|
|
|
return int64(total), backDatas, nil
|
|
|
|
}
|
|
|
|
|
2023-03-30 16:07:28 +08:00
|
|
|
func (u *FirewallService) OperateFirewall(operation string) error {
|
|
|
|
client, err := firewall.NewFirewallClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch operation {
|
|
|
|
case "start":
|
2023-04-01 00:51:25 +08:00
|
|
|
if err := client.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-14 15:10:46 +08:00
|
|
|
if err := u.addPortsBeforeStart(client); err != nil {
|
|
|
|
_ = client.Stop()
|
2023-04-01 00:51:25 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, _ = cmd.Exec("systemctl restart docker")
|
|
|
|
return nil
|
2023-03-30 16:07:28 +08:00
|
|
|
case "stop":
|
2023-04-01 00:51:25 +08:00
|
|
|
if err := client.Stop(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, _ = cmd.Exec("systemctl restart docker")
|
|
|
|
return nil
|
2023-03-30 18:03:21 +08:00
|
|
|
case "disablePing":
|
2023-04-14 15:10:46 +08:00
|
|
|
return u.updatePingStatus("0")
|
2023-03-30 18:03:21 +08:00
|
|
|
case "enablePing":
|
2023-04-14 15:10:46 +08:00
|
|
|
return u.updatePingStatus("1")
|
2023-03-30 16:07:28 +08:00
|
|
|
}
|
|
|
|
return fmt.Errorf("not support such operation: %s", operation)
|
|
|
|
}
|
|
|
|
|
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" {
|
2023-04-01 00:51:25 +08:00
|
|
|
if client.Name() == "firewalld" && strings.Contains(req.Port, ",") {
|
|
|
|
ports := strings.Split(req.Port, ",")
|
|
|
|
for _, port := range ports {
|
|
|
|
if len(port) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
req.Port = port
|
|
|
|
req.Protocol = "tcp"
|
|
|
|
if err := u.operatePort(client, req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.Protocol = "udp"
|
|
|
|
if err := u.operatePort(client, req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
req.Protocol = "tcp"
|
|
|
|
if err := u.operatePort(client, req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.Protocol = "udp"
|
|
|
|
if err := u.operatePort(client, req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if strings.Contains(req.Port, ",") {
|
|
|
|
ports := strings.Split(req.Port, ",")
|
|
|
|
for _, port := range ports {
|
|
|
|
req.Port = port
|
|
|
|
if err := u.operatePort(client, req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := u.operatePort(client, req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-27 23:29:46 +08:00
|
|
|
}
|
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()
|
|
|
|
}
|
|
|
|
|
2023-04-01 00:51:25 +08:00
|
|
|
func OperateFirewallPort(oldPorts, newPorts []int) error {
|
|
|
|
client, err := firewall.NewFirewallClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, port := range newPorts {
|
|
|
|
|
|
|
|
if err := client.Port(fireClient.FireInfo{Port: strconv.Itoa(port), Protocol: "tcp", Strategy: "accept"}, "add"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, port := range oldPorts {
|
|
|
|
if err := client.Port(fireClient.FireInfo{Port: strconv.Itoa(port), Protocol: "tcp", Strategy: "accept"}, "remove"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return client.Reload()
|
|
|
|
}
|
|
|
|
|
2023-03-28 15:17:13 +08:00
|
|
|
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
|
|
|
}
|
2023-04-01 00:51:25 +08:00
|
|
|
|
|
|
|
type portOfApp struct {
|
|
|
|
AppName string
|
|
|
|
HttpPort string
|
|
|
|
HttpsPort string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *FirewallService) loadPortByApp() []portOfApp {
|
|
|
|
var datas []portOfApp
|
|
|
|
apps, err := appInstallRepo.ListBy()
|
|
|
|
if err != nil {
|
|
|
|
return datas
|
|
|
|
}
|
|
|
|
for i := 0; i < len(apps); i++ {
|
|
|
|
datas = append(datas, portOfApp{
|
|
|
|
AppName: apps[i].App.Key,
|
|
|
|
HttpPort: strconv.Itoa(apps[i].HttpPort),
|
|
|
|
HttpsPort: strconv.Itoa(apps[i].HttpsPort),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
systemPort, err := settingRepo.Get(settingRepo.WithByKey("ServerPort"))
|
|
|
|
if err != nil {
|
|
|
|
return datas
|
|
|
|
}
|
|
|
|
datas = append(datas, portOfApp{AppName: "1panel", HttpPort: systemPort.Value})
|
|
|
|
|
|
|
|
return datas
|
|
|
|
}
|
2023-04-11 16:58:33 +08:00
|
|
|
|
2023-04-14 17:14:57 +08:00
|
|
|
func (u *FirewallService) pingStatus() string {
|
|
|
|
if _, err := os.Stat("/etc/sysctl.conf"); err != nil {
|
|
|
|
return constant.StatusNone
|
|
|
|
}
|
2023-04-12 11:22:29 +08:00
|
|
|
stdout, _ := cmd.Exec("sudo cat /etc/sysctl.conf | grep net/ipv4/icmp_echo_ignore_all= ")
|
2023-04-11 16:58:33 +08:00
|
|
|
if stdout == "net/ipv4/icmp_echo_ignore_all=1\n" {
|
2023-04-14 17:14:57 +08:00
|
|
|
return constant.StatusEnable
|
2023-04-11 16:58:33 +08:00
|
|
|
}
|
2023-04-14 17:14:57 +08:00
|
|
|
return constant.StatusDisable
|
2023-04-11 16:58:33 +08:00
|
|
|
}
|
|
|
|
|
2023-04-14 15:10:46 +08:00
|
|
|
func (u *FirewallService) updatePingStatus(enabel string) error {
|
2023-04-11 16:58:33 +08:00
|
|
|
lineBytes, err := os.ReadFile(confPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
files := strings.Split(string(lineBytes), "\n")
|
|
|
|
var newFiles []string
|
|
|
|
hasLine := false
|
|
|
|
for _, line := range files {
|
|
|
|
if strings.Contains(line, "net/ipv4/icmp_echo_ignore_all") || strings.HasPrefix(line, "net/ipv4/icmp_echo_ignore_all") {
|
|
|
|
newFiles = append(newFiles, "net/ipv4/icmp_echo_ignore_all="+enabel)
|
|
|
|
hasLine = true
|
|
|
|
} else {
|
|
|
|
newFiles = append(newFiles, line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !hasLine {
|
|
|
|
newFiles = append(newFiles, "net/ipv4/icmp_echo_ignore_all="+enabel)
|
|
|
|
}
|
|
|
|
file, err := os.OpenFile(confPath, os.O_WRONLY|os.O_TRUNC, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
_, err = file.WriteString(strings.Join(newFiles, "\n"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
stdout, err := cmd.Exec("sudo sysctl -p")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("update ping status failed, err: %v", stdout)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-04-14 15:10:46 +08:00
|
|
|
|
|
|
|
func (u *FirewallService) addPortsBeforeStart(client firewall.FirewallClient) error {
|
|
|
|
serverPort, err := settingRepo.Get(settingRepo.WithByKey("ServerPort"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := client.Port(fireClient.FireInfo{Port: serverPort.Value, Protocol: "tcp", Strategy: "accept"}, "add"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := client.Port(fireClient.FireInfo{Port: "22", Protocol: "tcp", Strategy: "accept"}, "add"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := client.Port(fireClient.FireInfo{Port: "80", Protocol: "tcp", Strategy: "accept"}, "add"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := client.Port(fireClient.FireInfo{Port: "443", Protocol: "tcp", Strategy: "accept"}, "add"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
apps := u.loadPortByApp()
|
|
|
|
for _, app := range apps {
|
|
|
|
if err := client.Port(fireClient.FireInfo{Port: app.HttpPort, Protocol: "tcp", Strategy: "accept"}, "add"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return client.Reload()
|
|
|
|
}
|