1 """ Websocket Gateway """
3 from configparser import ConfigParser
4 from datetime import datetime, timezone
5 from importlib import import_module
6 from json import dumps, loads
7 from logging import getLogger
8 from socket import socket, AF_INET6, SOCK_STREAM, SOL_SOCKET, SO_REUSEADDR
10 from typing import Any, cast, Dict, List, Optional, Set, Tuple
11 from wsproto import ConnectionType, WSConnection
12 from wsproto.events import (
21 from wsproto.utilities import RemoteProtocolError
25 from .evstore import initdb, fetch
26 from .protomodule import ProtoModule
27 from .zmsg import Rept, rtopic
29 log = getLogger("loctrkd/wsgateway")
34 def backlog(imei: str, numback: int) -> List[Dict[str, Any]]:
36 for report in fetch(imei, numback):
37 report["type"] = "location"
38 timestamp = report.pop("devtime")
39 report["timestamp"] = timestamp
44 def try_http(data: bytes, fd: int, e: Exception) -> bytes:
47 lines = data.decode().split("\r\n")
50 op, resource, proto = request.split(" ")
52 "HTTP %s for %s, proto %s from fd %d, headers: %s",
62 f"{proto} 500 No data configured\r\n"
63 f"Content-Type: text/plain\r\n\r\n"
64 f"HTML data not configured on the server\r\n".encode()
68 with open(htmlfile, "rb") as fl:
70 length = len(htmldata)
73 f"Content-Type: text/html; charset=utf-8\r\n"
74 f"Content-Length: {len(htmldata):d}\r\n\r\n"
75 ).encode("utf-8") + htmldata
78 f"{proto} 500 File not found\r\n"
79 f"Content-Type: text/plain\r\n\r\n"
80 f"HTML file could not be opened\r\n".encode()
84 f"{proto} 400 Bad request\r\n"
85 "Content-Type: text/plain\r\n\r\n"
86 "Bad request\r\n".encode()
89 log.warning("Unparseable data from fd %d: %s", fd, data)
94 """Websocket connection to the client"""
96 def __init__(self, sock: socket, addr: Tuple[str, int]) -> None:
99 self.ws = WSConnection(ConnectionType.SERVER)
102 self.imeis: Set[str] = set()
104 def close(self) -> None:
105 log.debug("Closing fd %d", self.sock.fileno())
108 def recv(self) -> Optional[List[Dict[str, Any]]]:
110 data = self.sock.recv(4096)
113 "Reading from fd %d: %s",
117 self.ws.receive_data(None)
119 if not data: # Client has closed connection
121 "EOF reading from fd %d",
124 self.ws.receive_data(None)
127 self.ws.receive_data(data)
128 except RemoteProtocolError as e:
130 "Websocket error on fd %d, try plain http (%s)",
134 self.ws_data = try_http(data, self.sock.fileno(), e)
135 # this `write` is a hack - writing _ought_ to be done at the
136 # stage when all other writes are performed. But I could not
137 # arrange it so in a logical way. Let it stay this way. The
138 # whole http server affair is a hack anyway.
140 log.debug("Sending HTTP response to %d", self.sock.fileno())
144 for event in self.ws.events():
145 if isinstance(event, Request):
146 log.debug("WebSocket upgrade on fd %d", self.sock.fileno())
147 # self.ws_data += self.ws.send(event.response()) # Why not?!
148 self.ws_data += self.ws.send(AcceptConnection())
150 elif isinstance(event, (CloseConnection, Ping)):
151 log.debug("%s on fd %d", event, self.sock.fileno())
152 self.ws_data += self.ws.send(event.response())
153 elif isinstance(event, TextMessage):
154 log.debug("%s on fd %d", event, self.sock.fileno())
155 msg = loads(event.data)
157 if msg.get("type", None) == "subscribe":
158 self.imeis = set(msg.get("imei", []))
160 "subs list on fd %s is %s",
165 log.warning("%s on fd %d", event, self.sock.fileno())
168 def wants(self, imei: str) -> bool:
170 "wants %s? set is %s on fd %d",
175 return imei in self.imeis
177 def send(self, message: Dict[str, Any]) -> None:
178 if self.ready and message["imei"] in self.imeis:
179 self.ws_data += self.ws.send(Message(data=dumps(message)))
181 def write(self) -> bool:
184 sent = self.sock.send(self.ws_data)
185 self.ws_data = self.ws_data[sent:]
188 "Sending to fd %d: %s",
193 return bool(self.ws_data)
197 def __init__(self) -> None:
198 self.by_fd: Dict[int, Client] = {}
200 def add(self, clntsock: socket, clntaddr: Tuple[str, int]) -> int:
201 fd = clntsock.fileno()
202 log.info("Start serving fd %d from %s", fd, clntaddr)
203 self.by_fd[fd] = Client(clntsock, clntaddr)
206 def stop(self, fd: int) -> None:
207 clnt = self.by_fd[fd]
208 log.info("Stop serving fd %d", clnt.sock.fileno())
212 def recv(self, fd: int) -> Optional[List[Dict[str, Any]]]:
213 clnt = self.by_fd[fd]
216 def send(self, msg: Dict[str, Any]) -> Set[int]:
218 for fd, clnt in self.by_fd.items():
219 if clnt.wants(msg["imei"]):
224 def write(self, towrite: Set[int]) -> Set[int]:
226 for fd, clnt in [(fd, self.by_fd.get(fd)) for fd in towrite]:
227 if clnt and clnt.write():
231 def subs(self) -> Set[str]:
233 for clnt in self.by_fd.values():
238 def runserver(conf: ConfigParser) -> None:
240 initdb(conf.get("storage", "dbfn"))
241 htmlfile = conf.get("wsgateway", "htmlfile", fallback=None)
242 # Is this https://github.com/zeromq/pyzmq/issues/1627 still not fixed?!
243 zctx = zmq.Context() # type: ignore
244 zsub = zctx.socket(zmq.SUB) # type: ignore
245 zsub.connect(conf.get("rectifier", "publishurl"))
246 tcpl = socket(AF_INET6, SOCK_STREAM)
247 tcpl.setblocking(False)
248 tcpl.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
249 tcpl.bind(("", conf.getint("wsgateway", "port")))
251 tcpfd = tcpl.fileno()
252 poller = zmq.Poller() # type: ignore
253 poller.register(zsub, flags=zmq.POLLIN)
254 poller.register(tcpfd, flags=zmq.POLLIN)
256 activesubs: Set[str] = set()
258 towait: Set[int] = set()
260 neededsubs = clients.subs()
261 for imei in neededsubs - activesubs:
262 zsub.setsockopt(zmq.SUBSCRIBE, rtopic(imei))
263 for imei in activesubs - neededsubs:
264 zsub.setsockopt(zmq.UNSUBSCRIBE, rtopic(imei))
265 activesubs = neededsubs
266 log.debug("Subscribed to: %s", activesubs)
271 events = poller.poll()
272 for sk, fl in events:
276 zmsg = Rept(zsub.recv(zmq.NOBLOCK))
277 msg = loads(zmsg.payload)
278 msg["imei"] = zmsg.imei
279 log.debug("Got %s, sending %s", zmsg, msg)
284 clntsock, clntaddr = tcpl.accept()
285 topoll.append((clntsock, clntaddr))
286 elif fl & zmq.POLLIN:
287 received = clients.recv(sk)
289 log.debug("Client gone from fd %d", sk)
293 for wsmsg in received:
294 log.debug("Received from %d: %s", sk, wsmsg)
295 if wsmsg.get("type", None) == "subscribe":
296 # Have to live w/o typeckeding from json
297 imeis = cast(List[str], wsmsg.get("imei"))
298 numback: int = wsmsg.get("backlog", 5)
300 tosend.extend(backlog(imei, numback))
302 elif fl & zmq.POLLOUT:
303 log.debug("Write now open for fd %d", sk)
307 log.debug("Stray event: %s on socket %s", fl, sk)
308 # poll queue consumed, make changes now
310 poller.unregister(fd) # type: ignore
313 log.debug("Sending to the clients: %s", wsmsg)
314 towrite |= clients.send(wsmsg)
315 for clntsock, clntaddr in topoll:
316 fd = clients.add(clntsock, clntaddr)
317 poller.register(fd, flags=zmq.POLLIN)
318 # Deal with actually writing the data out
319 trywrite = towrite - towait
320 morewait = clients.write(trywrite)
322 "towait %s, tried %s, still busy %s",
327 for fd in morewait - trywrite: # new fds waiting for write
328 poller.modify(fd, flags=zmq.POLLIN | zmq.POLLOUT) # type: ignore
329 for fd in trywrite - morewait: # no longer waiting for write
330 poller.modify(fd, flags=zmq.POLLIN) # type: ignore
333 except KeyboardInterrupt:
335 zctx.destroy() # type: ignore
339 if __name__.endswith("__main__"):
340 runserver(common.init(log))