Skip to content

Commit

Permalink
add http proxy type
Browse files Browse the repository at this point in the history
  • Loading branch information
asep.rojali committed Jul 10, 2023
1 parent 4b2b7e6 commit 91c9e17
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 18 deletions.
9 changes: 1 addition & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>avew.github.io</groupId>
<artifactId>retrofit-mask</artifactId>
<version>1.0.7</version>
<version>1.0.10</version>
<packaging>jar</packaging>
<description>Custom retrofit with masking log</description>

Expand Down Expand Up @@ -205,13 +205,6 @@
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.16.0</version>
<configuration>
<excludes>
<exclude>
org.springframework.boot:spring-boot-starter-parent
</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/io/github/avew/CustomProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class CustomProxy {
@Builder.Default
private boolean proxy = false;
@Builder.Default
private ProxyType type = ProxyType.HTTP;
@Builder.Default
private boolean auth = false;
private String username;
private String password;
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/io/github/avew/ProxyType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.github.avew;

public enum ProxyType {
HTTP,
SOCKS
}
44 changes: 34 additions & 10 deletions src/main/java/io/github/avew/VewHttp.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,32 @@ public OkHttpClient setupClient() throws NoSuchAlgorithmException, KeyManagement
log.info("CONNECTION TO {} USE PROXY", config.getUrl());

if (proxy.isAuth()) {
log.info("CONNECTION TO {} USE PROXY WITH AUTH", config.getUrl());
Authenticator proxyAuthenticator = (route, response) -> {
String credential = Credentials.basic(proxy.getUsername(), proxy.getPassword());
return response.request().newBuilder()
.header("Proxy-Authorization", credential)
.build();
};
httpClient.proxyAuthenticator(proxyAuthenticator);
log.info("CONNECTION TO {} USE PROXY WITH AUTH AND PROXY TYPE {}", config.getUrl(), proxy.getType());
switch (proxy.getType()) {
case SOCKS:
java.net.Authenticator.setDefault(new java.net.Authenticator() {
private final PasswordAuthentication authentication = new PasswordAuthentication(proxy.getUsername(), proxy.getPassword().toCharArray());

@Override
protected PasswordAuthentication getPasswordAuthentication() {
return authentication;
}
});
httpClient.proxy(new Proxy(Proxy.Type.SOCKS, InetSocketAddress.createUnresolved(proxy.getHost(), proxy.getPort())));
break;
case HTTP:
Authenticator proxyAuthenticator = (route, response) -> {
String credential = Credentials.basic(proxy.getUsername(), proxy.getPassword());
return response.request().newBuilder()
.header("Proxy-Authorization", credential)
.build();
};
httpClient.authenticator(proxyAuthenticator);
httpClient.proxyAuthenticator(proxyAuthenticator);
httpClient.proxy(new Proxy(Proxy.Type.HTTP, InetSocketAddress.createUnresolved(proxy.getHost(), proxy.getPort())));
break;

}
}

httpClient.proxySelector(new ProxySelector() {
Expand All @@ -131,8 +149,14 @@ public List<Proxy> select(URI uri) {
return List.of(Proxy.NO_PROXY);
} else {
// Add Proxy
return List.of(new Proxy(Proxy.Type.HTTP,
new InetSocketAddress(proxy.getHost(), proxy.getPort())));
if (proxy.getType().equals(ProxyType.HTTP)) {
return List.of(new Proxy(Proxy.Type.HTTP,
new InetSocketAddress(proxy.getHost(), proxy.getPort())));
} else {
return List.of(new Proxy(Proxy.Type.SOCKS,
new InetSocketAddress(proxy.getHost(), proxy.getPort())));
}

}
}

Expand Down

0 comments on commit 91c9e17

Please sign in to comment.