|
4 | 4 | import uuid |
5 | 5 | import string |
6 | 6 | import platform |
| 7 | +import traceback |
7 | 8 | import collections |
8 | 9 | from urllib.parse import urlparse, urlencode |
9 | 10 | import typing |
@@ -322,18 +323,38 @@ def _try_parse_url(url: str) -> Optional[str]: |
322 | 323 |
|
323 | 324 |
|
324 | 325 | def _try_connect_to_server( |
325 | | - url: str, timeout: Optional[float] = None |
326 | | -) -> bool: |
| 326 | + url: str, |
| 327 | + timeout: Optional[float], |
| 328 | + verify: Optional["Union[str, bool]"], |
| 329 | + cert: Optional[str], |
| 330 | +) -> Optional[str]: |
327 | 331 | if timeout is None: |
328 | 332 | timeout = get_default_timeout() |
| 333 | + |
| 334 | + if verify is None: |
| 335 | + verify = os.environ.get("AYON_CA_FILE") or True |
| 336 | + |
| 337 | + if cert is None: |
| 338 | + cert = os.environ.get("AYON_CERT_FILE") or None |
| 339 | + |
329 | 340 | try: |
330 | 341 | # TODO add validation if the url lead to AYON server |
331 | 342 | # - this won't validate if the url lead to 'google.com' |
332 | | - requests.get(url, timeout=timeout) |
| 343 | + response = requests.get( |
| 344 | + url, |
| 345 | + timeout=timeout, |
| 346 | + verify=verify, |
| 347 | + cert=cert, |
| 348 | + ) |
| 349 | + if response.history: |
| 350 | + return response.history[-1].headers["location"].rstrip("/") |
| 351 | + return url |
333 | 352 |
|
334 | | - except BaseException: |
335 | | - return False |
336 | | - return True |
| 353 | + except Exception: |
| 354 | + print(f"Failed to connect to '{url}'") |
| 355 | + traceback.print_exc() |
| 356 | + |
| 357 | + return None |
337 | 358 |
|
338 | 359 |
|
339 | 360 | def login_to_server( |
@@ -463,7 +484,12 @@ def is_token_valid( |
463 | 484 | return False |
464 | 485 |
|
465 | 486 |
|
466 | | -def validate_url(url: str, timeout: Optional[int] = None) -> str: |
| 487 | +def validate_url( |
| 488 | + url: str, |
| 489 | + timeout: Optional[int] = None, |
| 490 | + verify: Optional["Union[str, bool]"] = None, |
| 491 | + cert: Optional[str] = None, |
| 492 | +) -> str: |
467 | 493 | """Validate url if is valid and server is available. |
468 | 494 |
|
469 | 495 | Validation checks if can be parsed as url and contains scheme. |
@@ -520,12 +546,23 @@ def validate_url(url: str, timeout: Optional[int] = None) -> str: |
520 | 546 | # Try add 'https://' scheme if is missing |
521 | 547 | # - this will trigger UrlError if both will crash |
522 | 548 | if not parsed_url.scheme: |
523 | | - new_url = "https://" + modified_url |
524 | | - if _try_connect_to_server(new_url, timeout=timeout): |
| 549 | + new_url = _try_connect_to_server( |
| 550 | + "http://" + modified_url, |
| 551 | + timeout=timeout, |
| 552 | + verify=verify, |
| 553 | + cert=cert, |
| 554 | + ) |
| 555 | + if new_url: |
525 | 556 | return new_url |
526 | 557 |
|
527 | | - if _try_connect_to_server(modified_url, timeout=timeout): |
528 | | - return modified_url |
| 558 | + new_url = _try_connect_to_server( |
| 559 | + modified_url, |
| 560 | + timeout=timeout, |
| 561 | + verify=verify, |
| 562 | + cert=cert, |
| 563 | + ) |
| 564 | + if new_url: |
| 565 | + return new_url |
529 | 566 |
|
530 | 567 | hints = [] |
531 | 568 | if "/" in parsed_url.path or not parsed_url.scheme: |
|
0 commit comments