From b613110bb16c95d9b641882c2ad6869e3ced1a0c Mon Sep 17 00:00:00 2001 From: Eugene Crosser Date: Mon, 18 Apr 2022 13:19:05 +0200 Subject: [PATCH] rename protocol module to "gps303proto" --- gps303/__main__.py | 2 +- gps303/collector.py | 31 ++++++++++++++++----------- gps303/{GT06mod.py => gps303proto.py} | 15 +++++++++++-- gps303/lookaside.py | 2 +- gps303/mkgpx.py | 2 +- gps303/opencellid.py | 2 +- 6 files changed, 36 insertions(+), 18 deletions(-) rename gps303/{GT06mod.py => gps303proto.py} (93%) diff --git a/gps303/__main__.py b/gps303/__main__.py index e3fd84a..d3d6d1e 100755 --- a/gps303/__main__.py +++ b/gps303/__main__.py @@ -7,7 +7,7 @@ import sys from time import time from .config import readconfig -from .GT06mod import handle_packet, make_response, LOGIN, set_config +from .gps303proto import handle_packet, make_response, LOGIN, set_config from .evstore import initdb, stow from .lookaside import prepare_response diff --git a/gps303/collector.py b/gps303/collector.py index d8ca86c..28c636d 100644 --- a/gps303/collector.py +++ b/gps303/collector.py @@ -1,3 +1,5 @@ +""" TCP server that communicates with terminals """ + from getopt import getopt from logging import getLogger, StreamHandler, DEBUG, INFO from logging.handlers import SysLogHandler @@ -7,7 +9,7 @@ import sys import zmq from .config import readconfig -from .GT06mod import handle_packet, make_response, LOGIN, set_config +from .gps303proto import handle_packet, make_response, LOGIN, set_config CONF = "/etc/gps303.conf" @@ -15,41 +17,46 @@ log = getLogger("gps303/collector") class Bcast: + """Zmq message to broadcast what was received from the terminal""" def __init__(self, imei, msg): self.as_bytes = imei.encode() + msg.encode() -class Zmsg: +class Resp: + """Zmq message received from a third party to send to the terminal""" def __init__(self, msg): self.imei = msg[:16].decode() self.payload = msg[16:] class Client: - def __init__(self, clntsock, clntaddr): - self.clntsock = clntsock - self.clntaddr = clntaddr + """Connected socket to the terminal plus buffer and metadata""" + def __init__(self, sock, addr): + self.sock = sock + self.addr = addr self.buffer = b"" self.imei = None def close(self): - self.clntsock.close() + self.sock.close() + self.buffer = b"" + self.imei = None def recv(self): - packet = self.clntsock.recv(4096) - if not packet: + segment = self.sock.recv(4096) + if not segment: return None when = time() - self.buffer += packet + self.buffer += segment # implement framing properly - msg = handle_packet(packet, self.clntaddr, when) + msg = handle_packet(packet, self.addr, when) self.buffer = self.buffer[len(packet):] if isinstance(msg, LOGIN): self.imei = msg.imei return msg def send(self, buffer): - self.clntsock.send(buffer) + self.sock.send(buffer) class Clients: @@ -107,7 +114,7 @@ def runserver(opts, conf): while True: try: msg = zsub.recv(zmq.NOBLOCK) - tosend.append(Zmsg(msg)) + tosend.append(Resp(msg)) except zmq.Again: break elif sk == tcpfd: diff --git a/gps303/GT06mod.py b/gps303/gps303proto.py similarity index 93% rename from gps303/GT06mod.py rename to gps303/gps303proto.py index ded38f2..4789b03 100755 --- a/gps303/GT06mod.py +++ b/gps303/gps303proto.py @@ -1,6 +1,17 @@ """ -Implementation of the protocol used by zx303 GPS+GPRS module -Description from https://github.com/tobadia/petGPS/tree/master/resources +Implementation of the protocol used by zx303 "ZhongXun Topin Locator" +GPS+GPRS module. Description lifted from this repository: +https://github.com/tobadia/petGPS/tree/master/resources + +Forewarnings: +1. There is no security whatsoever. If you know the module's IMEI, + you can feed fake data to the server, including fake location. +2. Ad-hoc choice of framing of messages (that are transferred over + the TCP stream) makes it vulnerable to coincidental appearance + of framing bytes in the middle of the message. Most of the time + the server will receive one message in one TCP segment (i.e. in + one `recv()` operation, but relying on that would break things + if the path has lower MTU than the size of a message. """ from datetime import datetime, timezone diff --git a/gps303/lookaside.py b/gps303/lookaside.py index 3c3ee32..d9af989 100644 --- a/gps303/lookaside.py +++ b/gps303/lookaside.py @@ -2,7 +2,7 @@ For when responding to the terminal is not trivial """ -from .GT06mod import * +from .gps303proto import * from .opencellid import qry_cell def prepare_response(conf, msg): diff --git a/gps303/mkgpx.py b/gps303/mkgpx.py index e0084c3..0d1fe89 100644 --- a/gps303/mkgpx.py +++ b/gps303/mkgpx.py @@ -2,7 +2,7 @@ from datetime import datetime, timezone from sqlite3 import connect import sys -from .GT06mod import * +from .gps303proto import * from .opencellid import qry_cell db = connect(sys.argv[1]) diff --git a/gps303/opencellid.py b/gps303/opencellid.py index 192409a..843e595 100644 --- a/gps303/opencellid.py +++ b/gps303/opencellid.py @@ -58,7 +58,7 @@ def qry_cell(dbname, mcc, gsm_cells): if __name__.endswith("__main__"): from datetime import datetime, timezone import sys - from .GT06mod import * + from .gps303proto import * db = connect(sys.argv[1]) c = db.cursor() -- 2.43.0