some doco updates

This commit is contained in:
Ben Sarmiento
2024-01-25 00:26:31 +01:00
parent a802416d04
commit 0ce5c36276
3 changed files with 73 additions and 42 deletions

65
pkg/utils/ipaddress.go Normal file
View File

@@ -0,0 +1,65 @@
package utils
import (
"io"
"net"
"net/http"
"regexp"
)
// IPVersion specifies the IP version (IPv4 or IPv6).
type IPVersion int
const (
IPv4 IPVersion = 4
IPv6 IPVersion = 6
)
// fetchWebpage fetches the content of a URL and returns it as a string.
// It uses the specified IP version for the HTTP request.
func fetchWebpage(url string, version IPVersion) string {
client := &http.Client{
Transport: &http.Transport{
DialContext: (&net.Dialer{}).DialContext,
Dial: func(network, addr string) (net.Conn, error) {
if version == IPv6 {
addr = "[" + addr + "]"
}
return net.Dial(network, addr)
},
},
}
resp, err := client.Get(url)
if err != nil {
return ""
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return ""
}
return string(body)
}
// extractIPAddress extracts the IP address from the given string.
func extractIPAddress(input string) string {
re := regexp.MustCompile(`(?m)Your IP Address:</strong> ([^<]+)`)
matches := re.FindStringSubmatch(input)
if len(matches) < 1 {
return ""
}
return matches[0]
}
// GetPublicIPAddress returns the public IP address of the machine.
func GetPublicIPAddress() (string, string) {
ipv4 := fetchWebpage("https://real-debrid.com/vpn", IPv4)
ipv4address := extractIPAddress(ipv4)
ipv6 := fetchWebpage("https://real-debrid.com/vpn", IPv6)
ipv6address := extractIPAddress(ipv6)
return ipv4address, ipv6address
}