| 1 |
"""Manage instance API.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import logging |
| 6 |
from typing import Literal, NotRequired, TypedDict |
| 7 |
|
| 8 |
from aiohttp import hdrs |
| 9 |
|
| 10 |
from .api import ApiBase, CloudApiError, api_exception_handler |
| 11 |
|
| 12 |
_LOGGER = logging.getLogger(__name__) |
| 13 |
|
| 14 |
|
| 15 |
class InstanceApiError(CloudApiError): |
| 16 |
"""Exception raised when handling instance API.""" |
| 17 |
|
| 18 |
|
| 19 |
class InstanceConnectionDetails(TypedDict): |
| 20 |
"""Connection details from instance API.""" |
| 21 |
|
| 22 |
connected_at: str |
| 23 |
name: str |
| 24 |
remote_ip_address: str |
| 25 |
version: str |
| 26 |
|
| 27 |
|
| 28 |
class InstanceConnectionConnnected(TypedDict): |
| 29 |
"""Connection details from instance API.""" |
| 30 |
|
| 31 |
connected: Literal[True] |
| 32 |
details: InstanceConnectionDetails |
| 33 |
|
| 34 |
|
| 35 |
class InstanceConnectionDisconnected(TypedDict): |
| 36 |
"""Connection details from instance API.""" |
| 37 |
|
| 38 |
connected: Literal[False] |
| 39 |
|
| 40 |
|
| 41 |
type InstanceConnection = InstanceConnectionConnnected | InstanceConnectionDisconnected |
| 42 |
|
| 43 |
|
| 44 |
class InstanceRegistrationDetails(TypedDict): |
| 45 |
"""Registration details from instance API.""" |
| 46 |
|
| 47 |
alias: NotRequired[list[str]] |
| 48 |
domain: str |
| 49 |
email: str |
| 50 |
server: str |
| 51 |
|
| 52 |
|
| 53 |
class InstanceSnitunTokenDetails(TypedDict): |
| 54 |
"""Snitun token details from instance API.""" |
| 55 |
|
| 56 |
token: str |
| 57 |
server: str |
| 58 |
valid: int |
| 59 |
throttling: int |
| •60 |
protocol_version: NotRequired[int] |
| 61 |
|
| 62 |
|
| 63 |
class PingResult(TypedDict): |
| 64 |
"""Ping result for a single target.""" |
| 65 |
|
| 66 |
ip: str |
| 67 |
avg: float |
| 68 |
max: float |
| 69 |
min: float |
| 70 |
reachable: bool |
| 71 |
|
| 72 |
|
| 73 |
class PingHost(TypedDict): |
| 74 |
"""Host that can be pinged for latency measurement.""" |
| 75 |
|
| 76 |
ip: str |
| 77 |
location: str |
| 78 |
|
| 79 |
|
| 80 |
class PingTargetsResponse(TypedDict): |
| 81 |
"""Response from ping targets API.""" |
| 82 |
|
| 83 |
targets: list[PingHost] |
| 84 |
timeout: int |
| 85 |
count: int |
| 86 |
|
| 87 |
|
| 88 |
class InstanceApi(ApiBase): |
| 89 |
"""Class to help communicate with the instance API.""" |
| 90 |
|
| 91 |
@api_exception_handler(InstanceApiError) |
| 92 |
async def connection( |
| 93 |
self, |
| 94 |
*, |
| 95 |
access_token: str | None = None, |
| 96 |
skip_token_check: bool = False, |
| 97 |
) -> InstanceConnection: |
| 98 |
"""Get the connection details.""" |
| 99 |
_LOGGER.debug("Getting instance connection details") |
| 100 |
details: InstanceConnection = await self._call_cloud_api( |
| 101 |
action="instance_connection", |
| 102 |
headers={ |
| 103 |
hdrs.AUTHORIZATION: access_token or self._cloud.access_token, |
| 104 |
}, |
| 105 |
skip_token_check=skip_token_check, |
| 106 |
) |
| 107 |
return details |
| 108 |
|
| 109 |
@api_exception_handler(InstanceApiError) |
| 110 |
async def cleanup_dns_challenge_record(self, *, value: str) -> None: |
| 111 |
"""Remove DNS challenge.""" |
| 112 |
await self._call_cloud_api( |
| 113 |
method="POST", |
| 114 |
action="remote_access_dns_challenge_remove", |
| 115 |
jsondata={"txt": value}, |
| 116 |
) |
| 117 |
|
| 118 |
@api_exception_handler(InstanceApiError) |
| 119 |
async def create_dns_challenge_record(self, *, value: str) -> None: |
| 120 |
"""Set DNS challenge.""" |
| 121 |
await self._call_cloud_api( |
| 122 |
method="POST", |
| 123 |
action="remote_access_dns_challenge_set", |
| 124 |
jsondata={"txt": value}, |
| 125 |
) |
| 126 |
|
| 127 |
@api_exception_handler(InstanceApiError) |
| 128 |
async def ping_targets(self) -> PingTargetsResponse: |
| 129 |
"""Get ping targets for latency measurement.""" |
| 130 |
details: PingTargetsResponse = await self._call_cloud_api( |
| 131 |
action="remote_access_ping_targets", |
| 132 |
) |
| 133 |
return details |
| 134 |
|
| 135 |
@api_exception_handler(InstanceApiError) |
| 136 |
async def register( |
| 137 |
self, |
| 138 |
*, |
| 139 |
ping_result: list[PingResult] | None = None, |
| 140 |
) -> InstanceRegistrationDetails: |
| 141 |
"""Register the instance.""" |
| 142 |
details: InstanceRegistrationDetails = await self._call_cloud_api( |
| 143 |
method="POST", |
| 144 |
action="remote_access_register", |
| 145 |
jsondata={"ping_result": ping_result} if ping_result is not None else None, |
| 146 |
) |
| 147 |
return details |
| 148 |
|
| 149 |
@api_exception_handler(InstanceApiError) |
| 150 |
async def snitun_token( |
| 151 |
self, |
| 152 |
*, |
| 153 |
aes_key: bytes, |
| 154 |
aes_iv: bytes, |
| 155 |
) -> InstanceSnitunTokenDetails: |
| 156 |
"""Create a remote snitun token.""" |
| 157 |
details: InstanceSnitunTokenDetails = await self._call_cloud_api( |
| 158 |
method="POST", |
| 159 |
action="remote_access_snitun_token", |
| 160 |
jsondata={"aes_key": aes_key.hex(), "aes_iv": aes_iv.hex()}, |
| 161 |
) |
| 162 |
return details |