Smack 4.4 XMPP Proxy Settings with ProxyInfo
Attach ProxyInfo to the XMPP connection builder, with distinct proxy, service-domain and server-host settings.
Configure the connection that actually carries the XMPP stream. Setting a proxy on an unrelated URLConnection does not redirect Smack's TCP connection to the messaging service.
Smack 4.4 provides ProxyInfo factories for HTTP, SOCKS4 and SOCKS5. Attach the resulting object to the XMPP connection builder:
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.proxy.ProxyInfo;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
ProxyInfo proxy = ProxyInfo.forSocks5Proxy(
"proxy.example.net", 1080, proxyUser, proxyPassword
);
XMPPTCPConnectionConfiguration config =
XMPPTCPConnectionConfiguration.builder()
.setXmppDomain("chat.example.net")
.setHost("xmpp-node.example.net")
.setPort(5222)
.setProxyInfo(proxy)
.setSecurityMode(
ConnectionConfiguration.SecurityMode.required
)
.build();
This is a configuration fragment for an existing Smack 4.4 setup. The enclosing application handles the builder's checked exceptions and supplies the proxy credentials. The explicit host and port stand for the XMPP service's actual TCP endpoint.
There are three names with different roles:
proxy.example.netis the intermediary that receives the initial connection.xmpp-node.example.netis the server endpoint the proxy is asked to reach.chat.example.netis the XMPP service domain, such as the domain after@in an account's JID.
A service can use the same name for the latter two, but a proxy hostname does not replace either. Preserve the intended XMPP service identity in the connection and its TLS validation.
For an HTTP proxy, use ProxyInfo.forHttpProxy with the appropriate proxy port and credentials. That path needs a proxy capable of tunneling the XMPP connection and permitting its destination port. A proxy that handles HTTPS web browsing does not automatically permit TCP connections to an XMPP service on 5222.
The required security mode in the fragment makes TLS a condition of the XMPP connection. It does not perform proxy authentication, and the proxy credentials do not log in to an XMPP account. Configure the XMPP account through Smack's normal authentication flow after establishing the route and secure session.
Keep examples within their API generation. The older constructor-based ConnectionConfiguration examples express the same routing concept, but Smack 4.4's builder is the interface used here. There is no need to reproduce an obsolete messaging provider's hostnames to apply that concept to a current XMPP service.