def join_campaign(name): user = session.pop('user') camp = Campaign.objects(name=name).first() if user.brand: raise Exception(f'user: \'{user.username}\' is a brand') if not camp: raise Exception(f'campaign: \'{name}\' does not exist') if name in user.campaigns: msg = f'user: \'{user.username}\' is already part of this campaign' raise Exception(msg) while True: url = ''.join(random.SystemRandom().choices( string.ascii_uppercase + string.ascii_lowercase + string.digits, k=8 )) if not Url.objects(url=url).first(): break Url(url, user.username, camp.name).save() full_url = f'/{url}' camp.users.append({'username': user.username, 'url': full_url}) user.campaigns.append(name) user.save() camp.save() return jsonify(**{'successful': True, 'url': full_url})
def create_url_sample(self, f): url = self.session.query(Url).filter(Url.url == f["url"]).first() if url == None: url_ip = None url_asn = None url_country = None if self.do_ip_to_asn_resolution: url_ip, url_range = self.get_url_info(f["url"]) if url_range: url_asn = url_range.asn_id url_country = url_range.country url = Url(url=f["url"], date=f["date"], ip=url_ip, asn_id=url_asn, country=url_country) self.session.add(url) if f["sha256"] != None: sample = self.session.query(Sample).filter( Sample.sha256 == f["sha256"]).first() if sample == None: result = None try: if self.vt != None: vtobj = self.vt.query_hash_sha256(f["sha256"]) if vtobj: result = str(vtobj["positives"]) + "/" + str( vtobj["total"] ) + " " + self.vt.get_best_result(vtobj) except: pass sample = Sample(sha256=f["sha256"], name=f["name"], length=f["length"], date=f["date"], info=f["info"], result=result) self.session.add(sample) if sample.network_id != None and url.network_id == None: url.network_id = sample.network_id if sample.network_id == None and url.network_id != None: sample.network_id = url.network_id else: sample = None url.sample = sample return sample, url
def run_refcode(refcode): url = Url.objects(url=refcode).first() if not url: raise Exception(f'\'{refcode}\' is not valid') camp = Campaign.objects(name=url.campaign_name).first() user = User.objects(username=url.username).first() if 'clicks' not in camp.stats: camp.stats['clicks'] = 0 camp.stats['clicks'] += 1 if camp.name not in user.stats: user.stats.update({camp.name: {'clicks': 0}}) user.stats[camp.name]['clicks'] += 1 print(request.user_agent.platform, request.user_agent.browser) platform = request.user_agent.platform platform = platform if platform else 'none' browser = request.user_agent.browser browser = browser if browser else 'none' ip = request.access_route[0] ip_url = f'https://ipinfo.io/{ip}?token=3e7dac37784e5c' ip_data = requests.get(ip_url).json() print(ip, ip_url, ip_data) for base in [camp.stats, user.stats[camp.name]]: for x, y in [('platform', platform), ('browser', browser)]: try: base[x][y] += 1 except KeyError as e: if e.args[0] == x: base[x] = {y: 1} if e.args[0] == y: base[x].update({y: 1}) if 'location' not in base: base['location'] = [] base['location'].append(ip_data) camp.save() user.save() return redirect(camp.redirect_url)
def add_url(url): with session_scope() as session: new_url = Url(url=url, add_all_children='False') session.add(new_url)
def add_url(self, original_url, short_url): obj = Url(original_url=original_url, id=short_url) self.session.add(obj) self.session.commit() cache.set(short_url, original_url)