package utils

import (
	"fmt"
	"net"
	"net/url"
)

// ExtractIP 把 "http://1.2.3.4:8000" → "1.2.3.4"
func ExtractIP(rawURL string) (string, error) {
	u, err := url.Parse(rawURL)
	if err != nil {
		return "", err
	}
	host := u.Host
	if h, _, errSplit := net.SplitHostPort(host); errSplit == nil {
		host = h
	}
	ip := net.ParseIP(host)
	if ip == nil {
		return "", fmt.Errorf("invalid IP in url: %s", rawURL)
	}
	return ip.String(), nil
}
