def onion_redirect(request): """Add clicked information and redirect to .onion address.""" redirect_url = request.GET.get('redirect_url', '') if not redirect_url: answer = "Bad request: no GET parameter URL." return HttpResponseBadRequest(answer) onion = redirect_url.split("://")[1] onion_parts = onion.split(".") for part in onion_parts: if len(part) == 16: onion = part break try: md5 = hashlib.md5(onion+".onion").hexdigest() url = "http://" + onion + ".onion/" helpers.validate_onion_url(url) hs, hs_creat = HiddenWebsite.objects.get_or_create(id=onion, url=url, md5=md5) pop, creat = HiddenWebsitePopularity.objects.get_or_create(about=hs) if creat or hs.banned: pop.clicks = 0 pop.public_backlinks = 0 pop.tor2web = 0 pop.clicks = pop.clicks + 1 pop.full_clean() pop.save() except Exception as error: print "Error with redirect URL: " + redirect_url print error message = "Redirecting to hidden service." return helpers.redirect_page(message, 0, redirect_url)
def add_hs(json): """Adds an onion address description JSON data to the onion.""" url = json.get('url') relation = json.get('relation') id_str = url[7:-7] md5 = hashlib.md5(url[7:-1]).hexdigest() #official information cannot be edited hs_list = HiddenWebsiteDescription.objects.filter(about=id_str) if hs_list: hs = hs_list.latest('updated') if hs.officialInfo: answer = "This page has official info and it cannot be edited." return HttpResponseForbidden(answer) if hs.about.banned: answer = "This page is banned and it cannot be edited." return HttpResponseForbidden(answer) if relation: regex = re.compile( r'^(?:http|ftp)s?://' # http:// or https:// # Domain r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' r'localhost|' #localhost... r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip r'(?::\d+)?' # optional port r'(?:/?|[/?]\S+)$', re.IGNORECASE) if not regex.match(relation): json['relation'] = "" try: helpers.validate_onion_url(url) except ValidationError: print "Invalid onion domain" answer = "Invalid URL! URL must be exactly like http://something.onion/" return HttpResponseBadRequest(answer) try: hs, creat = HiddenWebsite.objects.get_or_create(id=id_str, url=url, md5=md5) if creat: hs.online = False hs.banned = False hs.full_clean() hs.save() add_description(json, hs) except ValidationError as error: print "Invalid data: %s" % error return HttpResponseBadRequest("Invalid data.") message = 'Hidden service added.' redirect_url = '/address/' + id_str return helpers.redirect_page(message, 3, redirect_url)
def add_hs(json): """Adds an onion address description JSON data to the onion.""" url = json.get('url') relation = json.get('relation') id_str = url[7:-7] md5 = hashlib.md5(url[7:-1]).hexdigest() #official information cannot be edited hs_list = HiddenWebsiteDescription.objects.filter(about=id_str) if hs_list: hs = hs_list.latest('updated') if hs.officialInfo: answer = "This page has official info and it cannot be edited." return HttpResponseForbidden(answer) if hs.about.banned: answer = "This page is banned and it cannot be edited." return HttpResponseForbidden(answer) if relation: regex = re.compile( r'^(?:http|ftp)s?://' # http:// or https:// # Domain r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' r'localhost|' #localhost... r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip r'(?::\d+)?' # optional port r'(?:/?|[/?]\S+)$', re.IGNORECASE) if not regex.match(relation): json['relation'] = "" try: helpers.validate_onion_url(url) except ValidationError: print "Invalid onion domain" answer = "Invalid URL! URL must be exactly like http://something.onion/" return HttpResponseBadRequest(answer) try: hs, creat = HiddenWebsite.objects.get_or_create(id=id_str, url=url, md5=md5) if creat: hs.online = False hs.banned = False hs.full_clean() hs.save() add_description(json, hs) except ValidationError as error: print "Invalid data: %s" % error return HttpResponseBadRequest("Invalid data.") message = 'Hidden service added.' redirect_url = '/address/'+id_str return helpers.redirect_page(message, 3, redirect_url)