Debian Codegraph @

asusrouter/connection_config.py

raw
1 """Connection configuration module.
2
3 This module handles the configuration of the connection settings
4 for the library.
5 """
6
7 from __future__ import annotations
8
9 from collections.abc import Callable
10 from typing import Any
11
12 from asusrouter.config import (
13 CONFIG_DEFAULT_ALREADY_NOTIFIED,
14 CONFIG_DEFAULT_BOOL,
15 CONFIG_DEFAULT_INT,
16 ARConfigBase,
17 ARConfigKeyBase,
18 safe_bool_config,
19 safe_int_config,
20 )
21
22
23 class ARConnectionConfigKey(ARConfigKeyBase):
24 """Connection configuration keys for AsusRouter."""
25
26 # Allow automatic fallback
27 ALLOW_FALLBACK = "allow_fallback"
28 # Allow multiple fallbacks
29 ALLOW_MULTIPLE_FALLBACKS = "allow_multiple_fallbacks"
30 # Allow upgrade from HTTP to HTTPS
31 ALLOW_UPGRADE_HTTP_TO_HTTPS = "allow_upgrade_http_to_https"
32 # Port
33 PORT = "port"
34 # Strict SSL
35 STRICT_SSL = "strict_ssl"
36 NOTIFIED_STRICT_SSL_NO_SSL = "notified_strict_ssl_fallback"
37 # Use SSL
38 USE_SSL = "use_ssl"
39 # Verify SSL certificate
40 VERIFY_SSL = "verify_ssl"
41 NOTIFIED_VERIFY_SSL_FAILED = "notified_verify_ssl_failed"
42
43
44 CONNECTION_CONFIG_DEFAULT: dict[ARConnectionConfigKey, Any] = {
45 # Allow automatic fallback
46 ARConnectionConfigKey.ALLOW_FALLBACK: CONFIG_DEFAULT_BOOL,
47 # Allow multiple fallbacks
48 # If set, AsusRouter will allow fallback to a different connection method
49 # any number of times
50 # (except for consecutive attempts, where loop protection will trigger)
51 ARConnectionConfigKey.ALLOW_MULTIPLE_FALLBACKS: CONFIG_DEFAULT_BOOL,
52 # Allow upgrade from HTTP to HTTPS
53 ARConnectionConfigKey.ALLOW_UPGRADE_HTTP_TO_HTTPS: True,
54 # Port
55 ARConnectionConfigKey.PORT: CONFIG_DEFAULT_INT,
56 # If set, AsusRouter will not allow falling back to a non-SSL connection
57 # automatically.
58 ARConnectionConfigKey.STRICT_SSL: CONFIG_DEFAULT_BOOL,
59 ARConnectionConfigKey.NOTIFIED_STRICT_SSL_NO_SSL: CONFIG_DEFAULT_ALREADY_NOTIFIED, # noqa: E501
60 # Use SSL
61 ARConnectionConfigKey.USE_SSL: CONFIG_DEFAULT_BOOL,
62 # If set, AsusRouter will verify SSL certificates when connecting
63 # via HTTPS. Most Asus routers use self-signed certificates, so
64 # connections may fail unless you manually add the issuer to your
65 # trusted certificates.
66 ARConnectionConfigKey.VERIFY_SSL: CONFIG_DEFAULT_BOOL,
67 ARConnectionConfigKey.NOTIFIED_VERIFY_SSL_FAILED: CONFIG_DEFAULT_ALREADY_NOTIFIED, # noqa: E501
68 }
69
70 CONNECTION_CONFIG_TYPES_DEFAULTS: dict[
71 ARConnectionConfigKey, Callable[[Any], Any]
72 ] = {
73 # Allow automatic fallback
74 ARConnectionConfigKey.ALLOW_FALLBACK: safe_bool_config,
75 # Allow multiple fallbacks
76 ARConnectionConfigKey.ALLOW_MULTIPLE_FALLBACKS: safe_bool_config,
77 # Allow upgrade from HTTP to HTTPS
78 ARConnectionConfigKey.ALLOW_UPGRADE_HTTP_TO_HTTPS: safe_bool_config,
79 # Port
80 ARConnectionConfigKey.PORT: safe_int_config,
81 # Strict SSL
82 ARConnectionConfigKey.STRICT_SSL: safe_bool_config,
83 ARConnectionConfigKey.NOTIFIED_STRICT_SSL_NO_SSL: safe_bool_config,
84 # Use SSL
85 ARConnectionConfigKey.USE_SSL: safe_bool_config,
86 # Verify SSL certificate
87 ARConnectionConfigKey.VERIFY_SSL: safe_bool_config,
88 ARConnectionConfigKey.NOTIFIED_VERIFY_SSL_FAILED: safe_bool_config,
89 }
90
91
92 class ARConnectionConfig(ARConfigBase):
93 """Connection configuration for AsusRouter."""
94
95 def __init__(
96 self,
97 defaults: dict[ARConnectionConfigKey, Any] | None = None,
98 ) -> None:
99 """Initialize the connection configuration."""
100
101 super().__init__()
102 self.reset(defaults)
103
104 def reset(
105 self,
106 defaults: dict[ARConnectionConfigKey, Any] | None = None,
107 ) -> None:
108 """Reset all connection configuration options."""
109
110 super().reset()
111
112 with self._lock:
113 defaults = defaults or CONNECTION_CONFIG_DEFAULT
114 for key in ARConnectionConfigKey:
115 self._options[key] = defaults.get(key)
116 self._types[key] = CONNECTION_CONFIG_TYPES_DEFAULTS.get(
117 key, safe_bool_config
118 )