mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2025-12-09 06:31:08 +01:00
Introduce util.InternetAddress
and use it where sensible. Also fixes a few unit tests along the way.
This commit is contained in:
parent
4334ca33ff
commit
b288768f77
11 changed files with 462 additions and 268 deletions
|
|
@ -17,6 +17,7 @@
|
|||
package org.jivesoftware.smackx.bytestreams.socks5;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
|
@ -663,22 +664,16 @@ public final class Socks5BytestreamManager extends Manager implements Bytestream
|
|||
EntityFullJid myJid = connection.getUser();
|
||||
|
||||
for (Socks5Proxy socks5Server : Socks5Proxy.getRunningProxies()) {
|
||||
List<String> addresses = socks5Server.getLocalAddresses();
|
||||
List<InetAddress> addresses = socks5Server.getLocalAddresses();
|
||||
if (addresses.isEmpty()) {
|
||||
// local address could not be determined
|
||||
return null;
|
||||
continue;
|
||||
}
|
||||
final int port = socks5Server.getPort();
|
||||
|
||||
outerloop: for (String address : addresses) {
|
||||
final int port = socks5Server.getPort();
|
||||
for (InetAddress 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) {
|
||||
// Use 'startsWith' here since IPv6 addresses may have scope ID,
|
||||
// ie. the part after the '%' sign.
|
||||
if (address.startsWith(loopbackAddress)) {
|
||||
continue outerloop;
|
||||
}
|
||||
if (address.isLoopbackAddress()) {
|
||||
continue;
|
||||
}
|
||||
streamHosts.add(new StreamHost(myJid, address, port));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -93,7 +93,7 @@ public class Socks5Client {
|
|||
|
||||
// initialize socket
|
||||
Socket socket = new Socket();
|
||||
SocketAddress socketAddress = new InetSocketAddress(streamHost.getAddress(),
|
||||
SocketAddress socketAddress = new InetSocketAddress(streamHost.getAddress().asInetAddress(),
|
||||
streamHost.getPort());
|
||||
socket.connect(socketAddress);
|
||||
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import java.io.DataInputStream;
|
|||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InterfaceAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
|
|
@ -52,7 +53,7 @@ import org.jivesoftware.smack.util.CloseableUtil;
|
|||
* <p>
|
||||
* If your application is running on a machine with multiple network interfaces or if you want to
|
||||
* provide your public address in case you are behind a NAT router, invoke
|
||||
* {@link #addLocalAddress(String)} or {@link #replaceLocalAddresses(Collection)} to modify the list of
|
||||
* {@link #addLocalAddress(InetAddress)} or {@link #replaceLocalAddresses(Collection)} to modify the list of
|
||||
* local network addresses used for outgoing SOCKS5 Bytestream requests.
|
||||
* <p>
|
||||
* The local SOCKS5 proxy server refuses all connections except the ones that are explicitly allowed
|
||||
|
|
@ -81,11 +82,17 @@ public final class Socks5Proxy {
|
|||
|
||||
private static boolean localSocks5ProxyEnabled = true;
|
||||
|
||||
/**
|
||||
* The default port of the local Socks5 Proxy. If this value is negative, the next ports will be tried
|
||||
* until a unused is found.
|
||||
*/
|
||||
private static int DEFAULT_LOCAL_SOCKS5_PROXY_PORT = -7777;
|
||||
|
||||
/**
|
||||
* The port of the local Socks5 Proxy. If this value is negative, the next ports will be tried
|
||||
* until a unused is found.
|
||||
*/
|
||||
private static int localSocks5ProxyPort = -7777;
|
||||
private int localSocks5ProxyPort = -7777;
|
||||
|
||||
/* reusable implementation of a SOCKS5 proxy server process */
|
||||
private Socks5ServerProcess serverProcess;
|
||||
|
|
@ -102,7 +109,7 @@ public final class Socks5Proxy {
|
|||
/* list of digests connections should be stored */
|
||||
private final List<String> allowedConnections = Collections.synchronizedList(new LinkedList<String>());
|
||||
|
||||
private final Set<String> localAddresses = new LinkedHashSet<>(4);
|
||||
private final Set<InetAddress> localAddresses = new LinkedHashSet<>(4);
|
||||
|
||||
/**
|
||||
* Private constructor.
|
||||
|
|
@ -116,21 +123,17 @@ public final class Socks5Proxy {
|
|||
} catch (SocketException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
Set<String> localHostAddresses = new HashSet<>();
|
||||
Set<InetAddress> localAddresses = new HashSet<>();
|
||||
for (NetworkInterface networkInterface : Collections.list(networkInterfaces)) {
|
||||
// We can't use NetworkInterface.getInterfaceAddresses here, which
|
||||
// would return a List instead the deprecated Enumeration, because
|
||||
// it's Android API 9 and Smack currently uses 8. Change that when
|
||||
// we raise Smack's minimum Android API.
|
||||
Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
|
||||
for (InetAddress address : Collections.list(inetAddresses)) {
|
||||
localHostAddresses.add(address.getHostAddress());
|
||||
List<InterfaceAddress> interfaceAddresses = networkInterface.getInterfaceAddresses();
|
||||
for (InterfaceAddress interfaceAddress : interfaceAddresses) {
|
||||
localAddresses.add(interfaceAddress.getAddress());
|
||||
}
|
||||
}
|
||||
if (localHostAddresses.isEmpty()) {
|
||||
throw new IllegalStateException("Could not determine any local host address");
|
||||
if (localAddresses.isEmpty()) {
|
||||
throw new IllegalStateException("Could not determine any local internet address");
|
||||
}
|
||||
replaceLocalAddresses(localHostAddresses);
|
||||
replaceLocalAddresses(localAddresses);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -151,12 +154,27 @@ public final class Socks5Proxy {
|
|||
Socks5Proxy.localSocks5ProxyEnabled = localSocks5ProxyEnabled;
|
||||
}
|
||||
|
||||
private static void checkLocalSocks5ProxyPortArgument(int port) {
|
||||
if (Math.abs(port) > 65535) {
|
||||
throw new IllegalArgumentException("Local SOCKS5 proxy port must be within (-65535,65535)");
|
||||
}
|
||||
}
|
||||
|
||||
public static int getDefaultLocalSocks5ProxyPort() {
|
||||
return DEFAULT_LOCAL_SOCKS5_PROXY_PORT;
|
||||
}
|
||||
|
||||
public static void setDefaultLocalSocsk5ProxyPort(int defaultLocalSocks5ProxyPort) {
|
||||
checkLocalSocks5ProxyPortArgument(defaultLocalSocks5ProxyPort);
|
||||
DEFAULT_LOCAL_SOCKS5_PROXY_PORT = defaultLocalSocks5ProxyPort;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the port of the local Socks5 proxy. Default is 7777.
|
||||
*
|
||||
* @return the port of the local Socks5 proxy
|
||||
*/
|
||||
public static int getLocalSocks5ProxyPort() {
|
||||
public int getLocalSocks5ProxyPort() {
|
||||
return localSocks5ProxyPort;
|
||||
}
|
||||
|
||||
|
|
@ -166,11 +184,9 @@ public final class Socks5Proxy {
|
|||
*
|
||||
* @param localSocks5ProxyPort the port of the local Socks5 proxy to set
|
||||
*/
|
||||
public static void setLocalSocks5ProxyPort(int localSocks5ProxyPort) {
|
||||
if (Math.abs(localSocks5ProxyPort) > 65535) {
|
||||
throw new IllegalArgumentException("localSocks5ProxyPort must be within (-65535,65535)");
|
||||
}
|
||||
Socks5Proxy.localSocks5ProxyPort = localSocks5ProxyPort;
|
||||
public void setLocalSocks5ProxyPort(int localSocks5ProxyPort) {
|
||||
checkLocalSocks5ProxyPortArgument(localSocks5ProxyPort);
|
||||
this.localSocks5ProxyPort = localSocks5ProxyPort;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -269,7 +285,7 @@ public final class Socks5Proxy {
|
|||
*
|
||||
* @param address the local network address to add
|
||||
*/
|
||||
public void addLocalAddress(String address) {
|
||||
public void addLocalAddress(InetAddress address) {
|
||||
if (address == null) {
|
||||
return;
|
||||
}
|
||||
|
|
@ -285,7 +301,7 @@ public final class Socks5Proxy {
|
|||
* @param address the local network address to remove
|
||||
* @return true if the address was removed.
|
||||
*/
|
||||
public boolean removeLocalAddress(String address) {
|
||||
public boolean removeLocalAddress(InetAddress address) {
|
||||
synchronized (localAddresses) {
|
||||
return localAddresses.remove(address);
|
||||
}
|
||||
|
|
@ -297,7 +313,7 @@ public final class Socks5Proxy {
|
|||
*
|
||||
* @return set of the local network addresses
|
||||
*/
|
||||
public List<String> getLocalAddresses() {
|
||||
public List<InetAddress> getLocalAddresses() {
|
||||
synchronized (localAddresses) {
|
||||
return new LinkedList<>(localAddresses);
|
||||
}
|
||||
|
|
@ -313,7 +329,7 @@ public final class Socks5Proxy {
|
|||
*
|
||||
* @param addresses the new list of local network addresses
|
||||
*/
|
||||
public void replaceLocalAddresses(Collection<String> addresses) {
|
||||
public void replaceLocalAddresses(Collection<? extends InetAddress> addresses) {
|
||||
if (addresses == null) {
|
||||
throw new IllegalArgumentException("list must not be null");
|
||||
}
|
||||
|
|
@ -482,12 +498,12 @@ public final class Socks5Proxy {
|
|||
throw new SmackException.SmackMessageException("Connection is not allowed");
|
||||
}
|
||||
|
||||
// Store the connection before we send the return status.
|
||||
Socks5Proxy.this.connectionMap.put(responseDigest, socket);
|
||||
|
||||
connectionRequest[1] = (byte) 0x00; // set return status to 0 (success)
|
||||
out.write(connectionRequest);
|
||||
out.flush();
|
||||
|
||||
// store connection
|
||||
Socks5Proxy.this.connectionMap.put(responseDigest, socket);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,14 +16,15 @@
|
|||
*/
|
||||
package org.jivesoftware.smackx.bytestreams.socks5.packet;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smack.packet.NamedElement;
|
||||
import org.jivesoftware.smack.util.InternetAddress;
|
||||
import org.jivesoftware.smack.util.Objects;
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||
|
||||
import org.jxmpp.jid.Jid;
|
||||
|
|
@ -117,7 +118,7 @@ public class Bytestream extends IQ {
|
|||
* @param address The internet address of the stream host.
|
||||
* @return The added stream host.
|
||||
*/
|
||||
public StreamHost addStreamHost(final Jid JID, final String address) {
|
||||
public StreamHost addStreamHost(final Jid JID, String address) {
|
||||
return addStreamHost(JID, address, 0);
|
||||
}
|
||||
|
||||
|
|
@ -129,7 +130,7 @@ public class Bytestream extends IQ {
|
|||
* @param port The port on which the remote host is seeking connections.
|
||||
* @return The added stream host.
|
||||
*/
|
||||
public StreamHost addStreamHost(final Jid JID, final String address, final int port) {
|
||||
public StreamHost addStreamHost(final Jid JID, String address, final int port) {
|
||||
StreamHost host = new StreamHost(JID, address, port);
|
||||
addStreamHost(host);
|
||||
|
||||
|
|
@ -271,7 +272,7 @@ public class Bytestream extends IQ {
|
|||
|
||||
private final Jid jid;
|
||||
|
||||
private final String address;
|
||||
private final InternetAddress address;
|
||||
|
||||
private final int port;
|
||||
|
||||
|
|
@ -287,8 +288,23 @@ public class Bytestream extends IQ {
|
|||
* @param port port of the stream host.
|
||||
*/
|
||||
public StreamHost(final Jid jid, final String address, int port) {
|
||||
this(jid, InternetAddress.from(address), port);
|
||||
}
|
||||
|
||||
public StreamHost(Jid jid, InetAddress address, int port) {
|
||||
this(jid, InternetAddress.from(address), port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stream Host constructor.
|
||||
*
|
||||
* @param jid The JID of the stream host.
|
||||
* @param address The internet address of the stream host.
|
||||
* @param port port of the stream host.
|
||||
*/
|
||||
public StreamHost(Jid jid, InternetAddress address, int port) {
|
||||
this.jid = Objects.requireNonNull(jid, "StreamHost JID must not be null");
|
||||
this.address = StringUtils.requireNotNullNorEmpty(address, "StreamHost address must not be null");
|
||||
this.address = Objects.requireNonNull(address);
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
|
|
@ -306,7 +322,7 @@ public class Bytestream extends IQ {
|
|||
*
|
||||
* @return Returns the internet address of the stream host.
|
||||
*/
|
||||
public String getAddress() {
|
||||
public InternetAddress getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
|
|
@ -328,7 +344,7 @@ public class Bytestream extends IQ {
|
|||
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
|
||||
XmlStringBuilder xml = new XmlStringBuilder(this);
|
||||
xml.attribute("jid", getJID());
|
||||
xml.attribute("host", getAddress());
|
||||
xml.attribute("host", address);
|
||||
if (getPort() != 0) {
|
||||
xml.attribute("port", Integer.toString(getPort()));
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@
|
|||
package org.jivesoftware.smackx.jingle.transports.jingle_s5b;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
|
@ -162,7 +163,7 @@ public class JingleS5BTransportSession extends JingleTransportSession<JingleS5BT
|
|||
private UsedCandidate connectToTheirCandidate(JingleS5BTransportCandidate candidate)
|
||||
throws InterruptedException, TimeoutException, SmackException, XMPPException, IOException {
|
||||
Bytestream.StreamHost streamHost = candidate.getStreamHost();
|
||||
String address = streamHost.getAddress();
|
||||
InetAddress address = streamHost.getAddress().asInetAddress();
|
||||
Socks5Client socks5Client = new Socks5Client(streamHost, theirProposal.getDestinationAddress());
|
||||
Socket socket = socks5Client.getSocket(10 * 1000);
|
||||
LOGGER.log(Level.INFO, "Connected to their StreamHost " + address + " using dstAddr "
|
||||
|
|
@ -173,7 +174,7 @@ public class JingleS5BTransportSession extends JingleTransportSession<JingleS5BT
|
|||
private UsedCandidate connectToOurCandidate(JingleS5BTransportCandidate candidate)
|
||||
throws InterruptedException, TimeoutException, SmackException, XMPPException, IOException {
|
||||
Bytestream.StreamHost streamHost = candidate.getStreamHost();
|
||||
String address = streamHost.getAddress();
|
||||
InetAddress address = streamHost.getAddress().asInetAddress();
|
||||
Socks5ClientForInitiator socks5Client = new Socks5ClientForInitiator(
|
||||
streamHost, ourProposal.getDestinationAddress(), jingleSession.getConnection(),
|
||||
ourProposal.getStreamId(), jingleSession.getRemote());
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
*/
|
||||
package org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements;
|
||||
|
||||
import org.jivesoftware.smack.util.InternetAddress;
|
||||
import org.jivesoftware.smack.util.Objects;
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||
|
|
@ -40,14 +41,17 @@ public final class JingleS5BTransportCandidate extends JingleContentTransportCan
|
|||
public static final String ATTR_TYPE = "type";
|
||||
|
||||
private final String cid;
|
||||
private final String host;
|
||||
private final InternetAddress host;
|
||||
private final Jid jid;
|
||||
private final int port;
|
||||
private final int priority;
|
||||
private final Type type;
|
||||
|
||||
public JingleS5BTransportCandidate(String candidateId, String host, Jid jid, int port, int priority, Type type) {
|
||||
public JingleS5BTransportCandidate(String candidateId, String hostString, Jid jid, int port, int priority, Type type) {
|
||||
this(candidateId, InternetAddress.from(hostString), jid, port, priority, type);
|
||||
}
|
||||
|
||||
public JingleS5BTransportCandidate(String candidateId, InternetAddress host, Jid jid, int port, int priority, Type type) {
|
||||
Objects.requireNonNull(candidateId);
|
||||
Objects.requireNonNull(host);
|
||||
Objects.requireNonNull(jid);
|
||||
|
|
@ -102,7 +106,7 @@ public final class JingleS5BTransportCandidate extends JingleContentTransportCan
|
|||
return cid;
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
public InternetAddress getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
|
|
@ -149,7 +153,7 @@ public final class JingleS5BTransportCandidate extends JingleContentTransportCan
|
|||
|
||||
public static final class Builder {
|
||||
private String cid;
|
||||
private String host;
|
||||
private InternetAddress host;
|
||||
private Jid jid;
|
||||
private int port = -1;
|
||||
private int priority = -1;
|
||||
|
|
@ -163,7 +167,12 @@ public final class JingleS5BTransportCandidate extends JingleContentTransportCan
|
|||
return this;
|
||||
}
|
||||
|
||||
public Builder setHost(String host) {
|
||||
public Builder setHost(String host) {
|
||||
InternetAddress inetAddress = InternetAddress.from(host);
|
||||
return setHost(inetAddress);
|
||||
}
|
||||
|
||||
public Builder setHost(InternetAddress host) {
|
||||
this.host = host;
|
||||
return this;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue