-
Notifications
You must be signed in to change notification settings - Fork 21
/
run.py
executable file
·208 lines (174 loc) · 7.79 KB
/
run.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
#!/usr/bin/python3
# -*-coding:utf-8 -*
import re
import os
import sys
import random
import datetime
import subprocess
import urllib.request
from installContainers import buildDockerImageLocal,instantiateContainer
from installUtils import prefixRepoLocal,prefixRepoHub
osImages = ["blinkfed","blinkubu"]
ubuntuName = "xenial"
fedoraName = "fc25"
downloadsPath = os.path.abspath("data/downloads")
profilePath = os.path.abspath("data/profile")
ldpreloadPath = os.path.abspath("ldpreload")
seccompPath = os.path.abspath("seccomp/chrome.json")
timezonePath = "/usr/share/zoneinfo/"
def checkInstallation():
#Check the presence of the two main containers
#and the two main OS images
regExp = re.compile("Exited \(0\).*?blink(fonts|browsers).*?\\n")
output = subprocess.check_output(["sudo","docker","ps","-a"]).decode()
if len(regExp.findall(output)) != 2:
sys.exit("Blink is not installed")
output = subprocess.check_output(["sudo","docker","images"]).decode()
for image in osImages :
if image not in output:
sys.exit("Blink is not installed")
#If the installation is correct, we generate the LD Preload libraries
generateLibrairies()
def getRandomTimezone():
timezone = []
for path, subdirs, files in os.walk(timezonePath):
for name in files:
timezone.append(os.path.join(path, name)[len(timezonePath):])
timezone = [t for t in timezone if "posix" not in t and "right" not in t and ".tab" not in t]
return timezone[random.randint(0,len(timezone)-1)]
def generateLibrairies():
#Fedora
#We retrieve the kernel version of Fedora
#ex: 3.18.5-201.fc21.x86_64
fedSource = urllib.request.urlopen("https://admin.fedoraproject.org/updates/kernel").read()
fedKernel = re.search("kernel-(.{1,20}"+fedoraName+")((?!testing).)*?stable",str(fedSource)).group(1)+".x86_64"
#We write the header file
with open('ldpreload/modUname.h', 'w') as f:
f.write("#define RELEASE \""+fedKernel+"\"")
#We compile the library
subprocess.call(["gcc","-Wall","-fPIC","-shared","-o","ldpreload/modFedUname.so","ldpreload/modUname.c"])
#Ubuntu
#We retrieve the kernel version of Ubuntu
#ex: 3.13.0-24-generic
ubuSource = urllib.request.urlopen("https://packages.ubuntu.com/"+ubuntuName+"/linux-image-generic").read()
ubuKernel = re.search("linux-image-([\.\w-]*?)-generic",str(ubuSource)).group(1)+"-generic"
#We write the header file
with open('ldpreload/modUname.h', 'w') as f:
f.write("#define RELEASE \""+ubuKernel+"\"")
#We compile the library
subprocess.call(["gcc","-Wall","-fPIC","-shared","-o","ldpreload/modUbuUname.so","ldpreload/modUname.c"])
print("LD Preload libraries generated")
def updateOS():
print("Start updating OS containers")
updateFile = profilePath+"/update"
#We write the "update" file to inform containers tu update
subprocess.call(["touch",updateFile])
#We run the update script in each container
#to update packages and plugins
for image in osImages:
subprocess.call(["sudo","docker","run","-it","-v",profilePath+":/home/blink/profile","--volumes-from","blinkbrowsers",prefixRepoLocal+image])
dockerID = subprocess.check_output(["sudo","docker","ps","-l","-q"])
subprocess.call(["sudo","docker","commit",dockerID.decode().strip(),prefixRepoLocal+image])
print("Update of "+image+" complete")
#We remove the update file
subprocess.call(["rm",updateFile])
print("OS Containers updated")
def updateBrowsers():
print("Start updating browsers")
#We remove the old browser container and image
subprocess.call(["sudo","docker","rm","-v","blinkbrowsers"])
subprocess.call(["sudo","docker","rmi","blinkbrowsers"])
#We build the new image and instantiate it
buildDockerImageLocal("blinkbrowsers","docker/browsers/")
instantiateContainer("blinkbrowsers")
print("Browsers updated")
def writeInstallComplete(mode):
#######
# We write a file that indicates that the installation is complete
# Format of the install complete file
# data = "XXXX YYYY"
# data[0] = "XXXX" -> days since the last update of OS containers
# data[1] = "YYYY" -> days since the last update of browsers
#######
data = []
if mode < 2:
#We read the current installComplete file
with open('installComplete', 'r') as f:
data = f.read().strip().split(" ")
#We update either the first or second counter
data[mode] = datetime.date.today().toordinal()
elif mode == 2:
# We update both counters
#(in the case of a fresh install)
nowDate = datetime.date.today().toordinal()
data = [nowDate,nowDate]
if len(data) == 2:
with open('installComplete', 'w') as f:
f.write("{} {}".format(data[0],data[1]))
print("installComplete file written")
def startTorProxy():
subprocess.call(["sudo","docker","start","torproxy"])
def stopTorProxy():
subprocess.call(["sudo","docker","stop","torproxy"])
def main():
if not os.path.isfile("installComplete"):
checkInstallation()
writeInstallComplete(2)
print("Installation verified")
else:
#We check if the "LD preload" libraries have been compiled in the last 30 days
#If not, we retrieve the latest version online and recompile the libraries
with open('installComplete', 'r') as f:
data = f.read().strip().split(" ")
pastOSDate = datetime.date.fromordinal(int(data[0]))
pastBrowsersDate = datetime.date.fromordinal(int(data[1]))
nowDate = datetime.date.today()
OSDays = (nowDate-pastOSDate).days
browsersDays = (nowDate-pastBrowsersDate).days
print("Days since last OS update : {}".format(OSDays))
print("Days since last browsers update : {}".format(browsersDays))
if OSDays > 15:
updateOS()
generateLibrairies()
writeInstallComplete(0)
if browsersDays > 45:
writeInstallComplete(1)
updateBrowsers()
if len(sys.argv) == 2:
chosenImage = sys.argv[1]
else :
chosenImage = osImages[random.randint(0,len(osImages)-1)]
print("Image " + chosenImage + " chosen")
chosenTimezone = getRandomTimezone()
print(chosenTimezone+" timezone chosen")
print("Launching Blink browsing environment")
launchCommand = "sudo docker run -ti --rm -e DISPLAY " \
"--group-add $(getent group audio | cut -d: -f3) " \
"-v /tmp/.X11-unix:/tmp/.X11-unix " \
"-v "+downloadsPath+":/home/blink/Downloads:z " \
"-v "+profilePath+":/home/blink/profile:z " \
"-v "+ldpreloadPath+":/home/blink/ldpreload:z " \
"--volumes-from blinkbrowsers " \
"--volumes-from blinkfonts " \
"--device /dev/snd " \
"-v /run/user/`id -u`/pulse/native:/run/user/`id -u`/pulse/native " \
"-v /dev/shm:/dev/shm " \
"-v /etc/machine-id:/etc/machine-id " \
"-v /var/lib/dbus:/var/lib/dbus " \
"--security-opt seccomp:"+seccompPath+" " \
"--net container:torproxy " \
"-e TZ="+chosenTimezone+" "+prefixRepoLocal
#We select the corresponding LD Preload library
if chosenImage is "blinkfed":
subprocess.call(["cp","-f","ldpreload/modFedUname.so","ldpreload/modUname.so"])
else:
subprocess.call(["cp","-f","ldpreload/modUbuUname.so","ldpreload/modUname.so"])
#Start Tor proxy
startTorProxy()
subprocess.call(launchCommand+chosenImage,shell=True)
#When browsing is finished, we stop the Tor proxy container
stopTorProxy()
print("End of script")
if __name__ == "__main__":
main()