This repository has been archived by the owner on Jul 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtornadomobilega.py
252 lines (215 loc) · 8.32 KB
/
tornadomobilega.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
"""
Python implementation of ga.php for tornado.
"""
import re
from hashlib import md5
from random import randint
import struct
import time
from urllib import unquote, quote, urlencode
import uuid
from tornado.web import RequestHandler
from tornado.httpclient import HTTPRequest
from datetime import datetime
try:
# The mod_python version is more efficient, so try importing it first.
from mod_python.util import parse_qsl
except ImportError:
from cgi import parse_qsl
VERSION = "4.4sh"
COOKIE_NAME = "__utmmobile"
COOKIE_PATH = "/"
COOKIE_USER_PERSISTENCE = 63072000
GIF_DATA = reduce(lambda x, y: x + struct.pack('B', y),
[0x47,0x49,0x46,0x38,0x39,0x61,
0x01,0x00,0x01,0x00,0x80,0x00,
0x00,0x00,0x00,0x00,0xff,0xff,
0xff,0x21,0xf9,0x04,0x01,0x00,
0x00,0x00,0x00,0x2c,0x00,0x00,
0x00,0x00,0x01,0x00,0x01,0x00,
0x00,0x02,0x01,0x44,0x00,0x3b], '')
# WHITE GIF:
# 47 49 46 38 39 61
# 01 00 01 00 80 ff
# 00 ff ff ff 00 00
# 00 2c 00 00 00 00
# 01 00 01 00 00 02
# 02 44 01 00 3b
# TRANSPARENT GIF:
# 47 49 46 38 39 61
# 01 00 01 00 80 00
# 00 00 00 00 ff ff
# ff 21 f9 04 01 00
# 00 00 00 2c 00 00
# 00 00 01 00 01 00
# 00 02 01 44 00 3b
def get_ip(remote_address):
if not remote_address:
return ""
matches = re.match('^([^.]+\.[^.]+\.[^.]+\.).*', remote_address)
if matches:
return matches.groups()[0] + "0"
else:
return ""
def get_visitor_id(guid, account, user_agent, cookie):
"""
// Generate a visitor id for this hit.
// If there is a visitor id in the cookie, use that, otherwise
// use the guid if we have one, otherwise use a random number.
"""
if cookie:
return cookie
message = ""
if guid:
# Create the visitor id using the guid.
message = guid + account
else:
# otherwise this is a new user, create a new random id.
message = user_agent + str(uuid.uuid4())
md5String = md5(message).hexdigest()
return "0x" + md5String[:16]
def get_random_number():
"""
// Get a random number string.
"""
return str(randint(0, 0x7fffffff))
def write_gif_data():
"""
// Writes the bytes of a 1x1 transparent gif into the response.
Returns a dictionary with the following values:
{ 'response_code': '200 OK',
'response_headers': [(Header_key, Header_value), ...]
'response_body': 'binary data'
}
"""
response = {'response_code': '204 No Content',
'response_headers': [('Content-Type', 'image/gif'),
('Cache-Control', 'private, no-cache, no-cache=Set-Cookie, proxy-revalidate'),
('Pragma', 'no-cache'),
('Expires', 'Wed, 17 Sep 1975 21:32:10 GMT'),
],
# 'response_body': GIF_DATA,
'response_body': '',
}
return response
def send_request_to_google_analytics(utm_url, handler):
"""
// Make a tracking request to Google Analytics from this server.
// Copies the headers from the original request to the new one.
// If request containg utmdebug parameter, exceptions encountered
// communicating with Google Analytics are thown.
"""
headers = {
'User-Agent': handler.request.headers.get('User-Agent', 'Unknown'),
'Accept-Language:': handler.request.headers.get("Accept-Language", '')
}
opera_ua = handler.request.headers.get("X-Operamini-Phone-Ua")
if opera_ua:
headers['User-Agent'] = opera_ua
request = HTTPRequest(url=utm_url,
method="GET",
headers=headers
)
from tornado import httpclient
http = httpclient.AsyncHTTPClient()
http.fetch(request, callback=lambda x: x)
return request
def track_page_view(handler):
"""
// Track a page view, updates all the cookies and campaign tracker,
// makes a server side request to Google Analytics and writes the transparent
// gif byte data to the response.
"""
time_tup = time.localtime(time.time() + COOKIE_USER_PERSISTENCE)
# set some useful items in environ:
x_utmac = handler.request.arguments.get('x_utmac', '')
domain = handler.request.headers.get('Host', '')
# Get the referrer from the utmr parameter, this is the referrer to the
# page that contains the tracking pixel, not the referrer for tracking
# pixel.
document_referer = handler.request.arguments.get('utmr', [])
if not document_referer or document_referer == "0":
document_referer = "-"
else:
document_referer = document_referer[0]
document_referer = unquote(document_referer)
document_path = handler.request.arguments.get('utmp', '')
if document_path:
document_path = document_path[0]
document_path = unquote(document_path)
account = handler.request.arguments.get('utmac', '')
if account:
account = account[0]
user_agent = handler.request.headers.get('User-Agent', '')
# // Try and get visitor cookie from the request.
cookie = RequestHandler.get_cookie(handler, COOKIE_NAME)
guidheader = handler.request.headers.get("X-DCMGUID", '')
if not guidheader:
guidheader = handler.request.headers.get("X-UP-SUBNO", '')
if not guidheader:
guidheader = handler.request.headers.get("X-JPHONE-UID", '')
if not guidheader:
guidheader = handler.request.headers.get("X-EM-UID", '')
visitor_id = get_visitor_id(guidheader, account, user_agent, cookie)
# // Always try and add the cookie to the response.
# cookie = SimpleCookie()
# cookie[COOKIE_NAME] = visitor_id
# morsel = cookie[COOKIE_NAME]
# morsel['expires'] = time.strftime('%a, %d-%b-%Y %H:%M:%S %Z', time_tup)
# morsel['path'] = COOKIE_PATH
expires = datetime(*time_tup[0:6])
RequestHandler.set_cookie(handler, COOKIE_NAME, visitor_id, expires=expires)
utm_gif_location = "http://www.google-analytics.com/__utm.gif"
i = handler.request.headers.get("X-Forwarded-For", handler.request.headers.get("X-Real-Ip", None))
if not i:
i = handler.request.remote_ip
i = i.split(",")[0]
for utmac in [account, x_utmac]:
if not utmac:
continue
# // Construct the gif hit url.
utm_url = (utm_gif_location + "?" +
"utmwv=" + VERSION +
"&utmn=" + get_random_number() +
"&utmhn=" + quote(domain) +
"&utmsr=" + handler.request.arguments.get('utmsr', [''])[0] +
"&utme=" + handler.request.arguments.get('utme', [''])[0] +
"&utmr=" + quote(document_referer) +
"&utmp=" + quote(document_path) +
"&utmac=" + utmac +
"&utmcc=__utma%3D999.999.999.999.999.1%3B" +
"&utmvid=" + visitor_id +
"&utmip=" + get_ip(i) +
"&utmul=" + handler.request.headers.get("Accept-Language", '-') +
"&utmcs=" + handler.request.headers.get("Accept-Charset", '-')
)
# dbgMsg("utm_url: " + utm_url)
send_request_to_google_analytics(utm_url, handler)
# // If the debug parameter is on, add a header to the response that contains
# // the url that was used to contact Google Analytics.
# headers = [('Set-Cookie', str(cookie).split(': ')[1])]
headers = []
if handler.request.arguments.get('utmdebug', False):
headers.append(('X-GA-MOBILE-URL', utm_url))
# Finally write the gif data to the response
response = write_gif_data()
response_headers = response['response_headers']
response_headers.extend(headers)
return response
import random
class GAHandler(RequestHandler):
def get(self):
response = track_page_view(self)
for header, value in response['response_headers']:
self.set_header(header, value)
self.write(GIF_DATA)
self.finish('')
def get_ga_img_src(handler, gif_handler_path):
vars = {}
vars['utmac'] = handler.google_analytics_code
vars['utmhn'] = handler.request.host
vars['utmn'] = random.randint(1000000000, 9999999999)
vars['utmr'] = handler.request.headers.get('Referer', "-")
vars['utmp'] = handler.request.uri
vars['guid'] = "ON"
return gif_handler_path + "?" + urlencode(vars)