From 6e5beac63cdfabf00ac43ba24e14651b04fa5061 Mon Sep 17 00:00:00 2001 From: Deng Yi Date: Tue, 11 Feb 2020 16:08:50 +0800 Subject: [PATCH 1/2] add udp reachable check add integration test for udp --- integration-tests/goss/alpine3/goss-expected-q.yaml | 3 +++ integration-tests/goss/alpine3/goss-expected.yaml | 3 +++ integration-tests/goss/centos7/goss-expected-q.yaml | 3 +++ integration-tests/goss/centos7/goss-expected.yaml | 3 +++ integration-tests/goss/generate_goss.sh | 2 ++ integration-tests/goss/precise/goss-expected-q.yaml | 3 +++ integration-tests/goss/precise/goss-expected.yaml | 3 +++ integration-tests/goss/wheezy/goss-expected-q.yaml | 3 +++ integration-tests/goss/wheezy/goss-expected.yaml | 3 +++ system/addr.go | 9 +++++++-- 10 files changed, 33 insertions(+), 2 deletions(-) diff --git a/integration-tests/goss/alpine3/goss-expected-q.yaml b/integration-tests/goss/alpine3/goss-expected-q.yaml index dc3f5c160..a0be9bf69 100644 --- a/integration-tests/goss/alpine3/goss-expected-q.yaml +++ b/integration-tests/goss/alpine3/goss-expected-q.yaml @@ -19,6 +19,9 @@ addr: tcp://google.com:443: reachable: true timeout: 1000 + udp://8.8.8.8:53: + reachable: true + timeout: 1000 port: tcp:80: listening: false diff --git a/integration-tests/goss/alpine3/goss-expected.yaml b/integration-tests/goss/alpine3/goss-expected.yaml index 056642ce2..f2528e9f6 100644 --- a/integration-tests/goss/alpine3/goss-expected.yaml +++ b/integration-tests/goss/alpine3/goss-expected.yaml @@ -26,6 +26,9 @@ addr: tcp://google.com:443: reachable: true timeout: 1000 + udp://8.8.8.8:53: + reachable: true + timeout: 1000 port: tcp:80: listening: false diff --git a/integration-tests/goss/centos7/goss-expected-q.yaml b/integration-tests/goss/centos7/goss-expected-q.yaml index 74a67d892..bc0de7d5f 100644 --- a/integration-tests/goss/centos7/goss-expected-q.yaml +++ b/integration-tests/goss/centos7/goss-expected-q.yaml @@ -19,6 +19,9 @@ addr: tcp://google.com:443: reachable: true timeout: 1000 + udp://8.8.8.8:53: + reachable: true + timeout: 1000 port: tcp:80: listening: true diff --git a/integration-tests/goss/centos7/goss-expected.yaml b/integration-tests/goss/centos7/goss-expected.yaml index 387b3960d..cf075b991 100644 --- a/integration-tests/goss/centos7/goss-expected.yaml +++ b/integration-tests/goss/centos7/goss-expected.yaml @@ -26,6 +26,9 @@ addr: tcp://google.com:443: reachable: true timeout: 1000 + udp://8.8.8.8:53: + reachable: true + timeout: 1000 port: tcp:80: listening: true diff --git a/integration-tests/goss/generate_goss.sh b/integration-tests/goss/generate_goss.sh index 81c682d37..16c7cc269 100755 --- a/integration-tests/goss/generate_goss.sh +++ b/integration-tests/goss/generate_goss.sh @@ -24,6 +24,8 @@ goss a "${args[@]}" package $package foobar vim-tiny goss a "${args[@]}" addr --timeout 1s google.com:443 google.com:22 +goss a "${args[@]}" addr --timeout 1s udp://8.8.8.8:53 + goss a "${args[@]}" port tcp:80 tcp6:80 9999 goss a "${args[@]}" service $package foobar diff --git a/integration-tests/goss/precise/goss-expected-q.yaml b/integration-tests/goss/precise/goss-expected-q.yaml index 9ca2e3173..e63c3e8f1 100644 --- a/integration-tests/goss/precise/goss-expected-q.yaml +++ b/integration-tests/goss/precise/goss-expected-q.yaml @@ -19,6 +19,9 @@ addr: tcp://google.com:443: reachable: true timeout: 1000 + udp://8.8.8.8:53: + reachable: true + timeout: 1000 port: tcp:80: listening: true diff --git a/integration-tests/goss/precise/goss-expected.yaml b/integration-tests/goss/precise/goss-expected.yaml index 3fe4a2983..670356bd0 100644 --- a/integration-tests/goss/precise/goss-expected.yaml +++ b/integration-tests/goss/precise/goss-expected.yaml @@ -26,6 +26,9 @@ addr: tcp://google.com:443: reachable: true timeout: 1000 + udp://8.8.8.8:53: + reachable: true + timeout: 1000 port: tcp:80: listening: true diff --git a/integration-tests/goss/wheezy/goss-expected-q.yaml b/integration-tests/goss/wheezy/goss-expected-q.yaml index a6db97afc..c7d0e254d 100644 --- a/integration-tests/goss/wheezy/goss-expected-q.yaml +++ b/integration-tests/goss/wheezy/goss-expected-q.yaml @@ -19,6 +19,9 @@ addr: tcp://google.com:443: reachable: true timeout: 1000 + udp://8.8.8.8:53: + reachable: true + timeout: 1000 port: tcp:80: listening: true diff --git a/integration-tests/goss/wheezy/goss-expected.yaml b/integration-tests/goss/wheezy/goss-expected.yaml index a719f6bb0..74569fb7e 100644 --- a/integration-tests/goss/wheezy/goss-expected.yaml +++ b/integration-tests/goss/wheezy/goss-expected.yaml @@ -26,6 +26,9 @@ addr: tcp://google.com:443: reachable: true timeout: 1000 + udp://8.8.8.8:53: + reachable: true + timeout: 1000 port: tcp:80: listening: true diff --git a/system/addr.go b/system/addr.go index 0505d9b7d..7a512c293 100644 --- a/system/addr.go +++ b/system/addr.go @@ -40,11 +40,16 @@ func (a *DefAddr) Exists() (bool, error) { return a.Reachable() } func (a *DefAddr) Reachable() (bool, error) { network, address := splitAddress(a.address) - localAddr := &net.TCPAddr{ + localTCPAddr, localUDPAddr := &net.TCPAddr{ + IP: net.ParseIP(a.LocalAddress), + }, &net.UDPAddr{ IP: net.ParseIP(a.LocalAddress), } - d := net.Dialer{LocalAddr: localAddr, Timeout: time.Duration(a.Timeout) * time.Millisecond} + d := net.Dialer{LocalAddr: localTCPAddr, Timeout: time.Duration(a.Timeout) * time.Millisecond} + if network == "udp" { + d = net.Dialer{LocalAddr: localUDPAddr, Timeout: time.Duration(a.Timeout) * time.Millisecond} + } conn, err := d.Dial(network, address) if err != nil { return false, nil From cf732fc8659f9562a7ad7f0553031f1141d15374 Mon Sep 17 00:00:00 2001 From: Deng Yi Date: Wed, 12 Feb 2020 14:18:32 +0800 Subject: [PATCH 2/2] refactor for how variable creating --- system/addr.go | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/system/addr.go b/system/addr.go index 7a512c293..cdf74e958 100644 --- a/system/addr.go +++ b/system/addr.go @@ -40,16 +40,13 @@ func (a *DefAddr) Exists() (bool, error) { return a.Reachable() } func (a *DefAddr) Reachable() (bool, error) { network, address := splitAddress(a.address) - localTCPAddr, localUDPAddr := &net.TCPAddr{ - IP: net.ParseIP(a.LocalAddress), - }, &net.UDPAddr{ - IP: net.ParseIP(a.LocalAddress), - } - - d := net.Dialer{LocalAddr: localTCPAddr, Timeout: time.Duration(a.Timeout) * time.Millisecond} + var localAddr net.Addr if network == "udp" { - d = net.Dialer{LocalAddr: localUDPAddr, Timeout: time.Duration(a.Timeout) * time.Millisecond} + localAddr = &net.UDPAddr{IP: net.ParseIP(a.LocalAddress)} + } else { + localAddr = &net.TCPAddr{IP: net.ParseIP(a.LocalAddress)} } + d := net.Dialer{LocalAddr: localAddr, Timeout: time.Duration(a.Timeout) * time.Millisecond} conn, err := d.Dial(network, address) if err != nil { return false, nil