package com.dsk.common.utils.ip;

import cn.hutool.core.net.NetUtil;
import cn.hutool.http.HtmlUtil;
import com.dsk.common.utils.StringUtils;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 获取地址类
 *
 * @author Lion Li
 */
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class AddressUtils {

    // 未知地址
    public static final String UNKNOWN = "XX XX";

    public static String getRealAddressByIP(String ip) {
        try {
            if (StringUtils.isBlank(ip)) {
                return UNKNOWN;
            }
            // 内网不查询
            ip = "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : HtmlUtil.cleanHtmlTag(ip);
            if (NetUtil.isInnerIP(ip)) {
                return "内网IP";
            }
            return RegionUtils.getCityInfo(ip);
        } catch (Exception e) {
            log.error("IP地址异常 "+ip);
            e.printStackTrace();
        }
        return null;
    }
}
