1 | /* |
---|
2 | * Jeti, a Java Jabber client, Copyright (C) 2003 E.S. de Boer |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation; either version 2 of the License, or |
---|
7 | * (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 | * For questions, comments etc, |
---|
19 | * use the website at http://jeti.jabberstudio.org |
---|
20 | * or mail/im me at jeti@jabber.org |
---|
21 | */ |
---|
22 | |
---|
23 | package nu.fw.jeti.backend; |
---|
24 | |
---|
25 | import java.io.IOException; |
---|
26 | import java.io.InputStream; |
---|
27 | import java.io.OutputStream; |
---|
28 | import java.io.UnsupportedEncodingException; |
---|
29 | import java.net.InetAddress; |
---|
30 | import java.net.Socket; |
---|
31 | import java.net.UnknownHostException; |
---|
32 | import java.security.KeyManagementException; |
---|
33 | import java.security.MessageDigest; |
---|
34 | import java.security.NoSuchAlgorithmException; |
---|
35 | import java.security.cert.CertificateExpiredException; |
---|
36 | import java.security.cert.CertificateNotYetValidException; |
---|
37 | import java.security.cert.X509Certificate; |
---|
38 | import java.text.MessageFormat; |
---|
39 | import java.util.Iterator; |
---|
40 | |
---|
41 | import javax.net.ssl.HandshakeCompletedEvent; |
---|
42 | import javax.net.ssl.HandshakeCompletedListener; |
---|
43 | import javax.net.ssl.SSLContext; |
---|
44 | import javax.net.ssl.SSLSocket; |
---|
45 | import javax.net.ssl.SSLSocketFactory; |
---|
46 | import javax.net.ssl.TrustManager; |
---|
47 | import javax.net.ssl.X509TrustManager; |
---|
48 | import javax.swing.JOptionPane; |
---|
49 | |
---|
50 | import nu.fw.jeti.events.DiscoveryListener; |
---|
51 | import nu.fw.jeti.events.LoginListener; |
---|
52 | import nu.fw.jeti.jabber.Backend; |
---|
53 | import nu.fw.jeti.jabber.JID; |
---|
54 | import nu.fw.jeti.jabber.elements.DiscoveryInfo; |
---|
55 | import nu.fw.jeti.jabber.elements.DiscoveryItem; |
---|
56 | import nu.fw.jeti.jabber.elements.IQAuth; |
---|
57 | import nu.fw.jeti.jabber.elements.IQAuthBuilder; |
---|
58 | import nu.fw.jeti.jabber.elements.IQExtension; |
---|
59 | import nu.fw.jeti.jabber.elements.IQPrivate; |
---|
60 | import nu.fw.jeti.jabber.elements.IQXRoster; |
---|
61 | import nu.fw.jeti.jabber.elements.InfoQuery; |
---|
62 | import nu.fw.jeti.jabber.elements.JetiPrivateExtension; |
---|
63 | import nu.fw.jeti.jabber.elements.JetiPrivateRosterExtension; |
---|
64 | import nu.fw.jeti.jabber.elements.Packet; |
---|
65 | import nu.fw.jeti.jabber.elements.Presence; |
---|
66 | import nu.fw.jeti.jabber.elements.PresenceBuilder; |
---|
67 | import nu.fw.jeti.jabber.elements.StreamError; |
---|
68 | import nu.fw.jeti.plugins.PluginsInfo; |
---|
69 | import nu.fw.jeti.plugins.XMPP; |
---|
70 | import nu.fw.jeti.util.I18N; |
---|
71 | import nu.fw.jeti.util.Log; |
---|
72 | import nu.fw.jeti.util.Utils; |
---|
73 | |
---|
74 | /** |
---|
75 | * @author E.S. de Boer |
---|
76 | */ |
---|
77 | |
---|
78 | //TODO stop packet start spul en de andere dingen in aparte class?? |
---|
79 | //TODO improve error reporting, make it translatable |
---|
80 | //class voor connectie |
---|
81 | public class Connect implements ConnectionPacketReceiver |
---|
82 | { |
---|
83 | private Output output; |
---|
84 | private Input input; |
---|
85 | private String authenticationId="yytr"; |
---|
86 | private JabberHandler jabberHandler; |
---|
87 | private LoginInfo loginInfo; |
---|
88 | private Backend backend; |
---|
89 | private static JID myJID = new JID("test","test","test"); |
---|
90 | private boolean authenticated = false; |
---|
91 | private boolean reconnecting = false; |
---|
92 | private long latestConnected=0; |
---|
93 | private int show; |
---|
94 | private String status; |
---|
95 | private String connectionID; |
---|
96 | private Discovery discovery; |
---|
97 | private OwnCapabilities capabilities; |
---|
98 | private Socket socket;//socket needed to close if abort |
---|
99 | private Thread connectThread;//login thread |
---|
100 | private volatile boolean abort = false;//abort login |
---|
101 | private IQTimerQueue iqTimerQueue; |
---|
102 | private Handlers handlers; |
---|
103 | private XMPP xmpp; |
---|
104 | |
---|
105 | public Connect(Backend backend,IQTimerQueue timerQueue,Handlers handlers) |
---|
106 | { |
---|
107 | this.backend = backend; |
---|
108 | iqTimerQueue = timerQueue; |
---|
109 | this.handlers = handlers; |
---|
110 | discovery = new Discovery(backend) ; |
---|
111 | capabilities = new OwnCapabilities(backend); |
---|
112 | } |
---|
113 | |
---|
114 | public void addCapability(String capability,String feature) |
---|
115 | { |
---|
116 | capabilities.addCapability(capability, feature); |
---|
117 | } |
---|
118 | |
---|
119 | public void removeCapability(String capability,String feature) |
---|
120 | { |
---|
121 | capabilities.removeCapability(capability, feature); |
---|
122 | } |
---|
123 | |
---|
124 | public int getStatus() |
---|
125 | { |
---|
126 | return show; |
---|
127 | } |
---|
128 | |
---|
129 | public void getItems(JID jid, DiscoveryListener listener, boolean useCache) |
---|
130 | { |
---|
131 | discovery.getItems(jid,listener,useCache); |
---|
132 | } |
---|
133 | |
---|
134 | public void getItems(JID jid, DiscoveryListener listener) |
---|
135 | { |
---|
136 | discovery.getItems(jid,listener); |
---|
137 | } |
---|
138 | |
---|
139 | public void getInfo(JID jid, DiscoveryListener listener) |
---|
140 | { |
---|
141 | discovery.getInfo(jid,listener); |
---|
142 | } |
---|
143 | |
---|
144 | public void getItems(JID jid,String node, DiscoveryListener listener) |
---|
145 | { |
---|
146 | discovery.getItems(jid,node,listener); |
---|
147 | } |
---|
148 | |
---|
149 | public void getInfo(JID jid,String node, DiscoveryListener listener) |
---|
150 | { |
---|
151 | discovery.getInfo(jid,node,listener); |
---|
152 | } |
---|
153 | |
---|
154 | public boolean isLoggedIn() |
---|
155 | { |
---|
156 | return authenticated; |
---|
157 | } |
---|
158 | |
---|
159 | public boolean isPasswordValid(String password) |
---|
160 | { |
---|
161 | return loginInfo.getPassword().equals(password); |
---|
162 | } |
---|
163 | |
---|
164 | public void login(LoginInfo info) |
---|
165 | { |
---|
166 | abort = false; |
---|
167 | loginInfo = info; |
---|
168 | connectThread = new Thread() |
---|
169 | { |
---|
170 | public void run() |
---|
171 | { |
---|
172 | connect(); |
---|
173 | } |
---|
174 | }; |
---|
175 | connectThread.start(); |
---|
176 | } |
---|
177 | |
---|
178 | public void autoLogin(LoginInfo info,final int tries) |
---|
179 | { |
---|
180 | if(System.currentTimeMillis() < latestConnected + 60000) |
---|
181 | {//prevent ping pong if things go wrong at login |
---|
182 | return; |
---|
183 | } |
---|
184 | abort = false; |
---|
185 | loginInfo = info; |
---|
186 | connectThread = new Thread() |
---|
187 | { |
---|
188 | int tel = 0; |
---|
189 | public void run() |
---|
190 | { |
---|
191 | boolean connected=false; |
---|
192 | while(tel < tries) |
---|
193 | { |
---|
194 | connected = connect(); |
---|
195 | if(connected) break; |
---|
196 | try |
---|
197 | { |
---|
198 | Thread.sleep(60000); |
---|
199 | } |
---|
200 | catch (InterruptedException e){} |
---|
201 | tel++; |
---|
202 | } |
---|
203 | } |
---|
204 | }; |
---|
205 | connectThread.start(); |
---|
206 | } |
---|
207 | |
---|
208 | public void abort() |
---|
209 | { |
---|
210 | abort = true; |
---|
211 | if(socket != null) |
---|
212 | { |
---|
213 | try |
---|
214 | { |
---|
215 | socket.close(); |
---|
216 | } |
---|
217 | catch (IOException e) |
---|
218 | { |
---|
219 | e.printStackTrace(); |
---|
220 | } |
---|
221 | } |
---|
222 | connectThread.interrupt(); |
---|
223 | disconnect(); |
---|
224 | } |
---|
225 | |
---|
226 | public boolean isAborted() |
---|
227 | { |
---|
228 | return abort; |
---|
229 | } |
---|
230 | |
---|
231 | synchronized private boolean connect() |
---|
232 | { |
---|
233 | latestConnected = System.currentTimeMillis(); |
---|
234 | |
---|
235 | if( loginInfo == null ) |
---|
236 | return true; |
---|
237 | |
---|
238 | if( authenticated ) |
---|
239 | disconnect();//clear old connection |
---|
240 | |
---|
241 | if (loginInfo.useProxy(LoginInfo.SOCKS_PROXY)) |
---|
242 | { |
---|
243 | System.getProperties().put( "proxySet", "true" ); |
---|
244 | System.getProperties().setProperty("socksProxyHost", loginInfo.getProxyServer()); |
---|
245 | System.getProperties().setProperty("socksProxyPort", loginInfo.getProxyPort()); |
---|
246 | System.getProperties().setProperty("socksProxyUserName", loginInfo.getProxyUsername()); |
---|
247 | if(loginInfo.getProxyPassword()!=null)System.getProperties().setProperty("socksProxyPassword", loginInfo.getProxyPassword()); |
---|
248 | } |
---|
249 | |
---|
250 | jabberHandler = new JabberHandler(this,handlers); |
---|
251 | |
---|
252 | if( xmpp == null && PluginsInfo.isPluginLoaded("xmpp") ) |
---|
253 | { |
---|
254 | xmpp = (XMPP)PluginsInfo.newPluginInstance("xmpp"); |
---|
255 | handlers.loadExtraHandlers(xmpp.getXMPPHandlers()); |
---|
256 | } |
---|
257 | |
---|
258 | try |
---|
259 | { |
---|
260 | host = loginInfo.getHost(); |
---|
261 | if (host == null || host.length() == 0) |
---|
262 | { |
---|
263 | if(loginInfo.useProxy(LoginInfo.NO_PROXY) && PluginsInfo.isPluginLoaded("xmpp")) |
---|
264 | { |
---|
265 | host = xmpp.resolveXMPPDomain(loginInfo.getServer()); |
---|
266 | } |
---|
267 | else host = loginInfo.getServer(); |
---|
268 | } |
---|
269 | |
---|
270 | if(loginInfo.isSSl()) |
---|
271 | { |
---|
272 | if( loginInfo.isHTTPProxy() && loginInfo.getJavaProxy() ) |
---|
273 | { |
---|
274 | Socket tunnel = createHTTPTunel(host); |
---|
275 | socket = new DummySSLSocketFactory().createSocket(tunnel,host,loginInfo.getPort(),true); |
---|
276 | } |
---|
277 | else |
---|
278 | socket = new DummySSLSocketFactory().createSocket(host,loginInfo.getPort()); |
---|
279 | } |
---|
280 | else |
---|
281 | { |
---|
282 | socket = new Socket(host,loginInfo.getPort()); |
---|
283 | } |
---|
284 | } |
---|
285 | catch (UnknownHostException ex) |
---|
286 | { |
---|
287 | sendLoginError(MessageFormat.format(I18N.gettext("main.loginstatus.Server_{0}_could_not_be_found"), new Object[] { loginInfo.getHost() } )); |
---|
288 | return false; |
---|
289 | } |
---|
290 | catch (IOException ex) |
---|
291 | { |
---|
292 | sendLoginError(ex.getMessage()); |
---|
293 | return false; |
---|
294 | } |
---|
295 | if( abort ) |
---|
296 | return false; |
---|
297 | |
---|
298 | try |
---|
299 | { |
---|
300 | input = new Input(socket.getInputStream(),this,jabberHandler); |
---|
301 | } |
---|
302 | catch (IOException ex) |
---|
303 | { |
---|
304 | sendLoginError(I18N.gettext("main.loginstatus.Could_not_open_input_because") + " " + ex.getMessage()); |
---|
305 | return false; |
---|
306 | } |
---|
307 | |
---|
308 | if( abort ) |
---|
309 | return false; |
---|
310 | |
---|
311 | try |
---|
312 | { |
---|
313 | output = new Output(socket,loginInfo.getServer(),this,socket.getOutputStream()); |
---|
314 | } |
---|
315 | catch (IOException ex) |
---|
316 | { |
---|
317 | sendLoginError(I18N.gettext("main.loginstatus.Could_not_open_output_because") + " " +ex.getMessage()); |
---|
318 | return false; |
---|
319 | } |
---|
320 | |
---|
321 | return true; |
---|
322 | } |
---|
323 | |
---|
324 | String host; |
---|
325 | |
---|
326 | public boolean startTls(ConnectionPacketReceiver cpr) |
---|
327 | { |
---|
328 | if(abort)return false; |
---|
329 | try{ |
---|
330 | //TODO stop output |
---|
331 | output.disconnect(false); |
---|
332 | input.disconnect(); |
---|
333 | socket = new DummySSLSocketFactory().createSocket(socket,host,loginInfo.getPort(),true); |
---|
334 | jabberHandler = new JabberHandler(cpr,handlers); |
---|
335 | |
---|
336 | |
---|
337 | try{ |
---|
338 | input = new Input(socket.getInputStream(),this,jabberHandler); |
---|
339 | }catch (IOException ex) |
---|
340 | { |
---|
341 | sendLoginError(I18N.gettext("main.loginstatus.Could_not_open_input_because") + " " + ex.getMessage()); |
---|
342 | return false; |
---|
343 | } |
---|
344 | |
---|
345 | try{ |
---|
346 | output = new Output(socket,loginInfo.getServer(),this,socket.getOutputStream()); |
---|
347 | }catch (IOException ex) |
---|
348 | { |
---|
349 | sendLoginError(I18N.gettext("main.loginstatus.Could_not_open_output_because") + " " +ex.getMessage()); |
---|
350 | return false; |
---|
351 | } |
---|
352 | |
---|
353 | if(abort) return false; |
---|
354 | |
---|
355 | }catch (IOException e) {e.printStackTrace();} |
---|
356 | return true; |
---|
357 | } |
---|
358 | |
---|
359 | //used by compression plugin |
---|
360 | public Socket getSocket(){ |
---|
361 | return socket; |
---|
362 | } |
---|
363 | |
---|
364 | public boolean startCompressed(ConnectionPacketReceiver cpr,InputStream inputStream,OutputStream outputStream) |
---|
365 | { |
---|
366 | jabberHandler = new JabberHandler(cpr,handlers); |
---|
367 | if ( abort ) |
---|
368 | return false; |
---|
369 | output.disconnect(false); |
---|
370 | Input input2 = input; |
---|
371 | input = new Input(inputStream,this,jabberHandler); |
---|
372 | try |
---|
373 | { |
---|
374 | output = new Output(socket,loginInfo.getServer(),this,outputStream); |
---|
375 | } |
---|
376 | catch (IOException ex) |
---|
377 | { |
---|
378 | sendLoginError(I18N.gettext("main.loginstatus.Could_not_open_output_because") + " " +ex.getMessage()); |
---|
379 | ex.printStackTrace(); |
---|
380 | return false; |
---|
381 | } |
---|
382 | |
---|
383 | if ( abort ) |
---|
384 | return false; |
---|
385 | input2.disconnect(); |
---|
386 | //FIXME Using thread.stop because throwing an exception to stop the sax parser in the input thread fails |
---|
387 | input2.stop(); |
---|
388 | //throw new RuntimeException(); |
---|
389 | |
---|
390 | return true; |
---|
391 | } |
---|
392 | |
---|
393 | |
---|
394 | public void startSasl(ConnectionPacketReceiver cpr) |
---|
395 | { |
---|
396 | if( abort ) |
---|
397 | return; |
---|
398 | |
---|
399 | //kill old input and send new stream |
---|
400 | //input.disconnect(); |
---|
401 | jabberHandler = new JabberHandler(cpr,handlers); |
---|
402 | |
---|
403 | try |
---|
404 | { |
---|
405 | input = new Input(socket.getInputStream(),this,jabberHandler); |
---|
406 | } |
---|
407 | catch (IOException e) |
---|
408 | { |
---|
409 | e.printStackTrace(); |
---|
410 | } |
---|
411 | |
---|
412 | try |
---|
413 | { |
---|
414 | output.writeHeader(); |
---|
415 | } |
---|
416 | catch (IOException e) |
---|
417 | { |
---|
418 | e.printStackTrace(); |
---|
419 | } |
---|
420 | |
---|
421 | throw new UnsupportedOperationException("end xmlparser"); |
---|
422 | } |
---|
423 | |
---|
424 | private Socket createHTTPTunel(String host) throws IOException |
---|
425 | { |
---|
426 | String tunnelHost = loginInfo.getProxyServer(); |
---|
427 | int tunnelPort = Integer.valueOf(loginInfo.getProxyPort()).intValue(); |
---|
428 | Socket tunnel = new Socket(tunnelHost, tunnelPort); |
---|
429 | doTunnelHandshake(tunnel,host,loginInfo.getPort()); |
---|
430 | return tunnel; |
---|
431 | } |
---|
432 | |
---|
433 | private void doTunnelHandshake(Socket tunnel, String host, int port) throws IOException |
---|
434 | { |
---|
435 | OutputStream out = tunnel.getOutputStream(); |
---|
436 | String msg = "CONNECT " + host + ":" + port + " HTTP/1.0\n" |
---|
437 | + "User-Agent: " |
---|
438 | + sun.net.www.protocol.http.HttpURLConnection.userAgent; |
---|
439 | |
---|
440 | if (loginInfo.getProxyUsername() != null && loginInfo.getProxyPassword() != null) { |
---|
441 | //add basic authentication header for the proxy |
---|
442 | sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder(); |
---|
443 | String encodedPassword = enc.encode((loginInfo.getProxyUsername() + ":" + loginInfo.getProxyPassword()).getBytes()); |
---|
444 | msg = msg + "\nProxy-Authorization: Basic " + encodedPassword; |
---|
445 | } |
---|
446 | msg = msg + "\nContent-Length: 0" |
---|
447 | +"\nPragma: no-cache" |
---|
448 | +"\r\n\r\n"; |
---|
449 | byte b[]; |
---|
450 | try |
---|
451 | { |
---|
452 | /* |
---|
453 | * We really do want ASCII7 -- the http protocol doesn't change |
---|
454 | * with locale. |
---|
455 | */ |
---|
456 | b = msg.getBytes("ASCII7"); |
---|
457 | } catch (UnsupportedEncodingException ignored) |
---|
458 | { |
---|
459 | /* |
---|
460 | * If ASCII7 isn't there, something serious is wrong, but |
---|
461 | * Paranoia Is Good (tm) |
---|
462 | */ |
---|
463 | b = msg.getBytes(); |
---|
464 | } |
---|
465 | out.write(b); |
---|
466 | out.flush(); |
---|
467 | |
---|
468 | /* |
---|
469 | * We need to store the reply so we can create a detailed error |
---|
470 | * message to the user. |
---|
471 | */ |
---|
472 | byte reply[] = new byte[200]; |
---|
473 | int replyLen = 0; |
---|
474 | int newlinesSeen = 0; |
---|
475 | boolean headerDone = false; /* Done on first newline */ |
---|
476 | InputStream in = tunnel.getInputStream(); |
---|
477 | |
---|
478 | while ( newlinesSeen < 2 ) |
---|
479 | { |
---|
480 | int i = in.read(); |
---|
481 | if (i < 0) { throw new IOException("Unexpected EOF from proxy"); } |
---|
482 | if (i == '\n') |
---|
483 | { |
---|
484 | headerDone = true; |
---|
485 | ++newlinesSeen; |
---|
486 | } else if (i != '\r') |
---|
487 | { |
---|
488 | newlinesSeen = 0; |
---|
489 | if (!headerDone && replyLen < reply.length) |
---|
490 | { |
---|
491 | reply[replyLen++] = (byte) i; |
---|
492 | } |
---|
493 | } |
---|
494 | } |
---|
495 | |
---|
496 | /* |
---|
497 | * Converting the byte array to a string is slightly wasteful in the |
---|
498 | * case where the connection was successful, but it's insignificant |
---|
499 | * compared to the network overhead. |
---|
500 | */ |
---|
501 | String replyStr; |
---|
502 | try |
---|
503 | { |
---|
504 | replyStr = new String(reply, 0, replyLen, "ASCII7"); |
---|
505 | } catch (UnsupportedEncodingException ignored) |
---|
506 | { |
---|
507 | replyStr = new String(reply, 0, replyLen); |
---|
508 | } |
---|
509 | |
---|
510 | /* |
---|
511 | * We check for Connection Established because our proxy returns |
---|
512 | * HTTP/1.1 instead of 1.0 |
---|
513 | */ |
---|
514 | // if (!replyStr.startsWith("HTTP/1.0 200")) { |
---|
515 | if (replyStr.toLowerCase().indexOf("200 connection established") == -1) |
---|
516 | { |
---|
517 | String tunnelHost = System.getProperty("http.proxyHost"); |
---|
518 | String tunnelPort = System.getProperty("http.proxyPort"); |
---|
519 | throw new IOException("Unable to tunnel through " + tunnelHost |
---|
520 | + ":" + tunnelPort + ". Proxy returns \"" + replyStr |
---|
521 | + "\""); |
---|
522 | } |
---|
523 | /* tunneling Handshake was successful! */ |
---|
524 | } |
---|
525 | |
---|
526 | public void sendLoginError(String message) |
---|
527 | { |
---|
528 | for(Iterator j = backend.getListeners(LoginListener.class);j.hasNext();) |
---|
529 | { |
---|
530 | ((LoginListener)j.next()).loginError(message); |
---|
531 | } |
---|
532 | } |
---|
533 | |
---|
534 | public void sendUnauthorized() |
---|
535 | { |
---|
536 | for(Iterator j = backend.getListeners(LoginListener.class);j.hasNext();) |
---|
537 | { |
---|
538 | ((LoginListener)j.next()).unauthorized(); |
---|
539 | } |
---|
540 | } |
---|
541 | |
---|
542 | synchronized public void connected(String connectionID,String xmppVersion) |
---|
543 | { |
---|
544 | if(!abort) |
---|
545 | { |
---|
546 | if(xmppVersion!=null && PluginsInfo.isPluginLoaded("xmpp")) |
---|
547 | { |
---|
548 | ConnectionPacketReceiver cpr = xmpp.getConnectionPacketReceiver(loginInfo,this); |
---|
549 | jabberHandler.changePacketReceiver(cpr); |
---|
550 | cpr.connected(connectionID, xmppVersion); |
---|
551 | } |
---|
552 | else |
---|
553 | { |
---|
554 | jabberHandler.changePacketReceiver(this); |
---|
555 | socket = null; //clear socket reference |
---|
556 | this.connectionID = connectionID; |
---|
557 | // TODO remove lowercase if filetransfer bug fixed |
---|
558 | output.send(new InfoQuery(null,"get",new IQAuth(loginInfo.getUsername().toLowerCase(),null,null))); |
---|
559 | } |
---|
560 | } |
---|
561 | |
---|
562 | } |
---|
563 | |
---|
564 | public void authenticate(IQAuth iqAuth) |
---|
565 | { |
---|
566 | if(!abort) |
---|
567 | { |
---|
568 | authenticationId = "Jeti_Auth_" + new java.util.Date().getTime(); |
---|
569 | |
---|
570 | if(iqAuth.hasDigest()) |
---|
571 | { |
---|
572 | MessageDigest sha = null; |
---|
573 | try { |
---|
574 | sha = MessageDigest.getInstance("SHA"); |
---|
575 | } catch (Exception ex){ |
---|
576 | Log.error(I18N.gettext("main.loginstatus.Could_not_login_with_SHA")); |
---|
577 | // TODO remove lowercase if filetransfer bug fixed |
---|
578 | output.send(new InfoQuery(null,"set",authenticationId,new IQAuth(loginInfo.getUsername().toLowerCase(),loginInfo.getPassword() ,loginInfo.getResource()))); |
---|
579 | return; |
---|
580 | } |
---|
581 | |
---|
582 | sha.update(connectionID.getBytes()); |
---|
583 | String digest = Utils.toString(sha.digest(loginInfo.getPassword().getBytes())); |
---|
584 | IQAuthBuilder iqab = new IQAuthBuilder(); |
---|
585 | iqab.digest = digest; |
---|
586 | // TODO remove lowercase if filetransfer bug fixed |
---|
587 | iqab.username = loginInfo.getUsername().toLowerCase(); |
---|
588 | iqab.resource = loginInfo.getResource(); |
---|
589 | output.send(new InfoQuery(null,"set",authenticationId,(IQExtension)iqab.build())); |
---|
590 | } |
---|
591 | else |
---|
592 | { |
---|
593 | if(!loginInfo.isSSl()) |
---|
594 | { |
---|
595 | int option = JOptionPane.showConfirmDialog(null, |
---|
596 | I18N.gettext("main.loginstatus.Sending_password_as_plain_text_over_an_unencrypted_connection,_continue?"),"Plain text",JOptionPane.YES_NO_OPTION); |
---|
597 | if (option == JOptionPane.NO_OPTION) |
---|
598 | { |
---|
599 | sendLoginError("Sending password in plain not allowed"); |
---|
600 | return; |
---|
601 | } |
---|
602 | } |
---|
603 | // TODO remove lowercase if filetransfer bug fixed |
---|
604 | output.send(new InfoQuery(null,"set",authenticationId,new IQAuth(loginInfo.getUsername().toLowerCase(),loginInfo.getPassword() ,loginInfo.getResource()))); |
---|
605 | } |
---|
606 | } |
---|
607 | } |
---|
608 | |
---|
609 | public void authenticated(InfoQuery infoQuery) |
---|
610 | { |
---|
611 | if(!abort) |
---|
612 | { |
---|
613 | if(infoQuery.getType().equals("error")) |
---|
614 | { |
---|
615 | if(infoQuery.getErrorCode() == 401) |
---|
616 | { |
---|
617 | sendUnauthorized(); |
---|
618 | } |
---|
619 | else |
---|
620 | { |
---|
621 | sendLoginError(I18N.gettext("main.loginstatus.Not_logged_in_because") + " " + infoQuery.getErrorDescription()); |
---|
622 | } |
---|
623 | return; |
---|
624 | } |
---|
625 | // TODO remove lowercase if filetransfer bug fixed |
---|
626 | JID jid = new JID(loginInfo.getUsername().toLowerCase(),loginInfo.getServer() ,loginInfo.getResource()); |
---|
627 | authenticated(jid); |
---|
628 | } |
---|
629 | } |
---|
630 | |
---|
631 | public void authenticated(JID jid) |
---|
632 | { |
---|
633 | myJID = jid; |
---|
634 | iqTimerQueue.clear();//remove iqs from previous connections |
---|
635 | jabberHandler.changePacketReceiver(new Jabber(backend,capabilities,discovery,iqTimerQueue)); |
---|
636 | authenticated = true; |
---|
637 | reconnecting = false; |
---|
638 | output.setAuthenticated(); |
---|
639 | //output.send(new InfoQuery(new JID(server),"get",new IQBrowse())); |
---|
640 | //browse(new JID(loginInfo.getServer()),null); |
---|
641 | //TODO increase timeout |
---|
642 | getItems(new JID(loginInfo.getServer()), new DiscoveryListener() |
---|
643 | { |
---|
644 | public void discoveryItemResult(JID jid, DiscoveryItem item) |
---|
645 | {//cache disco items for this server |
---|
646 | if (item.hasItems()) |
---|
647 | { |
---|
648 | for(Iterator i = item.getItems();i.hasNext();) |
---|
649 | { |
---|
650 | DiscoveryItem di = (DiscoveryItem)i.next(); |
---|
651 | backend.getInfo(di.getJID(),null); |
---|
652 | } |
---|
653 | } |
---|
654 | } |
---|
655 | public void discoveryInfoResult(JID jid, DiscoveryInfo info) {} |
---|
656 | }); |
---|
657 | output.send(new InfoQuery("get",new IQPrivate(new JetiPrivateExtension()))); |
---|
658 | output.send(new InfoQuery("get",new IQXRoster())); |
---|
659 | if(show == Presence.NONE) |
---|
660 | { |
---|
661 | show = Presence.AVAILABLE; |
---|
662 | //status = Presence.toLongShow(show); |
---|
663 | } |
---|
664 | latestConnected =System.currentTimeMillis(); |
---|
665 | //moved to backend rosterloaded |
---|
666 | // for(Iterator j = backend.getListeners(nu.fw.jeti.events.StatusChangeListener.class);j.hasNext();) |
---|
667 | // {//online |
---|
668 | // ((nu.fw.jeti.events.StatusChangeListener)j.next()).connectionChanged(true); |
---|
669 | // } |
---|
670 | //changeStatus(show,status); |
---|
671 | } |
---|
672 | |
---|
673 | |
---|
674 | public void connected() |
---|
675 | { |
---|
676 | sendStatus(); |
---|
677 | send(new InfoQuery("get",new IQPrivate(new JetiPrivateRosterExtension()))); |
---|
678 | for( Iterator j = backend.getListeners(nu.fw.jeti.events.StatusChangeListener.class) ; j.hasNext(); ) |
---|
679 | { |
---|
680 | //online |
---|
681 | ((nu.fw.jeti.events.StatusChangeListener)j.next()).connectionChanged(true); |
---|
682 | } |
---|
683 | } |
---|
684 | |
---|
685 | public void receivePackets(Packet packet) |
---|
686 | { |
---|
687 | if (packet instanceof StreamError) streamError((StreamError)packet); |
---|
688 | else if(authenticationId.equals(packet.getID())) authenticated((InfoQuery)packet); |
---|
689 | else if(packet instanceof InfoQuery) |
---|
690 | { |
---|
691 | IQExtension extension = packet.getIQExtension(); |
---|
692 | if(extension instanceof IQAuth) |
---|
693 | { |
---|
694 | if(((InfoQuery)packet).getType().equals("error")) |
---|
695 | { |
---|
696 | sendLoginError(packet.getErrorDescription()); |
---|
697 | } |
---|
698 | else authenticate((IQAuth) extension); |
---|
699 | } |
---|
700 | } |
---|
701 | } |
---|
702 | |
---|
703 | public void inputDeath() |
---|
704 | { |
---|
705 | //if(true)return; something todo with xmpp plugin, should not be here????? or server deads will not be detected |
---|
706 | try |
---|
707 | { |
---|
708 | if( socket != null ) socket.close(); |
---|
709 | } |
---|
710 | catch (IOException e) |
---|
711 | { |
---|
712 | e.printStackTrace(); |
---|
713 | } |
---|
714 | |
---|
715 | if((authenticated || reconnecting)) |
---|
716 | { |
---|
717 | authenticated = false; |
---|
718 | if (reconnecting) { |
---|
719 | try { |
---|
720 | Thread.sleep(10000); |
---|
721 | } catch (InterruptedException e) {}; |
---|
722 | } |
---|
723 | reconnecting = true; |
---|
724 | output.disconnect(true); |
---|
725 | for(Iterator j = backend.getListeners(nu.fw.jeti.events.StatusChangeListener.class);j.hasNext();) |
---|
726 | { |
---|
727 | //offline |
---|
728 | ((nu.fw.jeti.events.StatusChangeListener)j.next()).connectionChanged(false); |
---|
729 | } |
---|
730 | } |
---|
731 | sendLoginError(I18N.gettext("main.loginstatus.Lost_Input")); |
---|
732 | //else |
---|
733 | //io error while logging in |
---|
734 | //do something usefull |
---|
735 | } |
---|
736 | |
---|
737 | public void streamError(StreamError error) |
---|
738 | { |
---|
739 | //only when logging in |
---|
740 | if(authenticated) |
---|
741 | { |
---|
742 | authenticated = false; |
---|
743 | output.disconnect(true); |
---|
744 | for(Iterator j = backend.getListeners(nu.fw.jeti.events.StatusChangeListener.class);j.hasNext();) |
---|
745 | { |
---|
746 | //offline |
---|
747 | ((nu.fw.jeti.events.StatusChangeListener)j.next()).connectionChanged(false); |
---|
748 | } |
---|
749 | } |
---|
750 | sendLoginError(error.getErrorDescription()); |
---|
751 | } |
---|
752 | |
---|
753 | public void outputDeath() |
---|
754 | { |
---|
755 | authenticated = false; |
---|
756 | for(Iterator j = backend.getListeners(nu.fw.jeti.events.StatusChangeListener.class);j.hasNext();) |
---|
757 | { |
---|
758 | //offline |
---|
759 | ((nu.fw.jeti.events.StatusChangeListener)j.next()).connectionChanged(false); |
---|
760 | } |
---|
761 | } |
---|
762 | |
---|
763 | public static JID getMyJID() |
---|
764 | { |
---|
765 | return myJID; |
---|
766 | } |
---|
767 | |
---|
768 | public boolean getOnline() |
---|
769 | { |
---|
770 | return authenticated; |
---|
771 | } |
---|
772 | |
---|
773 | public void disconnect() |
---|
774 | { |
---|
775 | if( authenticated ) |
---|
776 | { |
---|
777 | send(new Presence(myJID, "unavailable")); |
---|
778 | authenticated = false; |
---|
779 | output.disconnect(true); |
---|
780 | } |
---|
781 | output = null; |
---|
782 | for(Iterator j = backend.getListeners(nu.fw.jeti.events.StatusChangeListener.class);j.hasNext();) |
---|
783 | { |
---|
784 | //offline |
---|
785 | ((nu.fw.jeti.events.StatusChangeListener)j.next()).connectionChanged(false); |
---|
786 | } |
---|
787 | } |
---|
788 | |
---|
789 | public void exit() |
---|
790 | { |
---|
791 | if(authenticated) |
---|
792 | { |
---|
793 | send(new nu.fw.jeti.jabber.elements.Presence(myJID,"unavailable")); |
---|
794 | authenticated = false; |
---|
795 | output.disconnect(true); |
---|
796 | } |
---|
797 | output = null; |
---|
798 | for(Iterator j = backend.getListeners(nu.fw.jeti.events.StatusChangeListener.class);j.hasNext();) |
---|
799 | {//exit |
---|
800 | ((nu.fw.jeti.events.StatusChangeListener)j.next()).exit(); |
---|
801 | } |
---|
802 | } |
---|
803 | |
---|
804 | public void send(Packet packet) |
---|
805 | { |
---|
806 | if(authenticated) output.send(packet); |
---|
807 | else nu.fw.jeti.util.Log.notSend(packet.toString()); |
---|
808 | } |
---|
809 | |
---|
810 | public void sendWhileConnecting(Packet packet) |
---|
811 | { |
---|
812 | output.send(packet); |
---|
813 | } |
---|
814 | |
---|
815 | public String getAccountInfo() |
---|
816 | { |
---|
817 | return MessageFormat.format(I18N.gettext("main.popup.logged_in_as_{0}_on_server_{1}_with_resource_{2}"),new Object[]{loginInfo.getUsername(),loginInfo.getServer(),loginInfo.getResource()}); |
---|
818 | } |
---|
819 | |
---|
820 | public void sendStatus() |
---|
821 | { |
---|
822 | if( authenticated ) |
---|
823 | { |
---|
824 | PresenceBuilder pb = new PresenceBuilder(); |
---|
825 | pb.show = show; |
---|
826 | pb.status = status; |
---|
827 | pb.priority = loginInfo.getPriority(); |
---|
828 | pb.addExtension(capabilities.getCaps()); |
---|
829 | |
---|
830 | try |
---|
831 | { |
---|
832 | send(pb.build()); |
---|
833 | } catch (InstantiationException e) |
---|
834 | { |
---|
835 | e.printStackTrace(); |
---|
836 | } |
---|
837 | for(Iterator j = backend.getListeners(nu.fw.jeti.events.StatusChangeListener.class);j.hasNext();) |
---|
838 | { |
---|
839 | ((nu.fw.jeti.events.StatusChangeListener)j.next()).ownPresenceChanged(show,status); |
---|
840 | } |
---|
841 | } |
---|
842 | } |
---|
843 | |
---|
844 | public void changeStatus(int show,String status) |
---|
845 | { |
---|
846 | this.show = show; |
---|
847 | this.status = status; |
---|
848 | sendStatus(); |
---|
849 | } |
---|
850 | |
---|
851 | public static class DummySSLSocketFactory extends SSLSocketFactory |
---|
852 | { |
---|
853 | private SSLSocketFactory factory; |
---|
854 | |
---|
855 | public DummySSLSocketFactory() { |
---|
856 | try { |
---|
857 | SSLContext sslcontent = SSLContext.getInstance("TLS"); |
---|
858 | sslcontent.init(null, // KeyManager not required |
---|
859 | new TrustManager[] {new DummyTrustManager()}, |
---|
860 | new java.security.SecureRandom()); |
---|
861 | factory = sslcontent.getSocketFactory(); |
---|
862 | } |
---|
863 | catch (NoSuchAlgorithmException e) { |
---|
864 | e.printStackTrace(); |
---|
865 | } |
---|
866 | catch (KeyManagementException e) { |
---|
867 | e.printStackTrace(); |
---|
868 | } |
---|
869 | } |
---|
870 | |
---|
871 | |
---|
872 | public Socket createSocket(Socket socket, String s, int i,boolean flag) throws IOException |
---|
873 | { |
---|
874 | |
---|
875 | return factory.createSocket(socket, s, i, flag); |
---|
876 | } |
---|
877 | |
---|
878 | public Socket createSocket(InetAddress inaddr, int i,InetAddress inaddr2, int j) throws IOException |
---|
879 | { |
---|
880 | return factory.createSocket(inaddr, i, inaddr2, j); |
---|
881 | } |
---|
882 | |
---|
883 | public Socket createSocket(InetAddress inaddr, int i) throws IOException |
---|
884 | { |
---|
885 | return factory.createSocket(inaddr, i); |
---|
886 | } |
---|
887 | |
---|
888 | public Socket createSocket(String s, int i, InetAddress inaddr, int j) throws IOException |
---|
889 | { |
---|
890 | return factory.createSocket(s, i, inaddr, j); |
---|
891 | } |
---|
892 | |
---|
893 | public Socket createSocket(String s, int i) throws IOException |
---|
894 | { |
---|
895 | /* |
---|
896 | * register a callback for handshaking completion event |
---|
897 | */ |
---|
898 | |
---|
899 | Socket so = factory.createSocket(s, i); |
---|
900 | ((SSLSocket)so).addHandshakeCompletedListener( |
---|
901 | new HandshakeCompletedListener() { |
---|
902 | public void handshakeCompleted( |
---|
903 | HandshakeCompletedEvent event) { |
---|
904 | System.out.println("Handshake finished!"); |
---|
905 | System.out.println( |
---|
906 | "\t CipherSuite:" + event.getCipherSuite()); |
---|
907 | System.out.println( |
---|
908 | "\t SessionId " + event.getSession()); |
---|
909 | System.out.println( |
---|
910 | "\t PeerHost " + event.getSession().getPeerHost()); |
---|
911 | } |
---|
912 | } |
---|
913 | ); |
---|
914 | return so; |
---|
915 | } |
---|
916 | |
---|
917 | public String[] getDefaultCipherSuites() { |
---|
918 | return factory.getSupportedCipherSuites(); |
---|
919 | } |
---|
920 | |
---|
921 | public String[] getSupportedCipherSuites() { |
---|
922 | return factory.getSupportedCipherSuites(); |
---|
923 | } |
---|
924 | } |
---|
925 | |
---|
926 | /** |
---|
927 | * Trust manager which accepts certificates without any validation |
---|
928 | * except date validation. |
---|
929 | */ |
---|
930 | private static class DummyTrustManager implements X509TrustManager { |
---|
931 | |
---|
932 | public void checkClientTrusted(X509Certificate[] chain, String authType) { } |
---|
933 | |
---|
934 | public void checkServerTrusted(X509Certificate[] chain, String authType) { |
---|
935 | try { |
---|
936 | chain[0].checkValidity(); |
---|
937 | } |
---|
938 | catch (CertificateExpiredException e) { |
---|
939 | } |
---|
940 | catch (CertificateNotYetValidException e) { |
---|
941 | } |
---|
942 | } |
---|
943 | |
---|
944 | public X509Certificate[] getAcceptedIssuers() { |
---|
945 | return new X509Certificate[0]; |
---|
946 | } |
---|
947 | } |
---|
948 | } |
---|
949 | |
---|
950 | |
---|
951 | /* |
---|
952 | * Overrides for emacs |
---|
953 | * Local variables: |
---|
954 | * tab-width: 4 |
---|
955 | * End: |
---|
956 | */ |
---|