You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by no...@apache.org on 2007/01/15 15:23:29 UTC

svn commit: r496327 - in /james/server/sandbox/handlerapi-experiment/src: java/org/apache/james/smtpserver/core/filter/fastfail/SpamTrapHandler.java test/org/apache/james/smtpserver/SpamTrapHandlerTest.java

Author: norman
Date: Mon Jan 15 06:23:28 2007
New Revision: 496327

URL: http://svn.apache.org/viewvc?view=rev&rev=496327
Log:
Add SpamTrapHandler

Added:
    james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/filter/fastfail/SpamTrapHandler.java   (with props)
    james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/SpamTrapHandlerTest.java   (with props)

Added: james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/filter/fastfail/SpamTrapHandler.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/filter/fastfail/SpamTrapHandler.java?view=auto&rev=496327
==============================================================================
--- james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/filter/fastfail/SpamTrapHandler.java (added)
+++ james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/filter/fastfail/SpamTrapHandler.java Mon Jan 15 06:23:28 2007
@@ -0,0 +1,145 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+
+package org.apache.james.smtpserver.core.filter.fastfail;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.avalon.framework.configuration.Configurable;
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.logger.AbstractLogEnabled;
+import org.apache.james.smtpserver.SMTPSession;
+import org.apache.james.smtpserver.hook.HookResult;
+import org.apache.james.smtpserver.hook.HookReturnCode;
+import org.apache.james.smtpserver.hook.RcptHook;
+import org.apache.mailet.MailAddress;
+
+/**
+ * This handler can be used for providing a spam trap. IPAddresses which send emails to the configured
+ * recipients will get blacklisted for the configured time.
+ */
+public class SpamTrapHandler extends AbstractLogEnabled implements RcptHook,Configurable{
+
+    // Map which hold blockedIps and blockTime in memory
+    private Map blockedIps = new HashMap();
+    
+    private Collection spamTrapRecips = new ArrayList();
+    
+    // Default blocktime 12 hours
+    private long blockTime = 4320000; 
+    
+    /**
+     * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
+     */
+    public void configure(Configuration arg0) throws ConfigurationException {
+        Configuration[] rcptsConf = arg0.getChildren("spamTrapRecip");
+    
+        if (rcptsConf.length > 0 ) {
+            for (int i= 0; i < rcptsConf.length; i++) {
+                String rcpt = rcptsConf[i].getValue().toLowerCase();
+                
+                getLogger().debug("Add spamTrapRecip " + rcpt);
+           
+                spamTrapRecips.add(rcpt);
+            }
+        } else {
+            throw new ConfigurationException("Please configure a spamTrapRecip.");
+        }
+    
+        Configuration blockTimeConf = arg0.getChild("blockTime",false);
+    
+        if (blockTimeConf != null) {
+            blockTime = blockTimeConf.getValueAsLong(blockTime);
+        }
+    }
+    
+    public void setSpamTrapRecipients(Collection spamTrapRecips) {
+        this.spamTrapRecips = spamTrapRecips;
+    }
+    
+    public void setBlockTime(long blockTime) {
+        this.blockTime = blockTime;
+    }
+    
+    /**
+     * @see org.apache.james.smtpserver.hook.RcptHook#doRcpt(org.apache.james.smtpserver.SMTPSession, org.apache.mailet.MailAddress, org.apache.mailet.MailAddress)
+     */
+    public HookResult doRcpt(SMTPSession session, MailAddress sender, MailAddress rcpt) {
+        if (isBlocked(session.getRemoteIPAddress())) {
+            return new HookResult(HookReturnCode.DENY);
+        } else {
+         
+            if (spamTrapRecips.contains(rcpt.toString().toLowerCase())){
+        
+                addIp(session.getRemoteIPAddress());
+            
+                return new HookResult(HookReturnCode.DENY);
+            }
+        }
+        return new HookResult(HookReturnCode.DECLINED);
+    }
+    
+    
+    /**
+     * Check if ipAddress is in the blockList.
+     * 
+     * @param ip ipAddress to check
+     * @return true or false
+     */
+    private boolean isBlocked(String ip) {
+        Object rawTime = blockedIps.get(ip);
+    
+        if (rawTime != null) {
+            long blockTime = ((Long) rawTime).longValue();
+           
+            if (blockTime > System.currentTimeMillis()) {
+                getLogger().debug("BlockList contain Ip " + ip);
+                return true;
+            } else {
+                getLogger().debug("Remove ip " + ip + " from blockList");
+               
+                synchronized(blockedIps) {
+                    blockedIps.remove(ip);
+                }
+            }
+        }
+        return false;
+    }
+    
+    /**
+     * Add ipaddress to blockList
+     * 
+     * @param ip IpAddress to add
+     */
+    private void addIp(String ip) {
+        long bTime = System.currentTimeMillis() + blockTime;
+        
+        getLogger().debug("Add ip " + ip + " for " + bTime + " to blockList");
+    
+        synchronized(blockedIps) {
+            blockedIps.put(ip, new Long(bTime));
+        }
+    
+    }
+}

