Skip to content
Snippets Groups Projects
Commit 7a79b514 authored by Erick Hitter's avatar Erick Hitter
Browse files

Ensure IPs are valid

parent 553a60f0
Branches
Tags
No related merge requests found
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"github.com/joshbetz/config" "github.com/joshbetz/config"
"io/ioutil" "io/ioutil"
"log" "log"
"net"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
...@@ -55,9 +56,15 @@ func main() { ...@@ -55,9 +56,15 @@ func main() {
// IPv4 is required // IPv4 is required
if ipv4, err := getUrl(ipv4Endpoint); err == nil { if ipv4, err := getUrl(ipv4Endpoint); err == nil {
query := endpoint.Query() if ipv4Valid := net.ParseIP(ipv4); ipv4Valid == nil {
query.Set("myip", ipv4) logger.Println("Invalid IPv4 address")
endpoint.RawQuery = query.Encode() logger.Printf("%s", err)
return
} else {
query := endpoint.Query()
query.Set("myip", ipv4Valid.String())
endpoint.RawQuery = query.Encode()
}
} else { } else {
logger.Println("Couldn't retrieve IPv4 address") logger.Println("Couldn't retrieve IPv4 address")
logger.Printf("%s", err) logger.Printf("%s", err)
...@@ -66,9 +73,16 @@ func main() { ...@@ -66,9 +73,16 @@ func main() {
// IPv6 is optional // IPv6 is optional
if ipv6, err := getUrl(ipv6Endpoint); err == nil { if ipv6, err := getUrl(ipv6Endpoint); err == nil {
query := endpoint.Query() if ipv6Valid := net.ParseIP(ipv6); ipv6Valid == nil {
query.Set("myip6", ipv6) logger.Println("Invalid IPv6 address")
endpoint.RawQuery = query.Encode() logger.Printf("%s", err)
} else {
// TODO: parse to /64, use ::1.
query := endpoint.Query()
query.Set("myip6", ipv6)
endpoint.RawQuery = query.Encode()
}
} else { } else {
logger.Println("Couldn't retrieve IPv6 address") logger.Println("Couldn't retrieve IPv6 address")
logger.Printf("%s", err) logger.Printf("%s", err)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment