https://github.com/polaris1119/goutils/blob/master/ip.go

package goutils

import (
    "encoding/binary"
    "net"
    "net/http"
)

const (
    XForwardedFor = "X-Forwarded-For"
    XRealIP       = "X-Real-IP"
)

// RemoteIp 返回远程客户端的 IP,如 192.168.1.1
func RemoteIp(req *http.Request) string {
    remoteAddr := req.RemoteAddr
    if ip := req.Header.Get(XRealIP); ip != "" {
        remoteAddr = ip
    } else if ip = req.Header.Get(XForwardedFor); ip != "" {
        remoteAddr = ip
    } else {
        remoteAddr, _, _ = net.SplitHostPort(remoteAddr)
    }

    if remoteAddr == "::1" {
        remoteAddr = "127.0.0.1"
    }

    return remoteAddr
}

// Ip2long 将 IPv4 字符串形式转为 uint32
func Ip2long(ipstr string) uint32 {
    ip := net.ParseIP(ipstr)
    if ip == nil {
        return 0
    }
    ip = ip.To4()
    return binary.BigEndian.Uint32(ip)
}
文档更新时间: 2019-06-07 10:32   作者:月影鹏鹏