1 11 package org.eclipse.jsch.internal.core; 12 13 import java.util.Hashtable ; 14 15 import org.eclipse.core.net.proxy.IProxyData; 16 import org.eclipse.core.net.proxy.IProxyService; 17 import org.eclipse.core.runtime.IStatus; 18 import org.eclipse.core.runtime.Preferences; 19 import org.eclipse.core.runtime.preferences.InstanceScope; 20 21 import com.jcraft.jsch.*; 22 23 public class Utils{ 24 25 public static String loadPrivateKeys(JSch jsch, String current_pkeys){ 26 Preferences preferences=JSchCorePlugin.getPlugin().getPluginPreferences(); 27 String ssh_home=preferences.getString(IConstants.KEY_SSH2HOME); 28 String pkeys=preferences.getString(IConstants.KEY_PRIVATEKEY); 29 30 if(ssh_home.length()==0) 31 ssh_home=PreferenceInitializer.SSH_HOME_DEFAULT; 32 33 java.io.File file; 34 String [] pkey=pkeys.split(","); String [] _pkey=current_pkeys.split(","); String result=""; for(int i=0; i<pkey.length; i++){ 38 file=new java.io.File (pkey[i]); 39 if(!file.isAbsolute()){ 40 file=new java.io.File (ssh_home, pkey[i]); 41 } 42 if(file.exists()){ 43 boolean notyet=true; 44 for(int j=0; j<_pkey.length; j++){ 45 if(pkey[i].equals(_pkey[j])){ 46 notyet=false; 47 break; 48 } 49 } 50 try{ 51 if(notyet) 52 jsch.addIdentity(file.getPath()); 53 if(result.length()==0){ 54 result=pkey[i]; 55 } 56 else{ 57 result+=(","+pkey[i]); } 59 } 60 catch(JSchException e){ 61 JSchCorePlugin.log(IStatus.ERROR, 62 "An error occurred loading the SSH2 private keys", e); } 64 } 65 } 66 return result; 67 } 68 69 public static Session createSession(JSch jsch, String username, 70 String hostname, int port) throws JSchException{ 71 Session session=jsch.getSession(username, hostname, port); 72 setProxy(session); 73 Hashtable config=new Hashtable (); 74 config.put("PreferredAuthentications", "gssapi-with-mic,publickey,password,keyboard-interactive"); session.setConfig(config); 77 return session; 78 } 79 80 public static void setProxy(Session session){ 81 Proxy proxy=getProxyForHost(session.getHost(), IProxyData.SOCKS_PROXY_TYPE); 82 if(proxy==null) 83 proxy=getProxyForHost(session.getHost(), IProxyData. HTTPS_PROXY_TYPE); 84 if(proxy!=null) 85 session.setProxy(proxy); 86 } 87 88 private static int getPort(IProxyData data){ 89 int port=data.getPort(); 90 if(port==-1){ 91 if(data.getType().equals(IProxyData.HTTP_PROXY_TYPE)) 92 port=80; 93 else if(data.getType().equals(IProxyData.HTTPS_PROXY_TYPE)) 94 port=443; 95 else if(data.getType().equals(IProxyData.SOCKS_PROXY_TYPE)) 96 port=1080; 97 } 98 return port; 99 } 100 101 private static IProxyData getProxyData(String host, String type){ 102 IProxyService proxyService=JSchCorePlugin.getPlugin().getProxyService(); 103 if(proxyService==null) 104 return null; 105 IProxyData data=proxyService.getProxyDataForHost(host, type); 106 if(data==null||data.getHost()==null||getJSchProxyType(data)==null) 107 return null; 108 return data; 109 } 110 111 private static String getJSchProxyType(IProxyData data){ 112 if(data.getType().equals(IProxyData.HTTPS_PROXY_TYPE)) 113 return IConstants.PROXY_TYPE_HTTP; 114 if(data.getType().equals(IProxyData.SOCKS_PROXY_TYPE)) 115 return IConstants.PROXY_TYPE_SOCKS5; 116 return null; 117 } 118 119 public static Proxy getProxyForHost(String host, String proxyType){ 120 IProxyService proxyService=JSchCorePlugin.getPlugin().getProxyService(); 121 if(proxyService==null) 122 return null; 123 boolean useProxy=proxyService.isProxiesEnabled(); 124 if(!useProxy) 125 return null; 126 Proxy proxy=null; 127 IProxyData data=getProxyData(host, proxyType); 128 if(data==null) 129 return null; 130 String _type=getJSchProxyType(data); 131 if(_type==null) 132 return null; 133 String _host=data.getHost(); 134 int _port=getPort(data); 135 136 boolean useAuth=data.isRequiresAuthentication(); 137 String _user=""; String _pass=""; 140 if(useAuth){ 142 _user=data.getUserId(); 143 _pass=data.getPassword(); 144 } 145 146 String proxyhost=_host+":"+_port; if(_type.equals(IConstants.PROXY_TYPE_HTTP)){ 148 proxy=new ProxyHTTP(proxyhost); 149 if(useAuth){ 150 ((ProxyHTTP)proxy).setUserPasswd(_user, _pass); 151 } 152 } 153 else if(_type.equals(IConstants.PROXY_TYPE_SOCKS5)){ 154 proxy=new ProxySOCKS5(proxyhost); 155 if(useAuth){ 156 ((ProxySOCKS5)proxy).setUserPasswd(_user, _pass); 157 } 158 } 159 return proxy; 160 } 161 162 public static void migrateSSH2Preferences() { 163 Preferences preferences = JSchCorePlugin.getPlugin().getPluginPreferences(); 164 if(!preferences.getBoolean(IConstants.PREF_HAS_MIGRATED_SSH2_PREFS)){ 165 preferences.setValue(IConstants.PREF_HAS_MIGRATED_SSH2_PREFS, true); 166 migrateSSH2Preferences(new InstanceScope().getNode("")); } 168 } 169 170 public static void migrateSSH2Preferences(org.osgi.service.prefs.Preferences node) { 171 org.osgi.service.prefs.Preferences jschPrefs=node.node(JSchCorePlugin.ID); 172 org.osgi.service.prefs.Preferences ssh2Prefs=node.node("org.eclipse.team.cvs.ssh2"); String oldHome = ssh2Prefs.get(IConstants.KEY_OLD_SSH2HOME, null); 174 String oldKey = ssh2Prefs.get(IConstants.KEY_OLD_PRIVATEKEY, null); 175 if (oldHome != null) { 176 jschPrefs.put(IConstants.KEY_SSH2HOME, oldHome); 177 ssh2Prefs.remove(IConstants.KEY_OLD_SSH2HOME); 178 } 179 if (oldKey != null) { 180 jschPrefs.put(IConstants.KEY_PRIVATEKEY, oldKey); 181 ssh2Prefs.remove(IConstants.KEY_OLD_PRIVATEKEY); 182 } 183 } 184 185 } 186 | Popular Tags |