44 "bufio"
55 "encoding/json"
66 "errors"
7+ "fmt"
78 "net"
89 "os"
910 "os/exec"
@@ -40,17 +41,16 @@ func newHetznerConfigurer(config *IPConfiguration, verbose bool) (*HetznerConfig
4041 * In order to tell the Hetzner API to route the failover-ip to
4142 * this machine, we must attach our own IP address to the API request.
4243 */
43- func getOutboundIP () net.IP {
44+ func getOutboundIP () ( net.IP , error ) {
4445 conn , err := net .Dial ("udp" , "8.8.8.8:80" )
4546 if err != nil || conn == nil {
46- log .Error ("error dialing 8.8.8.8 to retrieve preferred outbound IP" , err )
47- return nil
47+ return nil , fmt .Errorf ("error dialing 8.8.8.8 to retrieve preferred outbound IP: %w" , err )
4848 }
4949 defer conn .Close ()
5050
5151 localAddr := conn .LocalAddr ().(* net.UDPAddr )
5252
53- return localAddr .IP
53+ return localAddr .IP , nil
5454}
5555
5656func (c * HetznerConfigurer ) curlQueryFailover (post bool ) (string , error ) {
@@ -98,10 +98,10 @@ func (c *HetznerConfigurer) curlQueryFailover(post bool) (string, error) {
9898 */
9999 var cmd * exec.Cmd
100100 if post {
101- myOwnIP := getOutboundIP ()
102- if myOwnIP = = nil {
103- log .Error ("Error determining this machine's IP address." )
104- return "" , errors . New ("error determining this machine's IP address" )
101+ myOwnIP , err := getOutboundIP ()
102+ if err ! = nil {
103+ log .Error ("Error determining this machine's IP address." , err )
104+ return "" , fmt . Errorf ("error determining this machine's IP address: %w" , err )
105105 }
106106 log .Infof ("my_own_ip: %s\n " , myOwnIP .String ())
107107
@@ -224,7 +224,14 @@ func (c *HetznerConfigurer) queryAddress() bool {
224224 c .cachedState = unknown
225225 }
226226
227- if currentFailoverDestinationIP .Equal (getOutboundIP ()) {
227+ myOwnIP , err := getOutboundIP ()
228+ if err != nil {
229+ log .Error ("Error determining this machine's IP address." , err )
230+ c .cachedState = unknown
231+ return false
232+ }
233+
234+ if currentFailoverDestinationIP .Equal (myOwnIP ) {
228235 //We "are" the current failover destination.
229236 c .cachedState = configured
230237 return true
@@ -262,7 +269,14 @@ func (c *HetznerConfigurer) runAddressConfiguration() bool {
262269
263270 c .lastAPICheck = time .Now ()
264271
265- if currentFailoverDestinationIP .Equal (getOutboundIP ()) {
272+ myOwnIP , err := getOutboundIP ()
273+ if err != nil {
274+ log .Error ("Error determining this machine's IP address." , err )
275+ c .cachedState = unknown
276+ return false
277+ }
278+
279+ if currentFailoverDestinationIP .Equal (myOwnIP ) {
266280 //We "are" the current failover destination.
267281 log .Info ("Failover was successfully executed!" )
268282 c .cachedState = configured
@@ -271,7 +285,7 @@ func (c *HetznerConfigurer) runAddressConfiguration() bool {
271285
272286 log .Infof ("The failover command was issued, but the current Failover destination (%s) is different from what it should be (%s)." ,
273287 currentFailoverDestinationIP .String (),
274- getOutboundIP () .String ())
288+ myOwnIP .String ())
275289 //Something must have gone wrong while trying to switch IP's...
276290 c .cachedState = unknown
277291 return false
0 commit comments