-
Notifications
You must be signed in to change notification settings - Fork 27
/
MonsterBrowser.py
executable file
·98 lines (82 loc) · 2.71 KB
/
MonsterBrowser.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
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
from PyQt4.QtNetwork import *
import getopt
import sys
import re
from fancybrowser import FancyBrowser
import MonsterLogger
class MonsterBrowser():
urlPat = re.compile("https?://([^/]*)(.*)")
def usage(self):
print """
Usage: python MonsterBrowser.py [options] url
Options:
-c --cookie <Cookie> set cookie
-u --useragent <UserAgent> set useragent
"""
def parseArguments(self, argv):
try:
opts, args = getopt.getopt(argv, "c:u:", ["cookie=", "useragent="])
except getopt.GetoptError:
self.usage()
sys.exit(2)
if len(args) < 1:
self.usage()
sys.exit(2)
url = args[0]
cookie = None
useragent = None
for opt, args in opts:
if opt in ("-c", "--cookie"):
cookie = args
if opt in ("-u", "--useragent"):
useragent = args
if useragent is None:
useragent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20120427 Firefox/15.0a1"
print cookie, useragent, url
self.launch(cookie, useragent, url)
def launch(self, rawcookie, useragent, url):
'''
url: http://xxx.yyy.zzz/aaa/bbb?ccc/
host: xxx.yyy.zzz
domain: yyy.zzz
'''
cookies = []
# if no http protocol header, append it
if not url.startswith("http://"):
url = "http://" + url
match = self.urlPat.match(url)
host = match.group(1)
uri = match.group(2)
domain = ".".join(host.split(".")[-2:])
# adding cookies to cookiejar
for cookie in rawcookie.split(";"):
qnc = QNetworkCookie()
qnc.setDomain("."+domain)
key = cookie.split("=")[0]
value = "=".join(cookie.split("=")[1:])
qnc.setName(key)
qnc.setValue(value)
cookies.append(qnc)
self.open_web(url, cookies, useragent)
return
def open_web(self, url, cookies, useragent):
app = QApplication(sys.argv)
wind = FancyBrowser()
view = wind.getView()
nam = QNetworkAccessManager()
view.page().setNetworkAccessManager(nam)
MonsterLogger.logger.info(" [!] Spawning web view of " + url)
ncj = QNetworkCookieJar()
ncj.setAllCookies(cookies)
nam.setCookieJar(ncj)
qnr = QNetworkRequest(QUrl(url))
qnr.setRawHeader("User-Agent", useragent)
view.load(qnr)
wind.show()
app.exec_()
if __name__ == "__main__":
browser = MonsterBrowser()
browser.parseArguments(sys.argv[1:])