flush on socks connection

This commit is contained in:
Daniel Gultsch 2019-10-07 22:02:36 +02:00
parent 799fffcb4a
commit 8578af2b2b

View file

@ -14,23 +14,25 @@ public class SocksSocketFactory {
private static final byte[] LOCALHOST = new byte[]{127,0,0,1}; private static final byte[] LOCALHOST = new byte[]{127,0,0,1};
public static void createSocksConnection(Socket socket, String destination, int port) throws IOException { public static void createSocksConnection(final Socket socket, final String destination, final int port) throws IOException {
final InputStream proxyIs = socket.getInputStream(); final InputStream proxyIs = socket.getInputStream();
final OutputStream proxyOs = socket.getOutputStream(); final OutputStream proxyOs = socket.getOutputStream();
proxyOs.write(new byte[]{0x05, 0x01, 0x00}); proxyOs.write(new byte[]{0x05, 0x01, 0x00});
byte[] response = new byte[2]; proxyOs.flush();
proxyIs.read(response); final byte[] handshake = new byte[2];
if (response[0] != 0x05 || response[1] != 0x00) { proxyIs.read(handshake);
if (handshake[0] != 0x05 || handshake[1] != 0x00) {
throw new SocksConnectionException("Socks 5 handshake failed"); throw new SocksConnectionException("Socks 5 handshake failed");
} }
byte[] dest = destination.getBytes(); final byte[] dest = destination.getBytes();
final ByteBuffer request = ByteBuffer.allocate(7 + dest.length); final ByteBuffer request = ByteBuffer.allocate(7 + dest.length);
request.put(new byte[]{0x05, 0x01, 0x00, 0x03}); request.put(new byte[]{0x05, 0x01, 0x00, 0x03});
request.put((byte) dest.length); request.put((byte) dest.length);
request.put(dest); request.put(dest);
request.putShort((short) port); request.putShort((short) port);
proxyOs.write(request.array()); proxyOs.write(request.array());
response = new byte[7 + dest.length]; proxyOs.flush();
final byte[] response = new byte[7 + dest.length];
proxyIs.read(response); proxyIs.read(response);
if (response[1] != 0x00) { if (response[1] != 0x00) {
if (response[1] == 0x04) { if (response[1] == 0x04) {