-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
isayan
committed
Mar 11, 2023
1 parent
e2c8ac0
commit aa3a1d2
Showing
9 changed files
with
218 additions
and
108 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 13 additions & 12 deletions
25
src/main/java/okhttp/socks/SocksProxyAuthInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,27 @@ | ||
package okhttp.socks; | ||
|
||
import java.io.IOException; | ||
import java.net.Authenticator; | ||
import java.net.PasswordAuthentication; | ||
import okhttp3.Interceptor; | ||
import okhttp3.Response; | ||
|
||
public class SocksProxyAuthInterceptor implements Interceptor { | ||
private final PasswordAuthentication credentials; | ||
|
||
private final PasswordAuthentication authentication; | ||
|
||
public SocksProxyAuthInterceptor(PasswordAuthentication socksAuthentication) { | ||
this.authentication = socksAuthentication; | ||
public SocksProxyAuthInterceptor(PasswordAuthentication credentials) { | ||
this.credentials = credentials; | ||
} | ||
|
||
@Override | ||
public Response intercept(Chain chain) throws IOException { | ||
SocksProxyAuthenticator.getInstance().setCredentials(this.authentication); | ||
try { | ||
return chain.proceed(chain.request()); | ||
} finally { | ||
SocksProxyAuthenticator.clearCredentials(); | ||
public Response intercept(Interceptor.Chain chain) throws IOException { | ||
synchronized(Authenticator.class) { | ||
SocksProxyAuthenticator.getInstance().setCredentials(credentials); | ||
try { | ||
return chain.proceed(chain.request()); | ||
} finally { | ||
SocksProxyAuthenticator.resetCredentials(); | ||
} | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,38 @@ | ||
package okhttp.socks; | ||
|
||
import extension.helpers.HttpUtil; | ||
import java.net.Authenticator; | ||
import java.net.PasswordAuthentication; | ||
|
||
public class SocksProxyAuthenticator extends Authenticator { | ||
|
||
private final ThreadLocal<PasswordAuthentication> credentials = new ThreadLocal<>(); | ||
private final ThreadLocal<Authenticator> saveAuthenticator = new ThreadLocal<>(); | ||
|
||
private SocksProxyAuthenticator() { | ||
} | ||
private SocksProxyAuthenticator(){} | ||
|
||
private static class SingletonHolder { | ||
private static final SocksProxyAuthenticator instance = new SocksProxyAuthenticator(); | ||
} | ||
|
||
public static final SocksProxyAuthenticator getInstance() { | ||
public static SocksProxyAuthenticator getInstance() { | ||
return SingletonHolder.instance; | ||
} | ||
|
||
public void setCredentials(PasswordAuthentication socksAuthentication) { | ||
public void setCredentials(PasswordAuthentication credentials) { | ||
SocksProxyAuthenticator authenticator = SocksProxyAuthenticator.getInstance(); | ||
Authenticator.setDefault(authenticator); | ||
authenticator.credentials.set(socksAuthentication); | ||
//Authenticator.setDefault(authenticator); | ||
authenticator.saveAuthenticator.set(HttpUtil.putAuthenticator(authenticator)); | ||
authenticator.credentials.set(credentials); | ||
} | ||
|
||
public static void clearCredentials() { | ||
public static void resetCredentials() { | ||
SocksProxyAuthenticator authenticator = SocksProxyAuthenticator.getInstance(); | ||
Authenticator.setDefault(authenticator); | ||
authenticator.credentials.set(null); | ||
Authenticator.setDefault(authenticator.saveAuthenticator.get()); | ||
authenticator.credentials.remove(); | ||
} | ||
|
||
@Override | ||
public PasswordAuthentication getPasswordAuthentication() { | ||
return credentials.get(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.