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
| package com.xxx.ippermit;
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.InetAddress; import java.net.UnknownHostException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Map.Entry; import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.lang.StringUtils;
import com.xxx.utils.FileHelper; import com.xxx.utils.IPUtils;
public class TempBlackIpFileHelper { private static TempBlackIpFileHelper inst = new TempBlackIpFileHelper(FileHelper.combinPath(Configuration.getStorePath(), "ippermit/tempblackip.txt"));
public static TempBlackIpFileHelper getInst() { return inst; }
private ConcurrentHashMap<Long, Long> ipMap = new ConcurrentHashMap<Long, Long>(); private String fileName = ""; private Long codeFileDate = 0L; private File file = null; private Long lastCheckTime = 0L; private Long lastWriteTime = 0L; private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private TempBlackIpFileHelper(String sFileName) { fileName = sFileName; file = new File(fileName); }
private synchronized void checkReload() { Long curTime = (new Date()).getTime();
if (lastWriteTime == 0) { lastWriteTime = curTime; }
if ((curTime - lastWriteTime) > 600000) { synchronized (inst) { lastWriteTime = curTime;
file.delete(); FileWriter fWriter = null; PrintWriter pWriter = null; try { fWriter = new FileWriter(file, true); pWriter = new PrintWriter(fWriter); for (Entry<Long, Long> e : ipMap.entrySet()) { if (e.getValue() > curTime) { pWriter.println(IPUtils.longToIp(e.getKey()) + "|" + formatter.format(new Date(e.getValue()))); } } } catch (IOException e) { e.printStackTrace(); } finally { try { if (pWriter != null) { pWriter.close(); }
if (fWriter != null) { fWriter.close(); } } catch (IOException e) { } } codeFileDate = file.lastModified(); } }
if ((curTime - lastCheckTime) > 10000) { lastCheckTime = curTime;
if (file.exists()) { if (file.lastModified() != codeFileDate) { ipMap = loadMap(null); } } }
}
private ConcurrentHashMap<Long, Long> loadMap(ConcurrentHashMap<Long, Long> map) { if (map == null) { map = new ConcurrentHashMap<Long, Long>(); } BufferedReader reader = null; try { FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); reader = new BufferedReader(isr); String tempString = null; while ((tempString = reader.readLine()) != null) { if (StringUtils.isNotEmpty(tempString)) { String[] arr = tempString.split("\\|"); try { map.put(IPUtils.ipToLong(arr[0]), formatter.parse(arr[1].trim()).getTime()); } catch (Exception e) { System.out.println("动态ip黑名单加载失败"); } } } reader.close(); codeFileDate = file.lastModified(); return map; } catch (IOException e) { System.out.println("动态ip黑名单加载失败"); return map; } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } }
public synchronized void append(String data) { FileWriter fWriter = null; PrintWriter pWriter = null; try { fWriter = new FileWriter(file, true); pWriter = new PrintWriter(fWriter); pWriter.println(data); } catch (IOException e) { e.printStackTrace(); } finally { try { if (pWriter != null) { pWriter.close(); }
if (fWriter != null) { fWriter.close(); } } catch (IOException e) { } } }
public boolean checkIp(Long lIp) { checkReload(); if (ipMap.containsKey(lIp)) { return ipMap.get(lIp) > new Date().getTime(); } else { return false; } }
public boolean checkBaiduSpider(Long lIp) { boolean flag = false; InetAddress inetAddress; try { String ip = IPUtils.longToIp(lIp); inetAddress = InetAddress.getByName(ip); if (inetAddress != null) { String hostName = inetAddress.getHostName(); if (StringUtils.isNotEmpty(hostName)) { if (hostName.contains("baidu.com") || hostName.contains("baidu.jp")) { flag = true; System.out.println(ip + "|" + inetAddress.getHostName() + "|" + inetAddress.getHostAddress() + "|" + inetAddress.getAddress()); } } } } catch (Exception e) { return false; } return flag; }
public void addIp(Long lIp, Long time) { if (checkBaiduSpider(lIp) == false) { synchronized (inst) { ipMap.put(lIp, time); append(IPUtils.longToIp(lIp) + "|" + formatter.format(new Date(time))); } } } }
|