Skip to content

Commit

Permalink
Add update tool server access via proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
udda1996 committed Sep 12, 2023
1 parent 49ae472 commit b2d6d07
Showing 1 changed file with 73 additions and 25 deletions.
98 changes: 73 additions & 25 deletions src/main/java/org/ballerinalang/command/util/ToolUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import me.tongfei.progressbar.ProgressBarStyle;
import org.ballerinalang.command.Main;

import com.moandjiezana.toml.Toml;

import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
Expand All @@ -30,7 +33,11 @@
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
Expand All @@ -39,7 +46,9 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -57,6 +66,7 @@ public class ToolUtil {
public static final String CLI_HELP_FILE_PREFIX = "dist-";
private static final String BALLERINA_1_X_VERSIONS = "1.0.";
private static final String CONNECTION_ERROR_MESSAGE = "connection to the remote server failed";
private static final String BALLERINA_SETTINGS_FILE = "Settings.toml";
public static final boolean BALLERINA_STAGING_UPDATE = Boolean.parseBoolean(
System.getenv("BALLERINA_STAGING_UPDATE"));
public static final boolean BALLERINA_DEV_UPDATE = Boolean.parseBoolean(
Expand Down Expand Up @@ -193,13 +203,52 @@ public static boolean checkDependencyAvailable(String dependency) {
return dependencyLocation.exists();
}

public static HttpsURLConnection getServerUrlWithProxyAuthentication(URL serverURL) throws IOException {
Map<String, Object> proxyConfigs = getProxyConfigs();
String proxyHost = proxyConfigs.containsKey("host") ? proxyConfigs.get("host").toString() : null;
String proxyPort = proxyConfigs.containsKey("port") ? proxyConfigs.get("port").toString() : null;
String proxyUser = proxyConfigs.containsKey("user") ? proxyConfigs.get("user").toString() : null;
String proxyPassword = proxyConfigs.containsKey("password") ? proxyConfigs.get("password").toString() : null;

if (proxyHost != null && proxyPort != null && !"".equals(proxyHost) && Integer.parseInt(proxyPort) > 0 &&
Integer.parseInt(proxyPort) < 65536) {
if (proxyUser != null && proxyPassword != null && !"".equals(proxyUser) && !"".equals(proxyPassword)) {
Authenticator authenticator = new Authenticator() {

Check warning on line 216 in src/main/java/org/ballerinalang/command/util/ToolUtil.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/ballerinalang/command/util/ToolUtil.java#L216

Added line #L216 was not covered by tests
@Override
public PasswordAuthentication getPasswordAuthentication() {
return (new PasswordAuthentication(proxyUser,
proxyPassword.toCharArray()));

Check warning on line 220 in src/main/java/org/ballerinalang/command/util/ToolUtil.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/ballerinalang/command/util/ToolUtil.java#L219-L220

Added lines #L219 - L220 were not covered by tests
}
};
Authenticator.setDefault(authenticator);

Check warning on line 223 in src/main/java/org/ballerinalang/command/util/ToolUtil.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/ballerinalang/command/util/ToolUtil.java#L223

Added line #L223 was not covered by tests
}
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, Integer.parseInt(proxyPort)));
return (HttpsURLConnection) serverURL.openConnection(proxy);

Check warning on line 226 in src/main/java/org/ballerinalang/command/util/ToolUtil.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/ballerinalang/command/util/ToolUtil.java#L225-L226

Added lines #L225 - L226 were not covered by tests
} else {
return (HttpsURLConnection) serverURL.openConnection();
}
}

public static Map<String, Object> getProxyConfigs () {
Map<String, Object> proxyConfigs = new HashMap<>();
File settingsFile = new File(OSUtils.getBallerinaHomePath() + File.separator +
BALLERINA_SETTINGS_FILE );
if (settingsFile.exists()) {
Toml toml = new Toml().read(settingsFile);

Check warning on line 237 in src/main/java/org/ballerinalang/command/util/ToolUtil.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/ballerinalang/command/util/ToolUtil.java#L237

Added line #L237 was not covered by tests
if (toml.contains("proxy")) {
proxyConfigs = toml.getTable("proxy").toMap();

Check warning on line 239 in src/main/java/org/ballerinalang/command/util/ToolUtil.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/ballerinalang/command/util/ToolUtil.java#L239

Added line #L239 was not covered by tests
}
}
return proxyConfigs;
}

