1 20 21 package freecs.core; 22 23 import java.io.IOException ; 24 import java.net.InetAddress ; 25 import java.net.InetSocketAddress ; 26 import java.net.NetworkInterface ; 27 import java.net.ServerSocket ; 28 import java.nio.channels.CancelledKeyException ; 29 import java.nio.channels.SelectionKey ; 30 import java.nio.channels.Selector ; 31 import java.nio.channels.ServerSocketChannel ; 32 import java.nio.channels.SocketChannel ; 33 import java.nio.channels.spi.SelectorProvider ; 34 import java.util.Enumeration ; 35 import java.util.HashMap ; 36 import java.util.Iterator ; 37 import java.util.Vector ; 38 39 import freecs.Server; 40 import freecs.util.TrafficMonitor; 41 import freecs.util.TrafficMonitor.AddressState; 42 43 48 public class Listener extends Thread { 49 private HashMap ia2ssc = null; 50 private static final Listener l = new Listener(); 51 private Selector sel; 52 54 public Listener() { 55 try { 56 sel = SelectorProvider.provider().openSelector(); 57 } catch (Exception e) { 58 Server.debug (this, "Unable to start Listener!", e, Server.MSG_ERROR, Server.LVL_HALT); 59 } 60 } 61 62 public static void updateSscRecieveBuffer(int rbw) { 63 if (l.ia2ssc==null) 64 return; 65 for (Iterator i = l.ia2ssc.values().iterator(); i.hasNext(); ) { 66 try { 67 ServerSocketChannel ssc = (ServerSocketChannel ) i.next(); 68 if (ssc.socket().getReceiveBufferSize() == rbw) 69 continue; 70 ssc.socket().setReceiveBufferSize(rbw); 71 } catch (IOException ioe) { 72 Server.debug("static Listener", "updateSscRecieveBuffer: exception during updating recievebuffer-window",ioe, Server.MSG_ERROR, Server.LVL_MAJOR); 73 } 74 } 75 } 76 77 public static void startListener() throws IOException { 78 if (l.ia2ssc==null) 79 l.initSSC (); 80 if (!l.isAlive()) { 81 l.setName("Listener"); 82 l.start(); 84 } 85 } 86 87 private void bindSSC (InetAddress ia) throws IOException { 88 if (ia2ssc.get(ia)!=null) 89 return; 90 ServerSocketChannel ssc = ServerSocketChannel.open(); 91 ServerSocket ssoc = ssc.socket(); 92 ssoc.setReceiveBufferSize(Server.srv.TCP_RECEIVE_BUFFER_WINDOW); 93 ssoc.setReuseAddress(true); 94 ssc.configureBlocking(false); 95 ssc.socket().bind(new InetSocketAddress (ia, Integer.parseInt(Server.srv.getProperty("port")))); 96 if (Server.srv.allowedLoginHosts == null) 97 Server.srv.allowedLoginHosts = new Vector (); 98 Server.srv.allowedLoginHosts.addElement (ia); 99 ssc.register(sel, SelectionKey.OP_ACCEPT); 100 ia2ssc.put(ia, ssc); 101 } 102 103 private void reinitSSC () throws IOException { 104 if (ia2ssc != null) { 105 for (Iterator i = ia2ssc.values().iterator(); i.hasNext(); ) { 106 ServerSocketChannel ssc = (ServerSocketChannel ) i.next(); 107 ssc.close(); 108 } 109 } 110 initSSC(); 111 } 112 113 private void initSSC () { 114 if (ia2ssc == null) 115 ia2ssc = new HashMap (); 116 try { 117 if (Server.srv.getProperty("bindIp")!=null) { 118 String addresses[] = Server.srv.getProperty("bindIp").split(","); 119 for (int i = 0; i < addresses.length; i++) { 120 bindSSC(InetAddress.getByName(addresses[i])); 121 } 122 } else { 123 for (Enumeration e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements(); ) { 124 NetworkInterface ni = (NetworkInterface ) e.nextElement(); 125 for (Enumeration ee = ni.getInetAddresses(); ee.hasMoreElements(); ) 126 bindSSC((InetAddress ) ee.nextElement()); 127 } 128 } 129 } catch (IOException ioe) { 130 Server.debug(this, "initSSC: exception during obtaining ip-adresses", ioe, Server.MSG_ERROR, Server.LVL_HALT); 131 } 132 } 133 134 public void run () { 135 long lastMessage = 0; 136 while (Server.srv.isRunning()) { 137 try { 138 if (Server.DEBUG || lastMessage + 5000 > System.currentTimeMillis()) { 139 Server.log ("[Listener]", "loopstart", Server.MSG_STATE, Server.LVL_VERY_VERBOSE); 140 lastMessage = System.currentTimeMillis(); 141 } 142 while (sel.selectNow() < 1) try { 143 Thread.sleep(100); 144 } catch (InterruptedException ie) { } 145 } catch (IOException ioe) { 146 Server.debug (this, "run: ", ioe, Server.MSG_ERROR, Server.LVL_MAJOR); 147 try { 148 reinitSSC(); 149 } catch (IOException iioe) { 150 Server.debug (this, "MAJOR ERROR ON REOPENING LISTENER!", iioe, Server.MSG_ERROR, Server.LVL_MAJOR); 151 break; 152 } 153 } catch (Exception e) { 154 Server.debug (this, "run: ", e, Server.MSG_ERROR, Server.LVL_MAJOR); 155 } 156 for (Iterator i = sel.selectedKeys().iterator(); i.hasNext(); ) try { 157 SelectionKey ck = (SelectionKey ) i.next(); 158 i.remove(); 159 if (ck.isAcceptable()) { 160 accept(ck); 161 } else { 162 Server.log (this, ".run: SelectionKey doesn't have Accept in its interestOps! " + ck.toString(), Server.MSG_STATE, Server.LVL_MAJOR); 163 } 164 } catch (CancelledKeyException cke) { } 165 try { 166 Thread.sleep(33); 167 } catch (InterruptedException ie) {} 168 } 169 for (Iterator i = sel.keys().iterator(); i.hasNext(); ) { 170 SelectionKey ck = (SelectionKey ) i.next(); 171 try { 172 ck.channel().close(); 173 ck.cancel(); 174 i.remove(); 175 } catch (Exception e) { 176 Server.debug (this, "final cleanup: ", e, Server.MSG_ERROR, Server.LVL_MAJOR); 177 } 178 } 179 } 180 181 private void accept (SelectionKey sk) { 182 if (sk == null) return; 183 ServerSocketChannel ssc = (ServerSocketChannel ) sk.channel(); 184 SocketChannel sc; 185 try { 186 sc = ssc.accept(); 187 if (sc == null) 188 return; 189 InetAddress ia = sc.socket ().getInetAddress (); 190 if (Server.srv.USE_TRAFFIC_MONITOR) { 194 if (Server.srv.isBanned (ia) && !Server.srv.isAdminHost(ia)){ 195 sc.close(); 196 return; 197 } 198 199 AddressState as = TrafficMonitor.tm.mayPass (ia); 200 if (as!=null) { 201 sc.close(); 202 StringBuffer tsb = new StringBuffer ("TrafficMonitor is refusing connection to banned host: "); 203 tsb.append (ia.getHostAddress()); 204 tsb.append ("("); 205 tsb.append (ia.getHostName()); 206 tsb.append (")"); 207 Server.log (this, tsb.toString (), Server.MSG_TRAFFIC, Server.LVL_MAJOR); 208 tsb = new StringBuffer ("reached "); 209 tsb.append (as.reqCount); 210 tsb.append (" connects within "); 211 tsb.append (as.diff); 212 tsb.append (" millis"); 213 Server.srv.banHost (ia, System.currentTimeMillis() + Server.srv.HOST_BAN_DURATION, tsb.toString()); 214 return; 215 } 216 } 217 CentralSelector.cSel.registerSC (sc, Server.REQUEST_TYPE_HTTP); 218 } catch (Exception e) { 219 Server.debug (this, "accept: Exception encountered during accept: ", e, Server.MSG_ERROR, Server.LVL_MAJOR); 220 } 221 } 222 223 public String toString() { return "[Listener]"; } 224 } 225 | Popular Tags |