1 16 package net.sf.jftp.gui.hostchooser; 17 18 import java.awt.Cursor ; 19 import java.awt.GridLayout ; 20 import java.awt.Insets ; 21 import java.awt.event.ActionEvent ; 22 import java.awt.event.ActionListener ; 23 import java.awt.event.ComponentEvent ; 24 import java.awt.event.ComponentListener ; 25 import java.awt.event.WindowEvent ; 26 import java.awt.event.WindowListener ; 27 import java.io.File ; 28 import java.io.IOException ; 29 30 import javax.swing.JCheckBox ; 31 import javax.swing.JComboBox ; 32 import javax.swing.JDialog ; 33 import javax.swing.JFileChooser ; 34 import javax.swing.JLabel ; 35 import javax.swing.event.ChangeEvent ; 36 import javax.swing.event.ChangeListener ; 37 38 import net.sf.jftp.JFtp; 39 import net.sf.jftp.config.LoadSet; 40 import net.sf.jftp.config.SaveSet; 41 import net.sf.jftp.config.Settings; 42 import net.sf.jftp.gui.framework.HButton; 43 import net.sf.jftp.gui.framework.HFrame; 44 import net.sf.jftp.gui.framework.HPanel; 45 import net.sf.jftp.gui.framework.HPasswordField; 46 import net.sf.jftp.gui.framework.HTextField; 47 import net.sf.jftp.net.Sftp2Connection; 48 import net.sf.jftp.net.SftpConnection; 49 import net.sf.jftp.net.StartConnection; 50 import net.sf.jftp.system.logging.Log; 51 import net.sf.jftp.tools.SshShell; 52 53 54 public class SftpHostChooser extends HFrame implements ActionListener , 55 WindowListener , ChangeListener 56 { 57 public HTextField host = new HTextField("Host:", "localhost"); 58 public HTextField user = new HTextField("Username:", "guest"); 59 public HTextField port = new HTextField("Port:", "22"); 60 public HPasswordField pass = new HPasswordField("Password/Phrase:", 61 "nopasswd"); 62 public JComboBox enc = new JComboBox (); 63 public JComboBox cs = new JComboBox (); 64 public JComboBox keys = new JComboBox (); 65 public JLabel encL = new JLabel ("Pref. Encryption"); 66 public JLabel csL = new JLabel ("Pref. Message Auth."); 67 public JLabel keysL = new JLabel ("Pref. Public Key"); 68 public JLabel keyfileL = new JLabel ("(No File)"); 69 private HPanel okP = new HPanel(); 70 private HPanel keyP = new HPanel(); 71 private HButton ok = new HButton("Connect"); 72 private HButton keyfile = new HButton("Choose Key File"); 73 private ComponentListener listener = null; 74 private boolean useLocal = false; 75 private boolean shell = false; 76 private String keyfileName = null; 77 private JCheckBox useJSch = new JCheckBox ("Use JSch instead of j2ssh"); 78 79 public SftpHostChooser(ComponentListener l, boolean local) 80 { 81 listener = l; 82 useLocal = local; 83 init(); 84 } 85 86 public SftpHostChooser(ComponentListener l) 87 { 88 listener = l; 89 init(); 90 } 91 92 public SftpHostChooser() 93 { 94 init(); 95 } 96 97 public SftpHostChooser(boolean shell) 98 { 99 this.shell = shell; 100 init(); 101 } 102 103 public void init() 104 { 105 setLocation(100, 150); 107 setTitle("Sftp Connection..."); 108 setBackground(okP.getBackground()); 109 getContentPane().setLayout(new GridLayout (7, 2, 5, 3)); 110 111 try 113 { 114 File f = new File (Settings.appHomeDir); 115 f.mkdir(); 116 117 File f1 = new File (Settings.login); 118 f1.createNewFile(); 119 120 File f2 = new File (Settings.login_def_sftp); 121 f2.createNewFile(); 122 } 123 catch(IOException ex) 124 { 125 ex.printStackTrace(); 126 } 127 128 String [] login = LoadSet.loadSet(Settings.login_def_sftp); 129 130 if((login[0] != null) && (login.length > 1)) 131 { 132 host.setText(login[0]); 133 user.setText(login[1]); 134 } 135 136 143 if(Settings.getStorePasswords()) 144 { 145 if((login != null) && (login.length > 2) && (login[2] != null)) 146 { 147 pass.setText(login[2]); 148 } 149 } 150 else 151 { 152 pass.setText(""); 153 } 154 155 enc.addItem("3des-cbc"); 156 enc.addItem("blowfish-cbc"); 157 158 cs.addItem("hmac-sha1"); 159 cs.addItem("hmac-sha1-96"); 160 cs.addItem("hmac-md5"); 161 cs.addItem("hmac-md5-96"); 162 163 keys.addItem("ssh-rsa"); 164 keys.addItem("ssh-dss"); 165 166 getContentPane().add(host); 168 getContentPane().add(port); 169 getContentPane().add(user); 170 getContentPane().add(pass); 171 getContentPane().add(encL); 172 getContentPane().add(enc); 173 getContentPane().add(csL); 174 getContentPane().add(cs); 175 getContentPane().add(keysL); 176 getContentPane().add(keys); 177 getContentPane().add(keyP); 178 getContentPane().add(new JLabel ("Keyfiles are usually located ~/.ssh/ on UNIX")); 179 getContentPane().add(useJSch); 180 getContentPane().add(okP); 181 182 keyP.add(keyfileL); 183 keyP.add(keyfile); 184 okP.add(new JLabel (" ")); 185 okP.add(ok); 186 ok.addActionListener(this); 187 keyfile.addActionListener(this); 188 189 setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); 190 191 pass.text.addActionListener(this); 192 193 useJSch.addChangeListener(this); 194 195 pack(); 196 setModal(false); 197 setVisible(false); 198 addWindowListener(this); 199 } 200 201 public void stateChanged(ChangeEvent e) { 202 if(useJSch.isSelected()) { 203 enc.setEnabled(false); 204 cs.setEnabled(false); 205 keys.setEnabled(false); 206 } 207 else { 208 keyfile.setEnabled(true); 209 enc.setEnabled(true); 210 cs.setEnabled(true); 211 keys.setEnabled(true); 212 } 213 } 214 215 public void update() 216 { 217 fixLocation(); 218 setVisible(true); 219 toFront(); 220 host.requestFocus(); 221 } 222 223 public void setShell(boolean shell) 224 { 225 this.shell = shell; 226 } 227 228 public boolean getShell() 229 { 230 return shell; 231 } 232 233 public void actionPerformed(ActionEvent e) 234 { 235 if((e.getSource() == ok) || (e.getSource() == pass.text)) 236 { 237 setCursor(new Cursor (Cursor.WAIT_CURSOR)); 243 244 SftpConnection con = null; 245 246 String htmp = host.getText().trim(); 247 String utmp = user.getText().trim(); 248 String ptmp = pass.getText(); 249 250 int potmp = 22; 252 253 try 254 { 255 potmp = Integer.parseInt(port.getText()); 256 } 257 catch(Exception ex) 258 { 259 Log.debug("Error: Not a number!"); 260 } 261 262 String potmpString = new String ("" + potmp); 263 264 com.sshtools.j2ssh.configuration.SshConnectionProperties properties = new com.sshtools.j2ssh.configuration.SshConnectionProperties(); 265 properties.setHost(htmp); 266 properties.setPort(potmp); 268 properties.setPrefSCEncryption((String ) enc.getSelectedItem()); 269 properties.setPrefCSMac((String ) cs.getSelectedItem()); 270 properties.setPrefPublicKey((String ) keys.getSelectedItem()); 271 272 if(shell) 273 { 274 SshShell s = new SshShell(properties, utmp, ptmp, potmp); 275 setCursor(new Cursor (Cursor.DEFAULT_CURSOR)); 276 this.dispose(); 277 s.toFront(); 278 279 return; 280 } 281 else 282 { 283 try 284 { 285 boolean status; 286 287 SaveSet s = new SaveSet(Settings.login_def_sftp, htmp, 288 utmp, ptmp, potmpString, "null", "null"); 289 290 if(!useJSch.isSelected()) { 291 StartConnection.setSshProperties(properties); 292 StartConnection.setSshKeyfile(keyfileName); 293 status = StartConnection.startCon("SFTP", htmp, utmp, ptmp, 294 potmp, "", useLocal); 295 } 296 else { 297 Sftp2Connection con2 = new Sftp2Connection(htmp, ""+potmp, keyfileName); 298 299 if(con2.login(utmp, ptmp)) 300 { 301 if(useLocal) 302 { 303 JFtp.statusP.jftp.addLocalConnection(htmp, con2); 304 } 305 else 306 { 307 JFtp.statusP.jftp.addConnection(htmp, con2); 308 } 309 310 if(con2.chdir(con2.getPWD()) || con2.chdir("/")) 311 { 312 ; 313 } 314 } 315 } 316 } 317 catch(Exception ex) 318 { 319 ex.printStackTrace(); 320 Log.debug("Could not create SftpConnection, does this distribution come with j2ssh?"); 321 } 322 } 323 324 setCursor(new Cursor (Cursor.DEFAULT_CURSOR)); 325 this.dispose(); 326 JFtp.mainFrame.setVisible(true); 327 JFtp.mainFrame.toFront(); 328 329 if(listener != null) 330 { 331 listener.componentResized(new ComponentEvent (this, 0)); 332 } 333 } 334 else if(e.getSource() == keyfile) 335 { 336 JFileChooser chooser = new JFileChooser (); 337 int returnVal = chooser.showOpenDialog(this); 338 339 if(returnVal == JFileChooser.APPROVE_OPTION) 340 { 341 keyfileName = chooser.getSelectedFile().getPath(); 342 343 if(keyfileName != null) 344 { 345 keyfileL.setText("(File present)"); 346 } 347 } 348 else { 349 keyfileName = null; 350 351 if(keyfileName != null) 352 { 353 keyfileL.setText("(No File)"); 354 } 355 } 356 } 357 } 358 359 public void windowClosing(WindowEvent e) 360 { 361 this.dispose(); 363 } 364 365 public void windowClosed(WindowEvent e) 366 { 367 } 368 369 public void windowActivated(WindowEvent e) 370 { 371 } 372 373 public void windowDeactivated(WindowEvent e) 374 { 375 } 376 377 public void windowIconified(WindowEvent e) 378 { 379 } 380 381 public void windowDeiconified(WindowEvent e) 382 { 383 } 384 385 public void windowOpened(WindowEvent e) 386 { 387 } 388 389 public Insets getInsets() 390 { 391 Insets std = super.getInsets(); 392 393 return new Insets (std.top + 10, std.left + 10, std.bottom + 10, 394 std.right + 10); 395 } 396 397 public void pause(int time) 398 { 399 try 400 { 401 Thread.sleep(time); 402 } 403 catch(Exception ex) 404 { 405 } 406 } 407 } 408 | Popular Tags |