admin
2024-10-30 010ef2a907e66efd4702443c06cdd18f8a7ffa5b
src/main/java/com/yeshi/buwan/util/IPUtil.java
@@ -1,24 +1,40 @@
package com.yeshi.buwan.util;
import com.google.gson.Gson;
import com.show.api.ShowApiRequest;
import net.sf.json.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.lionsoul.ip2region.xdb.Searcher;
import org.yeshi.utils.HttpUtil;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.show.api.ShowApiRequest;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import net.sf.json.JSONObject;
public class IPUtil {
    private static Searcher searcher;
    static {
        try {
            URL url = IPUtil.class.getClassLoader().getResource("ip2region.xdb");
            String path = new File(url.toURI()).getAbsolutePath();
            byte[] vIndex = Searcher.loadVectorIndexFromFile(path);
            searcher = Searcher.newWithVectorIndex(path, vIndex);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // http://verx.daili666.com/ip/?tid=558287898012218&num=1&operator=2
    public static void changeIp() {
        System.getProperties().setProperty("proxySet", "true"); // 如果不设置,只要代理IP和代理端口正确,此项不设置也可以
@@ -61,177 +77,6 @@
        return result;
    }
    // 没有找到代理IP
    // 重庆
    public static String getDaiLiIP(String area) {
        HttpClient client = new HttpClient();
        GetMethod get = null;
        try {
            get = new GetMethod(
                    "http://verx.daili666.com/ip/?tid=559511877530791&num=1&operator=2&filter=on&area="
                            + URLEncoder.encode(area, "UTF-8") + "");
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            client.executeMethod(get);
            String result = get.getResponseBodyAsString();
            sendNoticeEmail(result);
            LogUtil.i(result);
            if (!result.contains("没有找到"))
                return result;
            else
                return null;
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static String getDaiLiIP1(String area) {
        HttpClient client = new HttpClient();
        GetMethod get = null;
        try {
            get = new GetMethod(
                    "http://verx.daili666.com/ip/?tid=559186334744019&num=1&area=" + URLEncoder.encode(area, "UTF-8")
                            + "");
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        try {
            client.executeMethod(get);
            String result = get.getResponseBodyAsString();
            sendNoticeEmail(result);
            LogUtil.i(result);
            if (!result.contains("没有找到"))
                return result;
            else
                return null;
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    // 成都
    public static String getDaiLiIP2(String area) {
        HttpClient client = new HttpClient();
        GetMethod get = null;
        try {
            get = new GetMethod("http://verx.daili666.com/ip/?tid=558287898012218&num=1&filter=on&area="
                    + URLEncoder.encode(area, "UTF-8"));// &area="+
            // URLEncoder.encode(area,
            // "UTF-8") + "
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        try {
            client.executeMethod(get);
            String result = get.getResponseBodyAsString();
            sendNoticeEmail(result);
            LogUtil.i(result);
            if (!result.contains("没有找到"))
                return result;
            else
                return null;
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static String getDaiLiIP3(String area) {
        HttpClient client = new HttpClient();
        GetMethod get = null;
        try {
            get = new GetMethod("http://verx.daili666.com/ip/?tid=557489926231876&num=1&filter=on&area="
                    + URLEncoder.encode(area, "UTF-8") + "");
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            client.executeMethod(get);
            String result = get.getResponseBodyAsString();
            sendNoticeEmail(result);
            LogUtil.i(result);
            if (!result.contains("没有找到"))
                return result;
            else
                return null;
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static String getDaiLiIP4(String area) {
        HttpClient client = new HttpClient();
        GetMethod get = null;
        try {
            get = new GetMethod("http://verx.daili666.com/ip/?tid=556436495975069&num=1&area="
                    + URLEncoder.encode(area, "UTF-8") + "");
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            client.executeMethod(get);
            String result = get.getResponseBodyAsString();
            sendNoticeEmail(result);
            LogUtil.i(result);
            if (!result.contains("没有找到"))
                return result;
            else
                return null;
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static String getDaiLiIP5(String area) {
        HttpClient client = new HttpClient();
        GetMethod get = null;
        try {
            get = new GetMethod("http://verx.daili666.com/ip/?tid=559418280288916&num=1&filter=on&area="
                    + URLEncoder.encode(area, "UTF-8") + "");
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        try {
            client.executeMethod(get);
            String result = get.getResponseBodyAsString();
            LogUtil.i(result);
            sendNoticeEmail(result);
            if (!result.contains("没有找到"))
                return result;
            else
                return null;
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    private static void sendNoticeEmail(String get) {
        if (get != null && get.contains("订单剩余数量不足")) {
            /*
@@ -272,37 +117,27 @@
        return request.getRemotePort();
    }
    public static String getIPContry(String ip) {
        IPInfo ipInfo = getIPInfo(ip);
        return ipInfo == null ? "" : ipInfo.getCountry();
    }
    public static String getIPProvince(String ip) {
        IPInfo ipInfo = getIPInfo(ip);
        return ipInfo == null ? "" : ipInfo.getProvince();
    }
    public static IPInfo getIPInfo(String ip) {
        try {
            String res = new ShowApiRequest("http://route.showapi.com/2152-1", "49053", "983e020ce60042cd95f39b3ebd4b9563")
                    .addTextPara("ip", ip)
                    .post();
            JSONObject json = JSONObject.fromObject(res);
            if (json.optInt("showapi_res_code") == 0) {
                JSONObject body = json.optJSONObject("showapi_res_body");
                if (body != null) {
                    String country = body.optString("country");
                    String state = body.optString("state");
                    String city = body.optString("city");
                    return new IPInfo(country, state, city);
                }
            }
        } catch (Exception e) {
    public static IPInfo getLocalIPInfo(String ip) throws Exception {
        String appcode = "46789780da4f4d92885c3d39b97e3ba9";
        // 通过阿里云的接口获取IP信息
        String url = "https://zjip.market.alicloudapi.com/lifeservice/QueryIpAddr/query?ip=" + URLEncoder.encode(ip, "UTF-8");
        // 网络请求
        HttpClient client = new HttpClient();
        client.getHttpConnectionManager().getParams().setConnectionTimeout(2000);
        GetMethod method = new GetMethod(url);
        method.setRequestHeader("Authorization",  "APPCODE " + appcode);
        client.executeMethod(method);
        String result = method.getResponseBodyAsString();
        System.out.println(result);
        JSONObject data = JSONObject.fromObject(result);
        if (data.optInt("error_code") == 0) {
            data = data.optJSONObject("result");
            IPInfo ipInfo = new IPInfo(data.optString("country"), data.optString("province"), data.optString("city"));
            return ipInfo;
        } else {
            throw new Exception(data.optString("reason"));
        }
        return null;
    }
    public static class IPInfo {
        private String province;
@@ -340,4 +175,16 @@
        }
    }
    public static void main(String[] args) {
        try {
            IPInfo info = getLocalIPInfo("113.250.254.8");
            System.out.println(new Gson().toJson(info));
            info = getLocalIPInfo("193.112.35.168");
            System.out.println(new Gson().toJson(info));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}