Skip to content

Commit 4e45c0e

Browse files
committed
Add support for client SSL.
1 parent e73979b commit 4e45c0e

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

python/mujinasync/asynctcp.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,21 @@ class TcpClient(TcpServerClientBase):
169169
TCP client base.
170170
"""
171171

172-
def __init__(self, ctx, endpoint, api=None, connectionClass=TcpConnection):
172+
def __init__(self, ctx, endpoint, api=None, connectionClass=TcpConnection, useSsl=False, sslKeyCert=None):
173173
"""Create a TCP client.
174174
175175
:param endpoint: a tuple of (hostname, port) to connect to
176176
:param api: an api object to receive callback on
177177
:param connectionClass: the class to create for each TCP connection
178178
"""
179-
super(TcpClient, self).__init__(ctx, endpoint=endpoint, api=api, connectionClass=connectionClass)
179+
sslContext = None
180+
if useSsl or sslKeyCert is not None:
181+
sslContext = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
182+
sslContext.check_hostname = False
183+
sslContext.verify_mode = ssl.CERT_NONE
184+
if sslKeyCert is not None:
185+
sslContext.load_cert_chain(sslKeyCert)
186+
super(TcpClient, self).__init__(ctx, endpoint=endpoint, api=api, connectionClass=connectionClass, sslContext=sslContext)
180187
self._ctx.RegisterClient(self)
181188

182189
def Destroy(self):
@@ -309,10 +316,10 @@ def SpinOnce(self, timeout=0):
309316
clientSocket = None
310317
try:
311318
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
312-
clientSocket.connect(client._endpoint)
313-
log.debug('new connection to %s', client._endpoint)
314319
if client._sslContext is not None:
315320
clientSocket = client._sslContext.wrap_socket(clientSocket, server_side=False)
321+
clientSocket.connect(client._endpoint)
322+
log.debug('new connection to %s', client._endpoint)
316323
clientSocket.setblocking(0) # TODO: deferred non-blocking after connect finishes, not ideal
317324
except Exception as e:
318325
if clientSocket:

0 commit comments

Comments
 (0)