Java源码示例:com.jcraft.jsch.ProxySOCKS5
示例1
private Proxy[] getProxies() {
String proxyString = authority.getProxyString();
if (!StringUtils.isEmpty(proxyString)) {
java.net.Proxy proxy = FileUtils.getProxy(authority.getProxyString());
if ((proxy != null) && (proxy.type() != Type.DIRECT)) {
URI proxyUri = URI.create(proxyString);
String hostName = proxyUri.getHost();
int port = proxyUri.getPort();
String userInfo = proxyUri.getUserInfo();
org.jetel.util.protocols.UserInfo proxyCredentials = null;
if (userInfo != null) {
proxyCredentials = new org.jetel.util.protocols.UserInfo(userInfo);
}
switch (proxy.type()) {
case HTTP:
ProxyHTTP proxyHttp = (port >= 0) ? new ProxyHTTP(hostName, port) : new ProxyHTTP(hostName);
if (proxyCredentials != null) {
proxyHttp.setUserPasswd(proxyCredentials.getUser(), proxyCredentials.getPassword());
}
return new Proxy[] {proxyHttp};
case SOCKS:
ProxySOCKS4 proxySocks4 = (port >= 0) ? new ProxySOCKS4(hostName, port) : new ProxySOCKS4(hostName);
ProxySOCKS5 proxySocks5 = (port >= 0) ? new ProxySOCKS5(hostName, port) : new ProxySOCKS5(hostName);
if (proxyCredentials != null) {
proxySocks4.setUserPasswd(proxyCredentials.getUser(), proxyCredentials.getPassword());
proxySocks5.setUserPasswd(proxyCredentials.getUser(), proxyCredentials.getPassword());
}
return new Proxy[] {proxySocks5, proxySocks4};
case DIRECT:
return new Proxy[1];
}
}
}
return new Proxy[1];
}
示例2
private com.jcraft.jsch.Proxy createProxy (String proxyHost, int proxyPort) {
return USE_PROXY_TUNNELING
? new ProxyHTTP(proxyHost, proxyPort)
: new ProxySOCKS5(proxyHost, proxyPort);
}
示例3
/**
* Connects the connection.
* @throws IOException if the unfortunate happens.
*/
@Override
public synchronized void connect() throws IOException {
logger.debug("connecting...");
Authentication auth = authentication;
if (updater != null) {
Authentication updatedAuth = updater.updateAuthentication(authentication);
if (updatedAuth != null && auth != updatedAuth) {
auth = updatedAuth;
}
}
try {
client = new JSch();
if (auth.getPrivateKeyPhrase() == null) {
client.addIdentity(auth.getPrivateKeyFile().getAbsolutePath(),
auth.getPrivateKeyFilePassword());
} else {
client.addIdentity(auth.getUsername(), auth.getPrivateKeyPhrase(), null,
auth.getPrivateKeyFilePassword().getBytes("UTF-8"));
}
client.setHostKeyRepository(new BlindHostKeyRepository());
connectSession = client.getSession(auth.getUsername(), host, port);
connectSession.setConfig("PreferredAuthentications", "publickey");
if (proxy != null && !proxy.isEmpty()) {
String[] splitted = proxy.split(":");
if (splitted.length > 2 && splitted[1].length() >= PROTO_HOST_DELIM_LENGTH) {
String pproto = splitted[0];
String phost = splitted[1].substring(2);
int pport = Integer.parseInt(splitted[2]);
if (pproto.equals("socks5") || pproto.equals("http")) {
if (pproto.equals("socks5")) {
connectSession.setProxy(new ProxySOCKS5(phost, pport));
} else {
connectSession.setProxy(new ProxyHTTP(phost, pport));
}
} else {
throw new MalformedURLException("Only http and socks5 protocols are supported");
}
} else {
throw new MalformedURLException(proxy);
}
}
connectSession.connect(this.connectionTimeout);
logger.debug("Connected: {}", connectSession.isConnected());
connectSession.setServerAliveInterval(ALIVE_INTERVAL);
} catch (JSchException ex) {
throw new SshException(ex);
}
}
示例4
private static ProxySOCKS5 createProxySOCKS5(final String proxyHost, final int proxyPort) {
return proxyPort == 0 ? new ProxySOCKS5(proxyHost) : new ProxySOCKS5(proxyHost, proxyPort);
}