From f44526e3ede62efe115f56d180fe7892bb5f7556 Mon Sep 17 00:00:00 2001 From: yndu13 Date: Mon, 4 Sep 2023 12:18:48 +0800 Subject: [PATCH] Support SOCKS5 proxy with username/password authentication --- .../hc/core5/reactor/SocksProxyProtocolHandler.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/httpcore5/src/main/java/org/apache/hc/core5/reactor/SocksProxyProtocolHandler.java b/httpcore5/src/main/java/org/apache/hc/core5/reactor/SocksProxyProtocolHandler.java index 4e40b2626..5db00a28c 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/reactor/SocksProxyProtocolHandler.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/reactor/SocksProxyProtocolHandler.java @@ -93,8 +93,14 @@ private enum State { @Override public void connected(final IOSession session) throws IOException { this.buffer.put(CLIENT_VERSION); - this.buffer.put((byte) 1); - this.buffer.put(NO_AUTHENTICATION_REQUIRED); + if (this.reactorConfig.getSocksProxyUsername() != null && this.reactorConfig.getSocksProxyPassword() != null) { + this.buffer.put((byte) 2); + this.buffer.put(NO_AUTHENTICATION_REQUIRED); + this.buffer.put(USERNAME_PASSWORD); + } else { + this.buffer.put((byte) 1); + this.buffer.put(NO_AUTHENTICATION_REQUIRED); + } this.buffer.flip(); session.setEventMask(SelectionKey.OP_WRITE); } @@ -171,6 +177,7 @@ public void inputReady(final IOSession session, final ByteBuffer src) throws IOE this.buffer.put(username); this.buffer.put((byte) password.length); this.buffer.put(password); + this.buffer.flip(); session.setEventMask(SelectionKey.OP_WRITE); this.state = State.SEND_USERNAME_PASSWORD; } else if (serverMethod == NO_AUTHENTICATION_REQUIRED) {