1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.james.smtpserver.core.filter.fastfail;
23
24 import java.util.ArrayList;
25 import java.util.Collection;
26
27 import org.apache.avalon.framework.configuration.Configurable;
28 import org.apache.avalon.framework.configuration.Configuration;
29 import org.apache.avalon.framework.configuration.ConfigurationException;
30 import org.apache.avalon.framework.logger.AbstractLogEnabled;
31 import org.apache.james.smtpserver.CommandHandler;
32 import org.apache.james.smtpserver.SMTPSession;
33
34 public class TarpitHandler extends AbstractLogEnabled implements
35 CommandHandler, Configurable {
36
37 private int tarpitRcptCount = 0;
38
39 private long tarpitSleepTime = 5000;
40
41
42
43
44 public void configure(Configuration handlerConfiguration)
45 throws ConfigurationException {
46
47 Configuration configTarpitRcptCount = handlerConfiguration.getChild(
48 "tarpitRcptCount", false);
49 if (configTarpitRcptCount != null) {
50 setTarpitRcptCount(configTarpitRcptCount.getValueAsInteger(0));
51 }
52
53 if (tarpitRcptCount == 0)
54 throw new ConfigurationException(
55 "Please set the tarpitRcptCount bigger values as 0");
56
57 Configuration configTarpitSleepTime = handlerConfiguration.getChild(
58 "tarpitSleepTime", false);
59 if (configTarpitSleepTime != null) {
60 setTarpitSleepTime(configTarpitSleepTime.getValueAsLong(5000));
61 }
62
63 if (tarpitSleepTime == 0)
64 throw new ConfigurationException(
65 "Please set the tarpitSleepTimeto a bigger values as 0");
66
67 }
68
69
70
71
72
73
74 public void setTarpitRcptCount(int tarpitRcptCount) {
75 this.tarpitRcptCount = tarpitRcptCount;
76 }
77
78
79
80
81
82
83
84 public void setTarpitSleepTime(long tarpitSleepTime) {
85 this.tarpitSleepTime = tarpitSleepTime;
86 }
87
88
89
90
91
92
93
94
95 private void sleep(float timeInMillis) throws InterruptedException {
96 Thread.sleep((long) timeInMillis);
97 }
98
99
100
101
102 public void onCommand(SMTPSession session) {
103
104 int rcptCount = 0;
105 rcptCount = session.getRcptCount();
106 rcptCount++;
107
108 if (rcptCount > tarpitRcptCount) {
109 try {
110 sleep(tarpitSleepTime);
111 } catch (InterruptedException e) {
112 }
113 }
114 }
115
116
117
118
119 public Collection getImplCommands() {
120 Collection implCommands = new ArrayList();
121 implCommands.add("RCPT");
122
123 return implCommands;
124 }
125 }