20 lines
294 B
Go
20 lines
294 B
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
)
|
|
|
|
func GetIP(r *http.Request) (string, error) {
|
|
ip, _, err := net.SplitHostPort(r.RemoteAddr)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
netIP := net.ParseIP(ip)
|
|
if netIP != nil {
|
|
return ip, nil
|
|
}
|
|
return "", fmt.Errorf("no valid ip found")
|
|
}
|