|
1 | 1 | import queue |
| 2 | +import ssl |
2 | 3 | import threading |
3 | 4 | from typing import Any, Dict, List, Callable, Optional |
4 | 5 |
|
@@ -39,6 +40,7 @@ def __init__( |
39 | 40 | self.hasFiredWsErrorCallback = False |
40 | 41 | self.hasPendingErrorCallbackFire = False |
41 | 42 | self.hasDeferredCloseCallbackDueToPendingErrorCallback = False |
| 43 | + self.disableCertCheck = False |
42 | 44 |
|
43 | 45 | # We use a send queue thread because it allows us to process downloads about 2x faster. |
44 | 46 | # This is because the downstream work of the WS can be made faster if it's done in parallel |
@@ -96,6 +98,12 @@ def OnError(ws:WebSocket, exception:Exception): |
96 | 98 | ) |
97 | 99 |
|
98 | 100 |
|
| 101 | + # This has it's own function so the caller very explicitly has to call it, rather than it being an init overload. |
| 102 | + # If set to true, this websocket connection will not validate the cert it's connecting to. This should only be done locally! |
| 103 | + def SetDisableCertCheck(self, disable:bool): |
| 104 | + self.disableCertCheck = disable |
| 105 | + |
| 106 | + |
99 | 107 | # Runs the websocket blocking until it closes. |
100 | 108 | def RunUntilClosed(self, pingIntervalSec:Optional[int]=None, pingTimeoutSec:Optional[int]=None): |
101 | 109 | # |
@@ -133,12 +141,17 @@ def RunUntilClosed(self, pingIntervalSec:Optional[int]=None, pingTimeoutSec:Opti |
133 | 141 | if pingIntervalSec > 0 and pingTimeoutSec <= 0: |
134 | 142 | raise Exception("The ping timeout must be greater than 0.") |
135 | 143 |
|
| 144 | + # Only if the client explicated called the function to disable this will we turn off cert verification. |
| 145 | + sslopt={"ca_certs":certifi.where()} |
| 146 | + if self.disableCertCheck: |
| 147 | + sslopt = {"cert_reqs": ssl.CERT_NONE, "check_hostname": False} |
| 148 | + |
136 | 149 | # Since some clients use RunAsync, check that we didn't close before the async action started. |
137 | 150 | with self.isClosedLock: |
138 | 151 | if self.isClosed: |
139 | 152 | return |
140 | 153 |
|
141 | | - self.Ws.run_forever(skip_utf8_validation=True, ping_interval=pingIntervalSec, ping_timeout=pingTimeoutSec, sslopt={"ca_certs":certifi.where()}) #pyright: ignore[reportUnknownMemberType] |
| 154 | + self.Ws.run_forever(skip_utf8_validation=True, ping_interval=pingIntervalSec, ping_timeout=pingTimeoutSec, sslopt=sslopt) #pyright: ignore[reportUnknownMemberType] |
142 | 155 | except Exception as e: |
143 | 156 | # There's a compat issue where run_forever will try to access "isAlive" when the socket is closing |
144 | 157 | # "isAlive" apparently doesn't exist in some PY versions of thread, so this throws. We will ignore that error, |
|
0 commit comments