use of org.jivesoftware.smackx.bytestreams.socks5.Socks5Proxy in project Smack by igniterealtime.
the class Socks5BytestreamManager method getLocalStreamHost.
/**
* Returns the stream host information of the local SOCKS5 proxy containing the IP address and
* the port or null if local SOCKS5 proxy is not running.
*
* @return the stream host information of the local SOCKS5 proxy or null if local SOCKS5 proxy
* is not running
*/
private List<StreamHost> getLocalStreamHost() {
XMPPConnection connection = connection();
// get local proxy singleton
Socks5Proxy socks5Server = Socks5Proxy.getSocks5Proxy();
if (!socks5Server.isRunning()) {
// server is not running
return null;
}
List<String> addresses = socks5Server.getLocalAddresses();
if (addresses.isEmpty()) {
// local address could not be determined
return null;
}
final int port = socks5Server.getPort();
List<StreamHost> streamHosts = new ArrayList<StreamHost>();
outerloop: for (String address : addresses) {
// Prevent loopback addresses from appearing as streamhost
final String[] loopbackAddresses = { "127.0.0.1", "0:0:0:0:0:0:0:1", "::1" };
for (String loopbackAddress : loopbackAddresses) {
// ie. the part after the '%' sign.
if (address.startsWith(loopbackAddress)) {
continue outerloop;
}
}
streamHosts.add(new StreamHost(connection.getUser(), address, port));
}
return streamHosts;
}
use of org.jivesoftware.smackx.bytestreams.socks5.Socks5Proxy in project Smack by igniterealtime.
the class Socks5ByteStreamTest method testSocks5BytestreamWithLocalSocks5Proxy.
/**
* Socks5 bytestream should be successfully established using the local Socks5 proxy.
*
* @throws Exception should not happen
*/
public void testSocks5BytestreamWithLocalSocks5Proxy() throws Exception {
// setup port for local socks5 proxy
SmackConfiguration.setLocalSocks5ProxyEnabled(true);
SmackConfiguration.setLocalSocks5ProxyPort(7778);
Socks5Proxy socks5Proxy = Socks5Proxy.getSocks5Proxy();
socks5Proxy.start();
assertTrue(socks5Proxy.isRunning());
XMPPConnection initiatorConnection = getConnection(0);
XMPPConnection targetConnection = getConnection(1);
// test data
final byte[] data = new byte[] { 1, 2, 3 };
final SynchronousQueue<byte[]> queue = new SynchronousQueue<byte[]>();
Socks5BytestreamManager targetByteStreamManager = Socks5BytestreamManager.getBytestreamManager(targetConnection);
Socks5BytestreamListener incomingByteStreamListener = new Socks5BytestreamListener() {
public void incomingBytestreamRequest(Socks5BytestreamRequest request) {
InputStream inputStream;
try {
Socks5BytestreamSession session = request.accept();
inputStream = session.getInputStream();
byte[] receivedData = new byte[3];
inputStream.read(receivedData);
queue.put(receivedData);
} catch (Exception e) {
fail(e.getMessage());
}
}
};
targetByteStreamManager.addIncomingBytestreamListener(incomingByteStreamListener);
Socks5BytestreamManager initiatorByteStreamManager = Socks5BytestreamManager.getBytestreamManager(initiatorConnection);
Socks5BytestreamSession session = initiatorByteStreamManager.establishSession(targetConnection.getUser());
OutputStream outputStream = session.getOutputStream();
assertTrue(session.isDirect());
// verify stream
outputStream.write(data);
outputStream.flush();
outputStream.close();
assertEquals("received data not equal to sent data", data, queue.take());
// reset default configuration
SmackConfiguration.setLocalSocks5ProxyPort(7777);
}
use of org.jivesoftware.smackx.bytestreams.socks5.Socks5Proxy in project Smack by igniterealtime.
the class Socks5ByteStreamManagerTest method shouldNotPrioritizeSocks5ProxyIfPrioritizationDisabled.
/**
* Invoking {@link Socks5BytestreamManager#establishSession(org.jxmpp.jid.Jid, String)} the first time
* should successfully negotiate a SOCKS5 Bytestream via the second SOCKS5 proxy. The second
* negotiation should run in the same manner if prioritization is disabled.
*
* @throws Exception should not happen
*/
@Test
public void shouldNotPrioritizeSocks5ProxyIfPrioritizationDisabled() throws Exception {
// disable clients local SOCKS5 proxy
Socks5Proxy.setLocalSocks5ProxyEnabled(false);
// get Socks5ByteStreamManager for connection
Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
byteStreamManager.setProxyPrioritizationEnabled(false);
assertFalse(byteStreamManager.isProxyPrioritizationEnabled());
Verification<Bytestream, Bytestream> streamHostUsedVerification = new Verification<Bytestream, Bytestream>() {
@Override
public void verify(Bytestream request, Bytestream response) {
assertEquals(response.getSessionID(), request.getSessionID());
assertEquals(2, request.getStreamHosts().size());
// verify that the used stream host is the second in list
StreamHost streamHost = (StreamHost) request.getStreamHosts().toArray()[1];
assertEquals(response.getUsedHost().getJID(), streamHost.getJID());
}
};
createResponses(streamHostUsedVerification);
// start a local SOCKS5 proxy
Socks5TestProxy socks5Proxy = Socks5TestProxy.getProxy(7778);
socks5Proxy.start();
// create digest to get the socket opened by target
String digest = Socks5Utils.createDigest(sessionID, initiatorJID, targetJID);
// call the method that should be tested
OutputStream outputStream = byteStreamManager.establishSession(targetJID, sessionID).getOutputStream();
// test the established bytestream
InputStream inputStream = socks5Proxy.getSocket(digest).getInputStream();
byte[] data = new byte[] { 1, 2, 3 };
outputStream.write(data);
byte[] result = new byte[3];
inputStream.read(result);
assertArrayEquals(data, result);
protocol.verifyAll();
createResponses(streamHostUsedVerification);
// call the method that should be tested again
outputStream = byteStreamManager.establishSession(targetJID, sessionID).getOutputStream();
// test the established bytestream
inputStream = socks5Proxy.getSocket(digest).getInputStream();
outputStream.write(data);
inputStream.read(result);
assertArrayEquals(data, result);
protocol.verifyAll();
byteStreamManager.setProxyPrioritizationEnabled(true);
}
use of org.jivesoftware.smackx.bytestreams.socks5.Socks5Proxy in project Smack by igniterealtime.
the class Socks5ByteStreamManagerTest method shouldUseMultipleAddressesForLocalSocks5Proxy.
/**
* If multiple network addresses are added to the local SOCKS5 proxy, all of them should be
* contained in the SOCKS5 Bytestream request.
*
* @throws Exception should not happen
*/
@Test
public void shouldUseMultipleAddressesForLocalSocks5Proxy() throws Exception {
// enable clients local SOCKS5 proxy on port 7778
Socks5Proxy.setLocalSocks5ProxyEnabled(true);
Socks5Proxy.setLocalSocks5ProxyPort(7778);
// start a local SOCKS5 proxy
Socks5Proxy socks5Proxy = Socks5Proxy.getSocks5Proxy();
socks5Proxy.start();
assertTrue(socks5Proxy.isRunning());
// get Socks5ByteStreamManager for connection
Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
/**
* create responses in the order they should be queried specified by the XEP-0065
* specification
*/
// build discover info that supports the SOCKS5 feature
DiscoverInfo discoverInfo = Socks5PacketUtils.createDiscoverInfo(targetJID, initiatorJID);
discoverInfo.addFeature(Bytestream.NAMESPACE);
// return that SOCKS5 is supported if target is queried
protocol.addResponse(discoverInfo, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
// build discover items containing no proxy item
DiscoverItems discoverItems = Socks5PacketUtils.createDiscoverItems(xmppServer, initiatorJID);
// return the discover item if XMPP server is queried
protocol.addResponse(discoverItems, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
// build used stream host response
Bytestream streamHostUsedPacket = Socks5PacketUtils.createBytestreamResponse(targetJID, initiatorJID);
streamHostUsedPacket.setSessionID(sessionID);
// local proxy used
streamHostUsedPacket.setUsedHost(initiatorJID);
// return used stream host info as response to the bytestream initiation
protocol.addResponse(streamHostUsedPacket, new Verification<Bytestream, Bytestream>() {
@Override
public void verify(Bytestream request, Bytestream response) {
assertEquals(response.getSessionID(), request.getSessionID());
StreamHost streamHost1 = request.getStreamHosts().get(0);
assertEquals(response.getUsedHost().getJID(), streamHost1.getJID());
StreamHost streamHost2 = request.getStreamHosts().get(request.getStreamHosts().size() - 1);
assertEquals(response.getUsedHost().getJID(), streamHost2.getJID());
assertEquals("localAddress", streamHost2.getAddress());
}
}, Verification.correspondingSenderReceiver, Verification.requestTypeSET);
// create digest to get the socket opened by target
String digest = Socks5Utils.createDigest(sessionID, initiatorJID, targetJID);
// connect to proxy as target
socks5Proxy.addTransfer(digest);
StreamHost streamHost = new StreamHost(targetJID, socks5Proxy.getLocalAddresses().get(0), socks5Proxy.getPort());
Socks5Client socks5Client = new Socks5Client(streamHost, digest);
InputStream inputStream = socks5Client.getSocket(2000).getInputStream();
// add another network address before establishing SOCKS5 Bytestream
socks5Proxy.addLocalAddress("localAddress");
// finally call the method that should be tested
OutputStream outputStream = byteStreamManager.establishSession(targetJID, sessionID).getOutputStream();
// test the established bytestream
byte[] data = new byte[] { 1, 2, 3 };
outputStream.write(data);
byte[] result = new byte[3];
inputStream.read(result);
assertArrayEquals(data, result);
protocol.verifyAll();
// reset proxy settings
socks5Proxy.stop();
socks5Proxy.removeLocalAddress("localAddress");
Socks5Proxy.setLocalSocks5ProxyPort(7777);
}
use of org.jivesoftware.smackx.bytestreams.socks5.Socks5Proxy in project Smack by igniterealtime.
the class Socks5ByteStreamManagerTest method shouldPrioritizeSecondSocks5ProxyOnSecondAttempt.
/**
* Invoking {@link Socks5BytestreamManager#establishSession(org.jxmpp.jid.Jid, String)} the first time
* should successfully negotiate a SOCKS5 Bytestream via the second SOCKS5 proxy and should
* prioritize this proxy for a second SOCKS5 Bytestream negotiation.
*
* @throws Exception should not happen
*/
@Test
public void shouldPrioritizeSecondSocks5ProxyOnSecondAttempt() throws Exception {
// disable clients local SOCKS5 proxy
Socks5Proxy.setLocalSocks5ProxyEnabled(false);
// get Socks5ByteStreamManager for connection
Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
assertTrue(byteStreamManager.isProxyPrioritizationEnabled());
Verification<Bytestream, Bytestream> streamHostUsedVerification1 = new Verification<Bytestream, Bytestream>() {
@Override
public void verify(Bytestream request, Bytestream response) {
assertEquals(response.getSessionID(), request.getSessionID());
assertEquals(2, request.getStreamHosts().size());
// verify that the used stream host is the second in list
StreamHost streamHost = (StreamHost) request.getStreamHosts().toArray()[1];
assertEquals(response.getUsedHost().getJID(), streamHost.getJID());
}
};
createResponses(streamHostUsedVerification1);
// start a local SOCKS5 proxy
Socks5TestProxy socks5Proxy = Socks5TestProxy.getProxy(7778);
socks5Proxy.start();
// create digest to get the socket opened by target
String digest = Socks5Utils.createDigest(sessionID, initiatorJID, targetJID);
// call the method that should be tested
OutputStream outputStream = byteStreamManager.establishSession(targetJID, sessionID).getOutputStream();
// test the established bytestream
InputStream inputStream = socks5Proxy.getSocket(digest).getInputStream();
byte[] data = new byte[] { 1, 2, 3 };
outputStream.write(data);
byte[] result = new byte[3];
inputStream.read(result);
assertArrayEquals(data, result);
protocol.verifyAll();
Verification<Bytestream, Bytestream> streamHostUsedVerification2 = new Verification<Bytestream, Bytestream>() {
@Override
public void verify(Bytestream request, Bytestream response) {
assertEquals(response.getSessionID(), request.getSessionID());
assertEquals(2, request.getStreamHosts().size());
// verify that the used stream host is the first in list
StreamHost streamHost = (StreamHost) request.getStreamHosts().toArray()[0];
assertEquals(response.getUsedHost().getJID(), streamHost.getJID());
}
};
createResponses(streamHostUsedVerification2);
// call the method that should be tested again
outputStream = byteStreamManager.establishSession(targetJID, sessionID).getOutputStream();
// test the established bytestream
inputStream = socks5Proxy.getSocket(digest).getInputStream();
outputStream.write(data);
inputStream.read(result);
assertArrayEquals(data, result);
protocol.verifyAll();
}
Aggregations