Skip to content

Commit

Permalink
adding proxy setting support from env to be more like other cli's
Browse files Browse the repository at this point in the history
didn't added authenticated proxy support since that would be a much more intrusive change..
  • Loading branch information
Damick committed Aug 4, 2015
1 parent 60aef58 commit 7a48cb8
Showing 1 changed file with 53 additions and 25 deletions.
78 changes: 53 additions & 25 deletions cli/src/main/java/denominator/cli/Denominator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package denominator.cli;

import com.google.common.base.Ascii;
import com.google.common.base.CaseFormat;
import com.google.common.base.Charsets;
import com.google.common.base.Function;
Expand All @@ -23,31 +24,6 @@
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.jce.provider.X509CertParser;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
import org.yaml.snakeyaml.Yaml;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.Type;
import java.security.KeyFactory;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.inject.Singleton;

import dagger.ObjectGraph;
import dagger.Provides;
import denominator.Credentials;
Expand Down Expand Up @@ -86,6 +62,30 @@
import io.airlift.airline.Help;
import io.airlift.airline.Option;
import io.airlift.airline.OptionType;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.jce.provider.X509CertParser;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
import org.yaml.snakeyaml.Yaml;

import javax.inject.Singleton;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.KeyFactory;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import static com.google.common.base.Preconditions.checkArgument;
import static denominator.CredentialsConfiguration.credentials;
Expand Down Expand Up @@ -274,6 +274,8 @@ public static abstract class DenominatorCommand implements Runnable {

@SuppressWarnings("unchecked")
public void run() {
setProxyFromEnv();

if (providerName != null && credentialArgs != null) {
credentials = ListCredentials.from(Lists.transform(credentialArgs, decodeAnyPems));
} else if (providerConfigurationName != null) {
Expand Down Expand Up @@ -426,6 +428,32 @@ void overrideFromEnv(Map<String, String> env) {
}
}

static void setProxyFromEnv() {
setProtocolProxyFromEnv("http");
setProtocolProxyFromEnv("https");
}

static void setProtocolProxyFromEnv(String proto) {
String lowerProto = Ascii.toLowerCase(proto);
String envProxy = System.getenv(Ascii.toUpperCase(proto) + "_PROXY");
if (envProxy != null && !envProxy.isEmpty()) {
try {
URL proxyUrl = new URL(envProxy);

String proxyHost = System.getProperty(lowerProto + ".proxyHost");
if ((proxyHost == null || proxyHost.isEmpty())) {
System.setProperty(lowerProto + ".proxyHost", proxyUrl.getHost());
System.setProperty(lowerProto + ".proxyPort",
Integer.toString(
proxyUrl.getPort() == -1 ? proxyUrl.getDefaultPort() : proxyUrl.getPort()));
}
} catch (MalformedURLException e) {
System.err.println("invalid " + proto + " proxy configuration: " + e.getMessage());
System.exit(1);
}
}
}

/**
* return a lazy iterator where possible to improve the perceived responsiveness of the cli
*/
Expand Down

0 comments on commit 7a48cb8

Please sign in to comment.