-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
NpmProxy.kt
104 lines (92 loc) · 4.38 KB
/
NpmProxy.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package com.github.gradle.node.npm.proxy
import java.net.URLEncoder
import java.util.stream.Collectors.toList
import java.util.stream.Stream
import kotlin.text.Charsets.UTF_8
internal class NpmProxy {
companion object {
// These are the environment variables that HTTPing applications checks, proxy is on and off.
// FTP skipped in hopes of a better future.
private val proxyVariables = listOf(
"HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY", "PROXY"
)
// And since npm also takes settings in the form of environment variables with the
// NPM_CONFIG_<setting> format, we should check those. Hopefully nobody does this.
private val npmProxyVariables = listOf(
"NPM_CONFIG_PROXY", "NPM_CONFIG_HTTPS-PROXY", "NPM_CONFIG_NOPROXY"
)
fun computeNpmProxyEnvironmentVariables(): Map<String, String> {
val proxyEnvironmentVariables = computeProxyUrlEnvironmentVariables()
if (proxyEnvironmentVariables.isNotEmpty()) {
addProxyIgnoredHostsEnvironmentVariable(proxyEnvironmentVariables)
}
return proxyEnvironmentVariables.toMap()
}
fun shouldConfigureProxy(env: Map<String, String>, settings: ProxySettings): Boolean {
if (settings == ProxySettings.FORCED) {
return true
} else if (settings == ProxySettings.SMART) {
return !hasProxyConfiguration(env)
}
return false
}
/**
* Returns true if the given map of environment variables already has
* proxy settings configured.
*
* @param env map of environment variables
*/
fun hasProxyConfiguration(env: Map<String, String>): Boolean {
return env.keys.any {
proxyVariables.contains(it.toUpperCase()) || npmProxyVariables.contains(it.toUpperCase())
}
}
private fun computeProxyUrlEnvironmentVariables(): MutableMap<String, String> {
val proxyArgs = mutableMapOf<String, String>()
for ((proxyProto, proxyParam) in
listOf(arrayOf("http", "HTTP_PROXY"), arrayOf("https", "HTTPS_PROXY"))) {
var proxyHost = System.getProperty("$proxyProto.proxyHost")
val proxyPort = System.getProperty("$proxyProto.proxyPort")
if (proxyHost != null && proxyPort != null) {
proxyHost = proxyHost.replace("^https?://".toRegex(), "")
val proxyUser = System.getProperty("$proxyProto.proxyUser")
val proxyPassword = System.getProperty("$proxyProto.proxyPassword")
if (proxyUser != null && proxyPassword != null) {
proxyArgs[proxyParam] =
"http://${encode(proxyUser)}:${encode(proxyPassword)}@$proxyHost:$proxyPort"
} else {
proxyArgs[proxyParam] = "http://$proxyHost:$proxyPort"
}
}
}
return proxyArgs
}
private fun encode(value: String): String {
return URLEncoder.encode(value, UTF_8.toString())
}
private fun addProxyIgnoredHostsEnvironmentVariable(proxyEnvironmentVariables: MutableMap<String, String>) {
val proxyIgnoredHosts = computeProxyIgnoredHosts()
if (proxyIgnoredHosts.isNotEmpty()) {
proxyEnvironmentVariables["NO_PROXY"] = proxyIgnoredHosts.joinToString(", ")
}
}
private fun computeProxyIgnoredHosts(): List<String> {
return Stream.of("http.nonProxyHosts", "https.nonProxyHosts")
.map { property ->
val propertyValue = System.getProperty(property)
if (propertyValue != null) {
val hosts = propertyValue.split("|")
return@map hosts
.map { host ->
if (host.contains(":")) host.split(":")[0]
else host
}
}
return@map listOf<String>()
}
.flatMap(List<String>::stream)
.distinct()
.collect(toList())
}
}
}