6
6
import time
7
7
from collections import OrderedDict
8
8
from logging import getLogger
9
+ from typing import Any , TypedDict , cast
9
10
10
11
from OpenSSL import crypto
11
12
from twisted .internet import reactor , ssl
13
+ from twisted .internet .defer import Deferred
12
14
from twisted .internet .interfaces import ITCPTransport
13
- from twisted .internet .protocol import Factory , defer , connectionDone
15
+ from twisted .internet .protocol import Factory , connectionDone , defer
14
16
from twisted .internet .task import LoopingCall
15
17
from twisted .protocols .basic import LineReceiver
18
+ from twisted .protocols .haproxy ._wrapper import HAProxyWrappingFactory
16
19
from twisted .python import log , usage
17
- from twisted .internet .defer import Deferred
18
20
from twisted .python .failure import Failure
19
- from typing import Any , TypedDict , cast
20
21
21
22
logger = getLogger ("remote-server" )
22
23
@@ -363,27 +364,32 @@ class Options(usage.Options):
363
364
["network-interface" , "i" , "::" , "Interface to listen on" ],
364
365
["port" , "p" , "6837" , "Server port" ],
365
366
]
367
+ optFlags = [
368
+ ["no-ssl" , "n" , "Disable SSL" ],
369
+ ]
366
370
367
371
368
372
# Exclude from coverage as it's hard to unit test.
369
373
def main () -> Deferred [None ]: # pragma: no cover
374
+ sslContext : ssl .CertificateOptions | None = None
370
375
# Read options from CLI.
371
376
config = Options ()
372
377
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 ()
377
378
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
+ )
387
393
# Initialise the server state machine
388
394
state = ServerState ()
389
395
if os .path .isfile (config ["motd" ]):
@@ -393,11 +399,15 @@ def main() -> Deferred[None]: # pragma: no cover
393
399
state .motd = None
394
400
# Set up the machinery of the server.
395
401
factory = RemoteServerFactory (state )
402
+ wrappedFactory = HAProxyWrappingFactory (factory )
396
403
looper = LoopingCall (factory .pingConnectedClients )
397
404
looper .start (PING_INTERVAL )
398
405
factory .protocol = Handler
399
406
# 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" ])
401
411
reactor .run ()
402
412
return defer .Deferred ()
403
413
0 commit comments