| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
import json |
| 6 |
import logging |
| 7 |
import threading |
| 8 |
import ssl |
| 9 |
import certifi |
| 10 |
|
| 11 |
import websocket |
| 12 |
|
| 13 |
from .keepalive import KeepAlive |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
LOG = logging.getLogger('JELLYFIN.' + __name__) |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
class WSClient(threading.Thread): |
| 23 |
multi_client = False |
| 24 |
global_wsc = None |
| 25 |
global_stop = False |
| 26 |
|
| 27 |
def __init__(self, client, allow_multiple_clients=False): |
| 28 |
|
| 29 |
LOG.debug("WSClient initializing...") |
| 30 |
|
| 31 |
self.client = client |
| 32 |
self.keepalive = None |
| 33 |
self.wsc = None |
| 34 |
self.stop = False |
| 35 |
self.message_ids = set() |
| 36 |
|
| 37 |
if self.multi_client or allow_multiple_clients: |
| 38 |
self.multi_client = True |
| 39 |
|
| 40 |
threading.Thread.__init__(self) |
| 41 |
|
| 42 |
def send(self, message, data=""): |
| 43 |
if self.wsc is None: |
| 44 |
raise ValueError("The websocket client is not started.") |
| 45 |
|
| 46 |
self.wsc.send(json.dumps({'MessageType': message, "Data": data})) |
| 47 |
|
| 48 |
def run(self): |
| 49 |
|
| 50 |
server = self.client.config.data['auth.server'] |
| 51 |
server = server.replace('https', "wss") if server.startswith('https') else server.replace('http', "ws") |
| 52 |
wsc_url = "%s/socket" % server |
| 53 |
verify = self.client.config.data.get('auth.ssl', False) |
| 54 |
|
| 55 |
LOG.info("Websocket url: %s", wsc_url) |
| 56 |
|
| 57 |
|
| 58 |
ssl_context = None |
| 59 |
if 'auth.tls_client_cert' in self.client.config.data and 'auth.tls_client_key' in self.client.config.data: |
| 60 |
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) |
| 61 |
ssl_context.load_default_certs() |
| 62 |
ssl_context.load_cert_chain(self.client.config.data['auth.tls_client_cert'], self.client.config.data['auth.tls_client_key']) |
| 63 |
|
| 64 |
if self.client.config.data['auth.tls_server_ca']: |
| 65 |
ssl_context.load_verify_locations(self.client.config.data['auth.tls_server_ca']) |
| 66 |
|
| 67 |
|
| 68 |
auth_params = {} |
| 69 |
if "app.device_name" in self.client.config.data: |
| 70 |
auth_params.update({ |
| 71 |
"Client": self.client.config.data['app.name'], |
| 72 |
"Device": self.client.config.data['app.device_name'], |
| 73 |
"DeviceId": self.client.config.data['app.device_id'], |
| 74 |
"Version": self.client.config.data['app.version'] |
| 75 |
}) |
| 76 |
if "auth.token" in self.client.config.data: |
| 77 |
auth_params["Token"] = self.client.config.data['auth.token'] |
| 78 |
auth_line = ", ".join(f'{k}="{v}"' for k, v in auth_params.items()) |
| 79 |
ws_headers = {"Authorization": f"MediaBrowser {auth_line}"} |
| 80 |
|
| 81 |
self.wsc = websocket.WebSocketApp(wsc_url, |
| 82 |
header=ws_headers, |
| 83 |
on_message=lambda ws, message: self.on_message(ws, message), |
| 84 |
on_error=lambda ws, error: self.on_error(ws, error)) |
| 85 |
self.wsc.on_open = lambda ws: self.on_open(ws) |
| 86 |
|
| 87 |
if not self.multi_client: |
| 88 |
if self.global_wsc is not None: |
| 89 |
self.global_wsc.close() |
| 90 |
self.global_wsc = self.wsc |
| 91 |
|
| 92 |
while not self.stop and not self.global_stop: |
| 93 |
if server.startswith('wss'): |
| 94 |
if not verify: |
| 95 |
|
| 96 |
self.wsc.run_forever( |
| 97 |
ping_interval=10, sslopt={"cert_reqs": ssl.CERT_NONE} |
| 98 |
) |
| 99 |
else: |
| 100 |
self.wsc.run_forever(ping_interval=10, sslopt={"context": ssl_context, "ca_certs": certifi.where()}) |
| 101 |
else: |
| 102 |
self.wsc.run_forever(ping_interval=10) |
| 103 |
|
| 104 |
if self.stop: |
| 105 |
break |
| 106 |
LOG.warning("Websocket disconnected, reconnecting...") |
| 107 |
|
| 108 |
LOG.info("---<[ websocket ]") |
| 109 |
self.client.callback('WebSocketDisconnect', None) |
| 110 |
|
| 111 |
def on_error(self, ws, error): |
| 112 |
LOG.error(error) |
| 113 |
self.client.callback('WebSocketError', error) |
| 114 |
|
| 115 |
def on_open(self, ws): |
| 116 |
LOG.info("--->[ websocket ]") |
| 117 |
self.client.callback('WebSocketConnect', None) |
| 118 |
|
| 119 |
def on_message(self, ws, message): |
| 120 |
|
| 121 |
message = json.loads(message) |
| 122 |
|
| 123 |
|
| 124 |
message_id = message.get("MessageId") |
| 125 |
if message_id is not None: |
| 126 |
if message_id in self.message_ids: |
| 127 |
return |
| 128 |
self.message_ids.add(message_id) |
| 129 |
|
| 130 |
data = message.get('Data', {}) |
| 131 |
|
| 132 |
if message['MessageType'] == "ForceKeepAlive": |
| 133 |
self.send("KeepAlive") |
| 134 |
if self.keepalive is not None: |
| 135 |
self.keepalive.stop() |
| 136 |
self.keepalive = KeepAlive(data, self) |
| 137 |
self.keepalive.start() |
| 138 |
LOG.debug("ForceKeepAlive received from server.") |
| 139 |
return |
| 140 |
elif message['MessageType'] == "KeepAlive": |
| 141 |
LOG.debug("KeepAlive received from server.") |
| 142 |
return |
| 143 |
|
| 144 |
if data is None: |
| 145 |
data = {} |
| 146 |
elif not isinstance(data, dict): |
| 147 |
data = {"value": data} |
| 148 |
|
| 149 |
if not self.client.config.data['app.default']: |
| 150 |
data['ServerId'] = self.client.auth.server_id |
| 151 |
|
| 152 |
self.client.callback(message['MessageType'], data) |
| 153 |
|
| 154 |
def stop_client(self): |
| 155 |
|
| 156 |
self.stop = True |
| 157 |
|
| 158 |
if self.keepalive is not None: |
| 159 |
self.keepalive.stop() |
| 160 |
|
| 161 |
if self.wsc is not None: |
| 162 |
self.wsc.close() |
| 163 |
|
| 164 |
if not self.multi_client: |
| 165 |
self.global_stop = True |
| 166 |
self.global_wsc = None |