portacode 2.2.28.dev4__py3-none-any.whl → 2.2.29.dev0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- portacode/_version.py +2 -2
- portacode/cli.py +2 -0
- portacode/connection/handlers/__init__.py +2 -0
- portacode/connection/handlers/domain_dns.py +16 -0
- portacode/connection/handlers/test_domain_dns.py +134 -0
- portacode/connection/terminal.py +2 -0
- portacode/dns_cli.py +27 -0
- portacode/tunneling/dns.py +177 -0
- {portacode-2.2.28.dev4.dist-info → portacode-2.2.29.dev0.dist-info}/METADATA +1 -1
- {portacode-2.2.28.dev4.dist-info → portacode-2.2.29.dev0.dist-info}/RECORD +14 -10
- {portacode-2.2.28.dev4.dist-info → portacode-2.2.29.dev0.dist-info}/WHEEL +0 -0
- {portacode-2.2.28.dev4.dist-info → portacode-2.2.29.dev0.dist-info}/entry_points.txt +0 -0
- {portacode-2.2.28.dev4.dist-info → portacode-2.2.29.dev0.dist-info}/licenses/LICENSE +0 -0
- {portacode-2.2.28.dev4.dist-info → portacode-2.2.29.dev0.dist-info}/top_level.txt +0 -0
portacode/_version.py
CHANGED
|
@@ -18,7 +18,7 @@ version_tuple: tuple[int | str, ...]
|
|
|
18
18
|
commit_id: str | None
|
|
19
19
|
__commit_id__: str | None
|
|
20
20
|
|
|
21
|
-
__version__ = version = '2.2.
|
|
22
|
-
__version_tuple__ = version_tuple = (2, 2,
|
|
21
|
+
__version__ = version = '2.2.29.dev0'
|
|
22
|
+
__version_tuple__ = version_tuple = (2, 2, 29, 'dev0')
|
|
23
23
|
|
|
24
24
|
__commit_id__ = commit_id = None
|
portacode/cli.py
CHANGED
|
@@ -70,6 +70,7 @@ from .proxmox_infra import (
|
|
|
70
70
|
RollbackProxmoxContainerSnapshotHandler,
|
|
71
71
|
)
|
|
72
72
|
from .cloudflare_tunnel import CloudflareTunnelSetupHandler
|
|
73
|
+
from .domain_dns import DomainDnsHandler
|
|
73
74
|
from .cloudflare_forwarding import (
|
|
74
75
|
CloudflareAccessHandler,
|
|
75
76
|
CloudflareForwardingHandler,
|
|
@@ -155,6 +156,7 @@ __all__ = [
|
|
|
155
156
|
"CloudflareTunnelSetupHandler",
|
|
156
157
|
"CloudflareForwardingHandler",
|
|
157
158
|
"CloudflareAccessHandler",
|
|
159
|
+
"DomainDnsHandler",
|
|
158
160
|
"ConfigureProxmoxContainerExposePortsHandler",
|
|
159
161
|
"AutomationV2StartHandler",
|
|
160
162
|
"AutomationV2StateHandler",
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"""Connected-domain DNS CRUD over the authenticated control channel."""
|
|
2
|
+
from .base import SyncHandler
|
|
3
|
+
from portacode.tunneling.dns import manage_dns
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class DomainDnsHandler(SyncHandler):
|
|
7
|
+
@property
|
|
8
|
+
def command_name(self):
|
|
9
|
+
return 'domain_dns'
|
|
10
|
+
|
|
11
|
+
def execute(self, message):
|
|
12
|
+
allowed = {'action', 'domain', 'record', 'record_id', 'name', 'record_type',
|
|
13
|
+
'page', 'per_page', 'if_match', 'confirm_delete'}
|
|
14
|
+
result = manage_dns(**{key: value for key, value in message.items() if key in allowed})
|
|
15
|
+
return {'event': 'domain_dns_result', 'success': True,
|
|
16
|
+
'device_id': str(message.get('device_id') or ''), **result}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import io
|
|
2
|
+
import json
|
|
3
|
+
import unittest
|
|
4
|
+
import urllib.error
|
|
5
|
+
from unittest.mock import MagicMock, patch
|
|
6
|
+
from unittest.mock import AsyncMock
|
|
7
|
+
|
|
8
|
+
from portacode.tunneling import dns
|
|
9
|
+
from portacode.connection.handlers.domain_dns import DomainDnsHandler
|
|
10
|
+
|
|
11
|
+
RID = 'a' * 32
|
|
12
|
+
ZONE = 'b' * 32
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class DomainDnsTests(unittest.TestCase):
|
|
16
|
+
def record(self, **values):
|
|
17
|
+
return {'type': 'TXT', 'name': '_proof.example.com', 'content': 'proof', **values}
|
|
18
|
+
|
|
19
|
+
def test_names_and_payload_are_confined_to_domain(self):
|
|
20
|
+
for value in ['other.com', 'evilexample.com', 'example.com.evil.test', '../../zones/other', '*.*.example.com']:
|
|
21
|
+
with self.subTest(value=value), self.assertRaises(ValueError):
|
|
22
|
+
dns.normalize_record(self.record(name=value), 'example.com')
|
|
23
|
+
for extra in [{'zone_id': ZONE}, {'proxied': True}, {'ttl': True}, {'ttl': 2}, {'data': {'name': 'evil.com'}}]:
|
|
24
|
+
with self.subTest(extra=extra), self.assertRaises(ValueError):
|
|
25
|
+
dns.normalize_record(self.record(**extra), 'example.com')
|
|
26
|
+
self.assertEqual(dns.normalize_record(self.record(name='@'), 'example.com')['name'], 'example.com')
|
|
27
|
+
self.assertEqual(dns.normalize_record(self.record(name='*.example.com'), 'example.com')['name'], '*.example.com')
|
|
28
|
+
|
|
29
|
+
def test_mx_and_structured_records(self):
|
|
30
|
+
self.assertEqual(dns.normalize_record(self.record(type='MX', content='MAIL.EXAMPLE.NET.', priority=10), 'example.com')['content'], 'mail.example.net')
|
|
31
|
+
with self.assertRaises(ValueError):
|
|
32
|
+
dns.normalize_record(self.record(type='MX', content='mail.example.net'), 'example.com')
|
|
33
|
+
with self.assertRaises(ValueError):
|
|
34
|
+
dns.normalize_record({'type':'SRV','name':'_sip._tcp.example.com','data':{'name':'evil.com'}}, 'example.com')
|
|
35
|
+
|
|
36
|
+
@patch.object(dns, 'DnsClient')
|
|
37
|
+
def test_list_pagination_and_safe_result(self, factory):
|
|
38
|
+
factory.return_value.request.return_value = {'result': [self.record(id=RID, secret='never')], 'result_info': {'page': 2}}
|
|
39
|
+
result = dns.manage_dns(action='list', domain='example.com', page=2, name='_proof.example.com')
|
|
40
|
+
self.assertNotIn('secret', result['records'][0])
|
|
41
|
+
self.assertEqual(result['pagination']['page'], 2)
|
|
42
|
+
self.assertIn('page=2', factory.return_value.request.call_args.args[1])
|
|
43
|
+
|
|
44
|
+
@patch.object(dns, 'DnsClient')
|
|
45
|
+
def test_crud_and_stale_update_guard(self, factory):
|
|
46
|
+
client = factory.return_value
|
|
47
|
+
client.request.return_value = {'result': self.record(id=RID)}
|
|
48
|
+
client.record.return_value = self.record(id=RID, modified_on='revision-1')
|
|
49
|
+
dns.manage_dns(action='create', domain='example.com', record=self.record())
|
|
50
|
+
self.assertEqual(client.request.call_args.args[0], 'POST')
|
|
51
|
+
dns.manage_dns(action='get', domain='example.com', record_id=RID)
|
|
52
|
+
dns.manage_dns(action='update', domain='example.com', record_id=RID, record=self.record(content='new'), if_match='revision-1')
|
|
53
|
+
self.assertEqual(client.request.call_args.args[0], 'PATCH')
|
|
54
|
+
for action in ['update', 'delete']:
|
|
55
|
+
client.request.reset_mock()
|
|
56
|
+
with self.assertRaisesRegex(ValueError, 'changed'):
|
|
57
|
+
dns.manage_dns(action=action, domain='example.com', record_id=RID, record=self.record(), if_match='old', confirm_delete=True)
|
|
58
|
+
client.request.assert_not_called()
|
|
59
|
+
result = dns.manage_dns(action='delete', domain='example.com', record_id=RID, if_match='revision-1', confirm_delete=True)
|
|
60
|
+
self.assertTrue(result['deleted'])
|
|
61
|
+
self.assertEqual(client.request.call_args.args[0], 'DELETE')
|
|
62
|
+
|
|
63
|
+
@patch.object(dns, 'DnsClient')
|
|
64
|
+
def test_delete_confirmation_and_snapshot_required(self, factory):
|
|
65
|
+
for kw in [{}, {'if_match':'now'}, {'if_match':'now', 'confirm_delete':'true'}]:
|
|
66
|
+
with self.assertRaises(ValueError):
|
|
67
|
+
dns.manage_dns(action='delete', domain='example.com', record_id=RID, **kw)
|
|
68
|
+
factory.assert_not_called()
|
|
69
|
+
|
|
70
|
+
@patch.object(dns, 'load_state', return_value={'domain':'example.com','tunnel_id':'tunnel'})
|
|
71
|
+
@patch.object(dns, 'extract_token_json', return_value={'zoneID':ZONE,'apiToken':'private-token'})
|
|
72
|
+
@patch.object(dns.DnsClient, 'request', return_value={'result': {'name':'other.com'}})
|
|
73
|
+
def test_actual_zone_must_match_local_domain(self, request, token, state):
|
|
74
|
+
with self.assertRaisesRegex(ValueError, 'different domain'):
|
|
75
|
+
dns.DnsClient('example.com')
|
|
76
|
+
with self.assertRaisesRegex(ValueError, 'requested domain'):
|
|
77
|
+
dns.DnsClient('other.com')
|
|
78
|
+
self.assertEqual(request.call_count, 1)
|
|
79
|
+
|
|
80
|
+
def test_invalid_ids_and_remote_record_names(self):
|
|
81
|
+
client = object.__new__(dns.DnsClient)
|
|
82
|
+
client.domain = 'example.com'
|
|
83
|
+
client.request = MagicMock(return_value={'result': self.record(name='other.com')})
|
|
84
|
+
for rid in ['../other', '', RID]:
|
|
85
|
+
with self.assertRaises(ValueError): client.record(rid)
|
|
86
|
+
self.assertEqual(client.request.call_count, 1)
|
|
87
|
+
|
|
88
|
+
@patch.object(dns.urllib.request, 'urlopen')
|
|
89
|
+
def test_provider_errors_do_not_expose_secrets(self, urlopen):
|
|
90
|
+
client = object.__new__(dns.DnsClient)
|
|
91
|
+
client.base = '/zones/' + ZONE
|
|
92
|
+
client._token = 'private-token'
|
|
93
|
+
urlopen.side_effect = urllib.error.HTTPError('url', 403, 'private-token', {}, io.BytesIO(b'private-token'))
|
|
94
|
+
with self.assertRaises(RuntimeError) as caught:
|
|
95
|
+
client.request('POST', '/dns_records', self.record())
|
|
96
|
+
self.assertNotIn('private-token', str(caught.exception))
|
|
97
|
+
self.assertIn('403', str(caught.exception))
|
|
98
|
+
|
|
99
|
+
@patch('portacode.connection.handlers.domain_dns.manage_dns', return_value={'records': []})
|
|
100
|
+
def test_handler_accepts_structured_parameters_only(self, manage):
|
|
101
|
+
handler = DomainDnsHandler(None, {})
|
|
102
|
+
self.assertEqual(handler.command_name, 'domain_dns')
|
|
103
|
+
result = handler.execute({'action':'list','domain':'example.com','device_id':42,'url':'evil','token':'secret'})
|
|
104
|
+
manage.assert_called_once_with(action='list', domain='example.com')
|
|
105
|
+
self.assertEqual(result['event'], 'domain_dns_result')
|
|
106
|
+
self.assertNotIn('secret', json.dumps(result))
|
|
107
|
+
|
|
108
|
+
@patch('portacode.dns_cli.manage_dns', return_value={'records': []})
|
|
109
|
+
def test_local_cli_and_invalid_json(self, manage):
|
|
110
|
+
from click.testing import CliRunner
|
|
111
|
+
from portacode.dns_cli import domain_dns
|
|
112
|
+
result = CliRunner().invoke(domain_dns, ['list', '--domain', 'example.com'])
|
|
113
|
+
self.assertEqual(result.exit_code, 0, result.output)
|
|
114
|
+
self.assertEqual(json.loads(result.output), {'records': []})
|
|
115
|
+
result = CliRunner().invoke(domain_dns, ['create', '--domain', 'example.com', '--record-json', '{'])
|
|
116
|
+
self.assertNotEqual(result.exit_code, 0)
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
class DnsProtocolTests(unittest.IsolatedAsyncioTestCase):
|
|
120
|
+
@patch('portacode.connection.handlers.domain_dns.manage_dns', return_value={'records': []})
|
|
121
|
+
async def test_response_and_error_keep_request_correlation(self, manage):
|
|
122
|
+
channel = MagicMock()
|
|
123
|
+
channel.send = AsyncMock()
|
|
124
|
+
handler = DomainDnsHandler(channel, {})
|
|
125
|
+
await handler.handle({'action':'list', 'domain':'example.com', 'request_id':'unique-request'})
|
|
126
|
+
self.assertEqual(channel.send.call_args.args[0]['request_id'], 'unique-request')
|
|
127
|
+
manage.side_effect = ValueError('Domain mismatch')
|
|
128
|
+
await handler.handle({'action':'list', 'domain':'example.com', 'request_id':'failed-request'})
|
|
129
|
+
self.assertEqual(channel.send.call_args.args[0]['request_id'], 'failed-request')
|
|
130
|
+
self.assertEqual(channel.send.call_args.args[0]['event'], 'error')
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
if __name__ == '__main__':
|
|
134
|
+
unittest.main()
|
portacode/connection/terminal.py
CHANGED
|
@@ -86,6 +86,7 @@ from .handlers import (
|
|
|
86
86
|
CloudflareTunnelSetupHandler,
|
|
87
87
|
CloudflareForwardingHandler,
|
|
88
88
|
CloudflareAccessHandler,
|
|
89
|
+
DomainDnsHandler,
|
|
89
90
|
ConfigureProxmoxContainerExposePortsHandler,
|
|
90
91
|
AutomationV2StartHandler,
|
|
91
92
|
AutomationV2StateHandler,
|
|
@@ -633,6 +634,7 @@ class TerminalManager:
|
|
|
633
634
|
self._command_registry.register(CloudflareTunnelSetupHandler)
|
|
634
635
|
self._command_registry.register(CloudflareForwardingHandler)
|
|
635
636
|
self._command_registry.register(CloudflareAccessHandler)
|
|
637
|
+
self._command_registry.register(DomainDnsHandler)
|
|
636
638
|
self._command_registry.register(ConfigureProxmoxContainerExposePortsHandler)
|
|
637
639
|
self._command_registry.register(UpdatePortacodeHandler)
|
|
638
640
|
self._command_registry.register(AutomationV2StartHandler)
|
portacode/dns_cli.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""Local interface to the same structured DNS operations used over WebSocket."""
|
|
2
|
+
import json
|
|
3
|
+
import click
|
|
4
|
+
from .tunneling.dns import manage_dns, TYPES
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@click.command('domain-dns')
|
|
8
|
+
@click.argument('action', type=click.Choice(['list', 'get', 'create', 'update', 'delete']))
|
|
9
|
+
@click.option('--domain', required=True, help='The currently connected Cloudflare domain.')
|
|
10
|
+
@click.option('--record-id')
|
|
11
|
+
@click.option('--record-json', help='Record object for create/update (full desired record).')
|
|
12
|
+
@click.option('--name', help='Exact fully qualified record-name filter, or @ for the apex.')
|
|
13
|
+
@click.option('--record-type', type=click.Choice(sorted(TYPES)))
|
|
14
|
+
@click.option('--page', default=1, type=int)
|
|
15
|
+
@click.option('--per-page', default=100, type=int)
|
|
16
|
+
@click.option('--if-match', help='modified_on value from the most recent record read.')
|
|
17
|
+
@click.option('--confirm-delete', is_flag=True)
|
|
18
|
+
def domain_dns(action, domain, record_id, record_json, name, record_type, page, per_page, if_match, confirm_delete):
|
|
19
|
+
"""Manage connected-domain DNS. Run as the Portacode service user."""
|
|
20
|
+
try:
|
|
21
|
+
result = manage_dns(action=action, domain=domain, record_id=record_id,
|
|
22
|
+
record=json.loads(record_json) if record_json is not None else None,
|
|
23
|
+
name=name, record_type=record_type, page=page, per_page=per_page,
|
|
24
|
+
if_match=if_match, confirm_delete=confirm_delete)
|
|
25
|
+
except (ValueError, RuntimeError) as exc:
|
|
26
|
+
raise click.ClickException(str(exc)) from None
|
|
27
|
+
click.echo(json.dumps(result, indent=2))
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"""Structured DNS operations using the connected domain's local login credential."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import ipaddress
|
|
5
|
+
import json
|
|
6
|
+
import re
|
|
7
|
+
import threading
|
|
8
|
+
import urllib.error
|
|
9
|
+
import urllib.parse
|
|
10
|
+
import urllib.request
|
|
11
|
+
|
|
12
|
+
from .get_domain import extract_token_json
|
|
13
|
+
from .state import default_cert_path, load_state
|
|
14
|
+
|
|
15
|
+
TYPES = frozenset({'A', 'AAAA', 'CNAME', 'MX', 'TXT', 'NS', 'PTR', 'SRV', 'CAA'})
|
|
16
|
+
FIELDS = frozenset({'type', 'name', 'content', 'ttl', 'priority', 'proxied', 'data', 'comment', 'tags'})
|
|
17
|
+
READ_FIELDS = FIELDS | {'id', 'created_on', 'modified_on', 'proxiable'}
|
|
18
|
+
_LOCK = threading.RLock()
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def domain_name(value):
|
|
22
|
+
if not isinstance(value, str):
|
|
23
|
+
raise ValueError('A domain name is required.')
|
|
24
|
+
try:
|
|
25
|
+
name = value.strip().rstrip('.').encode('idna').decode('ascii').lower()
|
|
26
|
+
except UnicodeError:
|
|
27
|
+
raise ValueError('Invalid domain name.') from None
|
|
28
|
+
if len(name) > 253 or not name or any(not re.fullmatch(r'[a-z0-9_-]{1,63}|\*', label) for label in name.split('.')):
|
|
29
|
+
raise ValueError('Invalid domain name.')
|
|
30
|
+
if '*' in name and (not name.startswith('*.') or name.count('*') != 1):
|
|
31
|
+
raise ValueError('Wildcard must be the first label.')
|
|
32
|
+
return name
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def scoped_name(value, domain):
|
|
36
|
+
name = domain if value == '@' else domain_name(value)
|
|
37
|
+
if name != domain and not name.endswith('.' + domain):
|
|
38
|
+
raise ValueError('DNS records must belong to the connected domain.')
|
|
39
|
+
return name
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def normalize_record(record, domain):
|
|
43
|
+
if not isinstance(record, dict) or set(record) - FIELDS:
|
|
44
|
+
raise ValueError('Invalid DNS record fields.')
|
|
45
|
+
result = dict(record)
|
|
46
|
+
kind = result.get('type')
|
|
47
|
+
if kind not in TYPES:
|
|
48
|
+
raise ValueError('Unsupported DNS record type.')
|
|
49
|
+
result['name'] = scoped_name(result.get('name'), domain)
|
|
50
|
+
ttl = result.setdefault('ttl', 1)
|
|
51
|
+
if type(ttl) is not int or not (ttl == 1 or 60 <= ttl <= 86400):
|
|
52
|
+
raise ValueError('TTL must be 1 (automatic), or 60–86400 seconds.')
|
|
53
|
+
if 'proxied' in result and (type(result['proxied']) is not bool or (result['proxied'] and kind not in {'A', 'AAAA', 'CNAME'})):
|
|
54
|
+
raise ValueError('Only A, AAAA and CNAME records can be proxied.')
|
|
55
|
+
if kind in {'CAA', 'SRV'}:
|
|
56
|
+
data = result.get('data')
|
|
57
|
+
allowed = {'flags', 'tag', 'value'} if kind == 'CAA' else {'priority', 'weight', 'port', 'target', 'service', 'proto', 'name'}
|
|
58
|
+
if not isinstance(data, dict) or set(data) - allowed or not data:
|
|
59
|
+
raise ValueError('Invalid structured DNS record data.')
|
|
60
|
+
if 'name' in data:
|
|
61
|
+
data = dict(data, name=scoped_name(data['name'], domain))
|
|
62
|
+
result['data'] = data
|
|
63
|
+
else:
|
|
64
|
+
if 'data' in result:
|
|
65
|
+
raise ValueError('This DNS record type does not accept structured data.')
|
|
66
|
+
content = result.get('content')
|
|
67
|
+
if not isinstance(content, str) or not content or len(content) > 8192 or '\x00' in content:
|
|
68
|
+
raise ValueError('Record content is required (maximum 8192 characters).')
|
|
69
|
+
if kind in {'A', 'AAAA'}:
|
|
70
|
+
try:
|
|
71
|
+
addr = ipaddress.ip_address(content)
|
|
72
|
+
if addr.version != (4 if kind == 'A' else 6):
|
|
73
|
+
raise ValueError()
|
|
74
|
+
result['content'] = str(addr)
|
|
75
|
+
except ValueError:
|
|
76
|
+
raise ValueError('Invalid IP address for this record type.') from None
|
|
77
|
+
elif kind in {'CNAME', 'MX', 'NS', 'PTR'}:
|
|
78
|
+
result['content'] = domain_name(content)
|
|
79
|
+
if kind == 'MX' or 'priority' in result:
|
|
80
|
+
if type(result.get('priority')) is not int or not 0 <= result['priority'] <= 65535:
|
|
81
|
+
raise ValueError('Record priority must be between 0 and 65535.')
|
|
82
|
+
if 'comment' in result and (not isinstance(result['comment'], str) or len(result['comment']) > 500):
|
|
83
|
+
raise ValueError('Invalid record comment.')
|
|
84
|
+
if 'tags' in result and (not isinstance(result['tags'], list) or len(result['tags']) > 20 or any(not isinstance(tag, str) or len(tag) > 200 for tag in result['tags'])):
|
|
85
|
+
raise ValueError('Invalid record tags.')
|
|
86
|
+
if len(json.dumps(result)) > 16384:
|
|
87
|
+
raise ValueError('DNS record is too large.')
|
|
88
|
+
return result
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
class DnsClient:
|
|
92
|
+
def __init__(self, domain):
|
|
93
|
+
state = load_state()
|
|
94
|
+
self.domain = domain_name(domain)
|
|
95
|
+
if domain_name(state.get('domain')) != self.domain or not state.get('tunnel_id'):
|
|
96
|
+
raise ValueError('The requested domain does not match the connected domain.')
|
|
97
|
+
try:
|
|
98
|
+
credential = extract_token_json(str(state.get('cert_path') or default_cert_path()))
|
|
99
|
+
except Exception:
|
|
100
|
+
raise ValueError('Cloudflare login credential unavailable. Reconnect the domain.') from None
|
|
101
|
+
self._token = credential.get('apiToken')
|
|
102
|
+
zone = credential.get('zoneID')
|
|
103
|
+
if not isinstance(zone, str) or not re.fullmatch(r'[a-fA-F0-9]{32}', zone) or not self._token:
|
|
104
|
+
raise ValueError('Cloudflare login credential is incomplete. Reconnect the domain.')
|
|
105
|
+
self.base = '/zones/' + zone
|
|
106
|
+
zone_data = self.request('GET', '')['result']
|
|
107
|
+
if domain_name(zone_data.get('name')) != self.domain:
|
|
108
|
+
raise ValueError('Cloudflare credential belongs to a different domain.')
|
|
109
|
+
|
|
110
|
+
def request(self, method, suffix, payload=None):
|
|
111
|
+
request = urllib.request.Request('https://api.cloudflare.com/client/v4' + self.base + suffix,
|
|
112
|
+
method=method, data=json.dumps(payload).encode() if payload is not None else None,
|
|
113
|
+
headers={'Authorization': 'Bearer ' + self._token, 'Content-Type': 'application/json'})
|
|
114
|
+
try:
|
|
115
|
+
with urllib.request.urlopen(request, timeout=15) as response:
|
|
116
|
+
value = json.load(response)
|
|
117
|
+
except urllib.error.HTTPError as exc:
|
|
118
|
+
# Do not relay provider bodies, request headers or credentials.
|
|
119
|
+
raise RuntimeError('Cloudflare DNS request failed (HTTP %s).' % exc.code) from None
|
|
120
|
+
except (OSError, ValueError):
|
|
121
|
+
raise RuntimeError('Cloudflare DNS is unavailable; check record state before retrying a change.') from None
|
|
122
|
+
if not isinstance(value, dict) or not value.get('success'):
|
|
123
|
+
raise RuntimeError('Cloudflare rejected the DNS operation.')
|
|
124
|
+
return value
|
|
125
|
+
|
|
126
|
+
def record(self, record_id):
|
|
127
|
+
if not isinstance(record_id, str) or not re.fullmatch(r'[a-fA-F0-9]{32}', record_id):
|
|
128
|
+
raise ValueError('Invalid DNS record ID.')
|
|
129
|
+
row = self.request('GET', '/dns_records/' + record_id)['result']
|
|
130
|
+
scoped_name(row.get('name'), self.domain)
|
|
131
|
+
return row
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def public_record(row):
|
|
135
|
+
return {key: value for key, value in row.items() if key in READ_FIELDS}
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def manage_dns(*, action, domain, record=None, record_id=None, name=None, record_type=None,
|
|
139
|
+
page=1, per_page=100, if_match=None, confirm_delete=False):
|
|
140
|
+
if action not in {'list', 'get', 'create', 'update', 'delete'}:
|
|
141
|
+
raise ValueError('Unsupported DNS action.')
|
|
142
|
+
domain = domain_name(domain)
|
|
143
|
+
if action in {'update', 'delete'} and (not isinstance(if_match, str) or not if_match):
|
|
144
|
+
raise ValueError('Read the record first and supply its modified_on value as if_match.')
|
|
145
|
+
if action == 'delete' and confirm_delete is not True:
|
|
146
|
+
raise ValueError('Explicit confirmation is required to delete a DNS record.')
|
|
147
|
+
desired = normalize_record(record, domain) if action in {'create', 'update'} else None
|
|
148
|
+
if type(page) is not int or page < 1 or type(per_page) is not int or not 1 <= per_page <= 100:
|
|
149
|
+
raise ValueError('Invalid DNS pagination.')
|
|
150
|
+
filters = {'page': page, 'per_page': per_page}
|
|
151
|
+
if name is not None:
|
|
152
|
+
filters['name'] = scoped_name(name, domain)
|
|
153
|
+
if record_type is not None:
|
|
154
|
+
if record_type not in TYPES:
|
|
155
|
+
raise ValueError('Unsupported DNS record type.')
|
|
156
|
+
filters['type'] = record_type
|
|
157
|
+
with _LOCK:
|
|
158
|
+
client = DnsClient(domain)
|
|
159
|
+
if action == 'list':
|
|
160
|
+
data = client.request('GET', '/dns_records?' + urllib.parse.urlencode(filters))
|
|
161
|
+
rows = data.get('result', [])
|
|
162
|
+
for row in rows:
|
|
163
|
+
scoped_name(row.get('name'), domain)
|
|
164
|
+
return {'records': [public_record(row) for row in rows], 'pagination': data.get('result_info', {})}
|
|
165
|
+
if action == 'get':
|
|
166
|
+
return {'record': public_record(client.record(record_id))}
|
|
167
|
+
if action == 'create':
|
|
168
|
+
row = client.request('POST', '/dns_records', desired)['result']
|
|
169
|
+
return {'record': public_record(row)}
|
|
170
|
+
existing = client.record(record_id)
|
|
171
|
+
if existing.get('modified_on') != if_match:
|
|
172
|
+
raise ValueError('DNS record changed since it was read. Refresh before modifying it.')
|
|
173
|
+
if action == 'delete':
|
|
174
|
+
client.request('DELETE', '/dns_records/' + record_id)
|
|
175
|
+
return {'deleted': True, 'record_id': record_id}
|
|
176
|
+
row = client.request('PATCH', '/dns_records/' + record_id, desired)['result']
|
|
177
|
+
return {'record': public_record(row)}
|
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
portacode/README.md,sha256=R76fnJCKOnLiBEirTtc7fj4A6j5vAN1pdYMGNGqu1cs,1215
|
|
2
2
|
portacode/__init__.py,sha256=Hwk8wm5KA79qi06r0qKJKT2kjUlHrWaDtH4CyhnWFw8,1086
|
|
3
3
|
portacode/__main__.py,sha256=jmHTGC1hzmo9iKJLv-SSYe9BSIbPPZ2IOpecI03PlTs,296
|
|
4
|
-
portacode/_version.py,sha256=
|
|
4
|
+
portacode/_version.py,sha256=5izFV0iRS3F2Y5h9YCC-cF4OS20eOLT4TTcUuUdSX8Y,535
|
|
5
5
|
portacode/browser_cli.py,sha256=MEc_WxTeezR4WP9wKcC89FGDeXbLyN6Ybfs-foUnrEY,5995
|
|
6
6
|
portacode/browser_prepare.py,sha256=xZRMkSEDZxw1YYz4LbiiYbmF9nOkXLcGsgkbGtVFfmQ,4805
|
|
7
7
|
portacode/browser_runtime.py,sha256=YR0duDqZaZrgSy0I9HrfZqV2fzcU2kIvUG1VyBiC93c,4276
|
|
8
|
-
portacode/cli.py,sha256=
|
|
8
|
+
portacode/cli.py,sha256=pmcnrLnyc2gwMAAy0m72r3IulALHOT9B2p9lwEVoifs,41568
|
|
9
9
|
portacode/codex_loopback_proxy.py,sha256=CK875nnfdyHGY61C-hC7IaQHG9V-jCKdgbdOf2WuaWA,24837
|
|
10
10
|
portacode/codex_prepare.py,sha256=eVIYcXjyk06XhNBGl46sC_PsBQvykSR1sIR7Ny69cR0,28284
|
|
11
11
|
portacode/codex_usage_limit.py,sha256=H3TTD-dest4bTLvgX-ZWKpxVP5Yv25PDteJnHrLOyu8,1414
|
|
12
12
|
portacode/data.py,sha256=M4nPVuLELi4-YwC0XVm32N58cQG-pWSPw6fiv6fNuYA,2322
|
|
13
|
+
portacode/dns_cli.py,sha256=vLb7boE3H22Ps1e9zeJrBGnlGdxlxUVhcqHKwlCueco,1525
|
|
13
14
|
portacode/exit_codes.py,sha256=Lc2VacACHJtNp7dNazZCcBkwQXpmn4w_h6FVe_TGrQI,327
|
|
14
15
|
portacode/github_credential.py,sha256=SVWP1StbRVzpHwgUx1YA7S804tk-5aMfxGIwMKfxZPA,6345
|
|
15
16
|
portacode/image_cli.py,sha256=FzFlRaSJGLsa6Pe8o_vi3wB65TOoFOYgvCAHheD_CZU,3442
|
|
@@ -36,14 +37,14 @@ portacode/connection/client.py,sha256=QDFRwBjATw5tp-wlhkiz5OfL_jVAwRHcVpPOYg6ntO
|
|
|
36
37
|
portacode/connection/codex_app_server.py,sha256=Q16bD78Hf14mtiWQ4QIB8eCTEY4wZrVytrXxr36Mo2E,23245
|
|
37
38
|
portacode/connection/guest_host_protocol.py,sha256=bXghGvUFuj2yg89pPljLuS4ivEYwgM3BwDK7CHIc2Dk,6199
|
|
38
39
|
portacode/connection/multiplex.py,sha256=L-TxqJ_ZEbfNEfu1cwxgJ5vUdyRzZjsMy2Kx1diiZys,5237
|
|
39
|
-
portacode/connection/terminal.py,sha256=
|
|
40
|
+
portacode/connection/terminal.py,sha256=MOPFGvsGyWbm-c3inw-KnNFlJ-jzD-umO8hTHOuqmRs,75694
|
|
40
41
|
portacode/connection/test_guest_host_protocol.py,sha256=KhoXv4YMDYh6ENQrzW5rQNDZ3PMTP7oSN6iSC9KH4NE,2561
|
|
41
42
|
portacode/connection/test_guest_host_terminal.py,sha256=3c54_1fBHljdKmxpj4LYudVpgMZ-W8p_wB14WYVswps,5151
|
|
42
43
|
portacode/connection/test_webmin_proxy_config.py,sha256=52Qv9C0E5UzKUvQDJfREAb3RwJWs5R_CkKECrTWOsNg,1121
|
|
43
44
|
portacode/connection/webmin_proxy_config.py,sha256=vxzucOUz_GgxDjZWcgNVj7ImNfbdpIRABWpG_ihcK6s,2569
|
|
44
45
|
portacode/connection/handlers/README.md,sha256=pnSmHxnmBIwLkWcuNBbZb2XQdYox_dOnSu8-cWoDWJA,13385
|
|
45
46
|
portacode/connection/handlers/WEBSOCKET_PROTOCOL.md,sha256=tKZS0I9mPSaH96U3gNqWn96NCERErVW2x9VMqaKJ51w,134520
|
|
46
|
-
portacode/connection/handlers/__init__.py,sha256=
|
|
47
|
+
portacode/connection/handlers/__init__.py,sha256=m-5rcXtP5TXkptQUYns4TehF3S1HKj51cw61w1_j3Oo,5343
|
|
47
48
|
portacode/connection/handlers/automation_v2_handlers.py,sha256=tCwXul5MZaZhdwGwKvIQEaUC2ySiX12e35R64Gm3wR0,54239
|
|
48
49
|
portacode/connection/handlers/base.py,sha256=tMsKJmnNhXA3a4YZA55a0WO2e8-4Uh4BsG9lWAr3Ris,10829
|
|
49
50
|
portacode/connection/handlers/browser_handlers.py,sha256=FRSxxkexiqJVq7zivDoPwEAyZvhN2GFpwN0N8_lAlno,29065
|
|
@@ -52,6 +53,7 @@ portacode/connection/handlers/cloudflare_forwarding.py,sha256=v7jLFnSYoECakB8O8c
|
|
|
52
53
|
portacode/connection/handlers/cloudflare_tunnel.py,sha256=qHBrm2rATxFmvfoC49YOBj0EUXjh_FETiWw66krxCIg,6655
|
|
53
54
|
portacode/connection/handlers/codex_handlers.py,sha256=TOMH5i-kWq9uK-TzNc1dX5gZPCreW6YB-pVsnj8vLi8,54363
|
|
54
55
|
portacode/connection/handlers/diff_handlers.py,sha256=coN8gfY85KctBEs_bL9RCrZ6iymbP7zaB4Mzwgqrsck,24676
|
|
56
|
+
portacode/connection/handlers/domain_dns.py,sha256=cgwWj9r7teiH41_JS_uczioIkgJxTfXvcJLCXWVTD88,680
|
|
55
57
|
portacode/connection/handlers/file_handlers.py,sha256=ZPenZ14fJu7TTZSS79pLrAF2PKHMdbgmpzz5xZalW2I,44093
|
|
56
58
|
portacode/connection/handlers/file_transfer_handlers.py,sha256=otUb6x4roms-zFuuomyZFH68CNY5abr0A-eUMVgYAO8,7746
|
|
57
59
|
portacode/connection/handlers/git_handlers.py,sha256=VtnNdbwOMbiyTR5sbNkz7Brmyn13Onf3fyECX8JL2lk,6012
|
|
@@ -68,6 +70,7 @@ portacode/connection/handlers/terminal_handlers.py,sha256=s6bNmnXkjbV7rjSoYVDbbB
|
|
|
68
70
|
portacode/connection/handlers/test_browser_handlers.py,sha256=Z5XuVrapOdCa5ZBcfmC6KLSuSV4Jl57YfIu0bLBLvAs,19407
|
|
69
71
|
portacode/connection/handlers/test_cloudflare_access.py,sha256=P43aeaVVZJ7Jr2GAwFzI5BSHIzMfKBXCcc0_vis6YQA,8743
|
|
70
72
|
portacode/connection/handlers/test_cloudflare_forwarding.py,sha256=ifKF-aSeEPr7IDuBlYjAfDowO7Q_qvji46I0iQe6T1w,11661
|
|
73
|
+
portacode/connection/handlers/test_domain_dns.py,sha256=GDm_A3t5BUewDT_r6C6az1EEkl9b2TuMoi0Y7P6wYk8,7710
|
|
71
74
|
portacode/connection/handlers/test_file_move_copy.py,sha256=uXejCCcPWKs9qfyL6ckp0NxjwGYK3-Y63i3lFz-F47M,2872
|
|
72
75
|
portacode/connection/handlers/test_git_handlers.py,sha256=T9gFkCjanEC2L8n_kS2Ht0e67he34vThQtJU7j68auo,3514
|
|
73
76
|
portacode/connection/handlers/test_proxmox_infra.py,sha256=8vDSP7RhYltWazKOkD7oVxp2BpvQwPScDK3sD0D7FNg,60419
|
|
@@ -122,6 +125,7 @@ portacode/tests/test_terminal_session_shell.py,sha256=nNM_fbT1vM8oknGDBeNk0eiRtZ
|
|
|
122
125
|
portacode/tunneling/__init__.py,sha256=qOrizn5awmPfsabVF59-EkmXnvS8AogvcBtVON6UfP0,54
|
|
123
126
|
portacode/tunneling/access.py,sha256=zRdhsT_fQ-cFyqN2ZdLkOWwzXsHifm9tz3qqpVkke_U,9865
|
|
124
127
|
portacode/tunneling/cloudflared_login.py,sha256=5guJT84CG-O1KgR5PgLNIJNzQ-HxCjGgtfqoHSKe8Zg,2456
|
|
128
|
+
portacode/tunneling/dns.py,sha256=kLOPLCi6zyrmwep5-iWJuXOl7WuT0W4xDpwKjvTeOTg,9076
|
|
125
129
|
portacode/tunneling/ensure_cloudflared.py,sha256=4NiN-WZ8-vTeoFTALg-mc_sksvhEaFo0oCZwUMvPBdY,6041
|
|
126
130
|
portacode/tunneling/ensure_pyyaml.py,sha256=cR4LOdJnUQnhH_chq5NL1MAxtSd5x0coNrwzZdPbzhY,2178
|
|
127
131
|
portacode/tunneling/forwarding_state.py,sha256=5FNnfYIKqFzU4_ufuHRlUHqIhYcgQW3Kc3WQOpo3tHM,1255
|
|
@@ -135,7 +139,7 @@ portacode/utils/diff_apply.py,sha256=ArkT-vEQ7iyA1Nfqufq1AiNtRLhoCMnC75FCRh2kNgI
|
|
|
135
139
|
portacode/utils/diff_renderer.py,sha256=S76StnQ2DLfsz4Gg0m07UwPfRp8270PuzbNaQq-rmYk,13850
|
|
136
140
|
portacode/utils/ntp_clock.py,sha256=VqCnWCTehCufE43W23oB-WUdAZGeCcLxkmIOPwInYHc,2499
|
|
137
141
|
portacode/utils/runtime_paths.py,sha256=G9ROho5f5gnDhoQ7vpEAX2XifBzoJTvqTylkF2Hkx8E,1660
|
|
138
|
-
portacode-2.2.
|
|
142
|
+
portacode-2.2.29.dev0.dist-info/licenses/LICENSE,sha256=iiV44E3po4OL77NL8diCk3WtYvCNGu6gUci2TYxVUgQ,5520
|
|
139
143
|
test_modules/README.md,sha256=Do_agkm9WhSzueXjRAkV_xEj6Emy5zB3N3VKY5Roce8,9274
|
|
140
144
|
test_modules/__init__.py,sha256=1LcbHodIHsB0g-g4NGjSn6AMuCoGbymvXPYLOb6Z7F0,53
|
|
141
145
|
test_modules/test_device_online.py,sha256=QtYq0Dq9vME8Gp2O4fGSheqVf8LUtpsSKosXXk56gGM,1654
|
|
@@ -161,8 +165,8 @@ testing_framework/core/playwright_manager.py,sha256=asMe7zQg8Kp3a71F7pv92OiFt63B
|
|
|
161
165
|
testing_framework/core/runner.py,sha256=t3fq0mGCVUCE_TbieEPwmLKjxe44ez29SzW8G33ACME,19746
|
|
162
166
|
testing_framework/core/shared_cli_manager.py,sha256=BESSNtyQb7BOlaOvZmm04T8Uezjms4KCBs2MzTxvzYQ,8790
|
|
163
167
|
testing_framework/core/test_discovery.py,sha256=2FZ9fJ8Dp5dloA-fkgXoJ_gCMC_nYPBnA3Hs2xlagzM,4928
|
|
164
|
-
portacode-2.2.
|
|
165
|
-
portacode-2.2.
|
|
166
|
-
portacode-2.2.
|
|
167
|
-
portacode-2.2.
|
|
168
|
-
portacode-2.2.
|
|
168
|
+
portacode-2.2.29.dev0.dist-info/METADATA,sha256=ABd8EvxpQD9j3_ACLtZQspxT7XAIb8K_EsJdIisa0gk,12881
|
|
169
|
+
portacode-2.2.29.dev0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
170
|
+
portacode-2.2.29.dev0.dist-info/entry_points.txt,sha256=0cV_SuIztVnLWACUY5KMR8K8pWdAsLiaFbLXE5He0rk,151
|
|
171
|
+
portacode-2.2.29.dev0.dist-info/top_level.txt,sha256=TGhTYUxfW8SyVZc_zGgzjzc24gGT7nSw8Qf73liVRKM,41
|
|
172
|
+
portacode-2.2.29.dev0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|