Skip to content

Commit cc59dff

Browse files
committed
Check TCP entries to make sure the connection is open
Signed-off-by: Matt Farina <[email protected]>
1 parent 6b4315c commit cc59dff

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

pkg/guestagent/iptables/iptables.go

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import (
88
"regexp"
99
"strconv"
1010
"strings"
11+
"time"
1112
)
1213

1314
type Entry struct {
15+
TCP bool
1416
IP net.IP
1517
Port int
1618
}
@@ -27,7 +29,7 @@ type Entry struct {
2729
// ipv4 IP address. We need to detect this IP.
2830
// --dport is the destination port. We need to detect this port
2931
// -j DNAT this tells us it's the line doing the port forwarding.
30-
var findPortRegex = regexp.MustCompile(`-A\s+CNI-DN-\w*\s+(?:-d ((?:\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}))?(?:/32\s+)?-p .*--dport (\d+) -j DNAT`)
32+
var findPortRegex = regexp.MustCompile(`-A\s+CNI-DN-\w*\s+(?:-d ((?:\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}))?(?:/32\s+)?-p (tcp)?.*--dport (\d+) -j DNAT`)
3133

3234
func GetPorts() ([]Entry, error) {
3335
// TODO: add support for ipv6
@@ -50,19 +52,29 @@ func GetPorts() ([]Entry, error) {
5052
return nil, err
5153
}
5254

53-
return parsePortsFromRules(res)
55+
pts, err := parsePortsFromRules(res)
56+
if err != nil {
57+
return nil, err
58+
}
59+
60+
return checkPortsOpen(pts)
5461
}
5562

5663
func parsePortsFromRules(rules []string) ([]Entry, error) {
5764
var entries []Entry
5865
for _, rule := range rules {
5966
if found := findPortRegex.FindStringSubmatch(rule); found != nil {
60-
if len(found) == 3 {
61-
port, err := strconv.Atoi(found[2])
67+
if len(found) == 4 {
68+
port, err := strconv.Atoi(found[3])
6269
if err != nil {
6370
return nil, err
6471
}
6572

73+
istcp := false
74+
if found[2] == "tcp" {
75+
istcp = true
76+
}
77+
6678
// if the IP is blank the port forwarding the portforwarding,
6779
// which gets information from this, will skip it. When no IP
6880
// is present localhost will work.
@@ -73,6 +85,7 @@ func parsePortsFromRules(rules []string) ([]Entry, error) {
7385
ent := Entry{
7486
IP: net.ParseIP(ip),
7587
Port: port,
88+
TCP: istcp,
7689
}
7790
entries = append(entries, ent)
7891
}
@@ -109,3 +122,20 @@ func listNATRules(pth string) ([]string, error) {
109122

110123
return rules, nil
111124
}
125+
126+
func checkPortsOpen(pts []Entry) ([]Entry, error) {
127+
var entries []Entry
128+
for _, pt := range pts {
129+
if pt.TCP {
130+
conn, err := net.DialTimeout("tcp", net.JoinHostPort(pt.IP.String(), strconv.Itoa(pt.Port)), time.Second)
131+
if err == nil && conn != nil {
132+
conn.Close()
133+
entries = append(entries, pt)
134+
}
135+
} else {
136+
entries = append(entries, pt)
137+
}
138+
}
139+
140+
return entries, nil
141+
}

pkg/guestagent/iptables/iptables_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ func TestParsePortsFromRules(t *testing.T) {
8484
t.Fatalf("expected 2 ports parsed from iptables but parsed %d", l)
8585
}
8686

87-
if res[0].IP.String() != "127.0.0.1" || res[0].Port != 8082 {
88-
t.Errorf("expected port 8082 on IP 127.0.0.1 but go port %d on IP %s", res[0].Port, res[0].IP.String())
87+
if res[0].IP.String() != "127.0.0.1" || res[0].Port != 8082 || res[0].TCP != true {
88+
t.Errorf("expected port 8082 on IP 127.0.0.1 with TCP true but go port %d on IP %s with TCP %t", res[0].Port, res[0].IP.String(), res[0].TCP)
8989
}
90-
if res[1].IP.String() != "127.0.0.1" || res[1].Port != 8081 {
91-
t.Errorf("expected port 8081 on IP 127.0.0.1 but go port %d on IP %s", res[1].Port, res[1].IP.String())
90+
if res[1].IP.String() != "127.0.0.1" || res[1].Port != 8081 || res[1].TCP != true {
91+
t.Errorf("expected port 8081 on IP 127.0.0.1 with TCP true but go port %d on IP %s with TCP %t", res[1].Port, res[1].IP.String(), res[1].TCP)
9292
}
9393
}

0 commit comments

Comments
 (0)