aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaximilian Hils <git@maximilianhils.com>2017-02-22 13:17:53 +0100
committerGitHub <noreply@github.com>2017-02-22 13:17:53 +0100
commitba76dbc672aeaa4454b82002f386bf22c06a039e (patch)
tree68f5e198dc10495fc4a4d2662643fb3d2c55d7b5
parent391f28f78cf9f4ee2f82c69c7f5ce370b69b77cd (diff)
parentaa6b0f299e7cebb70a7e5b1f8b63d7d45683e31f (diff)
downloadmitmproxy-ba76dbc672aeaa4454b82002f386bf22c06a039e.tar.gz
mitmproxy-ba76dbc672aeaa4454b82002f386bf22c06a039e.tar.bz2
mitmproxy-ba76dbc672aeaa4454b82002f386bf22c06a039e.zip
Merge pull request #2048 from ujjwal96/ipv6-addresses
Absolute IPv6 addresses supported
-rw-r--r--mitmproxy/net/check.py14
-rw-r--r--test/mitmproxy/net/test_check.py1
2 files changed, 13 insertions, 2 deletions
diff --git a/mitmproxy/net/check.py b/mitmproxy/net/check.py
index f793d397..d30c1df6 100644
--- a/mitmproxy/net/check.py
+++ b/mitmproxy/net/check.py
@@ -1,3 +1,4 @@
+import ipaddress
import re
# Allow underscore in host name
@@ -6,17 +7,26 @@ _label_valid = re.compile(b"(?!-)[A-Z\d\-_]{1,63}(?<!-)$", re.IGNORECASE)
def is_valid_host(host: bytes) -> bool:
"""
- Checks if a hostname is valid.
+ Checks if the passed bytes are a valid DNS hostname or an IPv4/IPv6 address.
"""
try:
host.decode("idna")
except ValueError:
return False
+ # RFC1035: 255 bytes or less.
if len(host) > 255:
return False
if host and host[-1:] == b".":
host = host[:-1]
- return all(_label_valid.match(x) for x in host.split(b"."))
+ # DNS hostname
+ if all(_label_valid.match(x) for x in host.split(b".")):
+ return True
+ # IPv4/IPv6 address
+ try:
+ ipaddress.ip_address(host.decode('idna'))
+ return True
+ except ValueError:
+ return False
def is_valid_port(port):
diff --git a/test/mitmproxy/net/test_check.py b/test/mitmproxy/net/test_check.py
index 9dbc02e0..0ffd6b2e 100644
--- a/test/mitmproxy/net/test_check.py
+++ b/test/mitmproxy/net/test_check.py
@@ -11,3 +11,4 @@ def test_is_valid_host():
assert check.is_valid_host(b"one.two.")
# Allow underscore
assert check.is_valid_host(b"one_two")
+ assert check.is_valid_host(b"::1")
> 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472