admin
2022-04-16 04f09e52ffd4681bdfd85e51acd3da0d1280c3d3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package com.yeshi.buwan.util;
 
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 javax.servlet.http.HttpServletRequest;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
 
public class IPUtil {
    // 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;
    }
 
    // 没有找到代理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("订单剩余数量不足")) {
            /*
             * MailSenderUtil.sendEmail("1101184511@qq.com", "系统提示:IP订单剩余数量不足",
             * "IP订单剩余数量不足,请登录到代理666充值");
             */
        }
    }
 
    public static String getRemotIP(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.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
 
 
    /**
     * 获取远程端口
     *
     * @param request
     * @return
     */
    public static int getRemotePort(HttpServletRequest request) {
        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) {
 
        }
        return null;
    }
 
 
    public static class IPInfo {
        private String province;
        private String city;
        private String country;
 
        public IPInfo(String country, String province, String city) {
            this.province = province;
            this.city = city;
            this.country = country;
        }
 
        public String getProvince() {
            return province;
        }
 
        public void setProvince(String province) {
            this.province = province;
        }
 
        public String getCity() {
            return city;
        }
 
        public void setCity(String city) {
            this.city = city;
        }
 
        public String getCountry() {
            return country;
        }
 
        public void setCountry(String country) {
            this.country = country;
        }
    }
 
}