mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-18 08:16:39 +00:00
Use net.JoinHostPort and net.SplitHostPort for IPv6-safe host:port handling (#5836)
This commit is contained in:
@@ -3,7 +3,10 @@ package dns
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
log "github.com/sirupsen/logrus"
|
||||
@@ -201,7 +204,7 @@ func (h *nameserversHandler) getNameserverGroup(w http.ResponseWriter, r *http.R
|
||||
func toServerNSList(apiNSList []api.Nameserver) ([]nbdns.NameServer, error) {
|
||||
var nsList []nbdns.NameServer
|
||||
for _, apiNS := range apiNSList {
|
||||
parsed, err := nbdns.ParseNameServerURL(fmt.Sprintf("%s://%s:%d", apiNS.NsType, apiNS.Ip, apiNS.Port))
|
||||
parsed, err := nbdns.ParseNameServerURL(fmt.Sprintf("%s://%s", apiNS.NsType, net.JoinHostPort(strings.Trim(apiNS.Ip, "[]"), strconv.Itoa(apiNS.Port))))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -233,3 +233,37 @@ func TestNameserversHandlers(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestToServerNSList_IPv6(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input []api.Nameserver
|
||||
expectIP netip.Addr
|
||||
}{
|
||||
{
|
||||
name: "IPv4",
|
||||
input: []api.Nameserver{
|
||||
{Ip: "1.1.1.1", NsType: "udp", Port: 53},
|
||||
},
|
||||
expectIP: netip.MustParseAddr("1.1.1.1"),
|
||||
},
|
||||
{
|
||||
name: "IPv6",
|
||||
input: []api.Nameserver{
|
||||
{Ip: "2001:4860:4860::8888", NsType: "udp", Port: 53},
|
||||
},
|
||||
expectIP: netip.MustParseAddr("2001:4860:4860::8888"),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result, err := toServerNSList(tc.input)
|
||||
assert.NoError(t, err)
|
||||
if assert.Len(t, result, 1) {
|
||||
assert.Equal(t, tc.expectIP, result[0].IP)
|
||||
assert.Equal(t, 53, result[0].Port)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user