File: ratls.py

package info (click to toggle)
python-pyaarlo 0.8.0.15-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 556 kB
  • sloc: python: 6,064; makefile: 6; sh: 1
file content (163 lines) | stat: -rw-r--r-- 5,412 bytes parent folder | download
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
import ssl
import json
import os
from urllib.request import Request, build_opener, HTTPSHandler
from .security_utils import SecurityUtils

from .constant import (
    RATLS_CONNECTIVITY_PATH,
    RATLS_TOKEN_GENERATE_PATH,
    CREATE_DEVICE_CERTS_PATH
)


class ArloRatls(object):
    def __init__(self, arlo, base, public=False):
        self._base_connection_details = None
        self._base_station_token = None
        self._arlo = arlo
        self._base = base
        self._public = public
        self._unique_id = base.unique_id
        self._device_id = base.device_id
        self._security = SecurityUtils(arlo.cfg.storage_dir)

        self._check_device_certs()
        self.open_port()

    def open_port(self):
        """ RATLS port will automatically close after 10 minutes """
        self._base_station_token = self._get_station_token()

        self._arlo.debug(f"Opening port for {self._unique_id}")

        response = self._arlo.be.notify(
            self._base,
            {
                "action": "open",
                "resource": "storage/ratls",
                "from": self._base.user_id,
                "publishResponse": True
            },
            wait_for="event"
        )

        if response is None or not response['success']:
            raise Exception(f"Failed to open ratls port: {response}")

        self._base_connection_details = response['properties']
        self._setup_base_client()

        response = self.get(RATLS_CONNECTIVITY_PATH)
        if response is None or not response['success']:
            raise Exception(f"Failed to gain connectivity to ratls!")

        return self._base_connection_details

    def get(self, path, raw=False):
        request = Request(f"{self.url}{path}")
        request.get_method = lambda: 'GET'

        for (k, v) in self._ratls_req_headers().items():
            request.add_header(k, v)

        try:
            response = self._base_client.open(request)
            if raw:
                return response
            return json.loads(response.read())
        except Exception as e:
            self._arlo.warning("request-error={}".format(type(e).__name__))
            return None

    def _ratls_req_headers(self):
        return {
            "Authorization": f"Bearer {self._base_station_token}",
            "Accept": "application/json; charset=utf-8;",
            "Accept-Language": "en-US,en;q=0.9",
            "Origin": "https://my.arlo.com",
            "SchemaVersion": "1",
            "User-Agent": self._arlo.be.user_agent(self._arlo.cfg.user_agent)
        }

    def _get_station_token(self):
        """ Tokens expire after 10 minutes """
        self._arlo.debug(f"Fetching token for {self._device_id}")

        response = self._arlo.be.get(
            RATLS_TOKEN_GENERATE_PATH + f"/{self._device_id}"
        )

        if response is None or not 'ratlsToken' in response:
            raise Exception(f"Failed get station token: {response}")

        return response['ratlsToken']

    def _setup_base_client(self):
        certs_path = self._security.certs_path
        device_certs_path = self._security.device_certs_path(self._unique_id)
        self._sslcontext = ssl.create_default_context(cafile=os.path.join(certs_path, "ica.crt"))

        # We are providing certs for the base station to trust us
        self._sslcontext.load_cert_chain(os.path.join(device_certs_path, "peer.crt"), self._security.private_key_path)

        # ... but we cannot validate the base station's certificate
        self._sslcontext.check_hostname = False
        self._sslcontext.verify_mode = ssl.CERT_NONE

        self._base_client = build_opener(HTTPSHandler(context=self._sslcontext))

    def _check_device_certs(self):
        self._arlo.debug(f"Checking for existing certificates for {self._unique_id}")

        if not self._security.has_device_certs(self._unique_id):
            response = self._arlo.be.post(
                CREATE_DEVICE_CERTS_PATH,
                params={
                    "uuid": self._device_id,
                    "uniqueIds": [
                        self._unique_id
                    ],
                    "publicKey": self._security.public_key.replace("\n", "").replace("-----BEGIN PUBLIC KEY-----", "").replace("-----END PUBLIC KEY-----", ""),
                },
                headers={"xcloudId": self._base.xcloud_id},
                raw=True
            )

            if not response["success"]:
                raise Exception(f"Error getting certs: {response['message']} - {response['reason']}")

            self._arlo.debug(f"Saving certificates for {self._unique_id}")

            self._security.save_device_certs(self._unique_id, response["data"])

    @property
    def security(self):
        return self._security

    @property
    def url(self):
        if self._public:
            return self.publicUrl
        else:
            return self.privateUrl

    @property
    def privateIp(self):
        return self._base_connection_details['privateIP']

    @property
    def publicIp(self):
        return self._base_connection_details['publicIP']

    @property
    def port(self):
        return self._base_connection_details['port']

    @property
    def privateUrl(self):
        return f"https://{self.privateIp}:{self.port}"

    @property
    def publicUrl(self):
        return f"https://{self.publicIp}:{self.port}"