获取ip地址
(ctx *context.Context, field string)
| 958 | |
| 959 | // 获取ip地址 |
| 960 | func GetIP(ctx *context.Context, field string) (ip string) { |
| 961 | ip = ctx.Request.Header.Get(field) |
| 962 | if ip != "" { |
| 963 | return |
| 964 | } |
| 965 | ip = ctx.Request.Header.Get("X-Real-Ip") |
| 966 | if ip != "" { |
| 967 | return |
| 968 | } |
| 969 | ip = ctx.Request.Header.Get("X-Forwarded-For") |
| 970 | if ip != "" { |
| 971 | return |
| 972 | } |
| 973 | ip = ctx.Request.Header.Get("Remote-Addr") |
| 974 | if ip != "" { |
| 975 | return |
| 976 | } |
| 977 | |
| 978 | slice := strings.Split(ctx.Request.RemoteAddr, ":") |
| 979 | if len(slice) == 2 && !strings.Contains(ctx.Request.RemoteAddr, ",") { |
| 980 | return slice[0] |
| 981 | } |
| 982 | return |
| 983 | } |
| 984 | |
| 985 | func IsMobile(userAgent string) bool { |
| 986 | return user_agent.New(userAgent).Mobile() |