Skip to content

Commit

Permalink
feat: better proxy from URI handling
Browse files Browse the repository at this point in the history
  • Loading branch information
drallieiv committed May 21, 2017
1 parent 045ea76 commit 4fe1ec2
Showing 1 changed file with 49 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -28,6 +32,9 @@
@Getter
public class HttpProxy implements HttpProxyProvider {

private static final int DEFAULT_PORT = 8080;
private static final String DEFAULT_PROTOCOL = "http";

private static Logger logger = LoggerFactory.getLogger(HttpProxy.class);

// Protocol
Expand All @@ -47,7 +54,7 @@ public class HttpProxy implements HttpProxyProvider {

// Type HTTP or SOCKS
private Type type;

// Connction timeout, default is 60s
@Setter
private int connectionTimeout = 60;
Expand All @@ -57,6 +64,17 @@ public class HttpProxy implements HttpProxyProvider {
"(?:(?<host>[a-zA-Z0-9\\.\\-_]+)" +
"(?::(?<port>\\d{1,5}))?)$";

static final Map<String, Integer> DEFAULT_MAPPING = Collections.unmodifiableMap(new HashMap<String, Integer>() {
private static final long serialVersionUID = 1L;
{
put("http", 8080);
put("https", 3128);
put("socks", 1080);
put("socks4", 1080);
put("socks5", 1080);
}
});

/**
* Constructor for a Http Proxy with auth
*
Expand Down Expand Up @@ -108,40 +126,45 @@ public static HttpProxy fromURI(String uri) {
if (matcher.find()) {
String host = matcher.group("host");

// Type from protocol. default to HTTP.
Type type = StringUtils.startsWith(StringUtils.lowerCase(matcher.group("protocol")), "socks") ? Type.SOCKS : Type.HTTP;
String protocol = StringUtils.lowerCase(matcher.group("protocol"));

String login = matcher.group("login");
String pass = matcher.group("pass");
String protocol = matcher.group("protocol");

// Port given or default from protocol.
int port;
int port = -1;
if (matcher.group("port") != null) {
port = Integer.parseInt(matcher.group("port"));
}

// Full Default is HTTP port 8080
if (port < 0 && protocol == null) {
protocol = "http";
port = DEFAULT_PORT;
} else {
if (protocol == null) {
port = 8080;
} else {
switch (protocol.toLowerCase()) {
case "http":
port = 8080;
break;
case "https":
port = 443;
break;
case "socks":
case "socks4":
case "socks5":
port = 1080;
break;
default:
port = 8080;
break;
if (port < 0) {
// Guess port from Protocol
for (Entry<String, Integer> entry : DEFAULT_MAPPING.entrySet()) {
if (entry.getValue() == port) {
protocol = entry.getKey();
break;
}
}
if (protocol == null) {
protocol = DEFAULT_PROTOCOL;
}
} else if (protocol == null) {
// Guess protocol from port
if (DEFAULT_MAPPING.get(protocol) != null) {
port = DEFAULT_MAPPING.get(protocol);
} else {
port = DEFAULT_PORT;
}
}
}

// Type from protocol. default to HTTP.
Type type = StringUtils.startsWith(protocol, "socks") ? Type.SOCKS : Type.HTTP;

return new HttpProxy(host, port, login, pass, type, protocol);
} else {
logger.warn("Cannot load URI [{}] as a HTTP Proxy", uri);
Expand All @@ -165,7 +188,7 @@ public HttpProxy(String host, int port) {
@Override
public OkHttpClient getClient() {
Builder clientBuilder = new OkHttpClient.Builder();

// TimeOuts
clientBuilder.connectTimeout(connectionTimeout, TimeUnit.SECONDS);
clientBuilder.readTimeout(2 * connectionTimeout, TimeUnit.SECONDS);
Expand Down Expand Up @@ -203,4 +226,4 @@ public String toString() {
return toURI();
}

}
}

0 comments on commit 4fe1ec2

Please sign in to comment.