Propchange: james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/filter/fastfail/SpamTrapHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/SpamTrapHandlerTest.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/SpamTrapHandlerTest.java?view=auto&rev=496327
==============================================================================
--- james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/SpamTrapHandlerTest.java (added)
+++ james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/SpamTrapHandlerTest.java Mon Jan 15 06:23:28 2007
@@ -0,0 +1,87 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+
+package org.apache.james.smtpserver;
+
+import java.util.ArrayList;
+
+import javax.mail.internet.ParseException;
+
+import org.apache.avalon.framework.container.ContainerUtil;
+import org.apache.james.smtpserver.core.filter.fastfail.SpamTrapHandler;
+import org.apache.james.smtpserver.hook.HookReturnCode;
+import org.apache.james.test.mock.avalon.MockLogger;
+import org.apache.mailet.MailAddress;
+
+import junit.framework.TestCase;
+
+public class SpamTrapHandlerTest extends TestCase {
+    private static final String SPAM_TRAP_RECIP1 = "spamtrap1@localhost";
+    private static final String RECIP1 = "recip@localhost";
+    
+    private SMTPSession setUpSMTPSession(final String ip) {
+        return new AbstractSMTPSession() {
+            public String getRemoteIPAddress() {
+                return ip;
+            }
+        
+        };
+    }
+    
+    public void testSpamTrap() throws ParseException {
+        String ip = "192.168.100.1";
+        String ip2 = "192.168.100.2";
+        long blockTime = 2000;
+    
+        ArrayList rcpts = new ArrayList();
+        rcpts.add(SPAM_TRAP_RECIP1);
+    
+        SpamTrapHandler handler = new SpamTrapHandler();
+        ContainerUtil.enableLogging(handler, new MockLogger());
+    
+        handler.setBlockTime(blockTime);
+        handler.setSpamTrapRecipients(rcpts);
+    
+        int result = handler.doRcpt(setUpSMTPSession(ip),null,new MailAddress(SPAM_TRAP_RECIP1)).getResult();
+    
+        assertEquals("Blocked on first connect",HookReturnCode.DENY,result);
+    
+
+        result = handler.doRcpt(setUpSMTPSession(ip),null,new MailAddress(RECIP1)).getResult();
+    
+        assertEquals("Blocked on second connect", HookReturnCode.DENY,result);
+    
+        
+        result = handler.doRcpt(setUpSMTPSession(ip2),null,new MailAddress(RECIP1)).getResult();
+    
+        assertEquals("Not Blocked", HookReturnCode.DECLINED,result);
+    
+        try {
+            // Wait for the blockTime to exceed
+            Thread.sleep(blockTime);
+        } catch (InterruptedException e) {
+            fail("Failed to sleep for " + blockTime +" ms");
+        }
+    
+        result = handler.doRcpt(setUpSMTPSession(ip),null,new MailAddress(RECIP1)).getResult();
+    
+        assertEquals("Not blocked. BlockTime exceeded", HookReturnCode.DECLINED,result); 
+    }
+}

Propchange: james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/SpamTrapHandlerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org