iptables-helper/pkg/utils/iptables/parser.go

125 lines
3.4 KiB
Go
Raw Normal View History

2023-11-02 15:02:55 +08:00
package iptables
import (
"fmt"
flag "github.com/spf13/pflag"
"iptables-helper/pkg/logger"
2023-11-02 18:30:54 +08:00
utils "iptables-helper/pkg/utils/json"
2023-11-02 15:02:55 +08:00
"strings"
)
// Parse
// iptables 规则解析
func Parse(rules string) {
results := strings.Split(rules, "\n")
policyList := make([]Policy, 0)
chainList := make([]Chain, 0)
ruleList := make([]Rule, 0)
for _, rule := range results {
logger.Log().Debug("解析规则: ", rule)
//rule := "-A OUTPUT ! -d 127.0.0.0/8 -m addrtype --dst-type LOCAL -j DOCKER"
// 转化以便解析
rule = strings.ReplaceAll(rule, "! -s", "--excludeS")
rule = strings.ReplaceAll(rule, "! -d", "--excludeD")
rule = strings.ReplaceAll(rule, "! -i", "--excludeI")
rule = strings.ReplaceAll(rule, "! -o", "--excludeO")
flagSet := flag.FlagSet{}
flagSet.ParseErrorsWhitelist = flag.ParseErrorsWhitelist{UnknownFlags: true}
//var reverse bool
//var dst string
// 优先解析/判断 P N A 三个基本类型
var policy string
// 策略
flagSet.StringVarP(&policy, "policy", "P", "", "")
var appendRule string
// 追加规则
flagSet.StringVarP(&appendRule, "append", "A", "", "")
var newChain string
// 创建自定义规则链
flagSet.StringVarP(&newChain, "new-chain", "N", "", "")
args := strings.Split(rule, " ")
// 解析一部分
_ = flagSet.Parse(args)
if len(policy) > 0 {
target := PolicyTarget(flagSet.Arg(0))
chain := Chain(policy)
chainList = append(chainList, chain)
policyList = append(policyList, Policy{chain, target})
continue
}
if len(newChain) > 0 {
chainList = append(chainList, Chain(newChain))
continue
}
if len(appendRule) > 0 {
// 来源
source := flagSet.StringP("source", "s", "", "")
excludeSource := flagSet.String("excludeS", "", "")
// 目标
destination := flagSet.StringP("destination", "d", "", "")
excludeDestination := flagSet.String("excludeD", "", "")
2023-11-02 18:30:54 +08:00
inputInterface := flagSet.StringP("in-interface", "i", "", "")
excludeInputInterface := flagSet.String("excludeI", "", "")
outputInterface := flagSet.StringP("out-interface", "o", "", "")
excludeOutputInterface := flagSet.String("excludeO", "", "")
protocol := flagSet.StringP("protocol", "p", "", "")
excludeProtocol := flagSet.String("excludeP", "", "")
jump := flagSet.StringP("jump", "j", "", "")
gotoChain := flagSet.StringP("goto", "g", "", "")
2023-11-02 15:02:55 +08:00
_ = flagSet.Parse(args)
r := Rule{
Chain: Chain(appendRule),
Source: *source,
ExcludeSource: *excludeSource,
Destination: *destination,
ExcludeDestination: *excludeDestination,
2023-11-02 18:30:54 +08:00
InputInterface: *inputInterface,
ExcludeInputInterface: *excludeInputInterface,
OutputInterface: *outputInterface,
ExcludeOutputInterface: *excludeOutputInterface,
Protocol: *protocol,
ExcludeProtocol: *excludeProtocol,
Jump: Chain(*jump),
Goto: Chain(*gotoChain),
2023-11-02 15:02:55 +08:00
}
ruleList = append(ruleList, r)
}
//logger.Log().Debugf("appendRule %+v", appendRule)
//logger.Log().Debugf("reverse %+v", reverse)
}
for i := 0; i < 50; i++ {
fmt.Print("=")
}
fmt.Println()
for _, policy := range policyList {
logger.Log().Infof("默认策略: %s => %s", policy.Name, policy.Target)
}
for _, chain := range chainList {
logger.Log().Infof("自定义规则链: %s", chain)
}
for _, rule := range ruleList {
2023-11-02 18:30:54 +08:00
logger.Log().Infof("规则: %+v", utils.Json(rule))
2023-11-02 15:02:55 +08:00
}
}