fix: accept bare Pi-hole IPv4 addresses
This commit is contained in:
1 parent
42fa6efc84
commit
2082b68794
5 files changed
+15
-7
No files matched your search
@@ -71,7 +71,11 @@ func New(rawURL string, insecureTLS bool) (*Client, error) {
|
||||
}
|
||||
|
||||
func normalizeBaseURL(raw string) (string, error) {
|
||||
parsed, err := url.Parse(strings.TrimSpace(raw))
|
||||
raw = strings.TrimSpace(raw)
|
||||
if address := net.ParseIP(raw); address != nil && address.To4() != nil {
|
||||
raw = "http://" + raw
|
||||
}
|
||||
parsed, err := url.Parse(raw)
|
||||
if err != nil || parsed.Host == "" || (parsed.Scheme != "http" && parsed.Scheme != "https") {
|
||||
return "", errors.New("Pi-hole-Adresse muss eine vollständige HTTP- oder HTTPS-URL sein")
|
||||
}
|
||||
|
||||
@@ -101,6 +101,10 @@ func TestNewValidatesAndNormalizesPiHoleURL(t *testing.T) {
|
||||
if err != nil || client.BaseURL() != "http://pihole.local/custom/api" {
|
||||
t.Fatalf("custom path not normalized: %q err=%v", client.BaseURL(), err)
|
||||
}
|
||||
client, err = New("10.0.20.100", false)
|
||||
if err != nil || client.BaseURL() != "http://10.0.20.100/api" {
|
||||
t.Fatalf("bare IPv4 address not normalized: %q err=%v", client.BaseURL(), err)
|
||||
}
|
||||
for _, invalid := range []string{"pi.hole", "ftp://pi.hole", "https://user:pass@pi.hole", "https://pi.hole?sid=secret"} {
|
||||
if _, err := New(invalid, false); err == nil {
|
||||
t.Errorf("invalid URL accepted: %s", invalid)
|
||||
|
||||
Reference in new issue
Block a user