Class Socks5BytestreamManager
- java.lang.Object
-
- org.jivesoftware.smack.Manager
-
- org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamManager
-
- All Implemented Interfaces:
BytestreamManager
public final class Socks5BytestreamManager extends Manager implements BytestreamManager
The Socks5BytestreamManager class handles establishing SOCKS5 Bytestreams as specified in the XEP-0065.A SOCKS5 Bytestream is negotiated partly over the XMPP XML stream and partly over a separate socket. The actual transfer though takes place over a separately created socket.
A SOCKS5 Bytestream generally has three parties, the initiator, the target, and the stream host. The stream host is a specialized SOCKS5 proxy setup on a server, or, the initiator can act as the stream host.
To establish a SOCKS5 Bytestream invoke the
establishSession(Jid)
method. This will negotiate a SOCKS5 Bytestream with the given target JID and return a socket.If a session ID for the SOCKS5 Bytestream was already negotiated (e.g. while negotiating a file transfer) invoke
establishSession(Jid, String)
.To handle incoming SOCKS5 Bytestream requests add an
Socks5BytestreamListener
to the manager. There are two ways to add this listener. If you want to be informed about incoming SOCKS5 Bytestreams from a specific user add the listener by invokingaddIncomingBytestreamListener(BytestreamListener, Jid)
. If the listener should respond to all SOCKS5 Bytestream requests invokeaddIncomingBytestreamListener(BytestreamListener)
.Note that the registered
Socks5BytestreamListener
will NOT be notified on incoming Socks5 bytestream requests sent in the context of XEP-0096 file transfer. (SeeFileTransferManager
)If no
Socks5BytestreamListener
s are registered, all incoming SOCKS5 Bytestream requests will be rejected by returning a <not-acceptable/> error to the initiator.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addIncomingBytestreamListener(BytestreamListener listener)
Adds BytestreamListener that is called for every incoming SOCKS5 Bytestream request unless there is a user specific BytestreamListener registered.void
addIncomingBytestreamListener(BytestreamListener listener, Jid initiatorJID)
Adds BytestreamListener that is called for every incoming SOCKS5 Bytestream request from the given user.List<Jid>
determineProxies()
Returns a list of JIDs of SOCKS5 proxies by querying the XMPP server.void
disableService()
Disables the SOCKS5 Bytestream manager by removing the SOCKS5 Bytestream feature from the service discovery, disabling the listener for SOCKS5 Bytestream initiation requests and resetting its internal state, which includes removing this instance from the managers map.Socks5BytestreamSession
establishSession(Jid targetJID)
Establishes a SOCKS5 Bytestream with the given user and returns the Socket to send/receive data to/from the user.Socks5BytestreamSession
establishSession(Jid targetJID, String sessionID)
Establishes a SOCKS5 Bytestream with the given user using the given session ID and returns the Socket to send/receive data to/from the user.protected List<BytestreamListener>
getAllRequestListeners()
Returns a list ofBytestreamListener
that are informed if there are no listeners for a specific initiator.static Socks5BytestreamManager
getBytestreamManager(XMPPConnection connection)
Returns the Socks5BytestreamManager to handle SOCKS5 Bytestreams for a givenXMPPConnection
.protected XMPPConnection
getConnection()
Returns the XMPP connection.protected List<String>
getIgnoredBytestreamRequests()
Returns the list of session IDs that should be ignored by the InitialtionListenerList<Bytestream.StreamHost>
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.int
getProxyConnectionTimeout()
Returns the timeout for connecting to the SOCKS5 proxy selected by the target.int
getTargetResponseTimeout()
Returns the timeout to wait for the response to the SOCKS5 Bytestream initialization request.protected BytestreamListener
getUserListener(Jid initiator)
Returns theBytestreamListener
that should be informed if a SOCKS5 Bytestream request from the given initiator JID is received.void
ignoreBytestreamRequestOnce(String sessionID)
Use this method to ignore the next incoming SOCKS5 Bytestream request containing the given session ID.boolean
isAnnouncingLocalStreamHostEnabled()
Returns if the bytestream manager will announce the local stream host(s), i.e.boolean
isProxyPrioritizationEnabled()
Returns if the prioritization of the last working SOCKS5 proxy on successive SOCKS5 Bytestream connections is enabled.void
removeIncomingBytestreamListener(BytestreamListener listener)
Removes the given listener from the list of listeners for all incoming SOCKS5 Bytestream requests.void
removeIncomingBytestreamListener(Jid initiatorJID)
Removes the listener for the given user.protected void
replyRejectPacket(IQ packet)
Responses to the given packet's sender with an XMPP error that a SOCKS5 Bytestream is not accepted.void
setAnnounceLocalStreamHost(boolean announceLocalStreamHost)
Set whether or not the bytestream manager will annouce the local stream host(s), i.e.void
setProxyConnectionTimeout(int proxyConnectionTimeout)
Sets the timeout for connecting to the SOCKS5 proxy selected by the target.void
setProxyPrioritizationEnabled(boolean proxyPrioritizationEnabled)
Enable/disable the prioritization of the last working SOCKS5 proxy on successive SOCKS5 Bytestream connections.void
setTargetResponseTimeout(int targetResponseTimeout)
Sets the timeout to wait for the response to the SOCKS5 Bytestream initialization request.-
Methods inherited from class org.jivesoftware.smack.Manager
connection, getAuthenticatedConnectionOrThrow, schedule, schedule, scheduleBlocking
-
-
-
-
Method Detail
-
getBytestreamManager
public static Socks5BytestreamManager getBytestreamManager(XMPPConnection connection)
Returns the Socks5BytestreamManager to handle SOCKS5 Bytestreams for a givenXMPPConnection
.If no manager exists a new is created and initialized.
- Parameters:
connection
- the XMPP connection ornull
if given connection isnull
- Returns:
- the Socks5BytestreamManager for the given XMPP connection
-
addIncomingBytestreamListener
public void addIncomingBytestreamListener(BytestreamListener listener)
Adds BytestreamListener that is called for every incoming SOCKS5 Bytestream request unless there is a user specific BytestreamListener registered.If no listeners are registered all SOCKS5 Bytestream request are rejected with a <not-acceptable/> error.
Note that the registered
BytestreamListener
will NOT be notified on incoming Socks5 bytestream requests sent in the context of XEP-0096 file transfer. (SeeFileTransferManager
)- Specified by:
addIncomingBytestreamListener
in interfaceBytestreamManager
- Parameters:
listener
- the listener to register
-
removeIncomingBytestreamListener
public void removeIncomingBytestreamListener(BytestreamListener listener)
Removes the given listener from the list of listeners for all incoming SOCKS5 Bytestream requests.- Specified by:
removeIncomingBytestreamListener
in interfaceBytestreamManager
- Parameters:
listener
- the listener to remove
-
addIncomingBytestreamListener
public void addIncomingBytestreamListener(BytestreamListener listener, Jid initiatorJID)
Adds BytestreamListener that is called for every incoming SOCKS5 Bytestream request from the given user.Use this method if you are awaiting an incoming SOCKS5 Bytestream request from a specific user.
If no listeners are registered all SOCKS5 Bytestream request are rejected with a <not-acceptable/> error.
Note that the registered
BytestreamListener
will NOT be notified on incoming Socks5 bytestream requests sent in the context of XEP-0096 file transfer. (SeeFileTransferManager
)- Specified by:
addIncomingBytestreamListener
in interfaceBytestreamManager
- Parameters:
listener
- the listener to registerinitiatorJID
- the JID of the user that wants to establish a SOCKS5 Bytestream
-
removeIncomingBytestreamListener
public void removeIncomingBytestreamListener(Jid initiatorJID)
Removes the listener for the given user.- Specified by:
removeIncomingBytestreamListener
in interfaceBytestreamManager
- Parameters:
initiatorJID
- the JID of the user the listener should be removed
-
ignoreBytestreamRequestOnce
public void ignoreBytestreamRequestOnce(String sessionID)
Use this method to ignore the next incoming SOCKS5 Bytestream request containing the given session ID. No listeners will be notified for this request and and no error will be returned to the initiator.This method should be used if you are awaiting a SOCKS5 Bytestream request as a reply to another stanza (e.g. file transfer).
- Parameters:
sessionID
- to be ignored
-
disableService
public void disableService()
Disables the SOCKS5 Bytestream manager by removing the SOCKS5 Bytestream feature from the service discovery, disabling the listener for SOCKS5 Bytestream initiation requests and resetting its internal state, which includes removing this instance from the managers map.To re-enable the SOCKS5 Bytestream feature invoke
getBytestreamManager(XMPPConnection)
. Using the file transfer API will automatically re-enable the SOCKS5 Bytestream feature.
-
getTargetResponseTimeout
public int getTargetResponseTimeout()
Returns the timeout to wait for the response to the SOCKS5 Bytestream initialization request. Default is 10000ms.- Returns:
- the timeout to wait for the response to the SOCKS5 Bytestream initialization request
-
setTargetResponseTimeout
public void setTargetResponseTimeout(int targetResponseTimeout)
Sets the timeout to wait for the response to the SOCKS5 Bytestream initialization request. Default is 10000ms.- Parameters:
targetResponseTimeout
- the timeout to set
-
getProxyConnectionTimeout
public int getProxyConnectionTimeout()
Returns the timeout for connecting to the SOCKS5 proxy selected by the target. Default is 10000ms.- Returns:
- the timeout for connecting to the SOCKS5 proxy selected by the target
-
setProxyConnectionTimeout
public void setProxyConnectionTimeout(int proxyConnectionTimeout)
Sets the timeout for connecting to the SOCKS5 proxy selected by the target. Default is 10000ms.- Parameters:
proxyConnectionTimeout
- the timeout to set
-
isProxyPrioritizationEnabled
public boolean isProxyPrioritizationEnabled()
Returns if the prioritization of the last working SOCKS5 proxy on successive SOCKS5 Bytestream connections is enabled. Default istrue
.- Returns:
true
if prioritization is enabled,false
otherwise
-
setProxyPrioritizationEnabled
public void setProxyPrioritizationEnabled(boolean proxyPrioritizationEnabled)
Enable/disable the prioritization of the last working SOCKS5 proxy on successive SOCKS5 Bytestream connections.- Parameters:
proxyPrioritizationEnabled
- enable/disable the prioritization of the last working SOCKS5 proxy
-
isAnnouncingLocalStreamHostEnabled
public boolean isAnnouncingLocalStreamHostEnabled()
Returns if the bytestream manager will announce the local stream host(s), i.e. the local SOCKS5 proxy.Local stream hosts will be announced if this option is enabled and at least one is running.
- Returns:
true
if- Since:
- 4.4.0
-
setAnnounceLocalStreamHost
public void setAnnounceLocalStreamHost(boolean announceLocalStreamHost)
Set whether or not the bytestream manager will annouce the local stream host(s), i.e. the local SOCKS5 proxy.- Parameters:
announceLocalStreamHost
- TODO javadoc me please- Since:
- 4.4.0
- See Also:
isAnnouncingLocalStreamHostEnabled()
-
establishSession
public Socks5BytestreamSession establishSession(Jid targetJID) throws XMPPException, IOException, InterruptedException, SmackException
Establishes a SOCKS5 Bytestream with the given user and returns the Socket to send/receive data to/from the user.Use this method to establish SOCKS5 Bytestreams to users accepting all incoming Socks5 bytestream requests since this method doesn't provide a way to tell the user something about the data to be sent.
To establish a SOCKS5 Bytestream after negotiation the kind of data to be sent (e.g. file transfer) use
establishSession(Jid, String)
.- Specified by:
establishSession
in interfaceBytestreamManager
- Parameters:
targetJID
- the JID of the user a SOCKS5 Bytestream should be established- Returns:
- the Socket to send/receive data to/from the user
- Throws:
XMPPException
- if the user doesn't support or accept SOCKS5 Bytestreams, if no Socks5 Proxy could be found, if the user couldn't connect to any of the SOCKS5 ProxiesIOException
- if the bytestream could not be establishedInterruptedException
- if the current thread was interrupted while waitingSmackException
- if there was no response from the server.
-
establishSession
public Socks5BytestreamSession establishSession(Jid targetJID, String sessionID) throws IOException, InterruptedException, XMPPException, SmackException.NoResponseException, SmackException.NotConnectedException, SmackException.SmackMessageException, SmackException.FeatureNotSupportedException
Establishes a SOCKS5 Bytestream with the given user using the given session ID and returns the Socket to send/receive data to/from the user.- Specified by:
establishSession
in interfaceBytestreamManager
- Parameters:
targetJID
- the JID of the user a SOCKS5 Bytestream should be establishedsessionID
- the session ID for the SOCKS5 Bytestream request- Returns:
- the Socket to send/receive data to/from the user
- Throws:
IOException
- if the bytestream could not be establishedInterruptedException
- if the current thread was interrupted while waitingXMPPException
- if an XMPP protocol error was received.SmackException.NotConnectedException
- if the XMPP connection is not connected.SmackException.NoResponseException
- if there was no response from the remote entity.SmackException.SmackMessageException
- if there was an error.SmackException.FeatureNotSupportedException
- if a requested feature is not supported by the remote entity.
-
determineProxies
public List<Jid> determineProxies() throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException
Returns a list of JIDs of SOCKS5 proxies by querying the XMPP server. The SOCKS5 proxies are in the same order as returned by the XMPP server.- Returns:
- list of JIDs of SOCKS5 proxies
- Throws:
XMPPException.XMPPErrorException
- if there was an error querying the XMPP server for SOCKS5 proxiesSmackException.NoResponseException
- if there was no response from the server.SmackException.NotConnectedException
- if the XMPP connection is not connected.InterruptedException
- if the calling thread was interrupted.
-
getLocalStreamHost
public List<Bytestream.StreamHost> 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.- Returns:
- the stream host information of the local SOCKS5 proxy or null if local SOCKS5 proxy is not running
-
replyRejectPacket
protected void replyRejectPacket(IQ packet) throws SmackException.NotConnectedException, InterruptedException
Responses to the given packet's sender with an XMPP error that a SOCKS5 Bytestream is not accepted.Specified in XEP-65 5.3.1 (Example 13)
- Parameters:
packet
- Stanza that should be answered with a not-acceptable error- Throws:
SmackException.NotConnectedException
- if the XMPP connection is not connected.InterruptedException
- if the calling thread was interrupted.
-
getConnection
protected XMPPConnection getConnection()
Returns the XMPP connection.- Returns:
- the XMPP connection
-
getUserListener
protected BytestreamListener getUserListener(Jid initiator)
Returns theBytestreamListener
that should be informed if a SOCKS5 Bytestream request from the given initiator JID is received.- Parameters:
initiator
- the initiator's JID- Returns:
- the listener
-
getAllRequestListeners
protected List<BytestreamListener> getAllRequestListeners()
Returns a list ofBytestreamListener
that are informed if there are no listeners for a specific initiator.- Returns:
- list of listeners
-
getIgnoredBytestreamRequests
protected List<String> getIgnoredBytestreamRequests()
Returns the list of session IDs that should be ignored by the InitialtionListener- Returns:
- list of session IDs
-
-