def test_http_x_forwarded_for_single_proxy_with_right_most(self): request = HttpRequest() request.META = { 'HTTP_X_FORWARDED_FOR': '177.139.233.139, 177.139.200.139, 198.84.193.157', } ip = get_trusted_ip(request, right_most_proxy=True, trusted_proxies=['177.139.233.139']) self.assertEqual(ip, "198.84.193.157")
def test_http_x_forwarded_for_all_proxies_in_subnet_2(self): request = HttpRequest() request.META = { 'HTTP_X_FORWARDED_FOR': '198.84.193.157, 177.139.200.139, 177.139.233.139', } ip = get_trusted_ip(request, trusted_proxies=['177.139']) self.assertEqual(ip, "198.84.193.157")
def test_http_x_forwarded_for_no_proxy(self): request = HttpRequest() request.META = { 'HTTP_X_FORWARDED_FOR': '198.84.193.157, 177.139.200.139, 177.139.233.139', } ip = get_trusted_ip(request, trusted_proxies=[]) self.assertIsNone(ip)
def test_x_forwarded_for_single_proxy_hyphens(self): request = HttpRequest() request.META = { 'X-FORWARDED-FOR': '198.84.193.157, 177.139.200.139, 177.139.233.139', } ip = get_trusted_ip(request, trusted_proxies=['177.139.233.139']) self.assertEqual(ip, "198.84.193.157")
def test_http_x_forwarded_for_conf_settings(self): request = HttpRequest() request.META = { 'HTTP_X_FORWARDED_FOR': '198.84.193.157, 177.139.200.139, 177.139.233.100', } ip = get_trusted_ip(request) self.assertEqual(ip, "198.84.193.157")
def test_http_x_forwarded_for_single_proxy(self): request = HttpRequest() request.META = { 'HTTP_X_FORWARDED_FOR': '3ffe:1900:4545:3:200:f8ff:fe21:67cf, 74dc::02ba', } ip = get_trusted_ip(request, trusted_proxies=['74dc::02ba']) self.assertEqual(ip, "3ffe:1900:4545:3:200:f8ff:fe21:67cf")
def test_http_x_forwarded_for_and_x_forward_for_single_proxy(self): request = HttpRequest() request.META = { 'HTTP_X_FORWARDED_FOR': '198.84.193.156, 177.139.200.139, 177.139.233.139', 'X_FORWARDED_FOR': '198.84.193.157, 177.139.200.139, 177.139.233.139', } ip = get_trusted_ip(request, trusted_proxies=['177.139.233.139']) self.assertEqual(ip, "198.84.193.156")
def hello(request): ip = get_trusted_ip(request, trusted_proxies=['23.91.45.15']) if ip is not None: print("we got user's real IP address from a known proxy") else: print("request wasn't from a unknown proxy. Not a trusted IP.") msg = request.META if request.META.has_key('HTTP_X_FORWARDED_FOR'): ip = request.META['HTTP_X_FORWARDED_FOR'] else: ip = request.META['REMOTE_ADDR'] print ip print msg return HttpResponse("hello")
def map(request, json_req=None): user = request.user player = Player.objects.get(user=user) country = player.country ip = get_trusted_ip(request, trusted_proxies=['nat-vrf-como.rete.polimi.it']) print "country was %s but ip is %s" % (country, ip) ct = dict() #ct['alpha3'] = ["gmb", "afg", "cub", "hun", "mng", "swe", "alb", "cyp", "ind", "mrt", "syr", "arg", "cze", "irn", "ner", "tcd", "aut", "deu", "irq", "nga", "tha", "bdi", "dnk", "isr", "nic", "tun", "bel", "dza", "ita", "nld", "tur", "bgr", "egy", "jor", "nor", "tza", "bih", "eri", "kaz", "pak", "uga", "bra", "esp", "ken", "pol", "ukr", "caf", "eth", "lbn", "rou", "usa", "can", "fin", "lby", "rus", "ven", "che", "fra", "mar", "sdn", "vnm", "chn", "gbr", "mex", "sen", "xxk", "civ", "gib", "mkd", "som", "yem", "cmr", "grc", "mli", "srb", "zaf", "cod", "gtm", "mlt", "ssd", "col", "hrv", "mmr", "svn"] ct['alpha3'] = [ "aaaaaa", "cpv", "ind", "moz", "ssd", "afg", "cri", "iot", "mrt", "sssss", "ago", "cub", "irl", "msr", "stp", "aia", "cym", "irn", "mus", "sur", "alb", "cyp", "irq", "mwi", "svk", "and", "cze", "isl", "mys", "svn", "are", "deu", "isr", "nam", "swe", "arg", "dji", "ita", "ner", "swz", "arm", "dma", "jam", "nga", "syc", "atg", "dnk", "jey", "nic", "syr", "aus", "dom", "jjjjj", "niu", "tca", "aut", "dza", "jor", "nld", "tcd", "aze", "ecu", "jpn", "nor", "tgo", "bbbbb", "egy", "kaz", "npl", "tha", "bdi", "eri", "ken", "nru", "tjk", "bel", "esp", "kgz", "nzl", "tkl", "ben", "est", "khm", "omn", "tkm", "bfa", "eth", "kir", "ooooo", "tls", "bgd", "fin", "kna", "pak", "ton", "bgr", "fji", "kor", "pan", "tto", "bhr", "flk", "kwt", "pcn", "ttttt", "bhs", "fra", "lao", "per", "tun", "bih", "fro", "lbn", "phl", "tur", "blr", "fsm", "lbr", "plw", "tuv", "blz", "gab", "lby", "png", "twn", "bmu", "gbr", "lca", "pol", "tza", "bol", "ggy", "lie", "prk", "uga", "bra", "gha", "lka", "prt", "ukr", "brb", "gib", "lso", "pry", "ury", "brn", "gin", "ltu", "qat", "usa", "btn", "gmb", "lux", "rou", "bwa", "gnb", "lva", "rus", "uzb", "caf", "gnq", "mar", "rwa", "vat", "can", "grc", "mco", "sau", "vct", "che", "grd", "mdg", "sdn", "ven", "chl", "grl", "mdv", "sen", "vgb", "chn", "gtm", "mex", "sgp", "vnm", "civ", "guy", "mhl", "shn", "vut", "cmr", "hnd", "mkd", "slb", "wsm", "cod", "hrv", "mli", "sle", "xxk", "cog", "hti", "mlt", "slv", "yem", "c*k", "hun", "mmr", "smr", "zaf", "col", "idn", "mne", "som", "zmb", "com", "imn", "mng", "srb", "zwe" ] if json_req == "restart": qs = Question.get_questions(country) q_list = list() game = Game(player=player) game.save() for q in qs: answers = q.get_mix_answers() aq = AnsweredQuestion(question=q, game=game) aq.option1 = answers[0] aq.option2 = answers[1] aq.option3 = answers[2] aq.option4 = answers[3] aq.save() qd = q.to_dict() qd['answers'] = answers qd['cnt_list'] = ast.literal_eval(q.cnt_list) qd['ans_cnt_names'] = [] if q._type == 'MB': for c in qd['answers']: qd['ans_cnt_names'].append(Country.get_country(c)) qd['game_id'] = game.id if q._type == 'MB': qd['answer_code'] = q.answer_code q_list.append(qd) ct['questions'] = q_list return JsonResponse(ct) return render(request, 'map.html', context=ct)
def post_comment_ajax(request, using=None): """ Post a comment, via an Ajax call. Most from django-fluent-comments """ if not request.is_ajax(): return HttpResponseBadRequest("Expecting Ajax call") data = request.POST.copy() if request.user.is_authenticated(): if not data.get('name', ''): data["name"] = request.user.get_full_name( ) or request.user.get_username() if not data.get('email', ''): data["email"] = request.user.email # Look up the object we're trying to comment about ctype = data.get("content_type") object_pk = data.get("object_pk") if ctype is None or object_pk is None: return CommentPostBadRequest( "Missing content_type or object_pk field.") try: model = apps.get_model(*ctype.split(".", 1)) target = model._default_manager.using(using).get(pk=object_pk) except TypeError: return CommentPostBadRequest("Invalid content_type value: %r" % escape(ctype)) except AttributeError: return CommentPostBadRequest( "The given content-type %r does not resolve to a valid model." % escape(ctype)) except ObjectDoesNotExist: return CommentPostBadRequest( "No object matching content-type %r and object PK %r exists." % (escape(ctype), escape(object_pk))) except (ValueError, ValidationError) as e: return CommentPostBadRequest( "Attempting go get content-type %r and object PK %r exists raised %s" % (escape(ctype), escape(object_pk), e.__class__.__name__)) # Do we want to preview the comment? preview = "preview" in data # Construct the comment form form = django_comments.get_form()(target, data=data) # Check security information if form.security_errors(): return CommentPostBadRequest( "The comment form failed security verification: %s" % escape(str(form.security_errors()))) # If there are errors or if we requested a preview show the comment if preview: comment = form.get_comment_object() if not form.errors else None if comment is not None and request.user.is_authenticated(): comment.user = request.user return _ajax_result(request, form, "preview", comment, object_id=object_pk) if form.errors: return _ajax_result(request, form, "post", object_id=object_pk) # Otherwise create the comment comment = form.get_comment_object() comment.ip_address = get_trusted_ip(request) if request.user.is_authenticated(): comment.user = request.user # Signal that the comment is about to be saved responses = signals.comment_will_be_posted.send(sender=comment.__class__, comment=comment, request=request) for (receiver, response) in responses: if response is False: return CommentPostBadRequest( "comment_will_be_posted receiver %r killed the comment" % receiver.__name__) # Save the comment and signal that it was saved comment.save() signals.comment_was_posted.send(sender=comment.__class__, comment=comment, request=request) return _ajax_result(request, form, "post", comment, object_id=object_pk)
def test_meta_none(self): request = HttpRequest() request.META = {} ip = get_trusted_ip(request) self.assertIsNone(ip)
def test_meta_none(self): request = HttpRequest() request.META = { } ip = get_trusted_ip(request) self.assertIsNone(ip)