KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > util > TrafficMonitor


1 /**
2  * Copyright (C) 2003 Manfred Andres
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */

18 package freecs.util;
19
20 import freecs.Server;
21
22 import java.util.Hashtable JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.net.InetAddress JavaDoc;
25
26
27 public class TrafficMonitor extends Thread JavaDoc {
28     public static final TrafficMonitor tm = new TrafficMonitor();
29
30     private Hashtable JavaDoc addr;
31     private long checkInterval = 10000;
32
33     private TrafficMonitor () {
34         addr = new Hashtable JavaDoc ();
35     }
36
37     /**
38      * Starts up the TrafficMonitor
39      */

40     public static void startTrafficMonitor () {
41         if (tm.isAlive())
42             return;
43         tm.setName("TrafficMonitor");
44         tm.start ();
45     }
46
47     /**
48      * mayPass get's called for every new connection-atempt. If the configured
49      * maximum number of connection-atempts per host is reached, this method will
50      * return false. The Thread calling this method is responsible for baning this
51      * host. There is the possibility to destinguish between normal hosts and proxy-servers.
52      * Proxy-Servers will be allowed to connect more often than normal hosts.
53      * @param ia the inet-address of the host to count the connection-atempts
54      * @return true if this inet-adresses Host may pass, false if too many connection-atempts from this host where made
55      * @see freecs.Server
56      */

57     public AddressState mayPass (InetAddress JavaDoc ia) {
58         AddressState as = (AddressState) addr.get(ia);
59         if (as == null) {
60             addr.put (ia, new AddressState ());
61             return null;
62         }
63         as.diff = System.currentTimeMillis () - as.lastCheck;
64         if (as.diff > checkInterval) {
65             as.reqCount = 1;
66             as.lastCheck = System.currentTimeMillis ();
67             return null;
68         }
69         as.reqCount++;
70         if ((as.isProxy
71             && as.reqCount > Server.srv.MAX_REQUESTS_PER_PROXY_IP)
72                 || as.reqCount > Server.srv.MAX_REQUESTS_PER_IP) {
73             return as;
74         }
75         return null;
76     }
77
78     /**
79      * markAsProxy will mark this inet-adresses Host as proxy-server. This makes it
80      * possible to distiguish between normal- and proxy-hosts. Proxy-hosts well be
81      * allowed to make more connection-atempts than normal hosts.
82      * @param ia the inet-address which was identified as proxy-server
83      */

84     public void markAsProxy (InetAddress JavaDoc ia) {
85         if (!Server.srv.USE_TRAFFIC_MONITOR)
86             return;
87         AddressState as;
88         as = (AddressState) addr.get(ia);
89         if (as == null) {
90             Server.log (this, "markAsProxy: AddressState is null", Server.MSG_STATE, Server.LVL_MAJOR);
91             return;
92         }
93         if (!as.isProxy) {
94             StringBuffer JavaDoc sb = new StringBuffer JavaDoc ("TrafficMonitor.markAsProxy: identified a proxy (");
95             sb.append (ia.toString ());
96             sb.append (")");
97             as.isProxy = true;
98         }
99     }
100
101     /**
102      * the run-method of this TrafficMonitor is responsible for cleaning up inet-addresses
103      * which didn't connect to this Server for mor than 60000 millis.
104      */

105     public void run () {
106         long lastMessage=0;
107         while (Server.srv.isRunning ()) try {
108             if (Server.DEBUG || lastMessage + 5000 > System.currentTimeMillis()) {
109                 Server.log (this, "loopstart", Server.MSG_STATE, Server.LVL_VERY_VERBOSE);
110                 lastMessage = System.currentTimeMillis();
111             }
112             long now = System.currentTimeMillis ();
113             long lowestValue = 60000;
114             for (Enumeration JavaDoc e = addr.keys (); e.hasMoreElements (); ) {
115                 InetAddress JavaDoc tia = (InetAddress JavaDoc) e.nextElement ();
116                 if (tia==null)
117                     continue;
118                 AddressState as = (AddressState) addr.get (tia);
119                 if (as==null)
120                     continue;
121                 long diff = now - as.lastCheck;
122                 if (diff > 60000) {
123                     addr.remove (tia);
124                 } else if (diff < lowestValue) {
125                     lowestValue=diff;
126                 }
127             }
128             if (lowestValue < 33)
129                 lowestValue = 33;
130             try {
131                 Thread.sleep (lowestValue);
132             } catch (InterruptedException JavaDoc ie) {}
133         } catch (Exception JavaDoc e) {
134             Server.debug (this, "run:", e, Server.MSG_ERROR, Server.LVL_MAJOR);
135         }
136     }
137
138     public class AddressState {
139         volatile long lastCheck;
140         public volatile long reqCount;
141         public volatile long diff;
142         volatile boolean isProxy = false;
143         private AddressState () {
144             lastCheck = System.currentTimeMillis ();
145             reqCount = 1;
146         }
147     }
148     
149     public String JavaDoc toString() {
150         return "[TrafficMonitor]";
151     }
152 }
Popular Tags