KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > drftpd > slave > socket > SocketSlaveListener


1 /*
2  * SocketSlaveListener.java
3  *
4  * Created on April 28, 2004, 2:03 PM
5  */

6
7 package org.drftpd.slave.socket;
8
9 import java.io.BufferedReader JavaDoc;
10 import java.io.EOFException JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.InputStreamReader JavaDoc;
13 import java.io.PrintWriter JavaDoc;
14 import java.io.StringReader JavaDoc;
15
16 import java.net.InetAddress JavaDoc;
17 import java.net.ServerSocket JavaDoc;
18 import java.net.Socket JavaDoc;
19
20 import java.util.Collection JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import java.security.MessageDigest JavaDoc;
24
25 import net.sf.drftpd.FatalException;
26 import net.sf.drftpd.master.ConnectionManager;
27 import net.sf.drftpd.master.RemoteSlave;
28
29 import org.apache.log4j.Logger;
30 import org.apache.oro.text.GlobCompiler;
31 import org.apache.oro.text.regex.MalformedPatternException;
32 import org.apache.oro.text.regex.Pattern;
33 import org.apache.oro.text.regex.Perl5Matcher;
34
35 import socks.server.Ident;
36
37 /**
38  *
39  * @author jbarrett
40  */

41 public class SocketSlaveListener extends Thread JavaDoc {
42
43     private static final Logger logger = Logger.getLogger(SocketSlaveListener.class.getName());
44
45     private int _port;
46     private ServerSocket JavaDoc sock;
47     static ConnectionManager _conman;
48     static String JavaDoc _pass = "";
49     
50         static void invalidSlave(String JavaDoc msg, Socket JavaDoc sock) throws IOException JavaDoc
51         {
52             BufferedReader JavaDoc _sinp = null;
53                 PrintWriter JavaDoc _sout = null;
54                 try {
55                 _sout = new PrintWriter JavaDoc(sock.getOutputStream(), true);
56                     _sinp = new BufferedReader JavaDoc(
57                             new InputStreamReader JavaDoc(sock.getInputStream())
58                                 );
59                 _sout.println(msg);
60                     logger.info("NEW< " + msg);
61                         String JavaDoc txt = SocketSlaveListener.readLine(_sinp, 30);
62             String JavaDoc sname = "";
63             String JavaDoc spass = "";
64             String JavaDoc shash = "";
65             try {
66                 String JavaDoc[] items = txt.split(" ");
67                 sname = items[1].trim();
68                 spass = items[2].trim();
69                 shash = items[3].trim();
70             } catch (Exception JavaDoc e) {
71                                 throw new IOException JavaDoc("Slave Inititalization Faailed");
72             }
73             // generate slave hash
74
String JavaDoc pass = sname + spass + _pass;
75             MessageDigest JavaDoc md5 = MessageDigest.getInstance("MD5");
76             md5.reset();
77             md5.update(pass.getBytes());
78             String JavaDoc hash = SocketSlaveListener.hash2hex(md5.digest()).toLowerCase();
79                         if (!hash.equals(shash))
80                         {
81                                 throw new IOException JavaDoc("Slave Inititalization Faailed");
82                         }
83                         
84                         // check the passkey and create the slave
85

86                 } catch (Exception JavaDoc e) {
87                     
88                 }
89                 throw new IOException JavaDoc("Slave Inititalization Faailed");
90     }
91
92     static String JavaDoc hash2hex(byte[] bytes) {
93         String JavaDoc res = "";
94         for (int i = 0; i < 16; i++) {
95             String JavaDoc hex = Integer.toHexString((int) bytes[i]);
96             if (hex.length() < 2)
97                 hex = "0" + hex;
98             res += hex.substring(hex.length() - 2);
99         }
100         return res;
101     }
102
103         
104     static String JavaDoc readLine(BufferedReader JavaDoc _sinp, int secs) {
105         int cnt = secs * 10;
106         try {
107             while (true) {
108                 while (!_sinp.ready()) {
109                     if (cnt < 1)
110                         return null;
111                     sleep(100);
112                     cnt--;
113                     if (cnt == 0)
114                         return null;
115                 }
116                 String JavaDoc txt = _sinp.readLine();
117                 logger.info("NEW> " + txt);
118                 return txt;
119             }
120         } catch (Exception JavaDoc e) {
121             return null;
122         }
123     }
124         
125     /** Creates a new instance of SocketSlaveListener */
126     public SocketSlaveListener(ConnectionManager conman, int port, String JavaDoc pass) {
127         _conman = conman;
128         _port = port;
129         _pass = pass;
130         start();
131     }
132     
133     public void run() {
134         try {
135             sock = new ServerSocket JavaDoc(_port);
136         } catch (Exception JavaDoc e) {
137             throw new FatalException(e);
138         }
139         Socket JavaDoc slave;
140         while (true) {
141             try {
142                 slave = sock.accept();
143             } catch (Exception JavaDoc e) {
144                 throw new FatalException(e);
145             }
146             InetAddress JavaDoc addr = slave.getInetAddress();
147             logger.info("SockSlaveListener: accepting " + addr);
148             Ident identObj = new Ident(slave);
149             String JavaDoc ident;
150             if (identObj.successful) {
151                 ident = identObj.userName;
152             } else {
153                 ident = "";
154             }
155             Perl5Matcher m = new Perl5Matcher();
156             
157             String JavaDoc ipmask = ident + "@" + addr.getHostAddress();
158             String JavaDoc hostmask = ident + "@" + addr.getHostName();
159             logger.info("SockSlaveListener: ipmask " + ipmask);
160             logger.info("SockSlaveListener: hostmask " + hostmask);
161             Collection JavaDoc slaves = _conman.getSlaveManager().getSlaves();
162             boolean match = false;
163             RemoteSlave thisone = null;
164             for (Iterator JavaDoc i=slaves.iterator(); i.hasNext();) {
165                 RemoteSlave rslave = (RemoteSlave)i.next();
166                 if (rslave.isAvailable()) {
167                     logger.info("SockSlaveListener: online> " + rslave.getName());
168                     continue; // already connected
169
}
170                 String JavaDoc saddr = (String JavaDoc)rslave.getConfig().get("addr");
171                 if (saddr == null) {
172                     logger.info("SockSlaveListener: noaddr> " + rslave.getName());
173                     continue; // not a socketslave
174
}
175                 if (!saddr.equals("Dynamic")) {
176                     logger.info("SockSlaveListener: static> " + rslave.getName());
177                     continue; // is a static slave
178
}
179                 // unconnected dynamic socket slave, test masks
180
logger.info("SockSlaveListener: testing " + rslave.getName());
181                 for (Iterator JavaDoc i2 = rslave.getMasks().iterator(); i2.hasNext(); ) {
182                     String JavaDoc mask = (String JavaDoc) i2.next();
183                     logger.info("SockSlaveListener: mask = " + mask);
184                     Pattern p;
185                     try {
186                         p = new GlobCompiler().compile(mask);
187                     } catch (MalformedPatternException ex) {
188                         throw new RuntimeException JavaDoc(
189                         "Invalid glob pattern: " + mask,
190                         ex
191                         );
192                     }
193                     
194                     // ip
195
if (m.matches(ipmask, p) || m.matches(hostmask, p)) {
196                         match = true;
197                         thisone = rslave;
198                         break;
199                     }
200                 } //for
201
if (match) break;
202             } //for
203
if (match) {
204                 // turn control over to the slave code
205
try {
206                     SocketSlaveImpl tmp = new SocketSlaveImpl(_conman, thisone.getConfig(), slave);
207                 } catch (Exception JavaDoc e) {
208                 }
209             } else {
210                 // allow the slave to auto-register
211
try {
212                     SocketSlaveListener.invalidSlave("INITFAIL Unregistered", slave);
213                 } catch (Exception JavaDoc e) {
214                 }
215             }
216         }
217     }
218 }
219
Popular Tags