-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
117 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
modules/sc-mesh-secure-deployment/src/2_0/features/jamming/nats_client.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import ssl | ||
import os | ||
import nats | ||
from nats.aio.client import Client | ||
import nats_config | ||
|
||
client_cert = "/etc/ssl/certs/comms_auth_cert.pem" | ||
key = "/etc/ssl/private/comms_auth_private_key.pem" | ||
ca_cert = "/etc/ssl/certs/root-ca.cert.pem" | ||
|
||
|
||
async def connect_nats(): | ||
if os.path.exists(client_cert) and \ | ||
os.path.exists(key) and \ | ||
os.path.exists(ca_cert): | ||
|
||
ssl_context = ssl.create_default_context() | ||
ssl_context.load_cert_chain(certfile=client_cert, keyfile=key) | ||
ssl_context.load_verify_locations(cafile=ca_cert) | ||
|
||
nats_client = await nats.connect(f"{nats_config.MODULE_IP}:{nats_config.MODULE_PORT}", | ||
tls=ssl_context) | ||
|
||
else: | ||
nats_client = await nats.connect(f"{nats_config.MODULE_IP}:{nats_config.MODULE_PORT}") | ||
|
||
return nats_client | ||
|
||
|
||
async def connect(nats_client: Client, recon_cb=None, closed_cb=None, max_reconnection_attempts=None): | ||
if os.path.exists(client_cert) and \ | ||
os.path.exists(key) and \ | ||
os.path.exists(ca_cert): | ||
|
||
ssl_context = ssl.create_default_context() | ||
ssl_context.load_cert_chain(certfile=client_cert, keyfile=key) | ||
ssl_context.load_verify_locations(cafile=ca_cert) | ||
|
||
await nats_client.connect(f"{nats_config.MODULE_IP}:{nats_config.MODULE_PORT}", | ||
tls=ssl_context, | ||
reconnected_cb=recon_cb, | ||
closed_cb=closed_cb, | ||
max_reconnect_attempts=max_reconnection_attempts) | ||
|
||
else: | ||
await nats_client.connect(f"{nats_config.MODULE_IP}:{nats_config.MODULE_PORT}", | ||
reconnected_cb=recon_cb, | ||
closed_cb=closed_cb, | ||
max_reconnect_attempts=max_reconnection_attempts) | ||
|
||
return None |
19 changes: 19 additions & 0 deletions
19
modules/sc-mesh-secure-deployment/src/2_0/features/jamming/nats_config.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
""" | ||
This file contains the configuration for the NATS client. | ||
""" | ||
try: | ||
from identity import MODULE_IDENTITY as IDENTITY | ||
except ImportError: | ||
IDENTITY = "no_identity" | ||
print("No identity found!!!!!!!!!!!!!!!!!!!!!!!!") | ||
print("Please run _cli_command_get_identity.py to get the identity (creates identity.py)") | ||
|
||
MODULE_IP = "10.10.20.2" # or 192.168.1.x - brlan ip address | ||
MODULE_PORT = "4222" | ||
|
||
# IPv6 connection example | ||
# MODULE_IP = "[2001:db8:1234:5678:2e0:4cff:fe68:7a73]" | ||
# MODULE_PORT = "6222" | ||
|
||
MODULE_ROLE = "drone" # drone, sleeve or gcs | ||
MODULE_IDENTITY = IDENTITY # messages are sent to this device |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters