Skip to content

Commit

Permalink
fix: determinate ipv6 availability before make ipv6 request (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
42atomys authored Jan 7, 2023
1 parent 64debba commit 19a2b20
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 14 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ require (
github.com/ovh/go-ovh v1.1.0
github.com/rs/zerolog v1.26.1
github.com/spf13/viper v1.10.1
github.com/stretchr/testify v1.8.1 // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -336,13 +336,19 @@ github.com/spf13/viper v1.10.1 h1:nuJZuYpG7gTj/XqiUwg8bA0cp1+M2mC3J4g5luUYBKk=
github.com/spf13/viper v1.10.1/go.mod h1:IGlFPqhNAPKRxohIzWpI5QEy4kuI7tcl5WvR+8qy1rU=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
Expand Down Expand Up @@ -784,6 +790,8 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
53 changes: 46 additions & 7 deletions internal/pkg/ip/ip.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package ip

import (
"context"
"io/ioutil"
"net"
"net/http"
"strings"
"time"

"github.com/rs/zerolog/log"
)
Expand All @@ -20,34 +22,71 @@ var (
* Fetch the IPv4 of current machine with https://ipconfig.co service
* Thanks to him
*/
func fetchIPv4() net.IP {
return fetch("https://v4.ifconfig.co/ip")
func fetchIPv4() *net.IP {
return fetch("https://ifconfig.co/ip", "tcp4")
}

/**
* Fetch the IPv6 of current machine with https://ipconfig.co service
* Thanks to him
*/
func fetchIPv6() net.IP {
return fetch("https://v6.ifconfig.co/ip")
func fetchIPv6() *net.IP {
return fetch("https://ifconfig.co/ip", "tcp6")
}

/**
* Fetch ip of current machine with given url
* url needs to return ip on text/plain Content-Type
*/
func fetch(url string) net.IP {
func fetch(url string, mode string) *net.IP {
log.Debug().Msg("Getting current IP")
resp, err := http.Get(url)
httpClient := &http.Client{
Timeout: 10 * time.Second,
}

if mode == "tcp6" && !ipv6Available() {
log.Debug().Msg("No IPv6 available")
return nil
}

transport := http.DefaultTransport.(*http.Transport).Clone()
transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
return (&net.Dialer{}).DialContext(ctx, mode, addr)
}
httpClient.Transport = transport

resp, err := httpClient.Get(url)
if err != nil {
log.Error().Err(err).Msg("cannot fetch current IP")
return nil
}

bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Error().Err(err).Msg("cannot fetch current IP")
return nil
}

s := strings.Trim(string(bodyBytes), " \n")
return net.ParseIP(s)
ip := net.ParseIP(s)

log.Debug().Msgf("Current IP %s: %s", mode, ip.String())
return &ip
}

func ipv6Available() bool {
addrs, err := net.InterfaceAddrs()
if err != nil {
return false
}

for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() == nil {
return true
}
}
}

return false
}
24 changes: 24 additions & 0 deletions internal/pkg/ip/ip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ip

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestFetchIpv4(t *testing.T) {
ip := fetchIPv4()

assert.NotNil(t, ip)
}

func TestFetchIpv6(t *testing.T) {
ip := fetchIPv6()

if ip == nil {
t.Skip("No IPv6")
}

assert.NotNil(t, ip)
assert.True(t, ip.To4() == nil)
}
16 changes: 9 additions & 7 deletions internal/pkg/ip/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ func FetchIPChangeRoutine(c chan IPChangeState, interval time.Duration) {

var oldIpv4 = CurrentIPv4
var fetchedIPv4 = fetchIPv4()
if !oldIpv4.Equal(fetchedIPv4) {
CurrentIPv4 = fetchedIPv4
if fetchedIPv4 != nil && !oldIpv4.Equal(*fetchedIPv4) {
CurrentIPv4 = *fetchedIPv4
state.IPV4Change = true
}

var oldIpv6 = CurrentIPv6
var fetchedIPv6 = fetchIPv6()
if !oldIpv6.Equal(fetchedIPv6) {
CurrentIPv6 = fetchedIPv6
state.IPV6Change = true
if ipv6Available() {
var oldIpv6 = CurrentIPv6
var fetchedIPv6 = fetchIPv6()
if fetchedIPv6 != nil && !oldIpv6.Equal(*fetchedIPv6) {
CurrentIPv6 = *fetchedIPv6
state.IPV6Change = true
}
}

c <- state
Expand Down

0 comments on commit 19a2b20

Please sign in to comment.