|
| 1 | +package io.ipfs.nbs.helper; |
| 2 | + |
| 3 | +import com.alibaba.fastjson.JSON; |
| 4 | +import io.nbs.client.Launcher; |
| 5 | +import io.nbs.client.cnsts.AppGlobalCnst; |
| 6 | +import io.nbs.commons.helper.ConfigurationHelper; |
| 7 | +import io.nbs.commons.utils.HttpUtils; |
| 8 | +import io.nbs.sdk.beans.TaobaoResult; |
| 9 | +import okhttp3.Response; |
| 10 | +import org.jsoup.Jsoup; |
| 11 | +import org.jsoup.nodes.Document; |
| 12 | +import org.jsoup.nodes.Element; |
| 13 | +import org.jsoup.select.Elements; |
| 14 | +import org.slf4j.Logger; |
| 15 | +import org.slf4j.LoggerFactory; |
| 16 | + |
| 17 | +import java.io.File; |
| 18 | +import java.io.IOException; |
| 19 | +import java.util.regex.Pattern; |
| 20 | + |
| 21 | +/** |
| 22 | + * @Package : io.ipfs.nbs.helper |
| 23 | + * @Description : <p></p> |
| 24 | + * @Author : lambor.c |
| 25 | + * @Date : 2018/7/9-16:05 |
| 26 | + * Copyright (c) 2018, NBS , lambor.c<[email protected]>. |
| 27 | + * All rights reserved. |
| 28 | + */ |
| 29 | +public class IPAddressHelper { |
| 30 | + private static final Logger logger = LoggerFactory.getLogger(IPAddressHelper.class); |
| 31 | + |
| 32 | + private static String IP = null; |
| 33 | + private static IPAddressHelper helper = null; |
| 34 | + private static String HTML_TEMP; |
| 35 | + public static final Pattern IPV4_PATTERN = Pattern.compile("^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$"); |
| 36 | + public static final Pattern IPV6_SID_REGEX = Pattern.compile("^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$"); |
| 37 | + public static final Pattern IPV6_COMPRESSED_REGEX = Pattern.compile("^^((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)$"); |
| 38 | + private static String CHECKED_URL; |
| 39 | + private static String CHECKED_LOCATION_URL; |
| 40 | + |
| 41 | + |
| 42 | + private IPAddressHelper() { |
| 43 | + HTML_TEMP = AppGlobalCnst.consturactPath(Launcher.CURRENT_DIR,"TEMP","html"); |
| 44 | + File dirFile = new File(HTML_TEMP); |
| 45 | + if(dirFile.exists())dirFile.delete(); |
| 46 | + dirFile.mkdirs(); |
| 47 | + CHECKED_URL = ConfigurationHelper.getInstance() |
| 48 | + .getCfgProps().getProperty("nbs.server.ip.checked-url","http://www.net.cn/static/customercare/yourip.asp"); |
| 49 | + CHECKED_LOCATION_URL = ConfigurationHelper.getInstance() |
| 50 | + .getCfgProps().getProperty("nbs.server.ip.checked-locations-url","http://ip.taobao.com/service/getIpInfo.php?ip="); |
| 51 | + } |
| 52 | + |
| 53 | + public static IPAddressHelper getInstance(){ |
| 54 | + if(helper==null)helper= new IPAddressHelper(); |
| 55 | + return helper; |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + public String getRealIP(){ |
| 60 | + logger.info(CHECKED_URL); |
| 61 | + Document document = null; |
| 62 | + try { |
| 63 | + document = Jsoup.connect(CHECKED_URL).get(); |
| 64 | + Elements h2s = document.getElementsByTag("h2"); |
| 65 | + for(Element h2:h2s){ |
| 66 | + String content = h2.text(); |
| 67 | + logger.info(content); |
| 68 | + if(isIP(content)){ |
| 69 | + return content; |
| 70 | + } |
| 71 | + } |
| 72 | + } catch (IOException e) { |
| 73 | + e.printStackTrace(); |
| 74 | + } |
| 75 | + return ""; |
| 76 | + } |
| 77 | + |
| 78 | + public String getLocations(String ip){ |
| 79 | + String url = CHECKED_LOCATION_URL + ip; |
| 80 | + String res = ""; |
| 81 | + Response response = null; |
| 82 | + |
| 83 | + try { |
| 84 | + response = HttpUtils.get(url); |
| 85 | + String tempResp = response.body().string(); |
| 86 | + logger.info(tempResp); |
| 87 | + TaobaoResult result = JSON.parseObject(tempResp,TaobaoResult.class); |
| 88 | + if(result!=null&&result.getData()!=null){ |
| 89 | + return result.getData().getLocations(); |
| 90 | + } |
| 91 | + } catch (IOException e) { |
| 92 | + logger.error(e.getMessage()); |
| 93 | + } |
| 94 | + |
| 95 | + return res; |
| 96 | + } |
| 97 | + |
| 98 | +/* public static void main(String[] args){ |
| 99 | + String ip="169.254.204.10"; |
| 100 | + boolean b = IPAddressHelper.getInstance().isIP(ip); |
| 101 | + System.out.println(">>>"+b); |
| 102 | + }*/ |
| 103 | + |
| 104 | + public boolean isIPV4(final String inStr){ |
| 105 | + return IPV4_PATTERN.matcher(inStr).matches(); |
| 106 | + } |
| 107 | + |
| 108 | + public boolean isIPV6(final String inStr){ |
| 109 | + return IPV6_COMPRESSED_REGEX.matcher(inStr).matches()||IPV6_SID_REGEX.matcher(inStr).matches(); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * |
| 114 | + * @param inAddress |
| 115 | + * @return |
| 116 | + */ |
| 117 | + public boolean isIP(final String inAddress){ |
| 118 | + return isIPV4(inAddress)||isIPV6(inAddress); |
| 119 | + } |
| 120 | +} |
0 commit comments