Ignore:
Timestamp:
09/21/09 14:39:41 (15 years ago)
Author:
alexandrecorreia
Message:

Ticket #588 - Conexão através do proxy ( Squid ) , utilizando mode Digest e Basic.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/jabberit_messenger/java_source/src/nu/fw/jeti/backend/Connect.java

    r1014 r1424  
    2323package nu.fw.jeti.backend; 
    2424 
     25import java.awt.Color; 
     26import java.awt.Dimension; 
     27import java.awt.Font; 
     28import java.awt.GridBagConstraints; 
     29import java.awt.GridBagLayout; 
     30import java.awt.Insets; 
     31import java.awt.event.ActionEvent; 
     32import java.io.BufferedReader; 
    2533import java.io.IOException; 
    2634import java.io.InputStream; 
     35import java.io.InputStreamReader; 
    2736import java.io.OutputStream; 
     37import java.io.PrintWriter; 
    2838import java.io.UnsupportedEncodingException; 
    2939import java.net.InetAddress; 
     
    4656import javax.net.ssl.TrustManager; 
    4757import javax.net.ssl.X509TrustManager; 
     58import javax.swing.JButton; 
     59import javax.swing.JDialog; 
     60import javax.swing.JLabel; 
    4861import javax.swing.JOptionPane; 
     62import javax.swing.JPanel; 
     63import javax.swing.JPasswordField; 
     64import javax.swing.JTextField; 
     65import javax.swing.border.EmptyBorder; 
    4966 
    5067import nu.fw.jeti.events.DiscoveryListener; 
     
    6885import nu.fw.jeti.plugins.PluginsInfo; 
    6986import nu.fw.jeti.plugins.XMPP; 
     87import nu.fw.jeti.util.Digest; 
    7088import nu.fw.jeti.util.I18N; 
    7189import nu.fw.jeti.util.Log; 
     
    7997//TODO improve error reporting, make it translatable 
    8098 //class voor connectie 
    81  public class Connect implements ConnectionPacketReceiver 
     99 
     100public class Connect implements ConnectionPacketReceiver 
    82101{ 
    83102        private Output output; 
     
    102121        private Handlers handlers; 
    103122        private XMPP xmpp; 
     123         
     124        // windowProxy 
     125        private JButton buttonCancel; 
     126        private JButton buttonOk; 
     127        private JDialog dialog; 
     128        private JLabel infoLabel; 
     129        private JLabel lblUsername; 
     130        private JLabel lblPassword; 
     131        private JPanel buttonPanel; 
     132        private JPanel panelMain; 
     133        private JPasswordField txtPassword; 
     134        private JOptionPane optionPane; 
     135        private JTextField txtUsername; 
    104136     
    105137        public Connect(Backend backend,IQTimerQueue timerQueue,Handlers handlers) 
     
    164196    public void login(LoginInfo info) 
    165197    { 
    166         abort = false; 
    167                 loginInfo = info; 
    168                 connectThread = new Thread() 
    169                 { 
    170                         public void run() 
    171                         { 
    172                                 connect(); 
    173                         } 
    174                 }; 
    175                 connectThread.start();  
     198        autoLogin(info,2); 
    176199    } 
    177200     
    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                 } 
     201    public void autoLogin(LoginInfo info,final int tries) 
     202        { 
    184203                abort = false; 
    185204                loginInfo = info; 
     
    187206                { 
    188207                        int tel = 0; 
     208                         
    189209                        public void run() 
    190210                        { 
    191                                 boolean connected=false; 
    192                                 while(tel < tries) 
     211                                boolean connected = false; 
     212                                int triesc      = 0;  
     213                                int posIn       = 0; 
     214                                int posFi       = 0; 
     215                                 
     216                                String hosts = loginInfo.getHost(); 
     217                                 
     218                                if( hosts.indexOf(" ") != -1 ) 
    193219                                { 
    194                                         connected = connect(); 
    195                                         if(connected) break; 
    196                                         try 
     220                                        hosts = hosts + " "; 
     221                                         
     222                                        for( int i = 0; i < hosts.length(); i++ ) 
    197223                                        { 
    198                                                 Thread.sleep(60000); 
     224                                                String w = String.valueOf(hosts.charAt(i)); 
     225                                                 
     226                                                if( w.equals(" ") ) 
     227                                                { 
     228                                                        posIn = posFi; 
     229                                                        posFi = i; 
     230                                                         
     231                                                        String hostConnect = hosts.substring(posIn, posFi).trim(); 
     232                                                         
     233                                                        while( tel < tries ) 
     234                                                        { 
     235                                                                if( tel == 0) 
     236                                                                { 
     237                                                                        connected = connect( hostConnect, false ); 
     238                                                                } 
     239                                                                else 
     240                                                                { 
     241                                                                        connected = connect( hostConnect, true ); 
     242                                                                } 
     243 
     244                                                                if( connected ) 
     245                                                                { 
     246                                                                        i = hosts.length(); 
     247                                                                        break; 
     248                                                                }        
     249                                                                 
     250                                                                try { Thread.sleep(600);} 
     251                                                                catch (InterruptedException e){} 
     252                                                                tel++; 
     253                                                        }        
     254                                                         
     255                                                                 
     256                                                        tel = 0; 
     257                                                } 
    199258                                        } 
    200                                         catch (InterruptedException e){} 
    201                                         tel++; 
    202259                                } 
     260                                else 
     261                                { 
     262                                         
     263                                        while( tel < tries ) 
     264                                        { 
     265                                                if( tel == 0) 
     266                                                { 
     267                                                        connected = connect( hosts, false ); 
     268                                                } 
     269                                                else 
     270                                                { 
     271                                                        connected = connect( hosts, true ); 
     272                                                } 
     273 
     274                                                if( connected ) 
     275                                                { 
     276                                                        break; 
     277                                                }        
     278                                                 
     279                                                try { Thread.sleep(600);} 
     280                                                catch (InterruptedException e){} 
     281                                                tel++; 
     282                                        }        
     283                                } 
     284 
     285                                if( !connected ) 
     286                                { 
     287                                        interrupt(); 
     288                                loginInfo.setProxyUsername(null); 
     289                                loginInfo.setProxyPassword(null); 
     290                                        System.out.println("***** Login Failed *****");                                  
     291                                } 
    203292                        } 
    204293                }; 
    205                 connectThread.start();  
     294                 
     295                connectThread.start(); 
    206296        } 
    207297     
     
    209299    { 
    210300                abort = true; 
    211                 if(socket != null) 
     301                 
     302                if( socket != null ) 
    212303                { 
    213304                        try 
     
    229320    } 
    230321 
    231         synchronized private boolean connect() 
     322        synchronized private boolean connect( String host, boolean proxy ) 
    232323        {        
    233324                latestConnected = System.currentTimeMillis(); 
     
    238329                if( authenticated ) 
    239330                        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                 } 
    249331                 
    250332                jabberHandler = new JabberHandler(this,handlers); 
     
    258340                try 
    259341                {                
    260             host = loginInfo.getHost(); 
    261             if (host == null || host.length() == 0) 
     342             
     343                        if (host == null || host.length() == 0) 
    262344            { 
    263345                if(loginInfo.useProxy(LoginInfo.NO_PROXY) && PluginsInfo.isPluginLoaded("xmpp")) 
     
    268350            } 
    269351                        
    270                         if(loginInfo.isSSl())  
     352                        if( loginInfo.isSSl() )  
    271353                { 
    272                                 if( loginInfo.isHTTPProxy() && loginInfo.getJavaProxy() )  
    273                         { 
    274                                         Socket tunnel = createHTTPTunel(host);  
    275                                 socket = new DummySSLSocketFactory().createSocket(tunnel,host,loginInfo.getPort(),true); 
     354                                if( proxy ) 
     355                                { 
     356                                        //System.out.println("CONNECT COM PROXY - SEGURA : " + host); 
     357                                        Socket tunnel = createHTTPTunel(host);  
     358                                socket = new DummySSLSocketFactory().createSocket(tunnel, host, loginInfo.getPort(),true); 
    276359                        } 
    277360                                else 
     361                                { 
     362                                        //System.out.println("CONNECT SEM PROXY - SEGURA : " + host); 
    278363                                        socket = new DummySSLSocketFactory().createSocket(host,loginInfo.getPort()); 
     364                                } 
    279365                } 
    280366                else  
    281367                { 
    282                         socket = new Socket(host,loginInfo.getPort()); 
     368                        if( proxy ) 
     369                        { 
     370                                //System.out.println("CONNECT COM PROXY - NÃO SEGURA : " + host); 
     371                                socket = createHTTPTunel(host); 
     372                        } 
     373                        else 
     374                        { 
     375                                //System.out.println("CONNECT SEM PROXY - NÃO SEGURA : " + host); 
     376                                socket = new Socket( host,loginInfo.getPort() ); 
     377                        } 
    283378                } 
    284379        } 
    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         } 
     380                catch (UnknownHostException ex){ return false; } 
     381                catch (IOException ex){ return false; } 
     382 
    295383                if( abort ) 
    296384                        return false; 
     
    298386                try 
    299387                { 
    300                         input = new Input(socket.getInputStream(),this,jabberHandler); 
     388                        input = new Input(socket.getInputStream(), this, jabberHandler); 
    301389        } 
    302                 catch (IOException ex) 
    303         { 
    304                         sendLoginError(I18N.gettext("main.loginstatus.Could_not_open_input_because") + " " + ex.getMessage()); 
    305                         return false; 
    306         } 
     390                catch (IOException ex){ return false; } 
    307391                 
    308392                if( abort ) 
     
    311395            try 
    312396            { 
    313                         output = new Output(socket,loginInfo.getServer(),this,socket.getOutputStream()); 
     397                        output = new Output(socket, loginInfo.getServer(), this, socket.getOutputStream()); 
    314398        } 
    315             catch (IOException ex) 
    316         { 
    317                         sendLoginError(I18N.gettext("main.loginstatus.Could_not_open_output_because") + " " +ex.getMessage()); 
     399            catch (IOException ex){ return false; } 
     400                 
     401        return true; 
     402        } 
     403         
     404        String host; 
     405         
     406        public boolean startTls(ConnectionPacketReceiver cpr) 
     407        { 
     408                if(abort) 
    318409                        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{             
     410                 
     411                try 
     412                {                
    330413           //TODO stop output 
    331414                        output.disconnect(false); 
     
    334417            jabberHandler = new JabberHandler(cpr,handlers); 
    335418           
    336              
    337             try{ 
     419            try 
     420            { 
    338421                    input = new Input(socket.getInputStream(),this,jabberHandler); 
    339                 }catch (IOException ex) 
     422                } 
     423            catch (IOException ex) 
    340424                { 
    341                                 sendLoginError(I18N.gettext("main.loginstatus.Could_not_open_input_because") + " " + ex.getMessage()); 
    342425                                return false; 
    343426                } 
    344427                 
    345             try{ 
     428            try 
     429            { 
    346430                        output = new Output(socket,loginInfo.getServer(),this,socket.getOutputStream()); 
    347                 }catch (IOException ex) 
     431                } 
     432            catch (IOException ex) 
    348433                { 
    349                                 sendLoginError(I18N.gettext("main.loginstatus.Could_not_open_output_because") + " " +ex.getMessage()); 
     434                                //sendLoginError(I18N.gettext("main.loginstatus.Could_not_open_output_because") + " " +ex.getMessage()); 
    350435                                return false; 
    351436                } 
    352437                             
    353                 if(abort) return false; 
     438                if(abort) 
     439                        return false; 
    354440                
    355                 }catch (IOException e) {e.printStackTrace();} 
     441                } 
     442                catch(IOException e){} 
     443                 
    356444                return true; 
    357445        } 
    358446         
    359447        //used by compression plugin 
    360         public Socket getSocket(){ 
     448        public Socket getSocket() 
     449        { 
    361450                return socket; 
    362451        } 
     
    367456                if ( abort ) 
    368457                        return false; 
     458         
    369459                output.disconnect(false); 
    370460                Input input2 = input; 
     
    391481        } 
    392482         
    393          
    394483        public void startSasl(ConnectionPacketReceiver cpr) 
    395484        { 
     
    426515                String tunnelHost = loginInfo.getProxyServer(); 
    427516            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 
     517            String typeProxy = typeProxy(tunnelHost, tunnelPort, host, loginInfo.getPort()); 
     518 
     519                if( typeProxy.indexOf("Proxy-Authenticate: Digest") != -1 ) 
     520                { 
     521                        // Proxy Username and Password 
     522                        if( loginInfo.getProxyPassword() == null && loginInfo.getProxyPassword() == null) 
     523                                windowProxy(tunnelHost); 
     524 
     525                        return doTunnelHandshakeDigest(host, loginInfo.getPort()); 
     526                } 
     527                else if( typeProxy.indexOf("Proxy-Authenticate: Basic") != -1 ) 
     528                { 
     529                        // Proxy Username and Password 
     530                        if( loginInfo.getProxyPassword() == null && loginInfo.getProxyPassword() == null) 
     531                                windowProxy(tunnelHost); 
     532 
     533                        Socket tunnel = new Socket(tunnelHost, tunnelPort); 
     534                        doTunnelHandshake(tunnel, host, loginInfo.getPort()); 
     535                        return tunnel; 
     536                } 
     537                else if( typeProxy.indexOf("HTTP/1.0 200") != -1 ) 
     538                { 
     539                        Socket tunnel = new Socket(tunnelHost, tunnelPort); 
     540                        PrintWriter writeConnection = new PrintWriter( tunnel.getOutputStream(), true );  
     541                        BufferedReader readConnection = new BufferedReader(new InputStreamReader(tunnel.getInputStream())); 
     542 
     543                        String msg = "CONNECT " + host + ":" + loginInfo.getPort() + " HTTP/1.1\r\n" + 
     544                                                 "User-Agent: " + sun.net.www.protocol.http.HttpURLConnection.userAgent + "\r\n" +  
     545                                                 "Proxy-Connection: Keep-Alive\r\n"; 
     546                         
     547                        writeConnection.println(msg); 
     548                        String line = ""; 
     549                         
     550                        while((line = readConnection.readLine()) != null ) 
     551                        { 
     552                                if( line.indexOf("HTTP/1.0 200") != -1 ) 
     553                                        break; 
     554                        } 
     555                         
     556                        return tunnel; 
     557                } 
     558 
     559                return new Socket(); 
     560        } 
     561 
     562        private void windowProxy(String tunnelHost) 
     563        { 
     564                panelMain = new JPanel(); 
     565                panelMain.setBackground(Color.ORANGE); 
     566                panelMain.setPreferredSize(new Dimension(400,120)); 
     567                panelMain.setLayout(new GridBagLayout()); 
     568                GridBagConstraints gbc = new GridBagConstraints(); 
     569 
     570                // Espaco entre elementos; 
     571                gbc.insets = new Insets(2, 2, 2, 2); 
     572            gbc.gridy = 0; // row 
     573            gbc.gridx = 0; // col 
     574            gbc.gridwidth = 2;  
     575            gbc.anchor = GridBagConstraints.WEST; 
     576             
     577            infoLabel = new JLabel(I18N.gettext("main.connect.proxy.Authentication_for") + " " + tunnelHost + " :"); 
     578            infoLabel.setFont(new Font("SansSerif",Font.BOLD, 14)); 
     579            infoLabel.setBorder(new EmptyBorder(0, 0, 5, 0)); 
     580            panelMain.add(infoLabel, gbc); 
     581 
     582            // Label Username 
     583            gbc.gridy = 1; // row 
     584            gbc.gridx = 0; // col 
     585            gbc.gridwidth = 1; 
     586            lblUsername = new JLabel(I18N.gettext("main.connect.proxy.Username")+":"); 
     587            panelMain.add(lblUsername, gbc); 
     588 
     589            // JTextField do Username 
     590            gbc.gridy = 1; // row 
     591            gbc.gridx = 1; // col 
     592            txtUsername = new JTextField(20); 
     593            panelMain.add(txtUsername, gbc); 
     594 
     595            // Label Password 
     596            gbc.gridy = 2; // row 
     597            gbc.gridx = 0; // col 
     598            lblPassword = new JLabel(I18N.gettext("main.connect.proxy.Password")+":"); 
     599            panelMain.add(lblPassword, gbc);     
     600 
     601            // JPasswordField                
     602            gbc.gridy = 2; // row 
     603            gbc.gridx = 1; // col 
     604            txtPassword = new JPasswordField(20); 
     605            panelMain.add(txtPassword, gbc); 
     606 
     607            // Buttons 
     608            gbc.gridy = 3; // linha 
     609            gbc.gridx = 1; // coluna 
     610            gbc.gridwidth = 2; // duas células na linha 
     611            gbc.anchor = GridBagConstraints.WEST; 
     612            buttonPanel = new JPanel(); 
     613            buttonCancel = new JButton(I18N.gettext("main.connect.proxy.Cancel")); 
     614            buttonOk = new JButton(I18N.gettext("main.connect.proxy.Ok")); 
     615             
     616            // Event click Ok 
     617                buttonOk.addActionListener(new java.awt.event.ActionListener() 
     618                { 
     619            public void actionPerformed(ActionEvent e) 
     620            { 
     621                String Username = txtUsername.getText().toString(); 
     622                String Password = txtPassword.getText().toString(); 
     623                 
     624                if( !Username.trim().equals("") && !Password.trim().equals("") ) 
     625                {         
     626                        loginInfo.setProxyUsername(Username); 
     627                        loginInfo.setProxyPassword(Password); 
     628                        dialog.setVisible(false); 
     629                } 
     630            } 
     631                }); 
     632 
     633            // Event click Cancel 
     634                buttonCancel.addActionListener(new java.awt.event.ActionListener() 
     635                { 
     636            public void actionPerformed(ActionEvent e) 
     637            { 
     638                        loginInfo.setProxyUsername(null); 
     639                        loginInfo.setProxyPassword(null); 
     640                dialog.setVisible(false); 
     641            } 
     642                }); 
     643                 
     644                buttonPanel.setBackground(Color.ORANGE); 
     645                buttonPanel.add(buttonOk); 
     646                buttonPanel.add(buttonCancel); 
     647            panelMain.add(buttonPanel, gbc); 
     648                 
     649            optionPane = new JOptionPane(); 
     650            optionPane.setMessage(I18N.gettext("main.connect.proxy.The_proxy_is_requesting_a_username_and_password") + "!"); 
     651            optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE); 
     652            optionPane.setOptions(new Object[] {panelMain}); 
     653            dialog = optionPane.createDialog(null, I18N.gettext("main.connect.proxy.Authentication_required")); 
     654            dialog.setVisible(true); 
     655        } 
     656         
     657        private String typeProxy(String proxyServer, int proxyPort, String host, int port) throws IOException 
     658        { 
     659                Socket connection = new Socket(proxyServer, proxyPort); 
     660                PrintWriter writeConnection = new PrintWriter( connection.getOutputStream(), true );  
     661                BufferedReader readConnection = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
     662                String _return = ""; 
     663                String line = ""; 
     664                int count = 0; 
     665                 
     666                String msg = "CONNECT " + host + ":" + port + " HTTP/1.1\r\n" + 
     667                                         "User-Agent: " + sun.net.www.protocol.http.HttpURLConnection.userAgent + "\r\n" +  
     668                                         "Proxy-Connection: Keep-Alive\r\n"; 
     669 
     670                writeConnection.println(msg); 
     671                 
     672                while((line = readConnection.readLine()) != null ) 
     673                { 
     674                        if( line.indexOf("Proxy") != -1 ) 
     675                                _return += line + "\n"; 
     676                         
     677                        if( line.indexOf("Proxy-Connection: close") != -1 || count == 12 ) 
     678                                break; 
     679 
     680                        if( line.indexOf("HTTP/1.0 200") != -1 ) 
     681                        { 
     682                                _return += line + "\n"; 
     683                                break; 
     684                        } 
     685                        count++; 
     686                } 
     687                 
     688                 
     689                writeConnection.close(); 
     690                readConnection.close(); 
     691                connection.close(); 
     692 
     693                return _return; 
     694        } 
     695 
     696        // Proxy Authorization: Basic    
     697        private void doTunnelHandshake(Socket tunnel, String host, int port) throws IOException 
    434698        { 
    435699                OutputStream out = tunnel.getOutputStream(); 
     700                 
    436701                String msg = "CONNECT " + host + ":" + port + " HTTP/1.0\n" 
    437702                                + "User-Agent: " 
    438703                                + 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; 
     704                 
     705                if ( loginInfo.getProxyUsername() != null && loginInfo.getProxyPassword() != null ) 
     706                { 
     707                        //add basic authentication header for the proxy 
     708                        sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder(); 
     709                        String encodedPassword = enc.encode((loginInfo.getProxyUsername() + ":" + loginInfo.getProxyPassword()).getBytes()); 
     710                        msg = msg + "\nProxy-Authorization: Basic " + encodedPassword; 
    445711            } 
    446             msg = msg + "\nContent-Length: 0" 
    447                                 +"\nPragma: no-cache" 
    448                                         +"\r\n\r\n"; 
     712                 
     713                msg = msg + "\nContent-Length: 0" 
     714                                  + "\nPragma: no-cache" 
     715                          + "\r\n\r\n"; 
     716 
    449717                byte b[]; 
    450718                try 
     
    455723                         */ 
    456724                        b = msg.getBytes("ASCII7"); 
    457                 } catch (UnsupportedEncodingException ignored) 
     725                }  
     726                catch (UnsupportedEncodingException ignored) 
    458727                { 
    459728                        /* 
     
    503772                { 
    504773                        replyStr = new String(reply, 0, replyLen, "ASCII7"); 
    505                 } catch (UnsupportedEncodingException ignored) 
     774                } 
     775                catch (UnsupportedEncodingException ignored) 
    506776                { 
    507777                        replyStr = new String(reply, 0, replyLen); 
     
    512782                 * HTTP/1.1 instead of 1.0 
    513783                 */ 
     784                 
    514785                // if (!replyStr.startsWith("HTTP/1.0 200")) { 
    515786                if (replyStr.toLowerCase().indexOf("200 connection established") == -1) 
     
    518789                        String tunnelPort = System.getProperty("http.proxyPort"); 
    519790                        throw new IOException("Unable to tunnel through " + tunnelHost 
    520                                         + ":" + tunnelPort + ".  Proxy returns \"" + replyStr 
    521                                         + "\""); 
    522                 } 
     791                                        + ":" + tunnelPort + ".  Proxy returns \"" + replyStr + "\""); 
     792                } 
     793                 
    523794                /* tunneling Handshake was successful! */ 
    524795        } 
    525796 
     797        //Proxy-Authenticate Digest 
     798        private Socket doTunnelHandshakeDigest(String host, int port) throws IOException 
     799        { 
     800                Digest hashProxy        = new Digest(); 
     801                String response         = ""; 
     802                String nonce            = ""; 
     803                String cnonce           = "ALEC290908" + String.valueOf(System.currentTimeMillis()); 
     804                String qop                      = ""; 
     805                String uri                      = "/"; 
     806                String username         = loginInfo.getProxyUsername(); 
     807                String password         = loginInfo.getProxyPassword(); 
     808                String realm            = ""; 
     809                String line                     = ""; 
     810 
     811                // Dados Proxy 
     812                String proxyServer = loginInfo.getProxyServer(); 
     813                int proxyPort = Integer.valueOf(loginInfo.getProxyPort()).intValue(); 
     814 
     815                // PRIMEIRA PARTE 
     816                Socket tunnel = new Socket(proxyServer, proxyPort); 
     817                PrintWriter writeTunnel = new PrintWriter(tunnel.getOutputStream(), true); 
     818                BufferedReader readTunnel = new BufferedReader(new InputStreamReader(tunnel.getInputStream()));          
     819 
     820                String msg = "CONNECT " + host + ":" + port + " HTTP/1.1\r\n" + 
     821                                         "User-Agent: " + sun.net.www.protocol.http.HttpURLConnection.userAgent + "\r\n" + 
     822                                         "Proxy-Connection: Keep-Alive\r\n"; 
     823 
     824                writeTunnel.println(msg); 
     825                 
     826                while((line = readTunnel.readLine()) != null ) 
     827                { 
     828                        // Get nonce; 
     829                        if ( line.indexOf("nonce=\"") != -1 ) 
     830                        { 
     831                        nonce = line.substring(line.indexOf("nonce=\"") + 7); 
     832                        nonce = nonce.substring(0,nonce.indexOf("\",")); 
     833                        }        
     834 
     835                        // Get realm; 
     836                        if ( line.indexOf("realm=\"") != -1 ) 
     837                        { 
     838                        realm = line.substring(line.indexOf("realm=\"") + 7); 
     839                        realm = realm.substring(0,realm.indexOf("\",")); 
     840                        } 
     841                         
     842                        // Get qop; 
     843                        if ( line.indexOf("qop=\"") != -1 ) 
     844                        { 
     845                                qop = line.substring(line.indexOf("qop=\"") + 5); 
     846                                qop = qop.substring(0,qop.indexOf("\",")); 
     847                        } 
     848                } 
     849 
     850                writeTunnel.close(); 
     851                readTunnel.close(); 
     852                tunnel.close(); 
     853                 
     854                response = hashProxy.hashMD5(nonce, cnonce, qop, "CONNECT:"+uri, username, password, realm); 
     855                line = ""; 
     856                 
     857                // SEGUNDA PARTE 
     858                tunnel          = new Socket(proxyServer, proxyPort); 
     859                writeTunnel     = new PrintWriter(tunnel.getOutputStream(), true); 
     860                readTunnel      = new BufferedReader(new InputStreamReader(tunnel.getInputStream()));            
     861 
     862                msg = "CONNECT " + host + ":" + port + " HTTP/1.1\r\n" + 
     863                          "Proxy-Authorization: Digest username=\""+username+"\", "+ 
     864                      "realm=\""+realm+"\", nonce=\""+nonce+"\", uri=\""+uri+"\", " + 
     865                      "cnonce=\""+cnonce+"\", nc=00000001, qop=\""+qop+"\", response=\""+response+"\"\r\n" + 
     866                          "User-Agent: " + sun.net.www.protocol.http.HttpURLConnection.userAgent + "\r\n" + 
     867                      "Proxy-Connection: Keep-Alive\r\n"; 
     868 
     869                int count = 0; 
     870                writeTunnel.println(msg); 
     871                 
     872                while((line = readTunnel.readLine().trim()) != null ) 
     873                { 
     874                        if( line.indexOf("HTTP/1.0 200 OK") != -1 ) 
     875                                break; 
     876                         
     877                        if( line.indexOf("HTTP/1.0 503") != -1 ) 
     878                                break; 
     879                         
     880                        if( count == 1 ) 
     881                                break; 
     882                         
     883                        count++; 
     884                } 
     885                 
     886                /* tunneling Handshake was successful! */                
     887                return tunnel; 
     888        } 
     889         
     890        private void sendLoginMessage(String message) 
     891        { 
     892                for(Iterator j = backend.getListeners(LoginListener.class);j.hasNext();) 
     893                { 
     894                        ((LoginListener)j.next()).loginMessage(message); 
     895                } 
     896        } 
     897         
     898        private void sendLoginStatus(int count) 
     899        { 
     900                /*for(Iterator j = backend.getListeners(LoginListener.class);j.hasNext();) 
     901                { 
     902                        ((LoginListener)j.next()).loginStatus(count); 
     903                }*/ 
     904        } 
     905         
    526906        public void sendLoginError(String message) 
    527907        { 
     
    6231003                                return; 
    6241004                        } 
    625 //                      TODO remove lowercase if filetransfer bug fixed 
     1005                        //TODO remove lowercase if filetransfer bug fixed 
    6261006                        JID jid = new JID(loginInfo.getUsername().toLowerCase(),loginInfo.getServer() ,loginInfo.getResource()); 
    6271007                        authenticated(jid); 
     
    6371017        reconnecting = false; 
    6381018                output.setAuthenticated();  
    639                 //output.send(new InfoQuery(new JID(server),"get",new IQBrowse())); 
    640                 //browse(new JID(loginInfo.getServer()),null); 
     1019 
    6411020                //TODO increase timeout 
    6421021                getItems(new JID(loginInfo.getServer()), new DiscoveryListener() 
    6431022                { 
    6441023                        public void discoveryItemResult(JID jid, DiscoveryItem item) 
    645                         {//cache disco items for this server 
     1024                        { 
     1025                                //cache disco items for this server 
    6461026                                if (item.hasItems()) 
    6471027                                { 
     
    6601040                { 
    6611041                        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          
     1042                } 
     1043                latestConnected = System.currentTimeMillis(); 
     1044                 
     1045        } 
    6731046         
    6741047        public void connected() 
     
    7031076        public void inputDeath() 
    7041077        { 
    705                 //if(true)return; something todo with xmpp plugin, should not be here????? or server deads will not be detected 
    7061078                try 
    7071079                { 
     
    7291101                        } 
    7301102                } 
    731                 sendLoginError(I18N.gettext("main.loginstatus.Lost_Input")); 
    732                 //else 
    733                 //io error while logging in 
    734                 //do something usefull 
     1103                 
    7351104        } 
    7361105 
     
    8531222                private SSLSocketFactory factory; 
    8541223                 
    855                 public DummySSLSocketFactory() { 
    856                         try { 
     1224                public DummySSLSocketFactory() 
     1225                { 
     1226                        try 
     1227                        { 
    8571228                                SSLContext sslcontent = SSLContext.getInstance("TLS"); 
    8581229                                sslcontent.init(null, // KeyManager not required 
     
    8611232                                factory = sslcontent.getSocketFactory(); 
    8621233                        } 
    863                         catch (NoSuchAlgorithmException e) { 
     1234                        catch (NoSuchAlgorithmException e) 
     1235                        { 
    8641236                                e.printStackTrace(); 
    8651237                        } 
    866                         catch (KeyManagementException e) { 
     1238                        catch (KeyManagementException e) 
     1239                        { 
    8671240                                e.printStackTrace(); 
    8681241                        } 
    8691242                } 
    8701243         
    871  
    8721244                public Socket createSocket(Socket socket, String s, int i,boolean flag) throws IOException 
    8731245                { 
    874                          
    8751246                        return factory.createSocket(socket, s, i, flag); 
    8761247                } 
     
    8931264                public Socket createSocket(String s, int i)     throws IOException 
    8941265                { 
    895                 /* 
    896                 * register a callback for handshaking completion event 
    897                 */ 
     1266                   
     1267                  /** 
     1268               * register a callback for handshaking completion event 
     1269               */ 
    8981270                         
    8991271                        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                  ); 
     1272                ((SSLSocket)so).addHandshakeCompletedListener(new HandshakeCompletedListener() 
     1273                                { 
     1274                                        public void handshakeCompleted(HandshakeCompletedEvent event) 
     1275                                        { 
     1276                                                System.out.println("** Handshake finished! **"); 
     1277                                                //System.out.println("\t CipherSuite:" + event.getCipherSuite()); 
     1278                                                //System.out.println("\t SessionId " + event.getSession()); 
     1279                                                //System.out.println("\t PeerHost " + event.getSession().getPeerHost()); 
     1280                                        } 
     1281                                } 
     1282                ); 
     1283                 
    9141284                return so; 
    9151285                } 
    9161286                 
    917                 public String[] getDefaultCipherSuites() { 
     1287                public String[] getDefaultCipherSuites() 
     1288                { 
    9181289                        return factory.getSupportedCipherSuites(); 
    9191290                } 
    9201291 
    921                 public String[] getSupportedCipherSuites() { 
     1292                public String[] getSupportedCipherSuites() 
     1293                { 
    9221294                        return factory.getSupportedCipherSuites(); 
    9231295                } 
     
    9281300         * except date validation. 
    9291301         */ 
    930         private static class DummyTrustManager implements X509TrustManager { 
     1302        private static class DummyTrustManager implements X509TrustManager 
     1303        { 
    9311304 
    9321305                public void checkClientTrusted(X509Certificate[] chain, String authType) {      } 
    9331306 
    934                 public void checkServerTrusted(X509Certificate[] chain, String authType)  { 
    935                         try { 
     1307                public void checkServerTrusted(X509Certificate[] chain, String authType) 
     1308                { 
     1309                        try 
     1310                        { 
    9361311                                chain[0].checkValidity(); 
    9371312                        } 
    938                         catch (CertificateExpiredException e) { 
    939                         } 
    940                         catch (CertificateNotYetValidException e) { 
    941                         } 
    942                 } 
    943  
    944                 public X509Certificate[] getAcceptedIssuers() { 
     1313                        catch (CertificateExpiredException e){} 
     1314                        catch (CertificateNotYetValidException e){} 
     1315                } 
     1316 
     1317                public X509Certificate[] getAcceptedIssuers() 
     1318                { 
    9451319                        return new X509Certificate[0]; 
    9461320                } 
Note: See TracChangeset for help on using the changeset viewer.