-
Notifications
You must be signed in to change notification settings - Fork 55
Proxy support for browserstack plugin #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 13 commits
e1c10de
df57025
57b23bd
53afae7
b29623b
7c1d9a0
869b2b2
f172efe
a37c18e
8af6936
6d717b6
30e85a1
caf57c3
d16ec50
062303a
2b3b017
f9263ec
58ab632
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,77 @@ | ||||||
package com.browserstack.automate.ci.common.proxysettings; | ||||||
|
||||||
import hudson.ProxyConfiguration; | ||||||
import jenkins.model.Jenkins; | ||||||
|
||||||
import java.net.InetSocketAddress; | ||||||
import java.net.Proxy; | ||||||
|
||||||
public class JenkinsProxySettings { | ||||||
|
||||||
static final ProxyConfiguration jenkinsProxy = Jenkins.getInstanceOrNull() != null ? Jenkins.getInstanceOrNull().proxy : null; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Please define correct accessSpecifiers wherever necessary. |
||||||
static final String protocol = "https"; | ||||||
static final String systemProxyHost = System.getProperty(protocol + ".proxyHost"); | ||||||
static final int systemProxyPort = Integer.parseInt(System.getProperty(protocol + ".proxyPort", "0")); | ||||||
static final String systemProxyUser = System.getProperty(protocol + ".proxyUser"); | ||||||
static final String systemProxyPassword = System.getProperty(protocol + ".proxyPassword"); | ||||||
|
||||||
public static Proxy getJenkinsProxy() { | ||||||
if (hasSystemProxy()) { | ||||||
return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(systemProxyHost, systemProxyPort)); | ||||||
} | ||||||
|
||||||
if (jenkinsProxy == null) return null; | ||||||
String proxyHost = jenkinsProxy.name; | ||||||
int proxyPort = jenkinsProxy.port; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can be resolved |
||||||
return (proxyHost != null && proxyPort != 0) ? new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)) : null; | ||||||
} | ||||||
|
||||||
public static String getHost() { | ||||||
if (hasSystemProxy()) { | ||||||
return systemProxyHost; | ||||||
} | ||||||
|
||||||
if (jenkinsProxy == null) return null; | ||||||
return jenkinsProxy.name; | ||||||
} | ||||||
|
||||||
public static int getPort() { | ||||||
if (hasSystemProxy()) { | ||||||
return systemProxyPort; | ||||||
} | ||||||
|
||||||
if (jenkinsProxy == null) return 0; | ||||||
return jenkinsProxy.port; | ||||||
} | ||||||
|
||||||
public static String getUsername() { | ||||||
if (hasSystemProxy() && systemProxyUser != null && systemProxyPassword != null) { | ||||||
return systemProxyUser; | ||||||
} | ||||||
|
||||||
if (jenkinsProxy == null) return null; | ||||||
return jenkinsProxy.getUserName(); | ||||||
} | ||||||
|
||||||
public static String getPassword() { | ||||||
if (hasSystemProxy() && systemProxyUser != null && systemProxyPassword != null) { | ||||||
return systemProxyPassword; | ||||||
} | ||||||
|
||||||
if (jenkinsProxy == null) return null; | ||||||
return jenkinsProxy.getPassword(); | ||||||
} | ||||||
|
||||||
public static ProxyConfiguration getProxyConfig() { | ||||||
return jenkinsProxy; | ||||||
} | ||||||
|
||||||
public static boolean hasProxy() { | ||||||
return getHost() != null && getPort() != 0; | ||||||
} | ||||||
|
||||||
public static boolean hasSystemProxy() { | ||||||
return systemProxyHost != null && systemProxyPort != 0; | ||||||
} | ||||||
|
||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,30 @@ | ||
package com.browserstack.automate.ci.common.tracking; | ||
|
||
|
||
import com.browserstack.automate.ci.common.Tools; | ||
import com.browserstack.automate.ci.common.constants.Constants; | ||
import com.browserstack.automate.ci.common.proxysettings.JenkinsProxySettings; | ||
import okhttp3.Authenticator; | ||
import okhttp3.Call; | ||
import okhttp3.Callback; | ||
import okhttp3.Credentials; | ||
import okhttp3.MediaType; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.RequestBody; | ||
import okhttp3.Response; | ||
import okhttp3.Route; | ||
import org.json.JSONObject; | ||
|
||
import java.io.IOException; | ||
import java.net.Proxy; | ||
import java.time.Instant; | ||
import java.util.Optional; | ||
|
||
public class PluginsTracker { | ||
private static final MediaType JSON = MediaType.get("application/json; charset=utf-8"); | ||
private static final String URL = "https://api.browserstack.com/ci_plugins/track"; | ||
private static final OkHttpClient client = new OkHttpClient(); | ||
private static OkHttpClient client; | ||
private final String trackingId; | ||
private String username; | ||
private String accessKey; | ||
|
@@ -27,12 +33,39 @@ public PluginsTracker(final String username, final String accessKey) { | |
this.username = username; | ||
this.accessKey = accessKey; | ||
this.trackingId = Tools.getUniqueString(true, true); | ||
initializeClient(); | ||
} | ||
|
||
public PluginsTracker() { | ||
this.username = null; | ||
this.accessKey = null; | ||
this.trackingId = Tools.getUniqueString(true, true); | ||
initializeClient(); | ||
} | ||
|
||
private void initializeClient() { | ||
|
||
Proxy proxy = JenkinsProxySettings.getJenkinsProxy() != null ? JenkinsProxySettings.getJenkinsProxy() : Proxy.NO_PROXY; | ||
if (proxy != Proxy.NO_PROXY) { | ||
String username = JenkinsProxySettings.getUsername(); | ||
String password = JenkinsProxySettings.getPassword(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can be resolved |
||
if (username != null && password != null) { | ||
Authenticator proxyAuthenticator = new Authenticator() { | ||
@Override | ||
public Request authenticate(Route route, Response response) throws IOException { | ||
String credential = Credentials.basic(username, password); | ||
return response.request().newBuilder() | ||
.header("Proxy-Authorization", credential) | ||
.build(); | ||
} | ||
}; | ||
this.client = new OkHttpClient.Builder().proxy(proxy).proxyAuthenticator(proxyAuthenticator).build(); | ||
} else { | ||
this.client = new OkHttpClient.Builder().proxy(proxy).build(); | ||
} | ||
} else { | ||
this.client = new OkHttpClient.Builder().build(); | ||
} | ||
} | ||
|
||
private static void asyncPostRequestSilent(final String url, final String json) { | ||
|
Uh oh!
There was an error while loading. Please reload this page.