View Javadoc

1   /***
2    * Jetrix TetriNET Server
3    * Copyright (C) 2001-2003  Emmanuel Bourg
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   */
19  
20  package net.jetrix;
21  
22  import java.util.*;
23  import java.util.regex.*;
24  import java.net.*;
25  
26  /***
27   * List of hosts banned from the server.
28   *
29   * @author Emmanuel Bourg
30   * @version $Revision: 794 $, $Date: 2009-02-17 20:08:39 +0100 (Tue, 17 Feb 2009) $
31   */
32  public class Banlist
33  {
34      private static Banlist instance = new Banlist();
35      private Map<String, Entry> banlist;
36  
37      private Banlist()
38      {
39          banlist = new HashMap<String, Entry>();
40      }
41  
42      public static Banlist getInstance()
43      {
44          return instance;
45      }
46  
47      /***
48       * Ban the specified host forever.
49       */
50      public void ban(String host)
51      {
52          ban(host, null);
53      }
54  
55      /***
56       * Ban the specified host till the expiration date.
57       */
58      public void ban(String host, Date expiration)
59      {
60          banlist.put(host, new Entry(host, expiration));
61      }
62  
63      /***
64       * Unban the specified host.
65       */
66      public void unban(String host)
67      {
68          banlist.remove(host);
69      }
70  
71      /***
72       * Check if the specified host is currently banned.
73       */
74      public boolean isBanned(String host)
75      {
76          boolean banned = false;
77  
78          Date now = new Date();
79  
80          Iterator<Entry> it = banlist.values().iterator();
81          while (it.hasNext() && !banned)
82          {
83              Entry entry = it.next();
84              banned = entry.matches(host);
85  
86              // test the expiration date
87              if (banned && entry.expiration != null)
88              {
89                  if (now.after(entry.expiration))
90                  {
91                      // the ban has expired
92                      banned = false;
93  
94                      // remove the entry from the banlist
95                      it.remove();
96                  }
97              }
98          }
99  
100         return banned;
101     }
102 
103     public boolean isBanned(InetAddress address)
104     {
105         return isBanned(address.getHostAddress()) || isBanned(address.getHostName());
106     }
107 
108     public Iterator<Entry> getBanlist()
109     {
110         return banlist.values().iterator();
111     }
112 
113     /***
114      * Remove all entries in the banlist.
115      */
116     public void clear()
117     {
118         banlist.clear();
119     }
120 
121     /***
122      * A ban list entry.
123      */
124     public class Entry
125     {
126         public String pattern;
127         public Date expiration;
128         private Pattern regexp;
129 
130         public Entry(String pattern, Date expiration)
131         {
132             this.pattern = pattern;
133             this.expiration = expiration;
134         }
135 
136         public boolean matches(String value)
137         {
138             if (regexp == null)
139             {
140                 // replace . by \., * by .* and ? by .
141                 String s = pattern.replaceAll("//.", "////.").replaceAll("//*", "(.*)").replaceAll("//?", ".");
142                 regexp = Pattern.compile(s);
143             }
144 
145             return regexp.matcher(value).matches();
146         }
147     }
148 
149 }