- Notifications
You must be signed in to change notification settings - Fork1
fix: support for downloading the CLI when proxy is configured#177
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
5 commits Select commitHold shift + click to select a range
5d3eef3
impl: extract OkHttpClient building logic into a reusable utility
fioan891a460aa
impl: extract http client interceptors logic into a reusable utility
fioan89ec51cb4
impl: let upstream code decide which interceptors to install
fioan89a819870
fix: support for downloading the CLI when proxy is configured
fioan899b72812
doc: how to bypass Bad Gateway when mitmproxy is acting as a proxy
fioan89File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletionsCHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletionsREADME.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
23 changes: 11 additions & 12 deletionssrc/main/kotlin/com/coder/toolbox/cli/CoderCLIManager.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletionssrc/main/kotlin/com/coder/toolbox/sdk/CoderHttpClientBuilder.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.coder.toolbox.sdk | ||
import com.coder.toolbox.CoderToolboxContext | ||
import com.coder.toolbox.util.CoderHostnameVerifier | ||
import com.coder.toolbox.util.coderSocketFactory | ||
import com.coder.toolbox.util.coderTrustManagers | ||
import com.jetbrains.toolbox.api.remoteDev.connection.ProxyAuth | ||
import okhttp3.Credentials | ||
import okhttp3.Interceptor | ||
import okhttp3.OkHttpClient | ||
import javax.net.ssl.X509TrustManager | ||
object CoderHttpClientBuilder { | ||
fun build( | ||
context: CoderToolboxContext, | ||
interceptors: List<Interceptor> | ||
): OkHttpClient { | ||
val settings = context.settingsStore.readOnly() | ||
val socketFactory = coderSocketFactory(settings.tls) | ||
val trustManagers = coderTrustManagers(settings.tls.caPath) | ||
var builder = OkHttpClient.Builder() | ||
if (context.proxySettings.getProxy() != null) { | ||
context.logger.info("proxy: ${context.proxySettings.getProxy()}") | ||
builder.proxy(context.proxySettings.getProxy()) | ||
} else if (context.proxySettings.getProxySelector() != null) { | ||
context.logger.info("proxy selector: ${context.proxySettings.getProxySelector()}") | ||
builder.proxySelector(context.proxySettings.getProxySelector()!!) | ||
} | ||
// Note: This handles only HTTP/HTTPS proxy authentication. | ||
// SOCKS5 proxy authentication is currently not supported due to limitations described in: | ||
// https://youtrack.jetbrains.com/issue/TBX-14532/Missing-proxy-authentication-settings#focus=Comments-27-12265861.0-0 | ||
builder.proxyAuthenticator { _, response -> | ||
val proxyAuth = context.proxySettings.getProxyAuth() | ||
if (proxyAuth == null || proxyAuth !is ProxyAuth.Basic) { | ||
return@proxyAuthenticator null | ||
} | ||
val credentials = Credentials.basic(proxyAuth.username, proxyAuth.password) | ||
response.request.newBuilder() | ||
.header("Proxy-Authorization", credentials) | ||
.build() | ||
} | ||
builder.sslSocketFactory(socketFactory, trustManagers[0] as X509TrustManager) | ||
.hostnameVerifier(CoderHostnameVerifier(settings.tls.altHostname)) | ||
.retryOnConnectionFailure(true) | ||
interceptors.forEach { interceptor -> | ||
builder.addInterceptor(interceptor) | ||
} | ||
return builder.build() | ||
} | ||
} |
85 changes: 14 additions & 71 deletionssrc/main/kotlin/com/coder/toolbox/sdk/CoderRestClient.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletionssrc/main/kotlin/com/coder/toolbox/sdk/interceptors/Interceptors.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.coder.toolbox.sdk.interceptors | ||
import com.coder.toolbox.CoderToolboxContext | ||
import com.coder.toolbox.util.getArch | ||
import com.coder.toolbox.util.getHeaders | ||
import com.coder.toolbox.util.getOS | ||
import okhttp3.Interceptor | ||
import java.net.URL | ||
/** | ||
* Factory of okhttp interceptors | ||
*/ | ||
object Interceptors { | ||
/** | ||
* Creates a token authentication interceptor | ||
*/ | ||
fun tokenAuth(token: String): Interceptor { | ||
return Interceptor { chain -> | ||
chain.proceed( | ||
chain.request().newBuilder() | ||
.addHeader("Coder-Session-Token", token) | ||
.build() | ||
) | ||
} | ||
} | ||
/** | ||
* Creates a User-Agent header interceptor | ||
*/ | ||
fun userAgent(pluginVersion: String): Interceptor { | ||
return Interceptor { chain -> | ||
chain.proceed( | ||
chain.request().newBuilder() | ||
.addHeader("User-Agent", "Coder Toolbox/$pluginVersion (${getOS()}; ${getArch()})") | ||
.build() | ||
) | ||
} | ||
} | ||
/** | ||
* Adds headers generated by executing a native command | ||
*/ | ||
fun externalHeaders(context: CoderToolboxContext, url: URL): Interceptor { | ||
val settings = context.settingsStore.readOnly() | ||
return Interceptor { chain -> | ||
var request = chain.request() | ||
val headers = getHeaders(url, settings.headerCommand) | ||
if (headers.isNotEmpty()) { | ||
val reqBuilder = request.newBuilder() | ||
headers.forEach { h -> reqBuilder.addHeader(h.key, h.value) } | ||
request = reqBuilder.build() | ||
} | ||
chain.proceed(request) | ||
} | ||
} | ||
/** | ||
* Creates a logging interceptor | ||
*/ | ||
fun logging(context: CoderToolboxContext): Interceptor { | ||
return LoggingInterceptor(context) | ||
} | ||
} |
25 changes: 18 additions & 7 deletionssrc/test/kotlin/com/coder/toolbox/cli/CoderCLIManagerTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.