Skip to content

Commit 14d56d5

Browse files
authored
Merge pull request #83 from yywing/fix_timeout
fix: timeout work on version >= v1.6.0
2 parents c8b6bcf + 071c14f commit 14d56d5

1 file changed

Lines changed: 27 additions & 14 deletions

File tree

iptables/iptables.go

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,17 @@ const (
6464
)
6565

6666
type IPTables struct {
67-
path string
68-
proto Protocol
69-
hasCheck bool
70-
hasWait bool
71-
hasRandomFully bool
72-
v1 int
73-
v2 int
74-
v3 int
75-
mode string // the underlying iptables operating mode, e.g. nf_tables
76-
timeout int // time to wait for the iptables lock, default waits forever
67+
path string
68+
proto Protocol
69+
hasCheck bool
70+
hasWait bool
71+
waitSupportSecond bool
72+
hasRandomFully bool
73+
v1 int
74+
v2 int
75+
v3 int
76+
mode string // the underlying iptables operating mode, e.g. nf_tables
77+
timeout int // time to wait for the iptables lock, default waits forever
7778
}
7879

7980
// Stat represents a structured statistic entry.
@@ -139,9 +140,10 @@ func New(opts ...option) (*IPTables, error) {
139140
ipt.v3 = v3
140141
ipt.mode = mode
141142

142-
checkPresent, waitPresent, randomFullyPresent := getIptablesCommandSupport(v1, v2, v3)
143+
checkPresent, waitPresent, waitSupportSecond, randomFullyPresent := getIptablesCommandSupport(v1, v2, v3)
143144
ipt.hasCheck = checkPresent
144145
ipt.hasWait = waitPresent
146+
ipt.waitSupportSecond = waitSupportSecond
145147
ipt.hasRandomFully = randomFullyPresent
146148

147149
return ipt, nil
@@ -495,7 +497,7 @@ func (ipt *IPTables) runWithOutput(args []string, stdout io.Writer) error {
495497
args = append([]string{ipt.path}, args...)
496498
if ipt.hasWait {
497499
args = append(args, "--wait")
498-
if ipt.timeout != 0 {
500+
if ipt.timeout != 0 && ipt.waitSupportSecond {
499501
args = append(args, strconv.Itoa(ipt.timeout))
500502
}
501503
} else {
@@ -541,8 +543,8 @@ func getIptablesCommand(proto Protocol) string {
541543
}
542544

543545
// Checks if iptables has the "-C" and "--wait" flag
544-
func getIptablesCommandSupport(v1 int, v2 int, v3 int) (bool, bool, bool) {
545-
return iptablesHasCheckCommand(v1, v2, v3), iptablesHasWaitCommand(v1, v2, v3), iptablesHasRandomFully(v1, v2, v3)
546+
func getIptablesCommandSupport(v1 int, v2 int, v3 int) (bool, bool, bool, bool) {
547+
return iptablesHasCheckCommand(v1, v2, v3), iptablesHasWaitCommand(v1, v2, v3), iptablesWaitSupportSecond(v1, v2, v3), iptablesHasRandomFully(v1, v2, v3)
546548
}
547549

548550
// getIptablesVersion returns the first three components of the iptables version
@@ -617,6 +619,17 @@ func iptablesHasWaitCommand(v1 int, v2 int, v3 int) bool {
617619
return false
618620
}
619621

622+
//Checks if an iptablse version is after 1.6.0, when --wait support second
623+
func iptablesWaitSupportSecond(v1 int, v2 int, v3 int) bool {
624+
if v1 > 1 {
625+
return true
626+
}
627+
if v1 == 1 && v2 >= 6 {
628+
return true
629+
}
630+
return false
631+
}
632+
620633
// Checks if an iptables version is after 1.6.2, when --random-fully was added
621634
func iptablesHasRandomFully(v1 int, v2 int, v3 int) bool {
622635
if v1 > 1 {

0 commit comments

Comments
 (0)