def start_new_session(self): with transaction.manager: DBSession.query(Measure).delete() HLRDBSession.query(Sms).delete() HLRDBSession.query(Subscriber).filter(Subscriber.extension != self.pf_phone_number).delete() return True
def process_unknown_adress_sms_worker(self): self.logger.info("Process unknown adressing sms thread START!") while not self.time_to_shutdown_event.is_set(): sms_info = self.measure_model.get_unknown_adresses_sms() if sms_info is not None: self.logger.info("Process sms {0}".format(sms_info)) reply_msg = self.get_formated_reply_message( ms_phone_number=sms_info['source'][0]) if reply_msg is None: self.logger.error( "Send reply message FAILD! There is no text message!") else: with transaction.manager: HLRDBSession.add_all([ create_sms(sms_info['source'][0], sms_info['destination'][0], sms_info['text'][0], sms_info['charset'][0]) ]) self.logger.debug("Send reply message. START!") if not self.send_sms_by_phone_number( sms_info['source'][0], reply_msg): self.logger.error("Reply message not send.") self.logger.debug("Send reply message. FINISH!") else: time.sleep(0.1) self.logger.info("Process unknown adressing sms thread FINISH!")
def start_new_session(self): with transaction.manager: DBSession.query(Measure).delete() HLRDBSession.query(Sms).delete() HLRDBSession.query(Subscriber).filter( Subscriber.extension != self.pf_phone_number).delete() return True
def get_imsi_messages(request): imsi = int(request.matchdict['imsi']) pfnum = request.xmlrpc.get_peoplefinder_number() pimsi = request.xmlrpc.get_peoplefinder_imsi() query = HLRDBSession.query( Sms.id, func.hex(Sms.user_data).label('user_data'), Sms.data_coding_scheme, Sms.ud_hdr_ind, Sms.src_addr, Sms.dest_addr, Sms.created, Sms.sent).filter(((Sms.src_addr == Subscriber.extension) | (Sms.dest_addr == Subscriber.extension)) & (Subscriber.imsi == imsi) & (Sms.protocol_id != 64)).order_by(Sms.created.asc()) result = {'imsi': imsi, 'sms': []} multipart_massages = {} for obj in query.all(): dest_subscriber_res = HLRDBSession.query(Subscriber.imsi).filter( Subscriber.extension == obj.dest_addr).all() dest = "xx" if len(dest_subscriber_res) > 0: dest_imsi = int(dest_subscriber_res[0].imsi) dest = "station" if dest_imsi == int(pimsi) else str(dest_imsi) direction = 'to' if obj.src_addr == pfnum else 'from' try: msg_id, msg_part_num, msg_parts_count, msg_part_text = user_data_decode( obj.user_data, obj.data_coding_scheme, (obj.ud_hdr_ind == 1)) except: msg_id, msg_part_num, msg_parts_count, msg_part_text = -1, 1, 1, "--Failed to decode the message--" if msg_id in multipart_massages: multipart_massages[msg_id]['text'] += msg_part_text else: sms = { 'id': obj.id, 'text': msg_part_text, 'type': direction, 'ts': time.strftime('%d %b %Y, %H:%M:%S', obj.created.timetuple()), 'dest': dest, } if direction == 'to': sms['sent'] = True if obj.sent else False result['sms'].append(sms) if msg_parts_count > 1: multipart_massages[msg_id] = result['sms'][-1] return result
def get_imsi_messages(request): imsi = int(request.matchdict['imsi']) pfnum = request.xmlrpc.get_peoplefinder_number() pimsi = request.xmlrpc.get_peoplefinder_imsi() query = HLRDBSession.query( Sms.id, Sms.text, Sms.src_addr, Sms.dest_addr, Sms.created, Sms.sent ).filter( ((Sms.src_addr == Subscriber.extension) | (Sms.dest_addr == Subscriber.extension)) & (Subscriber.imsi == imsi) & (Sms.protocol_id != 64) ).order_by(Sms.created.asc()) result = { 'imsi': imsi, 'sms': [] } for obj in query.all(): dest_subscriber_res = HLRDBSession.query( Subscriber.imsi ).filter( Subscriber.extension == obj.dest_addr ).all() dest = "xx" if len(dest_subscriber_res) > 0: dest_imsi = int(dest_subscriber_res[0].imsi) dest = "station" if dest_imsi == int(pimsi) else str(dest_imsi) direction = 'to' if obj.src_addr == pfnum else 'from' sms = { 'id': obj.id, 'text': obj.text, 'type': direction, 'ts': time.strftime('%d %b %Y, %H:%M:%S', obj.created.timetuple()), 'dest': dest, } if direction == 'to': sms['sent'] = True if obj.sent else False result['sms'].append(sms) return result
def get_imsi_list(request): result = [] messages = [] query = DBSession.query( Measure.id, Measure.imsi, func.max(Measure.timestamp).label('last') ).group_by(Measure.imsi).all() for measure in query: dtime = datetime.now() - measure.last result.append({ 'id': measure.id, 'imsi': measure.imsi, 'last_lur': dtime.total_seconds() // 60 }) # Calculate message count for each IMSI imsi_list = map(lambda item: item['imsi'], result); query = HLRDBSession.query( Subscriber.imsi, func.count(Sms.id).label('sms_count') ).filter(( (Sms.src_addr == Subscriber.extension) | (Sms.dest_addr == Subscriber.extension)) & (Sms.protocol_id != 64) & (Subscriber.imsi.in_(imsi_list)) ).group_by( Subscriber.imsi ).all() for record in query: messages.append({ 'imsi': int(record.imsi), 'count': record.sms_count }) if 'jtSorting' in request.GET: sorting_params = request.GET['jtSorting'].split(' ') sorting_field = sorting_params[0] reverse = sorting_params[1] == 'DESC' result.sort(key=lambda x: x[sorting_field], reverse=reverse) try: gps_status = all(request.xmlrpc.get_current_gps()) gps_status = 'yes' if gps_status else 'no' except (socket.error, xmlrpclib.Error) as e: gps_status = 'failed' return { 'Result': 'OK', 'Records': result, 'Messages': messages, 'GpsStatus': gps_status }
def send_sms(self, imsi, text): extensions = HLRDBSession.query( Subscriber.extension).filter(Subscriber.imsi == imsi).all() if len(extensions) != 1: self.logger.error( "Send sms failed. No subscriber with imsi {0}".format(imsi)) return False extension = extensions[0][0] return self.send_sms_by_phone_number(extension, text)
def main(argv=sys.argv): if len(argv) != 2: usage(argv) config_uri = argv[1] setup_logging(config_uri) settings = get_appsettings(config_uri) engine = engine_from_config(settings, 'sqlalchemy.pf.') DBSession.configure(bind=engine) Base.metadata.create_all(engine) hlr_engine = engine_from_config(settings, 'sqlalchemy.hlr.') HLRDBSession.configure(bind=hlr_engine) # initial settings with transaction.manager: DBSession.add_all([ Settings(name='imsiUpdate', value=3000), Settings(name='smsUpdate', value=3000), Settings(name='silentSms', value=3000), Settings( name='welcomeMessage', value='You are connected to a mobile search and rescue team. ' + \ 'Please SMS to {ph_phone_number} to communicate. ' + \ 'Your temporary phone number is {ms_phone_number}' ), Settings( name='replyMessage', value='Your SMSs are being sent to a mobile search and rescue team. ' + \ 'Reply to this message to communicate.' ), ]) HLRDBSession.add_all([ Subscriber( created=datetime.datetime.fromtimestamp(time.time()), updated=datetime.datetime.fromtimestamp(time.time()), imsi=pf_subscriber_imsi, name=pf_subscriber_name, extension=pf_subscriber_extension, authorized=1, lac=0 ) ])
def process_unknown_adress_sms_worker(self): self.logger.info("Process unknown adressing sms thread START!") while not self.time_to_shutdown_event.is_set(): sms_info = self.measure_model.get_unknown_adresses_sms() if sms_info is not None: self.logger.info("Process sms {0}".format(sms_info)) reply_msg = self.get_formated_reply_message( ms_phone_number=sms_info['source'][0]) if reply_msg is None: self.logger.error( "Send reply message FAILD! There is no text message!") else: self.logger.debug( "Send reply message: {0}".format(reply_msg)) if not self.vty_send_sms_by_phone_number( sms_info['source'][0], reply_msg): self.logger.error("Reply message not send.") with transaction.manager: HLRDBSession.add_all([ Sms(created=datetime.datetime.fromtimestamp( long(time.time())), sent=None, deliver_attempts=1, reply_path_req=0, status_rep_req=0, protocol_id=0, data_coding_scheme=0, ud_hdr_ind=0, src_addr=sms_info['source'][0], src_ton=0, src_npi=0, dest_addr=sms_info['destination'][0], dest_ton=0, dest_npi=0, text=sms_info['text'][0]) ]) else: time.sleep(0.1) self.logger.info("Process unknown adressing sms thread FINISH!")
def main(global_config, **settings): engine = engine_from_config(settings, 'sqlalchemy.pf.') DBSession.configure(bind=engine) Base.metadata.bind = engine engine = engine_from_config(settings, 'sqlalchemy.hlr.') HLRDBSession.configure(bind=engine) xmlrpc_url = 'http://%(xmlrpc.host)s:%(xmlrpc.port)s' % settings xmlrpc = lambda request: xmlrpclib.ServerProxy(xmlrpc_url) tile_dir = lambda request: settings.get('tile_dir') config = Configurator(settings=settings) config.include('pyramid_mako') config.add_static_view('static', 'static', cache_max_age=3600) config.add_route('home', '/') config.add_route('configuration', '/configuration') config.add_route('start_tracking', '/tracking/start') config.add_route('stop_tracking', '/tracking/stop') config.add_route('get_imsi_list', '/imsi/list') config.add_route('get_imsi_messages', 'imsi/{imsi}/messages') config.add_route('get_imsi_circles', 'imsi/{imsi}/circles') config.add_route('send_imsi_message', 'imsi/{imsi}/message') config.add_route('download_tiles_start', '/tiles/download/start') config.add_route('download_tiles_stop', '/tiles/download/stop') config.add_route('download_tiles_status', '/tiles/download/status') config.add_static_view(name='tiles', path=settings.get('tile_dir')) config.add_route('clear_data', '/data/clear/all') config.add_request_method(xmlrpc, 'xmlrpc', reify=True) config.add_request_method(tile_dir, 'tile_dir', reify=True) config.scan() return config.make_wsgi_app()
def get_imsi_messages(request): imsi = int(request.matchdict['imsi']) pfnum = request.xmlrpc.get_peoplefinder_number() pimsi = request.xmlrpc.get_peoplefinder_imsi() query = HLRDBSession.query( Sms.id, Sms.text, Sms.src_addr, Sms.dest_addr, Sms.created, Sms.sent).filter(((Sms.src_addr == Subscriber.extension) | (Sms.dest_addr == Subscriber.extension)) & (Subscriber.imsi == imsi) & (Sms.protocol_id != 64)).order_by(Sms.created.asc()) result = {'imsi': imsi, 'sms': []} for obj in query.all(): dest_subscriber_res = HLRDBSession.query(Subscriber.imsi).filter( Subscriber.extension == obj.dest_addr).all() dest = "xx" if len(dest_subscriber_res) > 0: dest_imsi = int(dest_subscriber_res[0].imsi) dest = "station" if dest_imsi == int(pimsi) else str(dest_imsi) direction = 'to' if obj.src_addr == pfnum else 'from' sms = { 'id': obj.id, 'text': obj.text, 'type': direction, 'ts': time.strftime('%d %b %Y, %H:%M:%S', obj.created.timetuple()), 'dest': dest, } if direction == 'to': sms['sent'] = True if obj.sent else False result['sms'].append(sms) return result
def process_unknown_adress_sms_worker(self): self.logger.info("Process unknown adressing sms thread START!") while not self.time_to_shutdown_event.is_set(): sms_info = self.measure_model.get_unknown_adresses_sms() if sms_info is not None: self.logger.info("Process sms {0}".format(sms_info)) reply_msg = self.get_formated_reply_message(ms_phone_number=sms_info['source'][0]) if reply_msg is None: self.logger.error("Send reply message FAILD! There is no text message!") else: self.logger.debug("Send reply message: {0}".format(reply_msg)) if not self.vty_send_sms_by_phone_number(sms_info['source'][0], reply_msg): self.logger.error("Reply message not send.") with transaction.manager: HLRDBSession.add_all([ Sms( created=datetime.datetime.fromtimestamp(long(time.time())), sent=None, deliver_attempts=1, reply_path_req=0, status_rep_req=0, protocol_id=0, data_coding_scheme=0, ud_hdr_ind=0, src_addr=sms_info['source'][0], src_ton=0, src_npi=0, dest_addr=sms_info['destination'][0], dest_ton=0, dest_npi=0, text=sms_info['text'][0] ) ]) else: time.sleep(0.1) self.logger.info("Process unknown adressing sms thread FINISH!")
def get_imsi_list(request): result = [] messages = [] query = DBSession.query(Measure.id, Measure.imsi, func.max( Measure.timestamp).label('last')).group_by( Measure.imsi).all() for measure in query: dtime = datetime.now() - measure.last result.append({ 'id': measure.id, 'imsi': measure.imsi, 'last_lur': dtime.total_seconds() // 60 }) # Calculate message count for each IMSI imsi_list = map(lambda item: item['imsi'], result) query = HLRDBSession.query( Subscriber.imsi, func.count(Sms.id).label( 'sms_count')).filter(((Sms.src_addr == Subscriber.extension) | (Sms.dest_addr == Subscriber.extension)) & (Sms.protocol_id != 64) & (Subscriber.imsi.in_(imsi_list))).group_by( Subscriber.imsi).all() for record in query: messages.append({'imsi': int(record.imsi), 'count': record.sms_count}) if 'jtSorting' in request.GET: sorting_params = request.GET['jtSorting'].split(' ') sorting_field = sorting_params[0] reverse = sorting_params[1] == 'DESC' result.sort(key=lambda x: x[sorting_field], reverse=reverse) try: gps_status = all(request.xmlrpc.get_current_gps()) gps_status = 'yes' if gps_status else 'no' except (socket.error, xmlrpclib.Error) as e: gps_status = 'failed' return { 'Result': 'OK', 'Records': result, 'Messages': messages, 'GpsStatus': gps_status }
def process_measure(self, meas): self.logger.info("Process meas: IMSI {0}".format(meas['imsi'])) imsi = meas['imsi'] extensions = HLRDBSession.query( Subscriber.extension).filter(Subscriber.imsi == imsi).all() if len(extensions) != 1: self.logger.error("HLR struct ERROR imsi {0} not one".format(imsi)) return extension = extensions[0][0] last_measure = self.get_last_measure(imsi) if last_measure is not None: self.logger.info("IMSI already detected.") last_measure_timestamp = time.mktime( last_measure.timestamp.timetuple()) if meas['time'] < last_measure_timestamp: self.logger.info( "Ignore measure because: measure is older then one in DB!") return if ((meas['time'] - last_measure_timestamp) < self.measure_update_period) and ( last_measure.timing_advance == meas['meas_rep']['L1_TA']): self.logger.info( "Ignore measure because: TA is no different from the last mesaure done less then {0} seconds!" .format(self.measure_update_period)) return else: self.logger.info("Detect new IMSI.") welcome_msg = self.get_formated_welcome_message( ms_phone_number=extension) if welcome_msg is None: self.logger.error( "Send welcome message FAILD! There is no text message!") else: self.logger.debug( "Send welcome message: {0}".format(welcome_msg)) if not self.send_sms(imsi, welcome_msg): self.logger.error("Welcome message not send.") self.save_measure_to_db(meas, extension)
def prepare_ready_for_silent_sms(self): while self.__stop_trackin_event.is_set() is False: sub_sms = HLRDBSession.query( Subscriber.imsi, Sms.dest_addr, Sms.sent, func.max(Sms.created) ).select_from( Subscriber ).outerjoin( Sms, ( (Sms.dest_addr == Subscriber.extension) and (Sms.protocol_id == 64) ) ).group_by( Sms.dest_addr ).filter( Subscriber.extension != self.pf_phone_number ).all() for (imsi, dest_addr, sent, created) in sub_sms: if dest_addr is not None: #if (sent is not None) or (self.__tracking_cicle_number == 0): if (sent is not None): self.__imis_reday_for_silent_sms_list.put(imsi) self.logger.debug("Tracking. Put imsi {0} to queue for send silent sms!".format(imsi)) else: self.logger.debug("Tracking. Don't put imsi {0} to queue for send silent sms - not answer previous one!".format(imsi)) else: self.logger.debug("Tracking. Put imsi {0} to queue for send silent sms!".format(imsi)) self.__imis_reday_for_silent_sms_list.put(imsi) silent_sms_interval = DBSession.query(Settings.value).filter(Settings.name == "silentSms").all() if len(silent_sms_interval) != 1: self.logger.error("Settings table not have silentSms value!") silent_sms_interval = self.measure_update_period else: silent_sms_interval = int(silent_sms_interval[0][0]) / 1000 time.sleep(silent_sms_interval) self.__tracking_cicle_number += 1
def prepare_ready_for_silent_sms(self): while self.__stop_trackin_event.is_set() is False: sub_sms = HLRDBSession.query( Subscriber.imsi, Sms.dest_addr, Sms.sent, func.max(Sms.created)).select_from(Subscriber).outerjoin( Sms, ((Sms.dest_addr == Subscriber.extension) and (Sms.protocol_id == 64))).group_by(Sms.dest_addr).filter( Subscriber.extension != self.pf_phone_number).all() for (imsi, dest_addr, sent, created) in sub_sms: if dest_addr is not None: #if (sent is not None) or (self.__tracking_cicle_number == 0): if (sent is not None): self.__imis_reday_for_silent_sms_list.put(imsi) self.logger.debug( "Tracking. Put imsi {0} to queue for send silent sms!" .format(imsi)) else: self.logger.debug( "Tracking. Don't put imsi {0} to queue for send silent sms - not answer previous one!" .format(imsi)) else: self.logger.debug( "Tracking. Put imsi {0} to queue for send silent sms!". format(imsi)) self.__imis_reday_for_silent_sms_list.put(imsi) silent_sms_interval = DBSession.query( Settings.value).filter(Settings.name == "silentSms").all() if len(silent_sms_interval) != 1: self.logger.error("Settings table not have silentSms value!") silent_sms_interval = self.measure_update_period else: silent_sms_interval = int(silent_sms_interval[0][0]) / 1000 time.sleep(silent_sms_interval) self.__tracking_cicle_number += 1
def process_measure(self, meas): self.logger.info("Process meas: IMSI {0}".format(meas['imsi'])) imsi = meas['imsi'] extensions = HLRDBSession.query(Subscriber.extension).filter(Subscriber.imsi == imsi).all() if len(extensions) != 1: self.logger.error("HLR struct ERROR imsi {0} not one".format(imsi)) return extension = extensions[0][0] last_measure = self.get_last_measure(imsi) if last_measure is not None: self.logger.info("IMSI already detected.") last_measure_timestamp = time.mktime(last_measure.timestamp.timetuple()) if meas['time'] < last_measure_timestamp: self.logger.info("Ignore measure because: measure is older then one in DB!") return if ((meas['time'] - last_measure_timestamp) < self.measure_update_period) and (last_measure.timing_advance == meas['meas_rep']['L1_TA']): self.logger.info("Ignore measure because: TA is no different from the last mesaure done less then {0} seconds!".format(self.measure_update_period)) return else: self.logger.info("Detect new IMSI.") welcome_msg = self.get_formated_welcome_message(ms_phone_number=extension) if welcome_msg is None: self.logger.error("Send welcome message FAILD! There is no text message!") else: self.logger.debug("Send welcome message: {0}".format(welcome_msg)) if not self.vty_send_sms(imsi, welcome_msg): self.logger.error("Welcome message not send.") self.save_measure_to_db(meas, extension)
DBSession.query(Settings).count() except: logger.error("People finder DB connection err") raise hlr_db_conn_str = None try: hlr_db_conn_str = configuration.get('app:main', 'sqlalchemy.hlr.url') except ConfigParser.Error as err: logger.error("Identification HLR fail: {0}".format(err.message)) sys.exit(1) logger.info("HLR db sqlite path: {0}".format(hlr_db_conn_str)) try: bind_hlr_session(hlr_db_conn_str) HLRDBSession.query(Subscriber).count() HLRDBSession.query(Sms).count() except: logger.error("HLR DB connection err") raise # Init shared objects ==================================================== manager = CommsModelManager() manager.start() comms_model = manager.CommsModel() # Init processes ========================================================= logger.info("Init comms interface server") try: srv = CommsInterfaceServer(configuration, comms_model, multiprocessing.Event()) comms_interface_server_process = multiprocessing.Process(target=srv.serve_forever)