admin
2025-02-20 f537abe9f3646c739beaf15076246a2f71a347e9
src/main/java/com/yeshi/buwan/util/IPUtil.java
@@ -1,85 +1,20 @@
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 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;
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和代理端口正确,此项不设置也可以
        System.getProperties().setProperty("http.proxyHost", "213.85.92.10");
        System.getProperties().setProperty("http.proxyPort", "80");
        LogUtil.i(getHtml("http://www.ip138.com/ip2city.asp")); // 判断代理是否设置成功
    }
    private static String getHtml(String address) {
        StringBuffer html = new StringBuffer();
        String result = null;
        try {
            URL url = new URL(address);
            URLConnection conn = url.openConnection();
            conn.setRequestProperty("User-Agent",
                    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; GTB5; .NET CLR 2.0.50727; CIBA)");
            BufferedInputStream in = new BufferedInputStream(conn.getInputStream());
            try {
                String inputLine;
                byte[] buf = new byte[4096];
                int bytesRead = 0;
                while (bytesRead >= 0) {
                    inputLine = new String(buf, 0, bytesRead, "ISO-8859-1");
                    html.append(inputLine);
                    bytesRead = in.read(buf);
                    inputLine = null;
                }
                buf = null;
            } finally {
                in.close();
                conn = null;
                url = null;
            }
            result = new String(html.toString().trim().getBytes("ISO-8859-1"), "gb2312").toLowerCase();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        html = null;
        return result;
    }
    private static void sendNoticeEmail(String get) {
        if (get != null && get.contains("订单剩余数量不足")) {
            /*
             * MailSenderUtil.sendEmail("1101184511@qq.com", "系统提示:IP订单剩余数量不足",
             * "IP订单剩余数量不足,请登录到代理666充值");
             */
        }
    }
    public static String getRemotIP(HttpServletRequest request) {
@@ -114,10 +49,25 @@
    }
    public static IPInfo getLocalIPInfo(String ip) throws Exception {
        String info = searcher.search(ip);
        String[] infos = info.split("\\|");
        IPInfo ipInfo = new IPInfo(infos[0], infos[2], infos[3]);
        return ipInfo;
        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"));
        }
    }
    public static class IPInfo {
@@ -158,7 +108,7 @@
    public static void main(String[] args) {
        try {
            IPInfo info =     getLocalIPInfo("113.250.254.8");
            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));