use of org.jivesoftware.smack.XMPPException in project Smack by igniterealtime.
the class Socks5BytestreamRequest method accept.
/**
* Accepts the SOCKS5 Bytestream initialization request and returns the socket to send/receive
* data.
* <p>
* Before accepting the SOCKS5 Bytestream request you can set timeouts by invoking
* {@link #setTotalConnectTimeout(int)} and {@link #setMinimumConnectTimeout(int)}.
*
* @return the socket to send/receive data
* @throws InterruptedException if the current thread was interrupted while waiting
* @throws XMPPErrorException
* @throws SmackException
*/
@Override
public Socks5BytestreamSession accept() throws InterruptedException, XMPPErrorException, SmackException {
Collection<StreamHost> streamHosts = this.bytestreamRequest.getStreamHosts();
// throw exceptions if request contains no stream hosts
if (streamHosts.size() == 0) {
cancelRequest();
}
StreamHost selectedHost = null;
Socket socket = null;
String digest = Socks5Utils.createDigest(this.bytestreamRequest.getSessionID(), this.bytestreamRequest.getFrom(), this.manager.getConnection().getUser());
/*
* determine timeout for each connection attempt; each SOCKS5 proxy has the same amount of
* time so that the first does not consume the whole timeout
*/
int timeout = Math.max(getTotalConnectTimeout() / streamHosts.size(), getMinimumConnectTimeout());
for (StreamHost streamHost : streamHosts) {
String address = streamHost.getAddress() + ":" + streamHost.getPort();
// check to see if this address has been blacklisted
int failures = getConnectionFailures(address);
if (CONNECTION_FAILURE_THRESHOLD > 0 && failures >= CONNECTION_FAILURE_THRESHOLD) {
continue;
}
// establish socket
try {
// build SOCKS5 client
final Socks5Client socks5Client = new Socks5Client(streamHost, digest);
// connect to SOCKS5 proxy with a timeout
socket = socks5Client.getSocket(timeout);
// set selected host
selectedHost = streamHost;
break;
} catch (TimeoutException | IOException | SmackException | XMPPException e) {
incrementConnectionFailures(address);
}
}
// throw exception if connecting to all SOCKS5 proxies failed
if (selectedHost == null || socket == null) {
cancelRequest();
}
// send used-host confirmation
Bytestream response = createUsedHostResponse(selectedHost);
this.manager.getConnection().sendStanza(response);
return new Socks5BytestreamSession(socket, selectedHost.getJID().equals(this.bytestreamRequest.getFrom()));
}
use of org.jivesoftware.smack.XMPPException in project Smack by igniterealtime.
the class Socks5ClientForInitiator method getSocket.
@Override
public Socket getSocket(int timeout) throws IOException, InterruptedException, TimeoutException, XMPPException, SmackException {
Socket socket = null;
// check if stream host is the local SOCKS5 proxy
if (this.streamHost.getJID().equals(this.connection.get().getUser())) {
Socks5Proxy socks5Server = Socks5Proxy.getSocks5Proxy();
socket = socks5Server.getSocket(this.digest);
if (socket == null) {
throw new SmackException("target is not connected to SOCKS5 proxy");
}
} else {
socket = super.getSocket(timeout);
try {
activate();
} catch (XMPPException e1) {
socket.close();
throw e1;
} catch (NoResponseException e2) {
socket.close();
throw e2;
}
}
return socket;
}
use of org.jivesoftware.smack.XMPPException in project Smack by igniterealtime.
the class BasicResolver method resolve.
/**
* Resolve the IP address.
* <p/>
* The BasicResolver takes the IP addresses of the interfaces and uses the
* first non-loopback, non-linklocal and non-sitelocal address.
* @throws NotConnectedException
* @throws InterruptedException
*/
@Override
public synchronized void resolve(JingleSession session) throws XMPPException, NotConnectedException, InterruptedException {
setResolveInit();
clearCandidates();
Enumeration<NetworkInterface> ifaces = null;
try {
ifaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
LOGGER.log(Level.WARNING, "exception", e);
}
while (ifaces.hasMoreElements()) {
NetworkInterface iface = ifaces.nextElement();
Enumeration<InetAddress> iaddresses = iface.getInetAddresses();
while (iaddresses.hasMoreElements()) {
InetAddress iaddress = iaddresses.nextElement();
if (!iaddress.isLoopbackAddress() && !iaddress.isLinkLocalAddress() && !iaddress.isSiteLocalAddress()) {
TransportCandidate tr = new TransportCandidate.Fixed(iaddress.getHostAddress() != null ? iaddress.getHostAddress() : iaddress.getHostName(), getFreePort());
tr.setLocalIp(iaddress.getHostAddress() != null ? iaddress.getHostAddress() : iaddress.getHostName());
addCandidate(tr);
setResolveEnd();
return;
}
}
}
try {
ifaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
LOGGER.log(Level.WARNING, "exception", e);
}
while (ifaces.hasMoreElements()) {
NetworkInterface iface = ifaces.nextElement();
Enumeration<InetAddress> iaddresses = iface.getInetAddresses();
while (iaddresses.hasMoreElements()) {
InetAddress iaddress = iaddresses.nextElement();
if (!iaddress.isLoopbackAddress() && !iaddress.isLinkLocalAddress()) {
TransportCandidate tr = new TransportCandidate.Fixed(iaddress.getHostAddress() != null ? iaddress.getHostAddress() : iaddress.getHostName(), getFreePort());
tr.setLocalIp(iaddress.getHostAddress() != null ? iaddress.getHostAddress() : iaddress.getHostName());
addCandidate(tr);
setResolveEnd();
return;
}
}
}
try {
TransportCandidate tr = new TransportCandidate.Fixed(InetAddress.getLocalHost().getHostAddress() != null ? InetAddress.getLocalHost().getHostAddress() : InetAddress.getLocalHost().getHostName(), getFreePort());
tr.setLocalIp(InetAddress.getLocalHost().getHostAddress() != null ? InetAddress.getLocalHost().getHostAddress() : InetAddress.getLocalHost().getHostName());
addCandidate(tr);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "exception", e);
}
setResolveEnd();
}
use of org.jivesoftware.smack.XMPPException in project Smack by igniterealtime.
the class MultiUserChatCreationTest method testCreateReservedRoom.
/**
* Tests creating a new "Reserved Room".
*/
public void testCreateReservedRoom() {
MultiUserChat muc = new MultiUserChat(getConnection(0), room);
try {
// Create the room
muc.create("testbot1");
// Get the the room's configuration form
Form form = muc.getConfigurationForm();
assertNotNull("No room configuration form", form);
// Create a new form to submit based on the original form
Form submitForm = form.createAnswerForm();
// Add default answers to the form to submit
for (Iterator<FormField> fields = form.getFields(); fields.hasNext(); ) {
FormField field = fields.next();
if (!FormField.TYPE_HIDDEN.equals(field.getType()) && field.getVariable() != null) {
// Sets the default value as the answer
submitForm.setDefaultAnswer(field.getVariable());
}
}
List<String> owners = new ArrayList<String>();
owners.add(getBareJID(0));
submitForm.setAnswer("muc#roomconfig_roomowners", owners);
// Update the new room's configuration
muc.sendConfigurationForm(submitForm);
// Destroy the new room
muc.destroy("The room has almost no activity...", null);
} catch (XMPPException e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.jivesoftware.smack.XMPPException in project Smack by igniterealtime.
the class MultiUserChatTest method testReservedNickname.
public void testReservedNickname() {
try {
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
// Check that user2 doesn't have a reserved nickname yet
String reservedNickname = muc2.getReservedNickname();
assertNull("Reserved nickname is not null", reservedNickname);
// User2 registers with the room and reserves a nickname
Form registrationForm = muc2.getRegistrationForm();
Form answerForm = registrationForm.createAnswerForm();
answerForm.setAnswer("muc#register_first", "MyFirstName");
answerForm.setAnswer("muc#register_last", "MyLastName");
answerForm.setAnswer("muc#register_roomnick", "MyNick");
muc2.sendRegistrationForm(answerForm);
// Check that user2 has a reserved nickname
reservedNickname = muc2.getReservedNickname();
assertEquals("Reserved nickname is wrong", "MyNick", reservedNickname);
// Check that user2 can join the room using his reserved nickname
muc2.join("MyNick");
muc2.leave();
// Check that other users cannot join the room with user2's reserved nickname
MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
try {
muc3.join("MyNick");
fail("Other user was able to join with other user's reserved nickname");
} catch (XMPPException e) {
XMPPError xmppError = e.getXMPPError();
assertNotNull("No XMPPError was received when joining with other user's reserved nickname", xmppError);
assertEquals("Different error code was received while joining with other user's reserved nickname", 409, xmppError.getCode());
}
// Check that user3 can join the room using his own nickname (not reserved)
muc3.join("MyNotReservedNick");
muc3.leave();
// Check that another user cannot reserve an already reserved nickname
registrationForm = muc3.getRegistrationForm();
answerForm = registrationForm.createAnswerForm();
answerForm.setAnswer("muc#register_first", "MyFirstName 2");
answerForm.setAnswer("muc#register_last", "MyLastName 2");
answerForm.setAnswer("muc#register_roomnick", "MyNick");
try {
muc3.sendRegistrationForm(answerForm);
} catch (XMPPException e) {
XMPPError xmppError = e.getXMPPError();
assertNotNull("No XMPPError was received when reserving an already reserved nickname", xmppError);
assertEquals("Different error code was received while reserving an already reserved nickname", 409, xmppError.getCode());
}
// Check that another user can reserve a new nickname
registrationForm = muc3.getRegistrationForm();
answerForm = registrationForm.createAnswerForm();
answerForm.setAnswer("muc#register_first", "MyFirstName 2");
answerForm.setAnswer("muc#register_last", "MyLastName 2");
answerForm.setAnswer("muc#register_roomnick", "MyNick 2");
muc3.sendRegistrationForm(answerForm);
} catch (XMPPException e) {
e.printStackTrace();
fail(e.getMessage());
}
}
Aggregations