mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-15 20:12:04 +01:00
Throw instead of return in Socks5Client
Returning a boolean is not really expressive in such cases. For example, if it returns 'false' then the callee has no information *what* went wrong. Instead throw an SmackException with some more information.
This commit is contained in:
parent
44d6a9d7c1
commit
4f7324103c
1 changed files with 21 additions and 18 deletions
|
@ -28,6 +28,8 @@ import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.FutureTask;
|
import java.util.concurrent.FutureTask;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import org.jivesoftware.smack.SmackException;
|
import org.jivesoftware.smack.SmackException;
|
||||||
import org.jivesoftware.smack.XMPPException;
|
import org.jivesoftware.smack.XMPPException;
|
||||||
|
@ -43,6 +45,8 @@ import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost;
|
||||||
*/
|
*/
|
||||||
class Socks5Client {
|
class Socks5Client {
|
||||||
|
|
||||||
|
private static final Logger LOGGER = Logger.getLogger(Socks5Client.class.getName());
|
||||||
|
|
||||||
/* stream host containing network settings and name of the SOCKS5 proxy */
|
/* stream host containing network settings and name of the SOCKS5 proxy */
|
||||||
protected StreamHost streamHost;
|
protected StreamHost streamHost;
|
||||||
|
|
||||||
|
@ -86,24 +90,23 @@ class Socks5Client {
|
||||||
streamHost.getPort());
|
streamHost.getPort());
|
||||||
socket.connect(socketAddress);
|
socket.connect(socketAddress);
|
||||||
|
|
||||||
boolean res;
|
|
||||||
// initialize connection to SOCKS5 proxy
|
// initialize connection to SOCKS5 proxy
|
||||||
try {
|
try {
|
||||||
res = establish(socket);
|
establish(socket);
|
||||||
}
|
}
|
||||||
catch (SmackException e) {
|
catch (SmackException e) {
|
||||||
|
if (!socket.isClosed()) {
|
||||||
|
try {
|
||||||
socket.close();
|
socket.close();
|
||||||
|
} catch (IOException e2) {
|
||||||
|
LOGGER.log(Level.WARNING, "Could not close SOCKS5 socket", e2);
|
||||||
|
}
|
||||||
|
}
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (res) {
|
|
||||||
return socket;
|
return socket;
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
socket.close();
|
|
||||||
throw new SmackException("SOCKS5 negotiation failed");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
});
|
||||||
Thread executor = new Thread(futureTask);
|
Thread executor = new Thread(futureTask);
|
||||||
|
@ -135,17 +138,12 @@ class Socks5Client {
|
||||||
* Initializes the connection to the SOCKS5 proxy by negotiating authentication method and
|
* Initializes the connection to the SOCKS5 proxy by negotiating authentication method and
|
||||||
* requesting a stream for the given digest. Currently only the no-authentication method is
|
* requesting a stream for the given digest. Currently only the no-authentication method is
|
||||||
* supported by the Socks5Client.
|
* supported by the Socks5Client.
|
||||||
* <p>
|
|
||||||
* Returns <code>true</code> if a stream could be established, otherwise <code>false</code>. If
|
|
||||||
* <code>false</code> is returned the given Socket should be closed.
|
|
||||||
*
|
*
|
||||||
* @param socket connected to a SOCKS5 proxy
|
* @param socket connected to a SOCKS5 proxy
|
||||||
* @return <code>true</code> if if a stream could be established, otherwise <code>false</code>.
|
|
||||||
* If <code>false</code> is returned the given Socket should be closed.
|
|
||||||
* @throws SmackException
|
* @throws SmackException
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
protected boolean establish(Socket socket) throws SmackException, IOException {
|
protected void establish(Socket socket) throws SmackException, IOException {
|
||||||
|
|
||||||
byte[] connectionRequest;
|
byte[] connectionRequest;
|
||||||
byte[] connectionResponse;
|
byte[] connectionResponse;
|
||||||
|
@ -171,7 +169,7 @@ class Socks5Client {
|
||||||
|
|
||||||
// check if server responded with correct version and no-authentication method
|
// check if server responded with correct version and no-authentication method
|
||||||
if (response[0] != (byte) 0x05 || response[1] != (byte) 0x00) {
|
if (response[0] != (byte) 0x05 || response[1] != (byte) 0x00) {
|
||||||
return false;
|
throw new SmackException("Remote SOCKS5 server responsed with unexpected version: " + response[0] + ' ' + response[1] + ". Should be 0x05 0x00.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// request SOCKS5 connection with given address/digest
|
// request SOCKS5 connection with given address/digest
|
||||||
|
@ -184,7 +182,12 @@ class Socks5Client {
|
||||||
|
|
||||||
// verify response
|
// verify response
|
||||||
connectionRequest[1] = (byte) 0x00; // set expected return status to 0
|
connectionRequest[1] = (byte) 0x00; // set expected return status to 0
|
||||||
return Arrays.equals(connectionRequest, connectionResponse);
|
if (!Arrays.equals(connectionRequest, connectionResponse)) {
|
||||||
|
throw new SmackException(
|
||||||
|
"Connection request does not equal connection reponse. Response: "
|
||||||
|
+ Arrays.toString(connectionResponse) + ". Request: "
|
||||||
|
+ Arrays.toString(connectionRequest));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue