Skip to content

Commit a142e42

Browse files
author
Harshit Vasu
committed
added support for env variable https_proxy set (facing issues with env in pipeline script)
1 parent d7f9772 commit a142e42

File tree

1 file changed

+70
-39
lines changed

1 file changed

+70
-39
lines changed

src/main/java/com/browserstack/automate/ci/common/proxysettings/JenkinsProxySettings.java

Lines changed: 70 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,61 +5,96 @@
55

66
import java.net.InetSocketAddress;
77
import java.net.Proxy;
8-
8+
import java.util.regex.Matcher;
9+
import java.util.regex.Pattern;
910
public class JenkinsProxySettings {
1011

1112
private static final ProxyConfiguration jenkinsProxy = Jenkins.getInstanceOrNull() != null ? Jenkins.getInstanceOrNull().proxy : null;
1213
private static final String protocol = "https";
13-
private static final String systemProxyHost = System.getProperty(protocol + ".proxyHost");
14-
private static final int systemProxyPort = Integer.parseInt(System.getProperty(protocol + ".proxyPort", "0"));
15-
private static final String systemProxyUser = System.getProperty(protocol + ".proxyUser");
16-
private static final String systemProxyPassword = System.getProperty(protocol + ".proxyPassword");
14+
private static String proxyHost;
15+
private static int proxyPort;
16+
private static String proxyUser;
17+
private static String proxyPassword;
18+
static {
19+
final String systemProxyHost = System.getProperty(protocol + ".proxyHost");
20+
final int systemProxyPort = Integer.parseInt(System.getProperty(protocol + ".proxyPort", "0"));
21+
final String systemProxyUser = System.getProperty(protocol + ".proxyUser");
22+
final String systemProxyPassword = System.getProperty(protocol + ".proxyPassword");
23+
if(systemProxyHost!=null && systemProxyPort!=0){
24+
proxyHost = systemProxyHost;
25+
proxyPort = systemProxyPort;
26+
if(systemProxyUser!=null && systemProxyPassword!=null){
27+
proxyUser = systemProxyUser;
28+
proxyPassword = systemProxyPassword;
29+
}
30+
}
1731

18-
public static Proxy getJenkinsProxy() {
19-
if (hasSystemProxy()) {
20-
return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(systemProxyHost, systemProxyPort));
32+
if(proxyHost==null && proxyPort==0 && jenkinsProxy!=null){
33+
final String host = jenkinsProxy.name;
34+
final int port = jenkinsProxy.port;
35+
final String user = jenkinsProxy.getUserName();
36+
final String password = jenkinsProxy.getPassword();
37+
if(host!=null && port!=0){
38+
proxyHost = host;
39+
proxyPort = port;
40+
if(user!=null && password!=null){
41+
proxyUser = user;
42+
proxyPassword = password;
43+
}
44+
}
45+
}
46+
if(proxyHost==null && proxyPort==0){
47+
String proxyEnv = System.getenv("https_proxy");
48+
String authRegex = "(https:\\/\\/)(.+):(.+)@(.+):(\\d+)";
49+
String basicRegex = "(https:\\/\\/)(.+):(\\d+)";
50+
51+
if(proxyEnv!=null && proxyEnv.matches(authRegex)){
52+
Pattern r = Pattern.compile(authRegex);
53+
Matcher m = r.matcher(proxyEnv);
54+
final String envHost = m.group(1);
55+
final int envPort = Integer.parseInt(m.group(2));
56+
final String envUser = m.group(3);
57+
final String envPassword = m.group(4);
58+
if(envHost!=null && envPort!=0){
59+
proxyHost = envHost;
60+
proxyPort = envPort;
61+
if(envUser!=null && envPassword!=null){
62+
proxyUser = envUser;
63+
proxyPassword = envPassword;
64+
}
65+
}
66+
}
67+
else if(proxyEnv!=null && proxyEnv.matches(basicRegex)) {
68+
Pattern r = Pattern.compile(authRegex);
69+
Matcher m = r.matcher(proxyEnv);
70+
final String envHost = m.group(1);
71+
final int envPort = Integer.parseInt(m.group(2));
72+
if(envHost!=null && envPort!=0){
73+
proxyHost = envHost;
74+
proxyPort = envPort;
75+
}
76+
}
2177
}
78+
}
2279

23-
if (jenkinsProxy == null) return null;
24-
final String proxyHost = jenkinsProxy.name;
25-
final int proxyPort = jenkinsProxy.port;
80+
public static Proxy getJenkinsProxy() {
2681
return (proxyHost != null && proxyPort != 0) ? new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)) : null;
2782
}
2883

2984
public static String getHost() {
30-
if (hasSystemProxy()) {
31-
return systemProxyHost;
32-
}
33-
34-
if (jenkinsProxy == null) return null;
35-
return jenkinsProxy.name;
85+
return proxyHost;
3686
}
3787

3888
public static int getPort() {
39-
if (hasSystemProxy()) {
40-
return systemProxyPort;
41-
}
42-
43-
if (jenkinsProxy == null) return 0;
44-
return jenkinsProxy.port;
89+
return proxyPort;
4590
}
4691

4792
public static String getUsername() {
48-
if (hasSystemProxy() && systemProxyUser != null && systemProxyPassword != null) {
49-
return systemProxyUser;
50-
}
51-
52-
if (jenkinsProxy == null) return null;
53-
return jenkinsProxy.getUserName();
93+
return proxyUser;
5494
}
5595

5696
public static String getPassword() {
57-
if (hasSystemProxy() && systemProxyUser != null && systemProxyPassword != null) {
58-
return systemProxyPassword;
59-
}
60-
61-
if (jenkinsProxy == null) return null;
62-
return jenkinsProxy.getPassword();
97+
return proxyPassword;
6398
}
6499

65100
public static ProxyConfiguration getProxyConfig() {
@@ -70,8 +105,4 @@ public static boolean hasProxy() {
70105
return getHost() != null && getPort() != 0;
71106
}
72107

73-
public static boolean hasSystemProxy() {
74-
return systemProxyHost != null && systemProxyPort != 0;
75-
}
76-
77108
}

0 commit comments

Comments
 (0)