66import time
77from collections import OrderedDict
88from logging import getLogger
9+ from typing import Any , TypedDict , cast
910
1011from OpenSSL import crypto
1112from twisted .internet import reactor , ssl
13+ from twisted .internet .defer import Deferred
1214from twisted .internet .interfaces import ITCPTransport
13- from twisted .internet .protocol import Factory , defer , connectionDone
15+ from twisted .internet .protocol import Factory , connectionDone , defer
1416from twisted .internet .task import LoopingCall
1517from twisted .protocols .basic import LineReceiver
18+ from twisted .protocols .haproxy ._wrapper import HAProxyWrappingFactory
1619from twisted .python import log , usage
17- from twisted .internet .defer import Deferred
1820from twisted .python .failure import Failure
19- from typing import Any , TypedDict , cast
2021
2122logger = 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.
369373def 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