Skip to content

Commit

Permalink
add a check for system proxy settings
Browse files Browse the repository at this point in the history
use open source library, proxy_vole (https://github.com/SwissAS/proxy-vole) which is licensed under BSD 3 to check system proxy settings and if set, use them to prefill proxy host and proxy port settings.
  • Loading branch information
ashitsalesforce committed Sep 19, 2024
1 parent 99c74b6 commit 181f51a
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 1 deletion.
4 changes: 4 additions & 0 deletions local-proj-repo/proxy/proxy_vole/1.1.1/_remote.repositories
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#NOTE: This is a Maven Resolver internal implementation file, its format can be changed without prior notice.
#Wed Sep 18 17:42:04 PDT 2024
proxy_vole-1.1.1.jar>=
proxy_vole-1.1.1.pom>=
Binary file not shown.
73 changes: 73 additions & 0 deletions local-proj-repo/proxy/proxy_vole/1.1.1/proxy_vole-1.1.1.pom
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.rosstauscher.proxy_vole</groupId>
<artifactId>proxy_vole</artifactId>
<version>1.1.1</version>
<name>proxy-vole</name>
<description>A Java library to auto detect the platform network proxy settings.

The library provides some proxy setting search strategies to read the proxy settings from the system config (Windows, KDE, Gnome, OSX), browser config (Firefox, IE), or environment variables, ... and provides you an ready to use proxy selector. </description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<configuration>
<!--stylesheetfile>resources/misc/stylesheet.css</stylesheetfile-->
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<includes>
<include>**/*.java</include>
<include>dll/**/*</include>
</includes>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>

</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.mozilla</groupId>
<artifactId>rhino</artifactId>
<version>1.7R4</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
<organization>
<name>Swiss AviationSoftware</name>
<url>http://swiss-as.com</url>
</organization>
<url>https://github.com/SwissAS/proxy_vole</url>
</project>

12 changes: 12 additions & 0 deletions local-proj-repo/proxy/proxy_vole/maven-metadata-local.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>proxy</groupId>
<artifactId>proxy_vole</artifactId>
<versioning>
<release>1.1.1</release>
<versions>
<version>1.1.1</version>
</versions>
<lastUpdated>20240919004204</lastUpdated>
</versioning>
</metadata>
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
<artifactId>swtmac_aarch64</artifactId>
<version>4.32</version>
</dependency>
<!-- proxy-vole from https://github.com/SwissAS/proxy-vole -->
<dependency>
<groupId>proxy</groupId>
<artifactId>proxy_vole</artifactId>
<version>1.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/com/salesforce/dataloader/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,13 @@ private void setDefaults() {
setDefaultValue(PROCESS_EXIT_WITH_ERROR_ON_FAILED_ROWS_BATCH_MODE, false);
setDefaultValue(INCLUDE_RICH_TEXT_FIELD_DATA_IN_QUERY_RESULTS, false);
setDefaultValue(OAUTH_INSTANCE_URL, false);
AppUtil.setSystemProxyValues();
String proxyHost = System.getProperty("http.proxyHost");
String proxyPort = System.getProperty("http.proxyPort");
if (proxyHost != null && !proxyHost.isBlank()) {
setDefaultValue(PROXY_HOST, proxyHost);
setDefaultValue(PROXY_PORT, proxyPort);
}
}

/**
Expand Down Expand Up @@ -971,6 +978,7 @@ private void load(InputStream in) throws ConfigInitializationException, IOExcept
try {
Properties propsFromFile = new LinkedProperties();
propsFromFile.load(in);
removeEmptyProperties(propsFromFile);
properties.putAll(propsFromFile);
for (String roprop : READ_ONLY_PROPERTY_NAMES) {
if (propsFromFile.containsKey(roprop)) {
Expand Down Expand Up @@ -1233,6 +1241,7 @@ public void save() throws IOException, GeneralSecurityException {
removeUnsupportedProperties();
removeDecryptedProperties();
removeCLIOptionsFromProperties();
removeEmptyProperties(this.properties);

FileOutputStream out = null;
try {
Expand Down Expand Up @@ -1334,6 +1343,11 @@ private void removeCLIOptionsFromProperties() {
}
}

private void removeEmptyProperties(Properties props) {
props.entrySet().removeIf(entry ->
(entry.getValue() == null || entry.getValue().toString().isBlank()));
}

/**
* Save statistics from the last run
*/
Expand Down Expand Up @@ -1441,7 +1455,7 @@ private <T> void setValue(String name, T value, boolean skipIfAlreadySet) {
*/
private void setProperty(String name, String newValue, boolean skipIfAlreadySet) {
final String oldValue = getString(name);
if (skipIfAlreadySet && oldValue != null && !oldValue.isEmpty()) {
if (skipIfAlreadySet && oldValue != null && !oldValue.isBlank()) {
// do not override the old value
return;
}
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/com/salesforce/dataloader/util/AppUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URISyntaxException;
Expand All @@ -51,6 +53,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
Expand All @@ -63,6 +66,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.btr.proxy.search.ProxySearch;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -535,4 +539,43 @@ public static <T> T deserializeJsonToObject (InputStream in, Class<T> tmpClass)
mapper.setTimeZone(TimeZone.getTimeZone("GMT"));
return mapper.readValue(in, tmpClass);
}

public static void setSystemProxyValues() {
// Use the static factory method getDefaultProxySearch to create a proxy search instance
// configured with the default proxy search strategies for the current environment.
ProxySearch proxySearch = ProxySearch.getDefaultProxySearch();

// Invoke the proxy search. This will create a ProxySelector with the detected proxy settings.
ProxySelector proxySelector = proxySearch.getProxySelector();

// Install this ProxySelector as default ProxySelector for all connections.
ProxySelector.setDefault(proxySelector);

System.setProperty("java.net.useSystemProxies", "true");
logger.debug("detecting proxies");
List<Proxy> l = null;
try {
l = ProxySelector.getDefault().select(new URI("https://www.salesforce.com"));
}
catch (URISyntaxException e) {
e.printStackTrace();
}
if (l != null) {
for (Iterator<Proxy> iter = l.iterator(); iter.hasNext();) {
java.net.Proxy proxy = (java.net.Proxy) iter.next();
logger.debug("System proxy type: " + proxy.type());

InetSocketAddress addr = (InetSocketAddress) proxy.address();

if (addr == null) {
logger.debug("No system proxy");
} else {
logger.info("System proxy hostname: " + addr.getHostName());
System.setProperty("http.proxyHost", addr.getHostName());
logger.info("System proxy port: " + addr.getPort());
System.setProperty("http.proxyPort", Integer.toString(addr.getPort()));
}
}
}
}
}

0 comments on commit 181f51a

Please sign in to comment.