@@ -34,6 +34,7 @@ import java.io.File
34
34
import java.io.FileInputStream
35
35
import java.net.HttpURLConnection.HTTP_CREATED
36
36
import java.net.InetAddress
37
+ import java.net.ProxySelector
37
38
import java.net.Socket
38
39
import java.net.URL
39
40
import java.nio.file.Path
@@ -75,63 +76,91 @@ class CoderRestClientService {
75
76
* @throws [AuthenticationResponseException] if authentication failed.
76
77
*/
77
78
fun initClientSession (url : URL ,token : String ,settings : CoderSettingsState ):User {
78
- client= CoderRestClient (url, token,null , settings)
79
+ client= CoderRestClient (url, token,defaultVersion() , settings, defaultProxy() )
79
80
me= client.me()
80
81
buildVersion= client.buildInfo().version
81
82
isReady= true
82
83
return me
83
84
}
84
85
}
85
86
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(
87
114
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 ,
90
118
) {
91
- private var httpClient: OkHttpClient
92
- private var retroRestClient: CoderV2RestFacade
119
+ private val httpClient: OkHttpClient
120
+ private val retroRestClient: CoderV2RestFacade
93
121
94
122
init {
95
123
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()
101
124
102
125
val socketFactory= coderSocketFactory(settings)
103
126
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
116
143
.sslSocketFactory(socketFactory, trustManagers[0 ]as X509TrustManager )
117
144
.hostnameVerifier(CoderHostnameVerifier (settings.tlsAlternateHostname))
118
145
.addInterceptor { it.proceed(it.request().newBuilder().addHeader(" Coder-Session-Token" , token).build()) }
119
146
.addInterceptor { it.proceed(it.request().newBuilder().addHeader(" User-Agent" ," Coder Gateway/${pluginVersion} (${SystemInfo .getOsNameAndVersion()} ;${SystemInfo .OS_ARCH } )" ).build()) }
120
147
.addInterceptor {
121
148
var request= it.request()
122
149
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()
127
154
}
128
155
it.proceed(request)
129
156
}
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.
131
158
.addInterceptor(HttpLoggingInterceptor ().apply { setLevel(HttpLoggingInterceptor .Level .BASIC ) })
132
159
.build()
133
160
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)
135
164
}
136
165
137
166
/* *