This repository was archived by the owner on Nov 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsumer.py
More file actions
80 lines (62 loc) · 2.36 KB
/
Copy pathconsumer.py
File metadata and controls
80 lines (62 loc) · 2.36 KB
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
#!/usr/bin/env python
import asyncio
from genericpath import isfile
import os, sys
import websockets
from datetime import datetime
import json
import pathlib
import socket
from OpenSSL import SSL
import logging
from wsproto import WSConnection
from wsproto.connection import ConnectionType
from wsproto.events import (
AcceptConnection,
CloseConnection,
Message,
Ping,
Pong,
Request,
TextMessage,
)
from pyeebus import x509_utils
logging.basicConfig(
format="%(message)s",
level=logging.DEBUG,
)
CONSUMER_PRIVATE_KEY_FN = os.getenv('CONSUMER_PRIVATE_KEY_FN', 'consumer.key')
CONSUMER_PUBLIC_KEY_FN = os.getenv('CONSUMER_PUBLIC_KEY_FN', 'consumer.pem')
CONSUMER_CERT_FN = os.getenv('CONSUMER_CERT_FN', 'consumer.crt')
#WEBSOCKET_URL = os.getenv('WEBSOCKET_URL', "wss://localhost:8765")
WEBSOCKET_HOST = os.getenv('WEBSOCKET_HOST', 'localhost')
WEBSOCKET_PORT = int(os.getenv('WEBSOCKET_PORt', '8765'))
if not os.path.isfile(CONSUMER_PRIVATE_KEY_FN) and not os.path.isfile(CONSUMER_PUBLIC_KEY_FN) and not os.path.isfile(CONSUMER_CERT_FN):
print(f"generating public and private keys and certificate...")
x509_utils.generate_key(private_key_fn=CONSUMER_PRIVATE_KEY_FN, public_key_fn=CONSUMER_PUBLIC_KEY_FN)
x509_utils.generate_x509_keys_by_fn(public_key_pem_fn=CONSUMER_PUBLIC_KEY_FN, private_key_pem_fn=CONSUMER_PRIVATE_KEY_FN, cert_fn=CONSUMER_CERT_FN)
else:
print(f"One of the following files does exist already, thus, NOT creating any of those: {CONSUMER_PRIVATE_KEY_FN} {CONSUMER_PUBLIC_KEY_FN} {CONSUMER_CERT_FN}")
assert os.path.isfile(CONSUMER_PRIVATE_KEY_FN)
assert os.path.isfile(CONSUMER_PUBLIC_KEY_FN)
assert os.path.isfile(CONSUMER_CERT_FN)
def verify_cb(conn, cert, err, depth, ok):
print(cert)
return 1 # TODO: security: 1 means cert check ok!
ssl_context = SSL.Context(SSL.SSLv23_METHOD)
ssl_context.set_verify(SSL.VERIFY_PEER, callback=verify_cb)
ssl_context.use_certificate_file(certfile=CONSUMER_CERT_FN)
ssl_context.use_privatekey_file(keyfile=CONSUMER_PRIVATE_KEY_FN)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn = SSL.Connection(ssl_context, sock)
conn.connect((WEBSOCKET_HOST, WEBSOCKET_PORT))
conn.do_handshake()
"""
conn.send('GET / HTTP/1.0')
while True:
msg = conn.recv(4096)
print(msg)
"""
ws = WSConnection(ConnectionType.CLIENT)
req = Request(host=WEBSOCKET_HOST, target="/")
conn.send(req)