36 lines
616 B
Go
36 lines
616 B
Go
package utils
|
|
|
|
import (
|
|
"bytes"
|
|
"io/ioutil"
|
|
"net/http"
|
|
)
|
|
|
|
// dig @resolver1.opendns.com ANY myip.opendns.com +short -4
|
|
|
|
func GetIP() (string, error) {
|
|
rsp, err := http.Get("http://checkip.amazonaws.com")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer rsp.Body.Close()
|
|
|
|
buf, err := ioutil.ReadAll(rsp.Body)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return string(bytes.TrimSpace(buf)), nil
|
|
}
|
|
|
|
func ReadUserIP(r *http.Request) string {
|
|
IPAddress := r.Header.Get("X-Real-Ip")
|
|
if IPAddress == "" {
|
|
IPAddress = r.Header.Get("X-Forwarded-For")
|
|
}
|
|
if IPAddress == "" {
|
|
IPAddress = r.RemoteAddr
|
|
}
|
|
return IPAddress
|
|
}
|