-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodbus_tcp_proxy.py
407 lines (378 loc) · 16.2 KB
/
modbus_tcp_proxy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import argparse
import os
import queue
import threading
import time
import logging
import socket
import ipaddress
import signal
import random
from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass
from contextlib import contextmanager
import yaml
from cerberus import Validator
from pymodbus.client import ModbusTcpClient
from ipaddress import ip_address, ip_network
# Datenklassen für Konfigurationen
@dataclass
class ModbusConfig:
host: str
port: int
timeout: int
delay: float
max_retries: int = 5
max_backoff: float = 30.0
@dataclass
class ProxyConfig:
host: str
port: int
allowed_ips: list = None
max_connections: int = 100
# Validierungsfunktion für Netzwerkeinstellungen
def validate_network_settings(field, value, error):
"""Custom validator for network settings"""
try:
if field == "ServerHost" or field == "ModbusServerHost":
try:
ipaddress.ip_address(value)
except ValueError:
if not all(c.isalnum() or c in ".-" for c in value):
error(field, "Invalid hostname or IP address")
except Exception as e:
error(field, f"Validation error: {str(e)}")
# Konfigurationsvalidierung
def validate_config(config):
"""Validate and normalize configuration with enhanced validation"""
schema = {
'Proxy': {
'type': 'dict',
'schema': {
'ServerHost': {
'type': 'string',
'required': True,
'check_with': validate_network_settings
},
'ServerPort': {
'type': 'integer',
'min': 1,
'max': 65535,
'required': True
},
'AllowedIPs': {
'type': 'list',
'schema': {'type': 'string'},
'default': []
},
'MaxConnections': {
'type': 'integer',
'min': 1,
'max': 10000,
'default': 100
}
}
},
'ModbusServer': {
'type': 'dict',
'schema': {
'ModbusServerHost': {
'type': 'string',
'required': True,
'check_with': validate_network_settings
},
'ModbusServerPort': {
'type': 'integer',
'min': 1,
'max': 65535,
'required': True
},
'ConnectionTimeout': {
'type': 'integer',
'min': 1,
'default': 10
},
'DelayAfterConnection': {
'type': 'float',
'min': 0.0,
'default': 0.5
},
'MaxRetries': {
'type': 'integer',
'min': 1,
'default': 5
},
'MaxBackoff': {
'type': 'float',
'min': 1.0,
'default': 30.0
}
}
},
'Logging': {
'type': 'dict',
'schema': {
'Enable': {'type': 'boolean', 'default': False},
'LogFile': {'type': 'string', 'required': False},
'LogLevel': {
'type': 'string',
'allowed': ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
'default': 'INFO'
}
}
}
}
validator = Validator(schema)
if not validator.validate(config):
raise ValueError(f"Configuration validation failed: {validator.errors}")
# Umgebungsvariablen überschreiben Konfigurationswerte, falls vorhanden
for section in config:
for key in config[section]:
env_var = f"MODBUS_PROXY_{section.upper()}_{key.upper()}"
if env_var in os.environ:
if isinstance(config[section][key], bool):
config[section][key] = os.environ[env_var].lower() in ('true', '1', 'yes')
elif isinstance(config[section][key], int):
config[section][key] = int(os.environ[env_var])
elif isinstance(config[section][key], float):
config[section][key] = float(os.environ[env_var])
else:
config[section][key] = os.environ[env_var]
return validator.normalized(config)
# Konfiguration laden
def load_config(config_path):
"""Load configuration from YAML file with environment variable support"""
with open(config_path, "r", encoding="utf-8") as file:
config = yaml.safe_load(file)
return validate_config(config)
# Logger initialisieren
def init_logger(config):
"""Initialize logger with appropriate configuration"""
logger = logging.getLogger()
for handler in logger.handlers[:]:
logger.removeHandler(handler)
logger.setLevel(config["Logging"].get("LogLevel", "INFO").upper())
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
if config["Logging"].get("Enable", False):
try:
file_handler = logging.FileHandler(config["Logging"].get("LogFile", "modbus_proxy.log"))
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
except (PermissionError, OSError) as e):
print(f"Warning: Could not set up file logging: {str(e)}")
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
return logger
# Modbus-Client-Klasse
class PersistentModbusClient:
"""Enhanced Modbus client with better connection management"""
def __init__(self, modbus_config, logger):
self.config = modbus_config
self.logger = logger
self.client = None
self._lock = threading.RLock()
def connect(self):
"""Connect to Modbus server with exponential backoff retry strategy"""
with self._lock:
attempts = 0
while not self.client or not self.client.is_socket_open():
try:
if self.client:
self.client.close()
self.logger.info(f"Connecting to Modbus server at {self.config.host}:{self.config.port}")
self.client = ModbusTcpClient(
host=self.config.host,
port=self.config.port,
timeout=self.config.timeout
)
if self.client.connect():
self.logger.info("Successfully connected to Modbus server.")
time.sleep(self.config.delay)
return True
else:
raise ConnectionError("Failed to connect to Modbus server.")
except (socket.error, OSError, ConnectionError) as exc:
attempts += 1
if attempts >= self.config.max_retries:
self.logger.critical(f"Max retries ({self.config.max_retries}) reached. Giving up.")
raise
backoff = min(self.config.max_backoff, (2 ** attempts) + random.uniform(0, 1))
self.logger.error(f"Connection error: {exc}. Attempt {attempts} of {self.config.max_retries}. "
f"Retrying in {backoff:.2f}s")
time.sleep(backoff)
@contextmanager
def connection(self):
"""Context manager for ensuring connection is active"""
try:
if not self.client or not self.client.is_socket_open():
self.connect()
yield self.client
except Exception as exc:
self.logger.error(f"Connection error: {exc}")
self.connect()
raise
def send_request(self, data):
"""Send request to Modbus server with improved error handling"""
with self._lock:
try:
with self.connection() as client:
self.logger.debug(f"Sending request: {data.hex()}")
client.socket.sendall(data)
response = b""
client.socket.settimeout(self.config.timeout)
while True:
try:
chunk = client.socket.recv(1024)
if not chunk:
break
response += chunk
if len(chunk) < 1024:
break
except socket.timeout:
self.logger.warning("Socket recv timeout reached.")
break
self.logger.debug(f"Received response: {response.hex()}")
return response
except (socket.error, ConnectionError) as exc:
self.logger.error(f"Communication error during request: {exc}")
raise
def close(self):
"""Safely close connection"""
with self._lock:
if self.client:
try:
self.client.close()
self.logger.info("Modbus connection closed.")
except Exception as exc:
self.logger.warning(f"Error closing Modbus connection: {exc}")
finally:
self.client = None
# Client-Verbindungen behandeln
def handle_client(client_socket, client_address, request_queue, logger, stop_event, active_connections, semaphore):
"""Handle individual client connections with improved resource management"""
connection_id = f"{client_address[0]}:{client_address[1]}"
active_connections[connection_id] = client_socket
try:
logger.info(f"New client connected: {connection_id}")
client_socket.settimeout(60)
while not stop_event.is_set():
try:
data = client_socket.recv(1024)
if not data:
logger.info(f"Client disconnected: {connection_id}")
break
request_queue.put((data, client_socket, connection_id))
except socket.timeout:
if stop_event.is_set():
break
continue
except (socket.error, OSError) as exc:
logger.error(f"Error with client {connection_id}: {exc}")
break
finally:
try:
client_socket.close()
except Exception:
pass
if connection_id in active_connections:
del active_connections[connection_id]
semaphore.release()
logger.info(f"Socket for {connection_id} closed")
# Anfragen verarbeiten
def process_requests(request_queue, persistent_client, logger, stop_event, active_connections):
"""Process requests from the queue with improved error handling"""
while not stop_event.is_set():
try:
data, client_socket, connection_id = request_queue.get(timeout=1)
if connection_id not in active_connections or client_socket.fileno() == -1:
logger.warning(f"Client {connection_id} disconnected before processing request")
continue
try:
response = persistent_client.send_request(data)
if connection_id in active_connections and client_socket.fileno() != -1:
client_socket.sendall(response)
else:
logger.warning(f"Client {connection_id} disconnected before sending response")
except (socket.error, ConnectionError) as exc:
logger.error(f"Error processing request from {connection_id}: {exc}")
try:
if client_socket.fileno() != -1:
client_socket.close()
except Exception:
pass
if connection_id in active_connections:
del active_connections[connection_id]
except queue.Empty:
continue
except Exception as exc:
logger.error(f"Unexpected error in request processor: {exc}")
# Server starten
def start_server(config):
"""Start the Modbus TCP proxy server with improved resource management"""
logger = init_logger(config)
logger.info("Starting Modbus TCP Proxy Server")
stop_event = threading.Event()
active_connections = {}
cpu_count = os.cpu_count() or 4
max_queue_size = max(10, min(1000, cpu_count * 25))
request_queue = queue.Queue(maxsize=max_queue_size)
max_workers = max(4, cpu_count * 2)
connection_semaphore = threading.Semaphore(config["Proxy"]["MaxConnections"])
# Unterstützung für CIDR-Notation in AllowedIPs
allowed_networks = []
if config["Proxy"]["AllowedIPs"]:
for ip in config["Proxy"]["AllowedIPs"]:
try:
allowed_networks.append(ip_network(ip))
except ValueError:
logger.warning(f"Ungültige IP oder Netzwerk in AllowedIPs: {ip}")
modbus_config = ModbusConfig(
host=config["ModbusServer"]["ModbusServerHost"],
port=config["ModbusServer"]["ModbusServerPort"],
timeout=config["ModbusServer"].get("ConnectionTimeout", 10),
delay=config["ModbusServer"].get("DelayAfterConnection", 0.5),
max_retries=config["ModbusServer"].get("MaxRetries", 5),
max_backoff=config["ModbusServer"].get("MaxBackoff", 30.0)
)
persistent_client = PersistentModbusClient(modbus_config, logger)
try:
persistent_client.connect()
with ThreadPoolExecutor(max_workers=max_workers) as executor:
executor.submit(process_requests, request_queue, persistent_client, logger, stop_event, active_connections)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((config["Proxy"]["ServerHost"], config["Proxy"]["ServerPort"]))
server_socket.listen(5)
server_socket.settimeout(1)
logger.info(f"Proxy server listening on {config['Proxy']['ServerHost']}:{config['Proxy']['ServerPort']}")
def shutdown_handler(signum, frame):
logger.info(f"Received shutdown signal {signum}. Shutting down gracefully...")
stop_event.set()
signal.signal(signal.SIGINT, shutdown_handler)
signal.signal(signal.SIGTERM, shutdown_handler)
while not stop_event.is_set():
try:
client_socket, client_address = server_socket.accept()
# Prüfe erlaubte IPs mit CIDR-Unterstützung
if allowed_networks and not any(ip_address(client_address[0]) in net for net in allowed_networks):
logger.warning(f"Connection from {client_address[0]} not allowed")
client_socket.close()
continue
# Prüfe maximale Verbindungen
if not connection_semaphore.acquire(blocking=False):
logger.warning("Maximum connections reached, connection rejected")
client_socket.close()
continue
executor.submit(handle_client, client_socket, client_address, request_queue, logger, stop_event, active_connections, connection_semaphore)
except socket.timeout:
continue
finally:
persistent_client.close()
# Hauptprogramm
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Modbus TCP Proxy Server")
parser.add_argument("--config", type=str, default="config.yaml", help="Path to configuration file")
args = parser.parse_args()
config = load_config(args.config)
start_server(config)