CCrypto VPN public website https://vpn.ccrypto.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

82 lines
2.5 KiB

  1. from datetime import datetime, timedelta
  2. import re
  3. from django.conf import settings
  4. from .models import User
  5. class ReferrerMiddleware():
  6. def process_request(self, request):
  7. if 'ref' in request.GET:
  8. id = request.GET['ref']
  9. elif 'referrer' in request.COOKIES:
  10. id = request.COOKIES['referrer']
  11. else:
  12. return
  13. try:
  14. id = int(id.strip())
  15. except (ValueError, TypeError):
  16. return
  17. try:
  18. u = User.objects.get(id=id)
  19. except User.DoesNotExist:
  20. return
  21. request.session['referrer'] = u.id
  22. def process_response(self, request, response):
  23. id = request.session.get('referrer')
  24. if not id:
  25. return response
  26. max_age = 365 * 24 * 60 * 60
  27. expires = (datetime.utcnow() + timedelta(seconds=max_age))
  28. expires = expires.strftime("%a, %d-%b-%Y %H:%M:%S GMT")
  29. response.set_cookie('referrer', id,
  30. max_age=max_age,
  31. expires=expires,
  32. domain=settings.SESSION_COOKIE_DOMAIN,
  33. secure=settings.SESSION_COOKIE_SECURE or None)
  34. return response
  35. class CampaignMiddleware():
  36. GET_FIELDS = ['pk_campaign', 'utm_campaign', 'utm_medium', 'utm_source']
  37. def _get_name(self, request):
  38. for f in self.GET_FIELDS:
  39. if f in request.GET and request.GET[f]:
  40. return request.GET[f]
  41. if 'campaign' in request.COOKIES:
  42. return request.COOKIES['campaign']
  43. return None
  44. def process_request(self, request):
  45. name = self._get_name(request)
  46. if not name:
  47. return
  48. name = name.strip()
  49. if len(name) >= 64 or not re.match('^[a-zA-Z0-9_.:-]+$', name):
  50. return
  51. request.session['campaign'] = name
  52. def process_response(self, request, response):
  53. name = request.session.get('campaign')
  54. if not name:
  55. return response
  56. max_age = 365 * 24 * 60 * 60
  57. expires = (datetime.utcnow() + timedelta(seconds=max_age))
  58. expires = expires.strftime("%a, %d-%b-%Y %H:%M:%S GMT")
  59. response.set_cookie('campaign', name,
  60. max_age=max_age,
  61. expires=expires,
  62. domain=settings.SESSION_COOKIE_DOMAIN,
  63. secure=settings.SESSION_COOKIE_SECURE or None)
  64. return response