Skip to content

Commit

Permalink
Fix proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
frombetelgeuse committed Sep 3, 2020
1 parent 8e3f8d0 commit 51baedf
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 38 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ dependencies {

implementation 'info.picocli:picocli:4.5.0'

implementation 'com.github.crowdin:crowdin-api-client-java:1.2.8'
implementation 'com.github.crowdin:crowdin-api-client-java:1.2.10'

testImplementation 'org.junit.jupiter:junit-jupiter:5.5.2'
testImplementation 'org.hamcrest:hamcrest:2.2'
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/crowdin/cli/BaseCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ public class BaseCli {
public static final String URL_OAUTH_AUTH = "https://accounts.crowdin.com/oauth/authorize?client_id=%s&redirect_uri=%s&response_type=token&scope=project";

public static final String OAUTH_CLIENT_ID = "wQEqvhU3vLOa2XicmUyT";

public static final String HTTP_PROXY_HOST_ENV = "HTTP_PROXY_HOST";
public static final String HTTP_PROXY_PORT_ENV = "HTTP_PROXY_PORT";
public static final String HTTP_PROXY_USER_ENV = "HTTP_PROXY_USER";
public static final String HTTP_PROXY_PASSWORD_ENV = "HTTP_PROXY_PASSWORD";
}
34 changes: 0 additions & 34 deletions src/main/java/com/crowdin/cli/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,10 @@
import com.crowdin.cli.commands.picocli.CommandNames;
import com.crowdin.cli.commands.picocli.PicocliRunner;

import java.net.Authenticator;
import java.net.PasswordAuthentication;

public class Cli {

public static void main(String[] args) {
try {
setSystemProperties();

PicocliRunner picocliRunner = PicocliRunner.getInstance();
Actions actions = new CliActions();
int exitCode = picocliRunner.execute(actions, args);
Expand All @@ -27,33 +22,4 @@ public static void main(String[] args) {
e.printStackTrace();
}
}

private static void setSystemProperties() {
if (System.getenv("HTTP_PROXY_HOST") != null) {
System.setProperty("http.proxyHost", System.getenv("HTTP_PROXY_HOST"));
System.setProperty("https.proxyHost", System.getenv("HTTP_PROXY_HOST"));
}
if (System.getenv("HTTP_PROXY_PORT") != null) {
System.setProperty("http.proxyPort", System.getenv("HTTP_PROXY_PORT"));
System.setProperty("https.proxyPort", System.getenv("HTTP_PROXY_PORT"));
}
String proxyUser = System.getenv("HTTP_PROXY_USER");
String proxyPassword = System.getenv("HTTP_PROXY_PASSWORD");

if (proxyUser != null && proxyPassword != null) {
Authenticator.setDefault(
new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
if (getRequestorType() == RequestorType.PROXY) {
return new PasswordAuthentication(proxyUser, proxyPassword.toCharArray());
} else {
return null;
}
}
}
);
System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
}
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/crowdin/cli/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ static Client getDefault(String apiToken, String baseUrl, long projectId) {
.jsonTransformer(new JacksonJsonTransformer())
.userAgent(Utils.buildUserAgent())
.build();
Utils.proxyHost()
.map(pair -> new ClientConfig.Host(pair.getKey(), pair.getValue()))
.ifPresent(clientConfig::setProxy);
Utils.proxyCredentials()
.map(pair -> new ClientConfig.UsernamePasswordCredentials(pair.getKey(), pair.getValue()))
.ifPresent(clientConfig::setProxyCreds);
com.crowdin.client.Client client = new com.crowdin.client.Client(credentials, clientConfig);
return new CrowdinClient(client, projectId);
}
Expand Down
30 changes: 27 additions & 3 deletions src/main/java/com/crowdin/cli/utils/Utils.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package com.crowdin.cli.utils;

import com.crowdin.cli.BaseCli;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;

import java.io.IOException;
import java.net.URL;
import java.nio.file.FileSystems;
import java.util.List;
import java.util.Optional;
import java.util.ResourceBundle;

import static com.crowdin.cli.BaseCli.HTTP_PROXY_HOST_ENV;
import static com.crowdin.cli.BaseCli.HTTP_PROXY_PASSWORD_ENV;
import static com.crowdin.cli.BaseCli.HTTP_PROXY_PORT_ENV;
import static com.crowdin.cli.BaseCli.HTTP_PROXY_USER_ENV;
import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE;


Expand Down Expand Up @@ -80,4 +83,25 @@ public static String normalizePath(String path) {
public static String regexPath(String path) {
return path.replaceAll("\\\\", "\\\\\\\\");
}

public static Optional<Pair<String, Integer>> proxyHost() {
if (System.getenv(HTTP_PROXY_HOST_ENV) == null || System.getenv(HTTP_PROXY_PORT_ENV) == null) {
return Optional.empty();
}
Integer port;
try {
port = new Integer(System.getenv(HTTP_PROXY_PORT_ENV));
} catch (NumberFormatException e) {
return Optional.empty();
}
return Optional.of(new ImmutablePair<>(System.getenv(HTTP_PROXY_HOST_ENV), port));
}

public static Optional<Pair<String, String>> proxyCredentials() {
if (System.getenv(HTTP_PROXY_USER_ENV) != null && System.getenv(HTTP_PROXY_PASSWORD_ENV) != null) {
return Optional.of(new ImmutablePair<>(System.getenv(HTTP_PROXY_USER_ENV), System.getenv(HTTP_PROXY_PASSWORD_ENV)));
} else {
return Optional.empty();
}
}
}

0 comments on commit 51baedf

Please sign in to comment.