mirror of
https://github.com/luxfi/net.git
synced 2026-10-30 15:24:11 +00:00
Modules declare go 1.27.1 and the build images follow. go fix applied the modernizers; omitzero is not applied, since it changes what the JSON says. Co-authored-by: Hanzo Dev <[email protected]>
100 lines
2.3 KiB
Go
100 lines
2.3 KiB
Go
// Copyright (C) 2025, Lux Industries, Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package netutil
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"net/netip"
|
|
"net/url"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
// GetUserIPAddress retrieves the IP address of the user.
|
|
func GetUserIPAddress() (string, error) {
|
|
resp, err := http.Get("https://api.ipify.org?format=json")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return "", errors.New("HTTP request failed")
|
|
}
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
var result map[string]any
|
|
if err := json.Unmarshal(body, &result); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
ipAddress, ok := result["ip"].(string)
|
|
if ok {
|
|
if net.ParseIP(ipAddress) == nil {
|
|
return "", errors.New("invalid IP address")
|
|
}
|
|
return ipAddress, nil
|
|
}
|
|
|
|
return "", errors.New("no IP address found")
|
|
}
|
|
|
|
func IsValidIP(ipStr string) bool {
|
|
return net.ParseIP(ipStr) != nil
|
|
}
|
|
|
|
// IsValidURL checks if a URL is valid.
|
|
func IsValidURL(urlString string) bool {
|
|
u, err := url.Parse(urlString)
|
|
if err != nil || u.Scheme == "" || u.Host == "" {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// IsValidIPPort checks if an string IP:port pair is valid.
|
|
func IsValidIPPort(ipPortPair string) bool {
|
|
if _, err := GetIPPort(ipPortPair); err != nil {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// GetIPPort parses netip.IPPort from string that also may include http schema
|
|
func GetIPPort(uri string) (netip.AddrPort, error) {
|
|
uri = strings.TrimPrefix(uri, "https://")
|
|
uri = strings.TrimPrefix(uri, "http://")
|
|
return netip.ParseAddrPort(uri)
|
|
}
|
|
|
|
// SplitLuxgoRPCURI splits the RPC URI into `endpoint` and `chain`.
|
|
// Reverse operation of `fmt.Sprintf("%s/v1/chain/%s", endpoint, chain)`.
|
|
// Returns the `uri` and `chain` as strings, or an error if the request URI is invalid.
|
|
func SplitLuxgoRPCURI(requestURI string) (string, string, error) {
|
|
// Define the regex pattern
|
|
pattern := `^(https?://[^/]+)/v1/chain/([^/]+)/rpc$`
|
|
regex := regexp.MustCompile(pattern)
|
|
|
|
// Match the pattern
|
|
matches := regex.FindStringSubmatch(requestURI)
|
|
if matches == nil || len(matches) != 3 {
|
|
return "", "", fmt.Errorf("invalid request URI format")
|
|
}
|
|
|
|
// Extract `endpoint` and `chain`
|
|
endpoint := matches[1]
|
|
chain := matches[2]
|
|
|
|
return endpoint, chain, nil
|
|
}
|