@@ -34,6 +34,7 @@ import java.io.File
3434import java.io.FileInputStream
3535import java.net.HttpURLConnection.HTTP_CREATED
3636import java.net.InetAddress
37+ import java.net.ProxySelector
3738import java.net.Socket
3839import java.net.URL
3940import java.nio.file.Path
@@ -75,63 +76,91 @@ class CoderRestClientService {
7576 * @throws [AuthenticationResponseException] if authentication failed.
7677 */
7778 fun initClientSession (url : URL , token : String , settings : CoderSettingsState ): User {
78- client = CoderRestClient (url, token, null , settings)
79+ client = CoderRestClient (url, token, defaultVersion() , settings, defaultProxy() )
7980 me = client.me()
8081 buildVersion = client.buildInfo().version
8182 isReady = true
8283 return me
8384 }
8485}
8586
86- class CoderRestClient (
87+ /* *
88+ * Holds proxy information. Exists only to interface with tests since they
89+ * cannot create an HttpConfigurable instance.
90+ */
91+ data class ProxyValues (
92+ val username : String? ,
93+ val password : String? ,
94+ val useAuth : Boolean ,
95+ val selector : ProxySelector ,
96+ )
97+
98+ fun defaultProxy (): ProxyValues {
99+ val inst = HttpConfigurable .getInstance()
100+ return ProxyValues (
101+ inst.proxyLogin,
102+ inst.plainProxyPassword,
103+ inst.PROXY_AUTHENTICATION ,
104+ inst.onlyBySettingsSelector
105+ )
106+ }
107+
108+ fun defaultVersion (): String {
109+ // This is the id from the plugin.xml.
110+ return PluginManagerCore .getPlugin(PluginId .getId(" com.coder.gateway" ))!! .version
111+ }
112+
113+ class CoderRestClient @JvmOverloads constructor(
87114 var url : URL , var token : String ,
88- private var pluginVersion : String? ,
89- private var settings : CoderSettingsState ,
115+ private val pluginVersion : String ,
116+ private val settings : CoderSettingsState ,
117+ private val proxyValues : ProxyValues ? = null ,
90118) {
91- private var httpClient: OkHttpClient
92- private var retroRestClient: CoderV2RestFacade
119+ private val httpClient: OkHttpClient
120+ private val retroRestClient: CoderV2RestFacade
93121
94122 init {
95123 val gson: Gson = GsonBuilder ().registerTypeAdapter(Instant ::class .java, InstantConverter ()).setPrettyPrinting().create()
96- if (pluginVersion.isNullOrBlank()) {
97- pluginVersion = PluginManagerCore .getPlugin(PluginId .getId(" com.coder.gateway" ))!! .version // this is the id from the plugin.xml
98- }
99-
100- val proxy = HttpConfigurable .getInstance()
101124
102125 val socketFactory = coderSocketFactory(settings)
103126 val trustManagers = coderTrustManagers(settings.tlsCAPath)
104- httpClient = OkHttpClient .Builder ()
105- .proxySelector(proxy.onlyBySettingsSelector)
106- .proxyAuthenticator { _, response ->
107- val login = proxy.proxyLogin
108- val pass = proxy.plainProxyPassword
109- if (proxy.PROXY_AUTHENTICATION && login != null && pass != null ) {
110- val credentials = Credentials .basic(login, pass)
111- response.request.newBuilder()
112- .header(" Proxy-Authorization" , credentials)
113- .build()
114- } else null
115- }
127+ var builder = OkHttpClient .Builder ()
128+
129+ if (proxyValues != null ) {
130+ builder = builder
131+ .proxySelector(proxyValues.selector)
132+ .proxyAuthenticator { _, response ->
133+ if (proxyValues.useAuth && proxyValues.username != null && proxyValues.password != null ) {
134+ val credentials = Credentials .basic(proxyValues.username, proxyValues.password)
135+ response.request.newBuilder()
136+ .header(" Proxy-Authorization" , credentials)
137+ .build()
138+ } else null
139+ }
140+ }
141+
142+ httpClient = builder
116143 .sslSocketFactory(socketFactory, trustManagers[0 ] as X509TrustManager )
117144 .hostnameVerifier(CoderHostnameVerifier (settings.tlsAlternateHostname))
118145 .addInterceptor { it.proceed(it.request().newBuilder().addHeader(" Coder-Session-Token" , token).build()) }
119146 .addInterceptor { it.proceed(it.request().newBuilder().addHeader(" User-Agent" , " Coder Gateway/${pluginVersion} (${SystemInfo .getOsNameAndVersion()} ; ${SystemInfo .OS_ARCH } )" ).build()) }
120147 .addInterceptor {
121148 var request = it.request()
122149 val headers = getHeaders(url, settings.headerCommand)
123- if (headers.size > 0 ) {
124- val builder = request.newBuilder()
125- headers.forEach { h -> builder .addHeader(h.key, h.value) }
126- request = builder .build()
150+ if (headers.isNotEmpty() ) {
151+ val reqBuilder = request.newBuilder()
152+ headers.forEach { h -> reqBuilder .addHeader(h.key, h.value) }
153+ request = reqBuilder .build()
127154 }
128155 it.proceed(request)
129156 }
130- // this should always be last if we want to see previous interceptors logged
157+ // This should always be last if we want to see previous interceptors logged.
131158 .addInterceptor(HttpLoggingInterceptor ().apply { setLevel(HttpLoggingInterceptor .Level .BASIC ) })
132159 .build()
133160
134- retroRestClient = Retrofit .Builder ().baseUrl(url.toString()).client(httpClient).addConverterFactory(GsonConverterFactory .create(gson)).build().create(CoderV2RestFacade ::class .java)
161+ retroRestClient = Retrofit .Builder ().baseUrl(url.toString()).client(httpClient)
162+ .addConverterFactory(GsonConverterFactory .create(gson))
163+ .build().create(CoderV2RestFacade ::class .java)
135164 }
136165
137166 /* *
0 commit comments