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 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
Example #3
0
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
Example #4
0
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
Example #5
0
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)
Example #7
0
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
Example #8
0
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)
Example #13
0
        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)