// disable clients local SOCKS5 proxy
SmackConfiguration.setLocalSocks5ProxyEnabled(false);
// 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(Socks5BytestreamManager.NAMESPACE);
// return that SOCKS5 is supported if target is queried
protocol.addResponse(discoverInfo, Verification.correspondingSenderReceiver,
Verification.requestTypeGET);
// build discover items containing a proxy item
DiscoverItems discoverItems = Socks5PacketUtils.createDiscoverItems(xmppServer,
initiatorJID);
Item item = new Item(proxyJID);
discoverItems.addItem(item);
// return the proxy item if XMPP server is queried
protocol.addResponse(discoverItems, Verification.correspondingSenderReceiver,
Verification.requestTypeGET);
// build discover info for proxy containing information about being a SOCKS5 proxy
DiscoverInfo proxyInfo = Socks5PacketUtils.createDiscoverInfo(proxyJID, initiatorJID);
Identity identity = new Identity("proxy", proxyJID);
identity.setType("bytestreams");
proxyInfo.addIdentity(identity);
// return the socks5 bytestream proxy identity if proxy is queried
protocol.addResponse(proxyInfo, Verification.correspondingSenderReceiver,
Verification.requestTypeGET);
// build a socks5 stream host info containing the address and the port of the
// proxy
Bytestream streamHostInfo = Socks5PacketUtils.createBytestreamResponse(proxyJID,
initiatorJID);
streamHostInfo.addStreamHost(proxyJID, proxyAddress, 7778);
// return stream host info if it is queried
protocol.addResponse(streamHostInfo, Verification.correspondingSenderReceiver,
Verification.requestTypeGET);
// build used stream host response
Bytestream streamHostUsedPacket = Socks5PacketUtils.createBytestreamResponse(targetJID,
initiatorJID);
streamHostUsedPacket.setSessionID(sessionID);
streamHostUsedPacket.setUsedHost(proxyJID);
// return used stream host info as response to the bytestream initiation
protocol.addResponse(streamHostUsedPacket, new Verification<Bytestream, Bytestream>() {
public void verify(Bytestream request, Bytestream response) {
assertEquals(response.getSessionID(), request.getSessionID());
assertEquals(1, request.getStreamHosts().size());
StreamHost streamHost = (StreamHost) request.getStreamHosts().toArray()[0];
assertEquals(response.getUsedHost().getJID(), streamHost.getJID());
}
}, Verification.correspondingSenderReceiver, Verification.requestTypeSET);
// build response to proxy activation
IQ activationResponse = Socks5PacketUtils.createActivationConfirmation(proxyJID,
initiatorJID);
// return proxy activation response if proxy should be activated
protocol.addResponse(activationResponse, new Verification<Bytestream, IQ>() {
public void verify(Bytestream request, IQ response) {
assertEquals(targetJID, request.getToActivate().getTarget());
}
}, Verification.correspondingSenderReceiver, Verification.requestTypeSET);
// 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);
// finally 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 };