mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-28 20:13:33 +00:00
fix: format IPv6 addr correclty for HTTP URL (#254)
* fix: format IPv6 addr correclty for HTTP URL * test: cover changes * test: refactor --------- Co-authored-by: gm0stache <gm0stache@noreply.codeberg.org>
This commit is contained in:
+8
-2
@@ -5,7 +5,9 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/netip"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -108,8 +110,7 @@ func verifyCaddyReachable(ctx context.Context, m *pb.MachineInfo) error {
|
|||||||
eventID := fmt.Sprintf("Machine %s (%s)", m.Name, publicIP)
|
eventID := fmt.Sprintf("Machine %s (%s)", m.Name, publicIP)
|
||||||
pw.Event(progress.NewEvent(eventID, progress.Working, "Querying"))
|
pw.Event(progress.NewEvent(eventID, progress.Working, "Querying"))
|
||||||
|
|
||||||
verifyURL := fmt.Sprintf("http://%s%s", publicIP, caddyconfig.VerifyPath)
|
verifyURL := getVerifyURL(publicIP)
|
||||||
|
|
||||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, verifyURL, nil)
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, verifyURL, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
pw.Event(progress.NewEvent(eventID, progress.Error, err.Error()))
|
pw.Event(progress.NewEvent(eventID, progress.Error, err.Error()))
|
||||||
@@ -170,6 +171,11 @@ func verifyCaddyReachable(ctx context.Context, m *pb.MachineInfo) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getVerifyURL(publicIP netip.Addr) string {
|
||||||
|
httpFormattedIP := net.JoinHostPort(publicIP.String(), "")
|
||||||
|
return fmt.Sprintf("http://%s%s", httpFormattedIP, caddyconfig.VerifyPath)
|
||||||
|
}
|
||||||
|
|
||||||
// unreachable creates a new Unreachable error event.
|
// unreachable creates a new Unreachable error event.
|
||||||
func unreachable(id string) progress.Event {
|
func unreachable(id string) progress.Event {
|
||||||
return progress.NewEvent(
|
return progress.NewEvent(
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/netip"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetVerifyURL(t *testing.T) {
|
||||||
|
tests := map[string]struct {
|
||||||
|
given netip.Addr
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
"IPv4": {
|
||||||
|
given: netip.MustParseAddr("93.184.216.34"),
|
||||||
|
want: "http://93.184.216.34:/.uncloud-verify",
|
||||||
|
},
|
||||||
|
"IPv6": {
|
||||||
|
given: netip.MustParseAddr("2001:db8::1"),
|
||||||
|
want: "http://[2001:db8::1]:/.uncloud-verify",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for name, tt := range tests {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
got := getVerifyURL(tt.given)
|
||||||
|
assert.Equal(t, tt.want, got)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user