package com.chaozhanggui.common.system.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

/**
 * @Des     ： ip工具
 * 1.获取ip
 * 2.获取所在地址
 *
 * @Author  ： Lgo
 * @Date    ： 2021/1/15  12:04
 **/
public class IPUtil {

    /**
     * 获取登录用户的IP地址
     *
     * @param request
     * @return
     */
    public static String getIpAddress( HttpServletRequest request ) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if ("0:0:0:0:0:0:0:1".equals(ip)) {
            ip = "127.0.0.1";
        }
        if (ip.split(",").length > 1) {
            ip = ip.split(",")[0];
        }
        return ip;
    }






    /**
     * 通过IP获取地址(需要联网，调用淘宝的IP库)
     *
     * @param ip
     * @return
     */
    public static String getIpInfo(String ip) {
        if ("127.0.0.1".equals(ip)) {
            ip = "127.0.0.1";
        }
        String info = "";
        try {
            URL url = new URL("http://ip.taobao.com/service/getIpInfo.php?ip=" + ip);
            HttpURLConnection htpcon = (HttpURLConnection) url.openConnection();
            htpcon.setRequestMethod("GET");
            htpcon.setDoOutput(true);
            htpcon.setDoInput(true);
            htpcon.setUseCaches(false);

            InputStream in = htpcon.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
            StringBuffer temp = new StringBuffer();
            String line = bufferedReader.readLine();
            while (line != null) {
                temp.append(line).append("\r\n");
                line = bufferedReader.readLine();
            }
            bufferedReader.close();
            JSONObject obj = (JSONObject) JSON.parse(temp.toString());
            if (obj.getIntValue("code") == 0) {
                JSONObject data = obj.getJSONObject("data");
                info += data.getString("country") + " ";
                info += data.getString("region") + " ";
                info += data.getString("city") + " ";
                info += data.getString("isp");
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return info;
    }


    public static void main(String[] args) {
        String t="ONLINE:USER:eyJhbGciOiJIUzI1NiJ9.eyJwaG9uZSI6IjE3NzI5Mzg1MTkzIiwibmlja05hbWUiOiIzM-W3heWzsOi1myIsImxvZ28iOiJodHRwczovL2dpbWcyLmJhaWR1LmNvbS9pbWFnZV9zZWFyY2gvc3JjPWh0dHAlM0ElMkYlMkZ3eDQuc2luYWltZy5jbiUyRmxhcmdlJTJGMDA2QVJFOXZneTFnMGl0Zmpxc214ajMwOGIwN3U3NGMuanBnJnJlZmVyPWh0dHAlM0ElMkYlMkZ3eDQuc2luYWltZy5jbiZhcHA9MjAwMiZzaXplPWY5OTk5LDEwMDAwJnE9YTgwJm49MCZnPTBuJmZtdD1hdXRvP3NlYz0xNjU1Njk2MDY3JnQ9YzMwZmNmNTliZTQ4MDRhYjNmMTdlNzc2YWU4MzJmYzgiLCJleHAiOjE2Njg5MTkwNTksInVzZXJJZCI6NCwiaWF0IjoxNjY3NDQ3ODMwLCJ1c2VyQ29kZSI6ImNzX25ianl0amxqIn0.zBfOCbmGAcu3Yd0xeKFyiYSlYj_5ZYHeDmnhloBnsxs";




        System.out.println();
    }









}
