forked from nvaccess/nvda
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
539 lines (467 loc) · 17 KB
/
Copy pathserver.py
File metadata and controls
539 lines (467 loc) · 17 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
# A part of NonVisual Desktop Access (NVDA)
# Copyright (C) 2015-2025 NV Access Limited, Christopher Toth, Tyler Spivey, Babbage B.V., David Sexton and others.
# This file is covered by the GNU General Public License.
# See the file COPYING for more details.
"""Server implementation for NVDA Remote relay functionality.
This module implements a relay server that enables NVDA Remote connections between
multiple clients. It provides:
- A secure SSL/TLS encrypted relay server
- Client authentication via channel password matching
- Message routing between connected clients
- Protocol version recording (clients declare their version)
- Connection monitoring with periodic one-way pings
- Separate IPv4 and IPv6 socket handling
- Dynamic certificate generation and management
The server creates separate IPv4 and IPv6 sockets but routes messages between all
connected clients regardless of IP version. Messages use JSON format and must be
newline-delimited. Invalid messages will cause client disconnection.
When clients disconnect or lose connection, the server automatically removes them and
notifies other connected clients of the departure.
"""
import os
import socket
import ssl
import time
from datetime import datetime, timedelta, timezone
from pathlib import Path
from select import select
from itertools import count
from typing import Any, Final
import cffi # noqa # required for cryptography
from cryptography import x509
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.x509.oid import NameOID
from logHandler import log
from . import configuration
from .protocol import RemoteMessageType
from .secureDesktop import getProgramDataTempPath
from .serializer import JSONSerializer
class RemoteCertificateManager:
"""Manages SSL certificates for the NVDA Remote relay server.
:ivar certDir: Directory where certificates and keys are stored
:ivar certPath: Path to the certificate file
:ivar keyPath: Path to the private key file
:ivar fingerprintPath: Path to the fingerprint file
"""
CERT_FILE = "NvdaRemoteRelay.pem"
KEY_FILE = "NvdaRemoteRelay.key"
FINGERPRINT_FILE = "NvdaRemoteRelay.fingerprint"
CERT_DURATION_DAYS = 365
CERT_RENEWAL_THRESHOLD_DAYS = 30
def __init__(self, certDir: Path | None = None):
"""Initialize the certificate manager.
:param certDir: Directory to store certificate files, defaults to program data temp path
"""
self.certDir: Path = certDir or getProgramDataTempPath()
self.certPath: Path = self.certDir / self.CERT_FILE
self.keyPath: Path = self.certDir / self.KEY_FILE
self.fingerprintPath: Path = self.certDir / self.FINGERPRINT_FILE
def ensureValidCertExists(self) -> None:
"""Ensures a valid certificate and key exist, regenerating if needed."""
log.info("Checking certificate validity")
os.makedirs(self.certDir, exist_ok=True)
if self._filesExist():
try:
self._validateCertificate()
return
except Exception as e:
log.warning(f"Certificate validation failed: {e}", exc_info=True)
self._generateSelfSignedCert()
def _filesExist(self) -> bool:
"""Check if both certificate and key files exist."""
return self.certPath.is_file() and self.keyPath.is_file()
def _validateCertificate(self) -> None:
"""Validates the existing certificate and key.
:raises ValueError: If the current date/time is outside the certificate's validity period, or if the certificate is approaching expiration.
:raises OSError: If the certificate or private key files cannot be opened.
:raises ValueError: If the private key data cannot be decoded.
:raises TypeError: If the private key is encrypted.
"""
# Load and validate certificate
with open(self.certPath, "rb") as f:
certData = f.read()
cert = x509.load_pem_x509_certificate(certData)
# Check validity period
now = datetime.now(timezone.utc)
if not (cert.not_valid_before_utc < now <= cert.not_valid_after_utc):
raise ValueError("Certificate is not within its validity period")
# Check renewal threshold
timeRemaining = cert.not_valid_after_utc - now
if timeRemaining.days <= self.CERT_RENEWAL_THRESHOLD_DAYS:
raise ValueError("Certificate is approaching expiration")
# Verify private key can be loaded
with open(self.keyPath, "rb") as f:
serialization.load_pem_private_key(f.read(), password=None)
def _generateSelfSignedCert(self) -> None:
"""Generates a self-signed certificate and private key."""
privateKey = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
)
subject = issuer = x509.Name(
[
x509.NameAttribute(NameOID.COMMON_NAME, "NVDA Remote Access Service"),
x509.NameAttribute(NameOID.ORGANIZATION_NAME, "NV Access"),
],
)
now = datetime.now(timezone.utc)
cert = (
x509.CertificateBuilder()
.subject_name(
subject,
)
.issuer_name(
issuer,
)
.public_key(
privateKey.public_key(),
)
.serial_number(
x509.random_serial_number(),
)
.not_valid_before(
now,
)
.not_valid_after(
now + timedelta(days=self.CERT_DURATION_DAYS),
)
.add_extension(
x509.BasicConstraints(ca=True, path_length=None),
critical=True,
)
.add_extension(
x509.SubjectAlternativeName(
[
x509.DNSName("localhost"),
],
),
critical=False,
)
.sign(privateKey, hashes.SHA256())
)
# Calculate fingerprint
fingerprint = cert.fingerprint(hashes.SHA256()).hex()
# Write private key
with open(self.keyPath, "wb") as f:
f.write(
privateKey.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption(),
),
)
# Write certificate
with open(self.certPath, "wb") as f:
f.write(cert.public_bytes(serialization.Encoding.PEM))
# Save fingerprint
with open(self.fingerprintPath, "w") as f:
f.write(fingerprint)
# Add to trusted certificates in config
config = configuration.getRemoteConfig()
if "trusted_certs" not in config:
config["trusted_certs"] = {}
config["trusted_certs"]["localhost"] = fingerprint
config["trusted_certs"]["127.0.0.1"] = fingerprint
log.info("Generated new self-signed certificate for NVDA Remote. " f"Fingerprint: {fingerprint}")
def getCurrentFingerprint(self) -> str | None:
"""Get the fingerprint of the current certificate."""
try:
if self.fingerprintPath.is_file():
with open(self.fingerprintPath, "r") as f:
return f.read().strip()
except Exception as e:
log.warning(f"Error reading fingerprint: {e}", exc_info=True)
return None
def createSSLContext(self) -> ssl.SSLContext:
"""Creates an SSL context using the certificate and key."""
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
# Load our certificate and private key
context.load_cert_chain(
certfile=str(self.certPath),
keyfile=str(self.keyPath),
)
# Trust our own CA for server verification
context.load_verify_locations(cafile=str(self.certPath))
# Require client cert verification
context.verify_mode = ssl.CERT_NONE # Don't require client certificates
context.check_hostname = False # Don't verify hostname since we're using self-signed certs
return context
class LocalRelayServer:
"""Secure relay server for NVDA Remote connections.
Accepts encrypted connections from NVDA Remote clients and routes messages between them.
Creates IPv4 and IPv6 listening sockets using SSL/TLS encryption.
Uses select() for non-blocking I/O and monitors connection health with periodic pings.
Clients must authenticate by providing the correct channel password in their join message
before they can exchange messages. Both IPv4 and IPv6 clients share the same channel
and can interact with each other transparently.
:ivar port: Port number to listen on
:ivar password: Channel password for client authentication
:ivar clients: Dictionary mapping sockets to Client objects
:ivar clientSockets: List of client sockets
:ivar PING_TIME_SECONDS: Seconds between ping messages
"""
PING_TIME_SECONDS: int = 300
SELECT_TIMEOUT_SECONDS: Final[int] = 60
def __init__(
self,
port: int,
password: str,
bindHost: str = "",
bindHost6: str = "[::]:",
certDir: Path | None = None,
):
"""Initialize the relay server.
:param port: Port number to listen on
:param password: Channel password for client authentication
:param bindHost: IPv4 address to bind to, defaults to all interfaces
:param bindHost6: IPv6 address to bind to, defaults to all interfaces
:param certDir: Directory to store certificate files, defaults to None
"""
self.port = port
self.password = password
self.certManager = RemoteCertificateManager(certDir)
self.certManager.ensureValidCertExists()
# Initialize other server components
self.serializer = JSONSerializer()
self.clients: dict[socket.socket, Client] = {}
self.clientSockets: list[socket.socket] = []
self._running = False
self.lastPingTime = 0
# Create server sockets
self.serverSocket = self.createServerSocket(
socket.AF_INET,
socket.SOCK_STREAM,
bindAddress=(bindHost, self.port),
)
self.serverSocket6 = self.createServerSocket(
socket.AF_INET6,
socket.SOCK_STREAM,
bindAddress=(bindHost6, self.port),
)
def createServerSocket(self, family: int, type: int, bindAddress: tuple[str, int]) -> ssl.SSLSocket:
"""Creates an SSL wrapped socket using the certificate.
:param family: Socket address family (AF_INET or AF_INET6)
:param type: Socket type (typically SOCK_STREAM)
:param bindAddress: Tuple of (host, port) to bind to
:return: SSL wrapped server socket
:raises socket.error: If socket creation or binding fails
"""
serverSocket = socket.socket(family, type)
sslContext = self.certManager.createSSLContext()
serverSocket = sslContext.wrap_socket(serverSocket, server_side=True)
serverSocket.bind(bindAddress)
serverSocket.listen(5) # Set the maximum number of queued connections
return serverSocket
def run(self) -> None:
"""Main server loop that handles client connections and message routing.
Continuously accepts new connections and processes messages from connected clients.
Sends periodic ping messages to maintain connection health.
:raises socket.error: If there are socket communication errors
"""
log.info(f"Starting NVDA Remote relay server on port {self.port}")
self._running = True
self.lastPingTime = time.time()
while self._running:
read, write, error = select(
self.clientSockets + [self.serverSocket, self.serverSocket6],
[],
self.clientSockets,
self.SELECT_TIMEOUT_SECONDS,
)
if not self._running:
break
for sock in read:
if sock is self.serverSocket or sock is self.serverSocket6:
self.acceptNewConnection(sock)
continue
self.clients[sock].handleData()
if time.time() - self.lastPingTime >= self.PING_TIME_SECONDS:
for client in self.clients.values():
if client.authenticated:
client.send(type=RemoteMessageType.PING)
self.lastPingTime = time.time()
def acceptNewConnection(self, sock: ssl.SSLSocket) -> None:
"""Accept and set up a new client connection."""
try:
clientSock, addr = sock.accept()
log.info(f"New client connection from {addr}")
except (ssl.SSLError, socket.error, OSError):
log.error("Error accepting connection", exc_info=True)
return
# Disable Nagle's algorithm so that packets are always sent immediately.
clientSock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
client = Client(server=self, socket=clientSock)
self.addClient(client)
def addClient(self, client: "Client") -> None:
"""Add a new client to the server."""
self.clients[client.socket] = client
self.clientSockets.append(client.socket)
def removeClient(self, client: "Client") -> None:
"""Remove a client from the server."""
del self.clients[client.socket]
self.clientSockets.remove(client.socket)
def clientDisconnected(self, client: "Client") -> None:
"""Handle client disconnection and notify other clients."""
log.info(f"Client {client.id} disconnected")
self.removeClient(client)
if client.authenticated:
client.sendToOthers(
type=RemoteMessageType.CLIENT_LEFT,
user_id=client.id,
client=client.asDict(),
)
def close(self) -> None:
"""Shut down the server and close all connections."""
log.info("Shutting down NVDA Remote relay server")
self._running = False
self.serverSocket.close()
self.serverSocket6.close()
log.info("Server shutdown complete")
class Client:
"""Handles a single connected NVDA Remote client.
Processes incoming messages, handles authentication via channel password,
records client protocol version, and routes messages to other connected clients.
Maintains a buffer of received data and processes complete messages delimited
by newlines.
:ivar id: Unique client identifier
:ivar socket: SSL socket for this client connection
:ivar buffer: Buffer for incomplete received data
:ivar authenticated: Whether client has authenticated successfully
:ivar connectionType: Type of client connection
:ivar protocolVersion: Client protocol version number
"""
_idCounter = count(1)
def __init__(self, server: LocalRelayServer, socket: ssl.SSLSocket) -> None:
"""Initialize a client connection.
:param server: The relay server instance this client belongs to
:param socket: The SSL socket for this client connection
"""
self.server: LocalRelayServer = server
self.socket: ssl.SSLSocket = socket
self.buffer: bytes = b""
self.serializer: JSONSerializer = server.serializer
self.authenticated: bool = False
self.id: int = next(self._idCounter)
self.connectionType: str | None = None
self.protocolVersion: int = 1
def handleData(self) -> None:
"""Process incoming data from the client socket."""
sockData = b""
try:
sockData = self.socket.recv(16384)
except Exception:
self.close()
return
if not sockData: # Disconnect
self.close()
return
data = self.buffer + sockData
if b"\n" not in data:
self.buffer = data
return
self.buffer = b""
while b"\n" in data:
line, sep, data = data.partition(b"\n")
try:
self.parse(line)
except ValueError:
log.error(f"Error parsing message from client {self.id}", exc_info=True)
self.close()
return
self.buffer += data
def parse(self, line: bytes) -> None:
"""Parse and handle an incoming message line."""
parsed = self.serializer.deserialize(line)
if "type" not in parsed:
return
if self.authenticated:
self.sendToOthers(**parsed)
return
fn = "do_" + parsed["type"]
if hasattr(self, fn):
getattr(self, fn)(parsed)
def asDict(self) -> dict[str, Any]:
"""Get client information as a dictionary."""
return dict(id=self.id, connection_type=self.connectionType)
def do_join(self, obj: dict[str, Any]) -> None:
"""Handle client join request and authentication."""
password = obj.get("channel", None)
if password != self.server.password:
log.warning("Client %s sent incorrect password", self.id)
self.send(
type=RemoteMessageType.ERROR,
message="incorrect_password",
)
self.close()
return
self.connectionType = obj.get("connection_type")
self.authenticated = True
log.info(f"Client {self.id} authenticated successfully " f"(connection type: {self.connectionType})")
clients = []
clientIds = []
for client in list(self.server.clients.values()):
if client is self or not client.authenticated:
continue
clients.append(client.asDict())
clientIds.append(client.id)
self.send(
type=RemoteMessageType.CHANNEL_JOINED,
channel=self.server.password,
user_ids=clientIds,
clients=clients,
)
self.sendToOthers(
type=RemoteMessageType.CLIENT_JOINED,
user_id=self.id,
client=self.asDict(),
)
def do_protocol_version(self, obj: dict[str, Any]) -> None:
"""Record client's protocol version."""
version = obj.get("version")
if not version:
return
self.protocolVersion = version
def close(self) -> None:
"""Close the client connection."""
self.socket.close()
self.server.clientDisconnected(self)
def send(
self,
type: str | RemoteMessageType,
origin: int | None = None,
clients: list[dict[str, Any]] | None = None,
client: dict[str, Any] | None = None,
**kwargs: Any,
) -> None:
"""Send a message to this client.
:param type: Message type
:param origin: Originating client ID
:param clients: List of connected clients
:param client: Client information
:note: Additional keyword arguments are included in the message data.
"""
msg = kwargs
if self.protocolVersion > 1:
if origin:
msg["origin"] = origin
if clients:
msg["clients"] = clients
if client:
msg["client"] = client
try:
data = self.serializer.serialize(type=type, **msg)
self.socket.sendall(data)
except Exception:
log.error(f"Error sending message to client {self.id}", exc_info=True)
self.close()
def sendToOthers(self, origin: int | None = None, **payload: Any) -> None:
"""Send a message to all other authenticated clients.
:param origin: Originating client ID
:param payload: Message data
"""
if origin is None:
origin = self.id
for c in self.server.clients.values():
if c is not self and c.authenticated:
c.send(origin=origin, **payload)