Skip to content

Commit 7f0c618

Browse files
committed
Authenticate SOCKS proxy (#172)
1 parent eaf165b commit 7f0c618

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

core/src/main/java/xyz/gianlu/librespot/core/Session.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.spotify.Authentication;
55
import com.spotify.Keyexchange;
66
import com.spotify.connectstate.Connect;
7+
import okhttp3.Authenticator;
78
import okhttp3.*;
89
import org.apache.log4j.Logger;
910
import org.jetbrains.annotations.NotNull;
@@ -31,10 +32,7 @@
3132
import javax.crypto.spec.SecretKeySpec;
3233
import java.io.*;
3334
import java.math.BigInteger;
34-
import java.net.InetSocketAddress;
35-
import java.net.Proxy;
36-
import java.net.Socket;
37-
import java.net.SocketTimeoutException;
35+
import java.net.*;
3836
import java.nio.ByteBuffer;
3937
import java.security.*;
4038
import java.security.spec.RSAPublicKeySpec;
@@ -865,6 +863,21 @@ static ConnectionHolder create(@NotNull String addr, @NotNull ProxyConfiguration
865863
LOGGER.info("Successfully connected to the HTTP proxy.");
866864
return new ConnectionHolder(sock);
867865
case SOCKS:
866+
if (conf.proxyAuth()) {
867+
java.net.Authenticator.setDefault(new java.net.Authenticator() {
868+
final String username = conf.proxyUsername();
869+
final String password = conf.proxyPassword();
870+
871+
@Override
872+
protected PasswordAuthentication getPasswordAuthentication() {
873+
if (Objects.equals(getRequestingProtocol(), "SOCKS5") && Objects.equals(getRequestingPrompt(), "SOCKS authentication"))
874+
return new PasswordAuthentication(username, password.toCharArray());
875+
876+
return super.getPasswordAuthentication();
877+
}
878+
});
879+
}
880+
868881
Proxy proxy = new Proxy(conf.proxyType(), new InetSocketAddress(conf.proxyAddress(), conf.proxyPort()));
869882
Socket proxySocket = new Socket(proxy);
870883
proxySocket.connect(new InetSocketAddress(apAddr, apPort));

0 commit comments

Comments
 (0)