Skip to content

Commit 006cd53

Browse files
Add the option to disable SSL (#8)
This allows the webserver to handle TLS, which is more flexible than the solution used internally. This is the approach used by nvdaremote.com --------- Co-authored-by: Tyler Spivey <[email protected]>
1 parent 4be3369 commit 006cd53

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

server.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@
66
import time
77
from collections import OrderedDict
88
from logging import getLogger
9+
from typing import Any, TypedDict, cast
910

1011
from OpenSSL import crypto
1112
from twisted.internet import reactor, ssl
13+
from twisted.internet.defer import Deferred
1214
from twisted.internet.interfaces import ITCPTransport
13-
from twisted.internet.protocol import Factory, defer, connectionDone
15+
from twisted.internet.protocol import Factory, connectionDone, defer
1416
from twisted.internet.task import LoopingCall
1517
from twisted.protocols.basic import LineReceiver
18+
from twisted.protocols.haproxy._wrapper import HAProxyWrappingFactory
1619
from twisted.python import log, usage
17-
from twisted.internet.defer import Deferred
1820
from twisted.python.failure import Failure
19-
from typing import Any, TypedDict, cast
2021

2122
logger = getLogger("remote-server")
2223

@@ -363,27 +364,32 @@ class Options(usage.Options):
363364
["network-interface", "i", "::", "Interface to listen on"],
364365
["port", "p", "6837", "Server port"],
365366
]
367+
optFlags = [
368+
["no-ssl", "n", "Disable SSL"],
369+
]
366370

367371

368372
# Exclude from coverage as it's hard to unit test.
369373
def main() -> Deferred[None]: # pragma: no cover
374+
sslContext: ssl.CertificateOptions | None = None
370375
# Read options from CLI.
371376
config = Options()
372377
config.parseOptions()
373-
# Open SSL keys.
374-
privkey = open(config["privkey"]).read()
375-
certData = open(config["certificate"], "rb").read()
376-
chain = open(config["chain"], "rb").read()
377378
log.startLogging(sys.stdout)
378-
# Initialise encryption
379-
privkey = crypto.load_privatekey(crypto.FILETYPE_PEM, privkey)
380-
certificate = crypto.load_certificate(crypto.FILETYPE_PEM, certData)
381-
chain = crypto.load_certificate(crypto.FILETYPE_PEM, chain)
382-
contextFactory = ssl.CertificateOptions(
383-
privateKey=privkey,
384-
certificate=certificate,
385-
extraCertChain=[chain],
386-
)
379+
if not config["no-ssl"]:
380+
# Initialise encryption
381+
# Open SSL keys.
382+
privkey = open(config["privkey"]).read()
383+
certData = open(config["certificate"], "rb").read()
384+
chain = open(config["chain"], "rb").read()
385+
privkey = crypto.load_privatekey(crypto.FILETYPE_PEM, privkey)
386+
certificate = crypto.load_certificate(crypto.FILETYPE_PEM, certData)
387+
chain = crypto.load_certificate(crypto.FILETYPE_PEM, chain)
388+
sslContext = ssl.CertificateOptions(
389+
privateKey=privkey,
390+
certificate=certificate,
391+
extraCertChain=[chain],
392+
)
387393
# Initialise the server state machine
388394
state = ServerState()
389395
if os.path.isfile(config["motd"]):
@@ -393,11 +399,15 @@ def main() -> Deferred[None]: # pragma: no cover
393399
state.motd = None
394400
# Set up the machinery of the server.
395401
factory = RemoteServerFactory(state)
402+
wrappedFactory = HAProxyWrappingFactory(factory)
396403
looper = LoopingCall(factory.pingConnectedClients)
397404
looper.start(PING_INTERVAL)
398405
factory.protocol = Handler
399406
# Start running the server.
400-
reactor.listenSSL(int(config["port"]), factory, contextFactory, interface=config["network-interface"])
407+
if config["no-ssl"]:
408+
reactor.listenTCP(int(config["port"]), wrappedFactory, interface=config["network-interface"])
409+
else:
410+
reactor.listenSSL(int(config["port"]), factory, sslContext, interface=config["network-interface"])
401411
reactor.run()
402412
return defer.Deferred()
403413

0 commit comments

Comments
 (0)