1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-29 09:42:06 +01:00

proxy: make it the caller's reponsibility to close the socket

This makes the code shorter as there is now a single place where the
socket should be closed.
This commit is contained in:
Florian Schmaus 2019-11-18 17:44:08 +01:00
parent 9d626bf787
commit 7afd1fdf46
4 changed files with 123 additions and 151 deletions

View file

@ -1,6 +1,6 @@
/** /**
* *
* Copyright 2015-2016 Florian Schmaus. * Copyright 2015-2019 Florian Schmaus.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -21,6 +21,16 @@ import java.net.Socket;
public interface ProxySocketConnection { public interface ProxySocketConnection {
/**
* Initiate a connection to the given host on the given port. Note that the caller is responsible for closing the
* socket in case this method throws.
*
* @param socket the socket to use to initiate the connection to the proxy.
* @param host the host to connect to.
* @param port the port to connect to.
* @param timeout the timeout in milliseconds.
* @throws IOException in case an I/O error occurs.
*/
void connect(Socket socket, String host, int port, int timeout) void connect(Socket socket, String host, int port, int timeout)
throws IOException; throws IOException;

View file

@ -39,16 +39,13 @@ public class Socks4ProxySocketConnection implements ProxySocketConnection {
@Override @Override
public void connect(Socket socket, String host, int port, int timeout) public void connect(Socket socket, String host, int port, int timeout)
throws IOException { throws IOException {
InputStream in = null;
OutputStream out = null;
String proxy_host = proxy.getProxyAddress(); String proxy_host = proxy.getProxyAddress();
int proxy_port = proxy.getProxyPort(); int proxy_port = proxy.getProxyPort();
String user = proxy.getProxyUsername(); String user = proxy.getProxyUsername();
try {
socket.connect(new InetSocketAddress(proxy_host, proxy_port), timeout); socket.connect(new InetSocketAddress(proxy_host, proxy_port), timeout);
in = socket.getInputStream(); InputStream in = socket.getInputStream();
out = socket.getOutputStream(); OutputStream out = socket.getOutputStream();
socket.setTcpNoDelay(true); socket.setTcpNoDelay(true);
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];
@ -134,28 +131,11 @@ public class Socks4ProxySocketConnection implements ProxySocketConnection {
"server returns VN " + buf[0]); "server returns VN " + buf[0]);
} }
if (buf[1] != 90) { if (buf[1] != 90) {
try {
socket.close();
}
catch (Exception eee) {
}
String message = "ProxySOCKS4: server returns CD " + buf[1]; String message = "ProxySOCKS4: server returns CD " + buf[1];
throw new ProxyException(ProxyInfo.ProxyType.SOCKS4, message); throw new ProxyException(ProxyInfo.ProxyType.SOCKS4, message);
} }
byte[] temp = new byte[2]; byte[] temp = new byte[2];
in.read(temp, 0, 2); in.read(temp, 0, 2);
} }
catch (RuntimeException e) {
throw e;
}
catch (Exception e) {
try {
socket.close();
}
catch (Exception eee) {
}
throw new ProxyException(ProxyInfo.ProxyType.SOCKS4, e.toString());
}
}
} }

View file

@ -22,9 +22,6 @@ import java.io.OutputStream;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.Socket; import java.net.Socket;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.logging.Logger;
import org.jivesoftware.smack.util.CloseableUtil;
/** /**
* Socket factory for Socks5 proxy. * Socket factory for Socks5 proxy.
@ -32,7 +29,6 @@ import org.jivesoftware.smack.util.CloseableUtil;
* @author Atul Aggarwal * @author Atul Aggarwal
*/ */
public class Socks5ProxySocketConnection implements ProxySocketConnection { public class Socks5ProxySocketConnection implements ProxySocketConnection {
private static final Logger LOGGER = Logger.getLogger(Socks5ProxySocketConnection.class.getName());
private final ProxyInfo proxy; private final ProxyInfo proxy;
@ -43,17 +39,14 @@ public class Socks5ProxySocketConnection implements ProxySocketConnection {
@Override @Override
public void connect(Socket socket, String host, int port, int timeout) public void connect(Socket socket, String host, int port, int timeout)
throws IOException { throws IOException {
InputStream in = null;
OutputStream out = null;
String proxy_host = proxy.getProxyAddress(); String proxy_host = proxy.getProxyAddress();
int proxy_port = proxy.getProxyPort(); int proxy_port = proxy.getProxyPort();
String user = proxy.getProxyUsername(); String user = proxy.getProxyUsername();
String passwd = proxy.getProxyPassword(); String passwd = proxy.getProxyPassword();
try {
socket.connect(new InetSocketAddress(proxy_host, proxy_port), timeout); socket.connect(new InetSocketAddress(proxy_host, proxy_port), timeout);
in = socket.getInputStream(); InputStream in = socket.getInputStream();
out = socket.getOutputStream(); OutputStream out = socket.getOutputStream();
socket.setTcpNoDelay(true); socket.setTcpNoDelay(true);
@ -142,7 +135,6 @@ public class Socks5ProxySocketConnection implements ProxySocketConnection {
System.arraycopy(passwordBytes, 0, buf, index, System.arraycopy(passwordBytes, 0, buf, index,
passwd.length()); passwd.length());
index += passwd.length(); index += passwd.length();
out.write(buf, 0, index); out.write(buf, 0, index);
/* /*
@ -168,7 +160,6 @@ public class Socks5ProxySocketConnection implements ProxySocketConnection {
} }
if (!check) { if (!check) {
CloseableUtil.maybeClose(socket, LOGGER);
throw new ProxyException(ProxyInfo.ProxyType.SOCKS5, throw new ProxyException(ProxyInfo.ProxyType.SOCKS5,
"fail in SOCKS5 proxy"); "fail in SOCKS5 proxy");
} }
@ -253,7 +244,6 @@ public class Socks5ProxySocketConnection implements ProxySocketConnection {
fill(in, buf, 4); fill(in, buf, 4);
if (buf[1] != 0) { if (buf[1] != 0) {
CloseableUtil.maybeClose(socket, LOGGER);
throw new ProxyException(ProxyInfo.ProxyType.SOCKS5, throw new ProxyException(ProxyInfo.ProxyType.SOCKS5,
"server returns " + buf[1]); "server returns " + buf[1]);
} }
@ -272,15 +262,6 @@ public class Socks5ProxySocketConnection implements ProxySocketConnection {
default: default:
} }
} }
catch (RuntimeException e) {
throw e;
}
catch (Exception e) {
CloseableUtil.maybeClose(socket, LOGGER);
// TODO convert to IOException(e) when minimum Android API level is 9 or higher
throw new IOException(e.getLocalizedMessage());
}
}
private static void fill(InputStream in, byte[] buf, int len) private static void fill(InputStream in, byte[] buf, int len)
throws IOException { throws IOException {

View file

@ -606,6 +606,7 @@ public class XMPPTCPConnection extends AbstractXMPPConnection {
try { try {
proxyInfo.getProxySocketConnection().connect(socket, host, port, timeout); proxyInfo.getProxySocketConnection().connect(socket, host, port, timeout);
} catch (IOException e) { } catch (IOException e) {
CloseableUtil.maybeClose(socket, LOGGER);
hostAddress.setException(e); hostAddress.setException(e);
failedAddresses.add(hostAddress); failedAddresses.add(hostAddress);
continue; continue;