@@ -7,7 +7,6 @@ import com.coder.toolbox.sdk.convertors.LoggingConverterFactory
7
7
import com.coder.toolbox.sdk.convertors.OSConverter
8
8
import com.coder.toolbox.sdk.convertors.UUIDConverter
9
9
import com.coder.toolbox.sdk.ex.APIResponseException
10
- import com.coder.toolbox.sdk.interceptors.LoggingInterceptor
11
10
import com.coder.toolbox.sdk.v2.CoderV2RestFacade
12
11
import com.coder.toolbox.sdk.v2.models.ApiErrorResponse
13
12
import com.coder.toolbox.sdk.v2.models.BuildInfo
@@ -21,23 +20,14 @@ import com.coder.toolbox.sdk.v2.models.WorkspaceBuildReason
21
20
import com.coder.toolbox.sdk.v2.models.WorkspaceResource
22
21
import com.coder.toolbox.sdk.v2.models.WorkspaceStatus
23
22
import com.coder.toolbox.sdk.v2.models.WorkspaceTransition
24
- import com.coder.toolbox.util.CoderHostnameVerifier
25
- import com.coder.toolbox.util.coderSocketFactory
26
- import com.coder.toolbox.util.coderTrustManagers
27
- import com.coder.toolbox.util.getArch
28
- import com.coder.toolbox.util.getHeaders
29
- import com.coder.toolbox.util.getOS
30
- import com.jetbrains.toolbox.api.remoteDev.connection.ProxyAuth
31
23
import com.squareup.moshi.Moshi
32
- import okhttp3.Credentials
33
24
import okhttp3.OkHttpClient
34
25
import retrofit2.Response
35
26
import retrofit2.Retrofit
36
27
import retrofit2.converter.moshi.MoshiConverterFactory
37
28
import java.net.HttpURLConnection
38
29
import java.net.URL
39
30
import java.util.UUID
40
- import javax.net.ssl.X509TrustManager
41
31
42
32
/* *
43
33
* An HTTP client that can make requests to the Coder API.
@@ -50,7 +40,6 @@ open class CoderRestClient(
50
40
val token : String? ,
51
41
private val pluginVersion : String = " development" ,
52
42
) {
53
- private val settings = context.settingsStore.readOnly()
54
43
private lateinit var moshi: Moshi
55
44
private lateinit var httpClient: OkHttpClient
56
45
private lateinit var retroRestClient: CoderV2RestFacade
@@ -71,68 +60,12 @@ open class CoderRestClient(
71
60
.add(UUIDConverter ())
72
61
.build()
73
62
74
- val socketFactory = coderSocketFactory(settings.tls)
75
- val trustManagers = coderTrustManagers(settings.tls.caPath)
76
- var builder = OkHttpClient .Builder ()
77
-
78
- if (context.proxySettings.getProxy() != null ) {
79
- context.logger.info(" proxy: ${context.proxySettings.getProxy()} " )
80
- builder.proxy(context.proxySettings.getProxy())
81
- } else if (context.proxySettings.getProxySelector() != null ) {
82
- context.logger.info(" proxy selector: ${context.proxySettings.getProxySelector()} " )
83
- builder.proxySelector(context.proxySettings.getProxySelector()!! )
84
- }
85
-
86
- // Note: This handles only HTTP/HTTPS proxy authentication.
87
- // SOCKS5 proxy authentication is currently not supported due to limitations described in:
88
- // https://youtrack.jetbrains.com/issue/TBX-14532/Missing-proxy-authentication-settings#focus=Comments-27-12265861.0-0
89
- builder.proxyAuthenticator { _, response ->
90
- val proxyAuth = context.proxySettings.getProxyAuth()
91
- if (proxyAuth == null || proxyAuth !is ProxyAuth .Basic ) {
92
- return @proxyAuthenticator null
93
- }
94
- val credentials = Credentials .basic(proxyAuth.username, proxyAuth.password)
95
- response.request.newBuilder()
96
- .header(" Proxy-Authorization" , credentials)
97
- .build()
98
- }
99
-
100
- if (context.settingsStore.requireTokenAuth) {
101
- if (token.isNullOrBlank()) {
102
- throw IllegalStateException (" Token is required for $url deployment" )
103
- }
104
- builder = builder.addInterceptor {
105
- it.proceed(
106
- it.request().newBuilder().addHeader(" Coder-Session-Token" , token).build()
107
- )
108
- }
109
- }
110
-
111
- httpClient =
112
- builder
113
- .sslSocketFactory(socketFactory, trustManagers[0 ] as X509TrustManager )
114
- .hostnameVerifier(CoderHostnameVerifier (settings.tls.altHostname))
115
- .retryOnConnectionFailure(true )
116
- .addInterceptor {
117
- it.proceed(
118
- it.request().newBuilder().addHeader(
119
- " User-Agent" ,
120
- " Coder Toolbox/$pluginVersion (${getOS()} ; ${getArch()} )" ,
121
- ).build(),
122
- )
123
- }
124
- .addInterceptor {
125
- var request = it.request()
126
- val headers = getHeaders(url, settings.headerCommand)
127
- if (headers.isNotEmpty()) {
128
- val reqBuilder = request.newBuilder()
129
- headers.forEach { h -> reqBuilder.addHeader(h.key, h.value) }
130
- request = reqBuilder.build()
131
- }
132
- it.proceed(request)
133
- }
134
- .addInterceptor(LoggingInterceptor (context))
135
- .build()
63
+ httpClient = CoderHttpClientBuilder .build(
64
+ context,
65
+ pluginVersion,
66
+ url,
67
+ token
68
+ )
136
69
137
70
retroRestClient =
138
71
Retrofit .Builder ().baseUrl(url.toString()).client(httpClient)
0 commit comments