55import ssl
66
77from pymodbus .client .tcp import AsyncModbusTcpClient , ModbusTcpClient
8- from pymodbus .client .base import ModbusClientProtocol
98from pymodbus .constants import Defaults
109from pymodbus .framer import ModbusFramer
1110from pymodbus .framer .tls_framer import ModbusTlsFramer
12- from pymodbus .transaction import FifoTransactionManager
1311
1412_logger = logging .getLogger (__name__ )
1513
@@ -28,18 +26,20 @@ def sslctx_provider(
2826 :param keyfile: The optional client"s key file path for TLS server request
2927 :param password: The password for for decrypting client"s private key file
3028 """
31- if sslctx is None :
32- # According to MODBUS/TCP Security Protocol Specification, it is
33- # TLSv2 at least
34- sslctx = ssl .SSLContext (ssl .PROTOCOL_TLSv1_2 )
35- sslctx .verify_mode = ssl .CERT_REQUIRED
36- sslctx .check_hostname = True
37-
38- if certfile and keyfile :
39- sslctx .load_cert_chain (
40- certfile = certfile , keyfile = keyfile , password = password
41- )
42-
29+ if sslctx :
30+ return sslctx
31+
32+ sslctx = ssl .SSLContext (ssl .PROTOCOL_TLS_CLIENT )
33+ sslctx .check_hostname = False
34+ sslctx .verify_mode = ssl .CERT_NONE
35+ sslctx .options |= ssl .OP_NO_TLSv1_1
36+ sslctx .options |= ssl .OP_NO_TLSv1
37+ sslctx .options |= ssl .OP_NO_SSLv3
38+ sslctx .options |= ssl .OP_NO_SSLv2
39+ if certfile and keyfile :
40+ sslctx .load_cert_chain (
41+ certfile = certfile , keyfile = keyfile , password = password
42+ )
4343 return sslctx
4444
4545
@@ -89,28 +89,11 @@ def __init__(
8989 self .params .keyfile = keyfile
9090 self .params .password = password
9191 self .params .server_hostname = server_hostname
92-
93- if not sslctx :
94- self .params .sslctx = ssl .create_default_context ()
95- # According to MODBUS/TCP Security Protocol Specification, it is
96- # TLSv2 at least
97- self .sslctx .options |= ssl .OP_NO_TLSv1_1
98- self .sslctx .options |= ssl .OP_NO_TLSv1
99- self .sslctx .options |= ssl .OP_NO_SSLv3
100- self .sslctx .options |= ssl .OP_NO_SSLv2
101- else :
102- self .sslctx = sslctx
10392 AsyncModbusTcpClient .__init__ (self , host , port = port , framer = framer , ** kwargs )
10493
105- async def connect (self ):
106- """Initiate connection to start client."""
107- # get current loop, if there are no loop a RuntimeError will be raised
108- self .loop = asyncio .get_running_loop ()
109- return await AsyncModbusTcpClient .connect (self )
110-
11194 async def _connect (self ):
11295 """Connect to server."""
113- _logger .debug ("Connecting." )
96+ _logger .debug ("Connecting tls ." )
11497 try :
11598 return await self .loop .create_connection (
11699 self ._create_protocol ,
@@ -128,13 +111,6 @@ async def _connect(self):
128111 _logger .info (txt )
129112 self .reset_delay ()
130113
131- def _create_protocol (self ):
132- """Create initialized protocol instance with Factory function."""
133- protocol = ModbusClientProtocol (framer = self .params .framer , ** self .params .kwargs )
134- protocol .transaction = FifoTransactionManager (self )
135- protocol .factory = self
136- return protocol
137-
138114
139115class ModbusTlsClient (ModbusTcpClient ):
140116 """**ModbusTlsClient**.
0 commit comments