Skip to content

Commit 930548d

Browse files
authored
Merge pull request #48 from singchia/fix/match_tcp_port_range
Fix/match tcp port range
2 parents 183216a + c34034f commit 930548d

File tree

1 file changed

+41
-32
lines changed

1 file changed

+41
-32
lines changed

iptables/match.go

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9531,23 +9531,22 @@ func (mTCP *MatchTCP) Short() string {
95319531
func (mTCP *MatchTCP) ShortArgs() []string {
95329532
args := make([]string, 0, 17)
95339533
args = append(args, "-m", mTCP.matchType.String())
9534-
if mTCP.SrcPortMin > -1 {
9534+
if mTCP.SrcPortMin > -1 && mTCP.SrcPortMax > -1 {
9535+
args = append(args, "--sport", strconv.Itoa(mTCP.SrcPortMin)+":"+strconv.Itoa(mTCP.SrcPortMax))
9536+
} else if mTCP.SrcPortMin > -1 {
95359537
if mTCP.SrcPortInvert {
95369538
args = append(args, "!")
95379539
}
95389540
args = append(args, "--sport", strconv.Itoa(mTCP.SrcPortMin))
9539-
if mTCP.SrcPortMax > -1 {
9540-
args = append(args, ":"+strconv.Itoa(mTCP.SrcPortMax))
9541-
}
95429541
}
9543-
if mTCP.DstPortMin > -1 {
9542+
9543+
if mTCP.DstPortMin > -1 && mTCP.DstPortMax > -1 {
9544+
args = append(args, "--dport", strconv.Itoa(mTCP.DstPortMin)+":"+strconv.Itoa(mTCP.DstPortMax))
9545+
} else if mTCP.DstPortMin > -1 {
95449546
if mTCP.DstPortInvert {
95459547
args = append(args, "!")
95469548
}
95479549
args = append(args, "--dport", strconv.Itoa(mTCP.DstPortMin))
9548-
if mTCP.DstPortMax > -1 {
9549-
args = append(args, ":"+strconv.Itoa(mTCP.DstPortMax))
9550-
}
95519550
}
95529551
if mTCP.FlagsMask != 0 && mTCP.FlagsSet != 0 {
95539552
if mTCP.FlagsInvert {
@@ -9571,24 +9570,24 @@ func (mTCP *MatchTCP) Long() string {
95719570
func (mTCP *MatchTCP) LongArgs() []string {
95729571
args := make([]string, 0, 17)
95739572
args = append(args, "-m", mTCP.matchType.String())
9574-
if mTCP.SrcPortMin > -1 {
9573+
if mTCP.SrcPortMin > -1 && mTCP.SrcPortMax > -1 {
9574+
args = append(args, "--source-port", strconv.Itoa(mTCP.SrcPortMin)+":"+strconv.Itoa(mTCP.SrcPortMax))
9575+
} else if mTCP.SrcPortMin > -1 {
95759576
if mTCP.SrcPortInvert {
95769577
args = append(args, "!")
95779578
}
95789579
args = append(args, "--source-port", strconv.Itoa(mTCP.SrcPortMin))
9579-
if mTCP.SrcPortMax > -1 {
9580-
args = append(args, ":"+strconv.Itoa(mTCP.SrcPortMax))
9581-
}
95829580
}
9583-
if mTCP.DstPortMin > -1 {
9581+
9582+
if mTCP.DstPortMin > -1 && mTCP.DstPortMax > -1 {
9583+
args = append(args, "--destination-port", strconv.Itoa(mTCP.DstPortMin)+":"+strconv.Itoa(mTCP.DstPortMax))
9584+
} else if mTCP.DstPortMin > -1 {
95849585
if mTCP.DstPortInvert {
95859586
args = append(args, "!")
95869587
}
95879588
args = append(args, "--destination-port", strconv.Itoa(mTCP.DstPortMin))
9588-
if mTCP.DstPortMax > -1 {
9589-
args = append(args, ":"+strconv.Itoa(mTCP.DstPortMax))
9590-
}
95919589
}
9590+
95929591
if mTCP.FlagsMask != 0 && mTCP.FlagsSet != 0 {
95939592
if mTCP.FlagsInvert {
95949593
args = append(args, "!")
@@ -10315,23 +10314,28 @@ func (mUDP *MatchUDP) Short() string {
1031510314
func (mUDP *MatchUDP) ShortArgs() []string {
1031610315
args := make([]string, 0, 17)
1031710316
args = append(args, "-m", mUDP.matchType.String())
10318-
if mUDP.SrcPortMin > -1 {
10317+
if mUDP.SrcPortMin > -1 && mUDP.SrcPortMax > -1 {
1031910318
if mUDP.SrcPortInvert {
1032010319
args = append(args, "!")
1032110320
}
10322-
args = append(args, "--sport", strconv.Itoa(mUDP.SrcPortMin))
10323-
if mUDP.SrcPortMax > -1 {
10324-
args = append(args, ":"+strconv.Itoa(mUDP.SrcPortMax))
10321+
args = append(args, "--sport", strconv.Itoa(mUDP.SrcPortMin)+":"+strconv.Itoa(mUDP.SrcPortMax))
10322+
} else if mUDP.SrcPortMin > -1 {
10323+
if mUDP.SrcPortInvert {
10324+
args = append(args, "!")
1032510325
}
10326+
args = append(args, "--sport", strconv.Itoa(mUDP.SrcPortMin))
1032610327
}
10327-
if mUDP.DstPortMin > -1 {
10328+
10329+
if mUDP.DstPortMin > -1 && mUDP.DstPortMax > -1 {
1032810330
if mUDP.DstPortInvert {
1032910331
args = append(args, "!")
1033010332
}
10331-
args = append(args, "--dport", strconv.Itoa(mUDP.DstPortMin))
10332-
if mUDP.DstPortMax > -1 {
10333-
args = append(args, ":"+strconv.Itoa(mUDP.DstPortMax))
10333+
args = append(args, "--dport", strconv.Itoa(mUDP.DstPortMin)+":"+strconv.Itoa(mUDP.DstPortMax))
10334+
} else if mUDP.DstPortMin > -1 {
10335+
if mUDP.DstPortInvert {
10336+
args = append(args, "!")
1033410337
}
10338+
args = append(args, "--dport", strconv.Itoa(mUDP.DstPortMin))
1033510339
}
1033610340
return args
1033710341
}
@@ -10343,23 +10347,28 @@ func (mUDP *MatchUDP) Long() string {
1034310347
func (mUDP *MatchUDP) LongArgs() []string {
1034410348
args := make([]string, 0, 17)
1034510349
args = append(args, "-m", mUDP.matchType.String())
10346-
if mUDP.SrcPortMin > -1 {
10350+
if mUDP.SrcPortMin > -1 && mUDP.SrcPortMax > -1 {
1034710351
if mUDP.SrcPortInvert {
1034810352
args = append(args, "!")
1034910353
}
10350-
args = append(args, "--source-port", strconv.Itoa(mUDP.SrcPortMin))
10351-
if mUDP.SrcPortMax > -1 {
10352-
args = append(args, ":"+strconv.Itoa(mUDP.SrcPortMax))
10354+
args = append(args, "--source-port", strconv.Itoa(mUDP.SrcPortMin)+":"+strconv.Itoa(mUDP.SrcPortMax))
10355+
} else if mUDP.SrcPortMin > -1 {
10356+
if mUDP.SrcPortInvert {
10357+
args = append(args, "!")
1035310358
}
10359+
args = append(args, "--source-port", strconv.Itoa(mUDP.SrcPortMin))
1035410360
}
10355-
if mUDP.DstPortMin > -1 {
10361+
10362+
if mUDP.DstPortMin > -1 && mUDP.DstPortMax > -1 {
1035610363
if mUDP.DstPortInvert {
1035710364
args = append(args, "!")
1035810365
}
10359-
args = append(args, "--destination-port", strconv.Itoa(mUDP.DstPortMin))
10360-
if mUDP.DstPortMax > -1 {
10361-
args = append(args, ":"+strconv.Itoa(mUDP.DstPortMax))
10366+
args = append(args, "--destination-port", strconv.Itoa(mUDP.DstPortMin)+":"+strconv.Itoa(mUDP.DstPortMax))
10367+
} else if mUDP.DstPortMin > -1 {
10368+
if mUDP.DstPortInvert {
10369+
args = append(args, "!")
1036210370
}
10371+
args = append(args, "--destination-port", strconv.Itoa(mUDP.DstPortMin))
1036310372
}
1036410373
return args
1036510374
}

0 commit comments

Comments
 (0)