-
Notifications
You must be signed in to change notification settings - Fork 11
/
Proxy.scala
217 lines (192 loc) · 7.05 KB
/
Proxy.scala
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
* Copyright 2020 http4s.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.http4s.netty.client
import cats.syntax.all._
import com.comcast.ip4s._
import io.netty.handler.proxy.HttpProxyHandler
import io.netty.handler.proxy.ProxyHandler
import io.netty.handler.proxy.Socks4ProxyHandler
import io.netty.handler.proxy.Socks5ProxyHandler
import org.http4s.BasicCredentials
import org.http4s.Uri
import org.http4s.client.RequestKey
import java.net.InetSocketAddress
import java.util.regex.Pattern.quote
import scala.concurrent.duration.DurationInt
import scala.concurrent.duration.FiniteDuration
import scala.util.Properties
import scala.util.matching.Regex
sealed trait Proxy
sealed trait Socks extends Proxy {
private[client] def toProxyHandler: ProxyHandler
}
final case class Socks5(
host: Host,
port: Port,
username: Option[String],
password: Option[String],
connectionTimeout: FiniteDuration
) extends Socks {
override private[client] def toProxyHandler: ProxyHandler =
Proxy.applySetters(
new Socks5ProxyHandler(
new InetSocketAddress(host.show, port.value),
username.orNull,
password.orNull
))(_.setConnectTimeoutMillis(connectionTimeout.toMillis))
}
object Socks5 {
def apply(host: Host, port: Port, username: Option[String], password: Option[String]): Socks5 =
apply(host, port, username, password, Proxy.defaultTimeoutDuration)
def apply(host: Host, port: Port): Socks5 =
apply(host, port, None, None)
}
final case class Socks4(
host: Host,
port: Port,
username: Option[String],
connectionTimeout: FiniteDuration
) extends Socks {
override private[client] def toProxyHandler: ProxyHandler =
Proxy.applySetters(
new Socks4ProxyHandler(
new InetSocketAddress(host.show, port.value),
username.orNull
))(_.setConnectTimeoutMillis(connectionTimeout.toMillis))
}
object Socks4 {
def apply(host: Host, port: Port, username: Option[String]): Socks4 =
apply(host, port, username, Proxy.defaultTimeoutDuration)
def apply(host: Host, port: Port): Socks4 =
apply(host, port, None)
}
final class IgnoredHosts private (regex: Regex) {
def ignored(uri: RequestKey): Boolean =
regex.pattern.matcher(uri.authority.host.renderString).matches()
override def toString: String = s"IgnoredHosts(${regex.pattern.pattern()})"
}
object IgnoredHosts {
val default: IgnoredHosts = fromString("localhost|127.*|[::1]").get
def fromString(mask: String): Option[IgnoredHosts] = {
def disjunctToRegex(disjunct: String) = disjunct.trim match {
case "*" => ".*"
case s if s.startsWith("*") && s.endsWith("*") =>
".*" + quote(s.substring(1, s.length - 1)) + ".*"
case s if s.startsWith("*") =>
".*" + quote(s.substring(1))
case s if s.endsWith("*") =>
quote(s.substring(0, s.length - 1)) + ".*"
case s => quote(s)
}
val joined = mask
.split("\\|")
.filterNot(_.trim.isEmpty)
.map(disjunct => disjunctToRegex(disjunct.toLowerCase))
.mkString("|")
if (joined.nonEmpty) new IgnoredHosts(joined.r).some else none
}
}
final case class HttpProxy(
scheme: Uri.Scheme,
host: Host,
port: Option[Port],
ignoreHosts: IgnoredHosts,
credentials: Option[BasicCredentials],
connectionTimeout: FiniteDuration
) extends Proxy {
def defaultPort: Int = if (scheme == Uri.Scheme.https) 443 else 80
// todo: should we enforce we need to use https proxy for https requests?
private[client] def toProxyHandler(key: RequestKey) = if (!ignoreHosts.ignored(key)) {
Proxy
.applySetters(
credentials
.fold(
new HttpProxyHandler(
new InetSocketAddress(host.show, port.map(_.value).getOrElse(defaultPort))
)
)(cred =>
new HttpProxyHandler(
new InetSocketAddress(host.show, port.map(_.value).getOrElse(defaultPort)),
cred.username,
cred.password
)))(_.setConnectTimeoutMillis(connectionTimeout.toMillis))
.some
} else none
}
object HttpProxy {
def apply(
scheme: Uri.Scheme,
host: Host,
port: Option[Port],
ignoreHosts: IgnoredHosts,
credentials: Option[BasicCredentials]
): HttpProxy = apply(scheme, host, port, ignoreHosts, credentials, Proxy.defaultTimeoutDuration)
def apply(
scheme: Uri.Scheme,
host: Host,
port: Option[Port],
ignoreHosts: IgnoredHosts
): HttpProxy = apply(scheme, host, port, ignoreHosts, None)
def apply(scheme: Uri.Scheme, host: Host, port: Option[Port]): HttpProxy =
apply(scheme, host, port, IgnoredHosts.default)
def apply(scheme: Uri.Scheme, host: Host): HttpProxy =
apply(scheme, host, None)
}
object Proxy {
val defaultTimeoutDuration: FiniteDuration = 10.seconds
private[client] def applySetters[A](make: A)(fns: ((A) => Unit)*): A =
fns.foldLeft(make) { (a, fn) => fn(a); a }
/** https://docs.oracle.com/javase/8/docs/api/java/net/doc-files/net-properties.html#Proxies
* @return
* a proxy instance if the system properties specified in the document above is detected
*/
def fromSystemProperties: Option[Proxy] = {
val http = {
val httpProxy = sys.props.get("http.proxyHost").flatMap(Host.fromString) -> sys.props
.get("http.proxyPort")
.map(Port.fromString)
val httpsProxy = sys.props.get("https.proxyHost").flatMap(Host.fromString) -> sys.props
.get("https.proxyPort")
.map(Port.fromString)
val ignoreHosts =
sys.props
.get("http.nonProxyHosts")
.flatMap(IgnoredHosts.fromString)
.getOrElse(IgnoredHosts.default)
httpProxy
.mapN((host, port) => HttpProxy(Uri.Scheme.http, host, port, ignoreHosts, None))
.orElse(
httpsProxy.mapN((host, port) =>
HttpProxy(Uri.Scheme.https, host, port, ignoreHosts, None))
)
}
val socks = {
val socksVersion = sys.props.getOrElse("socksProxyVersion", "5")
val socksHost = sys.props.get("socksProxyHost").flatMap(Host.fromString)
val socksPort = Port.fromString(sys.props.getOrElse("socksProxyPort", "1080"))
val socksUsername =
sys.props.getOrElse("java.net.socks.username", Properties.userName)
val socksPassword = sys.props.get("java.net.socks.password")
socksVersion match {
case "4" =>
(socksHost, socksPort).mapN((h, p) => Socks4(h, p, Some(socksUsername)))
case _ =>
(socksHost, socksPort).mapN((h, p) => Socks5(h, p, Some(socksUsername), socksPassword))
}
}
http.orElse(socks)
}
}