public static List<Channel> getDistributions(PrintStream printStream) {
HttpURLConnection conn = null;
HttpsURLConnection conn = null;
List<Channel> channels = new ArrayList<>();
List<Distribution> distributions = new ArrayList<>();
try {
URL url = new URL(getServerURL() + "/distributions");
conn = (HttpURLConnection) url.openConnection();
conn = getServerUrlWithProxyAuthentication(url);
conn.setRequestMethod("GET");
conn.setRequestProperty("user-agent",
OSUtils.getUserAgent(getCurrentBallerinaVersion(),
Expand Down Expand Up @@ -263,11 +312,11 @@ public static List<Channel> getDistributions(PrintStream printStream) {
}

public static String getLatest(String currentVersion, String type) {
HttpURLConnection conn = null;
HttpsURLConnection conn = null;
try {
URL url = new URL(getServerURL()
+ "/distributions/latest?version=" + currentVersion + "&type=" + type);
conn = (HttpURLConnection) url.openConnection();
conn = getServerUrlWithProxyAuthentication(url);
conn.setRequestMethod("GET");
conn.setRequestProperty("user-agent",
OSUtils.getUserAgent(getCurrentBallerinaVersion(),
Expand Down Expand Up @@ -299,10 +348,10 @@ private static String getValue(String key, String json) {
}

public static Tool getLatestToolVersion() {
HttpURLConnection conn = null;
HttpsURLConnection conn = null;
try {
URL url = new URL(getServerURL() + "/versions/latest");
conn = (HttpURLConnection) url.openConnection();
conn = getServerUrlWithProxyAuthentication(url);
conn.setRequestMethod("GET");
conn.setRequestProperty("user-agent", OSUtils.getUserAgent(getCurrentBallerinaVersion(),
getCurrentToolsVersion(), "jballerina"));
Expand Down Expand Up @@ -434,11 +483,11 @@ public static void checkForUpdate(PrintStream printStream) {

public static boolean downloadDistribution(PrintStream printStream, String distribution, String distributionType,
String distributionVersion, boolean testMode) {
HttpURLConnection conn = null;
HttpsURLConnection conn = null;
try {
if (!ToolUtil.checkDistributionAvailable(distribution)) {
URL url = new URL(ToolUtil.getServerURL() + "/distributions/" + distributionVersion);
conn = (HttpURLConnection) url.openConnection();
conn = getServerUrlWithProxyAuthentication(url);
conn.setRequestMethod("GET");
conn.setRequestProperty("user-agent",
OSUtils.getUserAgent(getCurrentBallerinaVersion(), ToolUtil.getCurrentToolsVersion(),
Expand All @@ -450,9 +499,9 @@ public static boolean downloadDistribution(PrintStream printStream, String distr
if (conn.getResponseCode() == 302) {
printStream.println("Fetching the '" + distribution + "' distribution from the remote server...");
String newUrl = conn.getHeaderField("Location");
conn = (HttpURLConnection) new URL(newUrl).openConnection();
conn.setRequestProperty("content-type", "binary/data");
ToolUtil.downloadAndSetupDist(printStream, conn, distribution);
HttpsURLConnection redirectedConn = getServerUrlWithProxyAuthentication(new URL(newUrl));
redirectedConn.setRequestProperty("content-type", "binary/data");
ToolUtil.downloadAndSetupDist(printStream, redirectedConn, distribution);

Check warning on line 504 in src/main/java/org/ballerinalang/command/util/ToolUtil.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/ballerinalang/command/util/ToolUtil.java#L502-L504

Added lines #L502 - L504 were not covered by tests
ToolUtil.getDependency(printStream, distribution, distributionType, distributionVersion);
return false;
} else if (conn.getResponseCode() == 200) {
Expand Down Expand Up @@ -511,11 +560,11 @@ private static void downloadAndSetupDist(PrintStream printStream, HttpURLConnect

public static void getDependency(PrintStream printStream, String distribution, String distributionType,
String distributionVersion) {
HttpURLConnection conn = null;
HttpsURLConnection conn = null;
try {
printStream.println("\nFetching the dependencies for '" + distribution + "' from the remote server...");
URL url = new URL(ToolUtil.getServerURL() + "/distributions");
conn = (HttpURLConnection) url.openConnection();
conn = getServerUrlWithProxyAuthentication(url);
conn.setRequestMethod("GET");
conn.setRequestProperty("user-agent",
OSUtils.getUserAgent(distributionVersion, ToolUtil.getCurrentToolsVersion(),
Expand All @@ -535,7 +584,7 @@ public static void getDependency(PrintStream printStream, String distribution, S
while (dependencyMatcher.find()) {
String dependencyName = dependencyMatcher.group(1);
if (!ToolUtil.checkDependencyAvailable(dependencyName)) {
downloadDependency(printStream, conn, dependencyName, distributionType,
downloadDependency(printStream, dependencyName, distributionType,
distributionVersion);
} else {
printStream.println("Dependency '" + dependencyName +
Expand All @@ -555,23 +604,22 @@ public static void getDependency(PrintStream printStream, String distribution, S
}
}

private static void downloadDependency(PrintStream printStream, HttpURLConnection conn,
String dependency, String distributionType,
private static void downloadDependency(PrintStream printStream, String dependency, String distributionType,
String distributionVersion) {
try {
String encodedDependencyName = encodePlusCharacters(dependency);
String url = ToolUtil.getServerURL() + "/dependencies/" + encodedDependencyName;
conn = (HttpURLConnection) new URL(url).openConnection();
HttpsURLConnection conn = getServerUrlWithProxyAuthentication(new URL(url));
conn.setRequestMethod("GET");
conn.setRequestProperty("user-agent",
OSUtils.getUserAgent(distributionVersion, ToolUtil.getCurrentToolsVersion(),
distributionType));
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() == 302) {
String newUrl = conn.getHeaderField("Location");
conn = (HttpURLConnection) new URL(newUrl).openConnection();
conn.setRequestProperty("content-type", "binary/data");
downloadAndSetupDependency(conn, printStream, dependency);
HttpsURLConnection redirectedConn = getServerUrlWithProxyAuthentication(new URL(newUrl));
redirectedConn.setRequestProperty("content-type", "binary/data");
downloadAndSetupDependency(redirectedConn, printStream, dependency);

Check warning on line 622 in src/main/java/org/ballerinalang/command/util/ToolUtil.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/ballerinalang/command/util/ToolUtil.java#L620-L622

Added lines #L620 - L622 were not covered by tests
} else if (conn.getResponseCode() == 200) {
downloadAndSetupDependency(conn, printStream, dependency);
} else {
Expand Down Expand Up @@ -601,19 +649,19 @@ private static void downloadAndSetupDependency(HttpURLConnection conn, PrintStre
}

public static void downloadTool(PrintStream printStream, String toolVersion) {
HttpURLConnection conn = null;
HttpsURLConnection conn = null;
try {
URL url = new URL(ToolUtil.getServerURL() + "/versions/" + toolVersion);
conn = (HttpURLConnection) url.openConnection();
conn = getServerUrlWithProxyAuthentication(url);
conn.setRequestMethod("GET");
conn.setRequestProperty("user-agent", OSUtils.getUserAgent(getCurrentBallerinaVersion(),
getCurrentToolsVersion(), "jballerina"));
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() == 302) {
String newUrl = conn.getHeaderField("Location");
conn = (HttpURLConnection) new URL(newUrl).openConnection();
conn.setRequestProperty("content-type", "binary/data");
downloadAndSetupTool(printStream, conn, "ballerina-command-" + toolVersion);
HttpsURLConnection redirectedConn = getServerUrlWithProxyAuthentication(new URL(newUrl));
redirectedConn.setRequestProperty("content-type", "binary/data");
downloadAndSetupTool(printStream, redirectedConn, "ballerina-command-" + toolVersion);

Check warning on line 664 in src/main/java/org/ballerinalang/command/util/ToolUtil.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/ballerinalang/command/util/ToolUtil.java#L662-L664

Added lines #L662 - L664 were not covered by tests
} else if (conn.getResponseCode() == 200) {
downloadAndSetupTool(printStream, conn, "ballerina-command-" + toolVersion);
} else {
Expand Down

0 comments on commit b2d6d07

Please sign in to comment.