in httpcore5-testing/src/main/java/org/apache/hc/core5/testing/SocksProxy.java [68:194]
public void start() {
new Thread(new Runnable() {
@Override
public void run() {
try {
final DataInputStream input = new DataInputStream(socket.getInputStream());
final DataOutputStream output = new DataOutputStream(socket.getOutputStream());
final Socket target = establishConnection(input, output);
remote = target;
final Thread t1 = pumpStream(input, target.getOutputStream());
final Thread t2 = pumpStream(target.getInputStream(), output);
try {
t1.join();
} catch (final InterruptedException ignored) {
}
try {
t2.join();
} catch (final InterruptedException ignored) {
}
} catch (final IOException ignored) {
} finally {
parent.cleanupSocksProxyHandler(SocksProxyHandler.this);
}
}
private Socket establishConnection(final DataInputStream input, final DataOutputStream output) throws IOException {
final int clientVersion = input.readUnsignedByte();
if (clientVersion != VERSION_5) {
throw new IOException("SOCKS implementation only supports version 5");
}
final int nMethods = input.readUnsignedByte();
for (int i = 0; i < nMethods; i++) {
input.readUnsignedByte(); // auth method
}
// response
output.writeByte(VERSION_5);
output.writeByte(0); // no auth method
output.flush();
input.readUnsignedByte(); // client version again
final int command = input.readUnsignedByte();
if (command != COMMAND_CONNECT) {
throw new IOException("SOCKS implementation only supports CONNECT command");
}
input.readUnsignedByte(); // reserved
final String targetHost;
final byte[] targetAddress;
final int addressType = input.readUnsignedByte();
switch (addressType) {
case InetAddressUtils.IPV4:
targetHost = null;
targetAddress = new byte[4];
for (int i = 0; i < targetAddress.length; i++) {
targetAddress[i] = input.readByte();
}
break;
case InetAddressUtils.IPV6:
targetHost = null;
targetAddress = new byte[16];
for (int i = 0; i < targetAddress.length; i++) {
targetAddress[i] = input.readByte();
}
break;
case ATYP_DOMAINNAME:
final int length = input.readUnsignedByte();
final StringBuilder domainname = new StringBuilder();
for (int i = 0; i < length; i++) {
domainname.append((char) input.readUnsignedByte());
}
targetHost = domainname.toString();
targetAddress = null;
break;
default:
throw new IOException("Unsupported address type: " + addressType);
}
final int targetPort = input.readUnsignedShort();
final Socket target;
if (targetHost != null) {
target = new Socket(targetHost, targetPort);
} else {
target = new Socket(InetAddress.getByAddress(targetAddress), targetPort);
}
output.writeByte(VERSION_5);
output.writeByte(0); /* success */
output.writeByte(0); /* reserved */
final byte[] localAddress = target.getLocalAddress().getAddress();
if (localAddress.length == 4) {
output.writeByte(InetAddressUtils.IPV4);
} else if (localAddress.length == 16) {
output.writeByte(InetAddressUtils.IPV6);
} else {
throw new IOException("Unsupported localAddress byte length: " + localAddress.length);
}
output.write(localAddress);
output.writeShort(target.getLocalPort());
output.flush();
return target;
}
private Thread pumpStream(final InputStream input, final OutputStream output) {
final Thread t = new Thread(() -> {
final byte[] buffer = new byte[1024 * 8];
try {
while (true) {
final int read = input.read(buffer);
if (read < 0) {
break;
}
output.write(buffer, 0, read);
output.flush();
}
} catch (final IOException ignored) {
} finally {
shutdown();
}
});
t.start();
return t;
}
}).start();
}