Usignregex to determine if it os ipv6 or not

This commit is contained in:
Paulo Nascimento 2021-03-20 18:57:27 -03:00
parent 08d1a1784a
commit e0baad92fe

View File

@ -17,6 +17,7 @@ package iptables
import ( import (
"fmt" "fmt"
"net" "net"
"regexp"
"sync" "sync"
"time" "time"
@ -353,6 +354,9 @@ func (c *Controller) Set(rules []Rule) error {
c.Lock() c.Lock()
defer c.Unlock() defer c.Unlock()
var i int var i int
ipv6Regex,_ := regexp.Compile("[-]d\\s(.*:.*\\s[-]m\\scomment)")
for ; i < len(rules); i++ { for ; i < len(rules); i++ {
if i < len(c.rules) { if i < len(c.rules) {
if rules[i].String() != c.rules[i].String() { if rules[i].String() != c.rules[i].String() {
@ -362,15 +366,19 @@ func (c *Controller) Set(rules []Rule) error {
} }
} }
if i >= len(c.rules) { if i >= len(c.rules) {
var proto = ProtocolIPv4 //rules[i].Proto() proto := ProtocolIPv4
var protocolName = "ipv4" ruleString := rules[i].String()
if ipv6Regex.MatchString(ruleString) {
proto = ProtocolIPv6
}
protocolName := "ipv4"
if proto == ProtocolIPv6 { if proto == ProtocolIPv6 {
protocolName = "ipv6" protocolName = "ipv6"
} }
var ruleString = rules[i].String()
level.Debug(c.logger).Log("msg", "Applying Firewall Rule...", "Rule", ruleString, "Protocol", protocolName) level.Debug(c.logger).Log("msg", "Applying Firewall Rule...", "Rule", ruleString, "Protocol", protocolName)
if err := rules[i].Add(c.client(proto)); err != nil { if err := rules[i].Add(c.client(proto)); err != nil {
return fmt.Errorf("failed to add rule: %v", err) return fmt.Errorf("failed to add rule: %v", err)