Files
EdgeNode/internal/iplibrary/action_iptables.go

137 lines
3.0 KiB
Go
Raw Normal View History

2021-02-06 17:34:33 +08:00
package iplibrary
import (
2023-08-11 14:51:23 +08:00
"fmt"
2024-07-27 15:42:50 +08:00
"runtime"
"strings"
"time"
2021-02-06 17:34:33 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
"github.com/TeaOSLab/EdgeNode/internal/utils"
2022-09-15 11:14:33 +08:00
executils "github.com/TeaOSLab/EdgeNode/internal/utils/exec"
2021-02-06 17:34:33 +08:00
)
2021-11-14 20:46:08 +08:00
// IPTablesAction IPTables动作
2021-02-06 17:34:33 +08:00
// 相关命令:
2022-09-15 11:14:33 +08:00
//
// iptables -A INPUT -s "192.168.2.32" -j ACCEPT
// iptables -A INPUT -s "192.168.2.32" -j REJECT
// iptables -D INPUT ...
// iptables -F INPUT
2021-02-06 17:34:33 +08:00
type IPTablesAction struct {
BaseAction
config *firewallconfigs.FirewallActionIPTablesConfig
iptablesNotFound bool
2021-02-06 17:34:33 +08:00
}
func NewIPTablesAction() *IPTablesAction {
return &IPTablesAction{}
}
func (this *IPTablesAction) Init(config *firewallconfigs.FirewallActionConfig) error {
this.config = &firewallconfigs.FirewallActionIPTablesConfig{}
err := this.convertParams(config.Params, this.config)
if err != nil {
return err
}
return nil
}
func (this *IPTablesAction) AddItem(listType IPListType, item *pb.IPItem) error {
return this.runAction("addItem", listType, item)
}
func (this *IPTablesAction) DeleteItem(listType IPListType, item *pb.IPItem) error {
return this.runAction("deleteItem", listType, item)
}
func (this *IPTablesAction) runAction(action string, listType IPListType, item *pb.IPItem) error {
if item.Type == "all" {
return nil
}
if len(item.IpTo) == 0 {
return this.runActionSingleIP(action, listType, item)
}
cidrList, err := iPv4RangeToCIDRRange(item.IpFrom, item.IpTo)
if err != nil {
// 不合法的范围不予处理即可
return nil
}
if len(cidrList) == 0 {
return nil
}
for _, cidr := range cidrList {
item.IpFrom = cidr
item.IpTo = ""
err := this.runActionSingleIP(action, listType, item)
if err != nil {
return err
}
}
return nil
}
func (this *IPTablesAction) runActionSingleIP(action string, listType IPListType, item *pb.IPItem) error {
// 暂时不支持ipv6
// TODO 将来支持ipv6
if utils.IsIPv6(item.IpFrom) {
return nil
}
2021-02-06 17:34:33 +08:00
if item.Type == "all" {
return nil
}
var path = this.config.Path
2021-02-06 17:34:33 +08:00
var err error
if len(path) == 0 {
path, err = executils.LookPath("iptables")
2021-02-06 17:34:33 +08:00
if err != nil {
if this.iptablesNotFound {
return nil
}
this.iptablesNotFound = true
2021-02-06 17:34:33 +08:00
return err
}
this.config.Path = path
2021-02-06 17:34:33 +08:00
}
iptablesAction := ""
switch action {
case "addItem":
iptablesAction = "-A"
case "deleteItem":
iptablesAction = "-D"
default:
return nil
}
args := []string{iptablesAction, "INPUT", "-s", item.IpFrom, "-j"}
switch listType {
case IPListTypeWhite:
args = append(args, "ACCEPT")
case IPListTypeBlack:
args = append(args, "REJECT")
default:
return nil
}
if runtime.GOOS == "darwin" {
// MAC OS直接返回
return nil
}
2022-09-15 11:14:33 +08:00
var cmd = executils.NewTimeoutCmd(30*time.Second, path, args...)
cmd.WithStderr()
2021-02-06 17:34:33 +08:00
err = cmd.Run()
if err != nil {
2022-09-15 11:14:33 +08:00
var output = cmd.Stderr()
if strings.Contains(output, "No chain/target/match") {
2021-02-06 17:34:33 +08:00
err = nil
} else {
2023-08-11 14:51:23 +08:00
return fmt.Errorf("%w, output: %s", err, output)
2021-02-06 17:34:33 +08:00
}
}
return nil
}