1 28 29 package com.caucho.server.security; 30 31 import com.caucho.log.Log; 32 import com.caucho.util.InetNetwork; 33 import com.caucho.util.L10N; 34 import com.caucho.util.LongKeyMap; 35 36 import javax.annotation.PostConstruct; 37 import java.net.InetAddress ; 38 import java.util.ArrayList ; 39 import java.util.logging.Level ; 40 import java.util.logging.Logger ; 41 42 45 public class ForbidHost { 46 static final protected Logger log = Log.open(ForbidHost.class); 47 static final L10N L = new L10N(ForbidHost.class); 48 49 private LongKeyMap _forbiddenHosts; 50 private ArrayList _forbiddenNets; 51 52 55 public void addForbidIP(String addrName) 56 { 57 try { 58 InetAddress addr = InetAddress.getByName(addrName); 59 60 if (_forbiddenHosts == null) 61 _forbiddenHosts = new LongKeyMap(); 62 63 _forbiddenHosts.put(inetAddressToLong(addr), "true"); 64 } catch (Exception e) { 65 log.log(Level.FINE, e.toString(), e); 66 } 67 } 68 69 72 public void removeForbidIP(String addrName) 73 { 74 try { 75 InetAddress addr = InetAddress.getByName(addrName); 76 77 if (_forbiddenHosts != null) 78 _forbiddenHosts.remove(inetAddressToLong(addr)); 79 } catch (Exception e) { 80 log.log(Level.FINE, e.toString(), e); 81 } 82 } 83 84 87 public void addForbidNet(String netmask) 88 { 89 try { 90 InetNetwork net = InetNetwork.create(netmask); 91 92 if (net == null) 93 return; 94 95 if (_forbiddenNets == null) 96 _forbiddenNets = new ArrayList (); 97 98 _forbiddenNets.add(net); 99 } catch (Exception e) { 100 log.log(Level.FINE, e.toString(), e); 101 } 102 } 103 104 107 public void removeForbidNet(String netmask) 108 { 109 try { 110 InetNetwork net = InetNetwork.create(netmask); 111 112 if (net == null) 113 return; 114 115 if (_forbiddenNets != null) 116 _forbiddenNets.remove(net); 117 } catch (Exception e) { 118 log.log(Level.FINE, e.toString(), e); 119 } 120 } 121 122 125 @PostConstruct 126 public void init() 127 { 128 } 129 130 133 public boolean isForbidden(long addr) 134 { 135 if (_forbiddenHosts != null) { 136 if (_forbiddenHosts.get(addr) != null) 137 return true; 138 } 139 140 if (_forbiddenNets != null) { 141 for (int i = _forbiddenNets.size(); i >= 0; i--) { 142 InetNetwork net = (InetNetwork) _forbiddenNets.get(i); 143 144 if (net.isMatch(addr)) 145 return true; 146 } 147 } 148 149 return false; 150 } 151 152 155 public boolean isForbidden(InetAddress addr) 156 { 157 if (_forbiddenHosts == null && _forbiddenNets == null) 158 return false; 159 160 long ip = inetAddressToLong(addr); 161 if (_forbiddenHosts != null) { 162 if (_forbiddenHosts.get(ip) != null) 163 return true; 164 } 165 166 if (_forbiddenNets != null) { 167 for (int i = _forbiddenNets.size(); i >= 0; i--) { 168 InetNetwork net = (InetNetwork) _forbiddenNets.get(i); 169 170 if (net.isMatch(ip)) 171 return true; 172 } 173 } 174 175 return false; 176 } 177 178 private static long inetAddressToLong(InetAddress addr) 179 { 180 byte []bytes = addr.getAddress(); 181 182 long address = 0; 183 for (int i = 0; i < bytes.length; i++) 184 address = 256 * address + (bytes[i] & 0xff); 185 186 return address; 187 } 188 } 189 | Popular Tags |