-
Notifications
You must be signed in to change notification settings - Fork 24
/
install.py
executable file
·545 lines (463 loc) · 19.1 KB
/
install.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import re
import sys
import time
import traceback
import copy
import string
from hashlib import sha256
INSTALL_PATH_BASE = '/usr/lib/enigma2/python/Plugins/Extensions/IPTVPlayer'
INSTALL_URL_BASE = 'https://www.e2iplayer.gitlab.io/'
UPDATE_URL_BASE = INSTALL_URL_BASE + 'update2/'
MSG_FORMAT = "\n\n=====================================================\n{0}\n=====================================================\n"
def printWRN(txt):
print(MSG_FORMAT.format(txt))
def printMSG(txt):
print(MSG_FORMAT.format(txt))
def printDBG(txt):
print(str(txt))
def printERR(txt):
print(MSG_FORMAT.format('ERROR: ' + txt))
sys.exit(1)
def printExc(msg=''):
print("===============================================")
print(" EXCEPTION ")
print("===============================================")
msg = msg + ': \n%s' % traceback.format_exc()
print(msg)
print("===============================================")
################################################################
# AES START
################################################################
class Rijndael(object):
@classmethod
def create(cls):
if hasattr(cls, "RIJNDAEL_CREATED"):
return
cls.num_rounds = {16: {16: 10, 24: 12, 32: 14}, 24: {16: 12, 24: 12, 32: 14}, 32: {16: 14, 24: 14, 32: 14}}
cls.shifts = [[[0, 0], [1, 3], [2, 2], [3, 1]],
[[0, 0], [1, 5], [2, 4], [3, 3]],
[[0, 0], [1, 7], [3, 5], [4, 4]]]
A = [[1, 1, 1, 1, 1, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 1, 1, 1, 1],
[1, 1, 0, 0, 0, 1, 1, 1],
[1, 1, 1, 0, 0, 0, 1, 1],
[1, 1, 1, 1, 0, 0, 0, 1]]
alog = [1]
for i in xrange(255):
j = (alog[-1] << 1) ^ alog[-1]
if j & 0x100 != 0:
j ^= 0x11B
alog.append(j)
log = [0] * 256
for i in xrange(1, 255):
log[alog[i]] = i
def mul(a, b):
if a == 0 or b == 0:
return 0
return alog[(log[a & 0xFF] + log[b & 0xFF]) % 255]
box = [[0] * 8 for i in xrange(256)]
box[1][7] = 1
for i in xrange(2, 256):
j = alog[255 - log[i]]
for t in xrange(8):
box[i][t] = (j >> (7 - t)) & 0x01
B = [0, 1, 1, 0, 0, 0, 1, 1]
cox = [[0] * 8 for i in xrange(256)]
for i in xrange(256):
for t in xrange(8):
cox[i][t] = B[t]
for j in xrange(8):
cox[i][t] ^= A[t][j] * box[i][j]
cls.S = [0] * 256
cls.Si = [0] * 256
for i in xrange(256):
cls.S[i] = cox[i][0] << 7
for t in xrange(1, 8):
cls.S[i] ^= cox[i][t] << (7-t)
cls.Si[cls.S[i] & 0xFF] = i
G = [[2, 1, 1, 3],
[3, 2, 1, 1],
[1, 3, 2, 1],
[1, 1, 3, 2]]
AA = [[0] * 8 for i in xrange(4)]
for i in xrange(4):
for j in xrange(4):
AA[i][j] = G[i][j]
AA[i][i+4] = 1
for i in xrange(4):
pivot = AA[i][i]
if pivot == 0:
t = i + 1
while AA[t][i] == 0 and t < 4:
t += 1
assert t != 4, 'G matrix must be invertible'
for j in xrange(8):
AA[i][j], AA[t][j] = AA[t][j], AA[i][j]
pivot = AA[i][i]
for j in xrange(8):
if AA[i][j] != 0:
AA[i][j] = alog[(255 + log[AA[i][j] & 0xFF] - log[pivot & 0xFF]) % 255]
for t in xrange(4):
if i != t:
for j in xrange(i+1, 8):
AA[t][j] ^= mul(AA[i][j], AA[t][i])
AA[t][i] = 0
iG = [[0] * 4 for i in xrange(4)]
for i in xrange(4):
for j in xrange(4):
iG[i][j] = AA[i][j + 4]
def mul4(a, bs):
if a == 0:
return 0
r = 0
for b in bs:
r <<= 8
if b != 0:
r = r | mul(a, b)
return r
cls.T1 = []
cls.T2 = []
cls.T3 = []
cls.T4 = []
cls.T5 = []
cls.T6 = []
cls.T7 = []
cls.T8 = []
cls.U1 = []
cls.U2 = []
cls.U3 = []
cls.U4 = []
for t in xrange(256):
s = cls.S[t]
cls.T1.append(mul4(s, G[0]))
cls.T2.append(mul4(s, G[1]))
cls.T3.append(mul4(s, G[2]))
cls.T4.append(mul4(s, G[3]))
s = cls.Si[t]
cls.T5.append(mul4(s, iG[0]))
cls.T6.append(mul4(s, iG[1]))
cls.T7.append(mul4(s, iG[2]))
cls.T8.append(mul4(s, iG[3]))
cls.U1.append(mul4(t, iG[0]))
cls.U2.append(mul4(t, iG[1]))
cls.U3.append(mul4(t, iG[2]))
cls.U4.append(mul4(t, iG[3]))
cls.rcon = [1]
r = 1
for t in xrange(1, 30):
r = mul(2, r)
cls.rcon.append(r)
cls.RIJNDAEL_CREATED = True
def __init__(self, key, block_size = 16):
self.create()
if block_size != 16 and block_size != 24 and block_size != 32:
raise ValueError('Invalid block size: ' + str(block_size))
if len(key) != 16 and len(key) != 24 and len(key) != 32:
raise ValueError('Invalid key size: ' + str(len(key)))
self.block_size = block_size
ROUNDS = Rijndael.num_rounds[len(key)][block_size]
BC = block_size / 4
Ke = [[0] * BC for i in xrange(ROUNDS + 1)]
Kd = [[0] * BC for i in xrange(ROUNDS + 1)]
ROUND_KEY_COUNT = (ROUNDS + 1) * BC
KC = len(key) / 4
tk = []
for i in xrange(0, KC):
tk.append((ord(key[i * 4]) << 24) | (ord(key[i * 4 + 1]) << 16) |
(ord(key[i * 4 + 2]) << 8) | ord(key[i * 4 + 3]))
t = 0
j = 0
while j < KC and t < ROUND_KEY_COUNT:
Ke[t / BC][t % BC] = tk[j]
Kd[ROUNDS - (t / BC)][t % BC] = tk[j]
j += 1
t += 1
tt = 0
rconpointer = 0
while t < ROUND_KEY_COUNT:
tt = tk[KC - 1]
tk[0] ^= (Rijndael.S[(tt >> 16) & 0xFF] & 0xFF) << 24 ^ \
(Rijndael.S[(tt >> 8) & 0xFF] & 0xFF) << 16 ^ \
(Rijndael.S[ tt & 0xFF] & 0xFF) << 8 ^ \
(Rijndael.S[(tt >> 24) & 0xFF] & 0xFF) ^ \
(Rijndael.rcon[rconpointer] & 0xFF) << 24
rconpointer += 1
if KC != 8:
for i in xrange(1, KC):
tk[i] ^= tk[i-1]
else:
for i in xrange(1, KC / 2):
tk[i] ^= tk[i-1]
tt = tk[KC / 2 - 1]
tk[KC / 2] ^= (Rijndael.S[ tt & 0xFF] & 0xFF) ^ \
(Rijndael.S[(tt >> 8) & 0xFF] & 0xFF) << 8 ^ \
(Rijndael.S[(tt >> 16) & 0xFF] & 0xFF) << 16 ^ \
(Rijndael.S[(tt >> 24) & 0xFF] & 0xFF) << 24
for i in xrange(KC / 2 + 1, KC):
tk[i] ^= tk[i-1]
j = 0
while j < KC and t < ROUND_KEY_COUNT:
Ke[t / BC][t % BC] = tk[j]
Kd[ROUNDS - (t / BC)][t % BC] = tk[j]
j += 1
t += 1
for r in xrange(1, ROUNDS):
for j in xrange(BC):
tt = Kd[r][j]
Kd[r][j] = Rijndael.U1[(tt >> 24) & 0xFF] ^ \
Rijndael.U2[(tt >> 16) & 0xFF] ^ \
Rijndael.U3[(tt >> 8) & 0xFF] ^ \
Rijndael.U4[ tt & 0xFF]
self.Ke = Ke
self.Kd = Kd
def encrypt(self, plaintext):
if len(plaintext) != self.block_size:
raise ValueError('wrong block length, expected ' + str(self.block_size) + ' got ' + str(len(plaintext)))
Ke = self.Ke
BC = self.block_size / 4
ROUNDS = len(Ke) - 1
if BC == 4:
Rijndael.SC = 0
elif BC == 6:
Rijndael.SC = 1
else:
Rijndael.SC = 2
s1 = Rijndael.shifts[Rijndael.SC][1][0]
s2 = Rijndael.shifts[Rijndael.SC][2][0]
s3 = Rijndael.shifts[Rijndael.SC][3][0]
a = [0] * BC
t = []
for i in xrange(BC):
t.append((ord(plaintext[i * 4 ]) << 24 |
ord(plaintext[i * 4 + 1]) << 16 |
ord(plaintext[i * 4 + 2]) << 8 |
ord(plaintext[i * 4 + 3]) ) ^ Ke[0][i])
for r in xrange(1, ROUNDS):
for i in xrange(BC):
a[i] = (Rijndael.T1[(t[ i ] >> 24) & 0xFF] ^
Rijndael.T2[(t[(i + s1) % BC] >> 16) & 0xFF] ^
Rijndael.T3[(t[(i + s2) % BC] >> 8) & 0xFF] ^
Rijndael.T4[ t[(i + s3) % BC] & 0xFF] ) ^ Ke[r][i]
t = copy.copy(a)
result = []
for i in xrange(BC):
tt = Ke[ROUNDS][i]
result.append((Rijndael.S[(t[ i ] >> 24) & 0xFF] ^ (tt >> 24)) & 0xFF)
result.append((Rijndael.S[(t[(i + s1) % BC] >> 16) & 0xFF] ^ (tt >> 16)) & 0xFF)
result.append((Rijndael.S[(t[(i + s2) % BC] >> 8) & 0xFF] ^ (tt >> 8)) & 0xFF)
result.append((Rijndael.S[ t[(i + s3) % BC] & 0xFF] ^ tt ) & 0xFF)
return string.join(map(chr, result), '')
def decrypt(self, ciphertext):
if len(ciphertext) != self.block_size:
raise ValueError('wrong block length, expected ' + str(self.block_size) + ' got ' + str(len(ciphertext)))
Kd = self.Kd
BC = self.block_size / 4
ROUNDS = len(Kd) - 1
if BC == 4:
Rijndael.SC = 0
elif BC == 6:
Rijndael.SC = 1
else:
Rijndael.SC = 2
s1 = Rijndael.shifts[Rijndael.SC][1][1]
s2 = Rijndael.shifts[Rijndael.SC][2][1]
s3 = Rijndael.shifts[Rijndael.SC][3][1]
a = [0] * BC
t = [0] * BC
for i in xrange(BC):
t[i] = (ord(ciphertext[i * 4 ]) << 24 |
ord(ciphertext[i * 4 + 1]) << 16 |
ord(ciphertext[i * 4 + 2]) << 8 |
ord(ciphertext[i * 4 + 3]) ) ^ Kd[0][i]
for r in xrange(1, ROUNDS):
for i in xrange(BC):
a[i] = (Rijndael.T5[(t[ i ] >> 24) & 0xFF] ^
Rijndael.T6[(t[(i + s1) % BC] >> 16) & 0xFF] ^
Rijndael.T7[(t[(i + s2) % BC] >> 8) & 0xFF] ^
Rijndael.T8[ t[(i + s3) % BC] & 0xFF] ) ^ Kd[r][i]
t = copy.copy(a)
result = []
for i in xrange(BC):
tt = Kd[ROUNDS][i]
result.append((Rijndael.Si[(t[ i ] >> 24) & 0xFF] ^ (tt >> 24)) & 0xFF)
result.append((Rijndael.Si[(t[(i + s1) % BC] >> 16) & 0xFF] ^ (tt >> 16)) & 0xFF)
result.append((Rijndael.Si[(t[(i + s2) % BC] >> 8) & 0xFF] ^ (tt >> 8)) & 0xFF)
result.append((Rijndael.Si[ t[(i + s3) % BC] & 0xFF] ^ tt ) & 0xFF)
return string.join(map(chr, result), '')
################################################################
# AES END
################################################################
def decryptFile(file, key):
cipher = Rijndael(key, block_size=len(key))
data = ''
with open(file, "rb") as f:
data = f.read()
with open(file, "wb") as f:
offset = 0
while True:
for enc in [True, False]:
if enc:
chunk = data[offset:offset+len(key)]
offset += len(key)
else:
chunk = data[offset:offset+len(key)*1000]
offset += len(key)*1000
if len(chunk) == len(key):
chunk = cipher.decrypt(chunk)
f.write(chunk)
if not chunk:
break
f.close()
def downloadWithWget(url, output):
if os.path.exists(output):
if not os.path.isfile(output) or os.path.islink(output):
printERR("%s exists - please remove it to be able to install!" % output)
os.unlink(output)
for cmd in ['/iptvplayer_rootfs/usr/bin/wget --no-check-certificate', 'wget', 'wget --no-check-certificate', 'fullwget --no-check-certificate', '/usr/lib/enigma2/python/Plugins/Extensions/IPTVPlayer/bin/wget --no-check-certificate']:
try:
file = os.popen(cmd + ' "%s" -O "%s" ' % (url, output))
data = file.read()
ret = file.close()
if ret in [0, None]:
return
else:
printWRN("Download using %s failed with return code: %s" % (url, ret))
except Exception,e:
printDBG(e)
printERR('Download %s failed!' % url)
def unpackArchive(input, output, removeInputAfterUnpack=True, clearOutputBeforeUnpack=True):
if not os.path.isfile(input) or os.path.islink(input):
printERR('input "%s" must be file!' % input)
if not os.path.isdir(output):
os.mkdir(output)
if '/' == os.path.realpath(output):
printERR('output directory can not be "/" !')
cmd = ''
if clearOutputBeforeUnpack: cmd += 'rm -rf "%s/"* > /dev/null 2>&1; ' % output
cmd += 'tar -xzf "%s" -C "%s" 2>&1; ' % (input, output)
if removeInputAfterUnpack: cmd += 'PREV_RET=$?; rm -f "%s" > /dev/null 2>&1; (exit $PREV_RET)' % (input)
printDBG("unpacking: " + cmd)
file = os.popen(cmd)
data = file.read()
ret = file.close()
if ret in [0, None]:
return
else:
printERR('Unpacking "%s" into "%s" failed with return code: %s.\nPlease check your password.' % (input, output, ret))
def copyDirContent(input, output, removeInputAfterUnpack=True, clearOutputBeforeUnpack=True):
if not os.path.isdir(input):
printERR('input "%s" is not directory!' % input)
if not os.path.isdir(output):
os.mkdir(output)
if '/' in (os.path.realpath(output), os.path.realpath(input)):
printERR('output and input directory can not be "/" !')
cmd = ''
if clearOutputBeforeUnpack: cmd += 'rm -rf "%s/"* > /dev/null 2>&1; ' % output
cmd += 'cp -r "%s/"* "%s/" 2>&1; ' % (input, output)
if removeInputAfterUnpack: cmd += 'PREV_RET=$?; rm -rf "%s" > /dev/null 2>&1; (exit $PREV_RET)' % (input)
printDBG("copy: " + cmd)
file = os.popen(cmd)
data = file.read()
ret = file.close()
if ret in [0, None]:
return
else:
printERR('Copying content of "%s" into "%s" failed with return code: %s.' % (input, output, ret))
def getE2iPlayerVersion(E2iPlayerPath):
sys.path.insert(0, E2iPlayerPath)
version = __import__('version', globals(), locals(), ['E2I_VERSION'], -1)
del sys.path[0]
return version.E2I_VERSION
try:
sys.stdin = open('/dev/tty')
except Exception:
pass
if len(sys.argv) != 4:
printERR("You need to provide login, password and icon size (100, 120, 135)!")
LOGIN = sys.argv[1]
PASSWORD = sys.argv[2]
ICONS_SIZE = sys.argv[3]
TMP_DIR = '/tmp/'
AVAILABLE_ICONS_SIZES = ['100', '120', '135']
if ICONS_SIZE not in AVAILABLE_ICONS_SIZES:
printERR('Wrong icon size "%s", available sizes %s' % (ICONS_SIZE, ', '.join(AVAILABLE_ICONS_SIZES)))
# Get Enc Key
nameHash = sha256(LOGIN).hexdigest()
url = INSTALL_URL_BASE + 'keys/%s.bin' % nameHash
encKey = os.path.join(TMP_DIR, 'e2iplayer_enc_key.bin')
downloadWithWget(url, encKey)
with open(encKey, "r") as f:
encKey = f.read()
key = sha256(PASSWORD).digest()[:16]
cipher = Rijndael(key, block_size=len(key))
decKey = cipher.decrypt(encKey)
# check free size in the rootfs
s = os.statvfs(INSTALL_PATH_BASE) if os.path.isdir(INSTALL_PATH_BASE) else os.statvfs(INSTALL_PATH_BASE.rsplit('/', 1)[0])
freeSpaceMB = s.f_bfree * s.f_frsize / (1024*1024) # in KB
availSpaceMB = s.f_bavail * s.f_frsize / (1024*1024) # in KB
requiredFreeSpaceMB = 12
printDBG("Free space %s MB in rootfs" % (availSpaceMB))
if availSpaceMB < requiredFreeSpaceMB:
msg = "Not enough disk space for installing E2iPlayer!\nAt least %s MB is required.\nYou have %s MB free space in the rootfs.\nDo you want to continue anyway?" % (requiredFreeSpaceMB, availSpaceMB)
answer = ''
while answer not in ['Y', 'N']:
answer = raw_input(MSG_FORMAT.format(msg) + "\nY/N: ").strip().upper()
msg = ''
if answer != 'Y':
printERR("Not enough disk space for installing E2iPlayer!\nAt least %s MB is required." % requiredFreeSpaceMB)
archFile = os.path.join(TMP_DIR, 'e2iplayer_last.tar.gz')
url = UPDATE_URL_BASE + 'latest.python%s.%s.tar.gz' % (sys.version_info[0], sys.version_info[1])
downloadWithWget(url, archFile)
decryptFile(archFile, decKey)
unpackDir = os.path.join(TMP_DIR, 'e2iplayer_last')
unpackArchive(archFile, unpackDir)
E2iPlayerDir = os.path.join(unpackDir, 'iptvplayer-for-e2.git', 'IPTVPlayer')
for archFile in ('graphics.tar.gz', 'icons%s.tar.gz' % ICONS_SIZE):
url = UPDATE_URL_BASE + archFile
archFile = os.path.join(TMP_DIR, archFile)
downloadWithWget(url, archFile)
unpackArchive(archFile, E2iPlayerDir, clearOutputBeforeUnpack=False)
try:
E2iPlayerVer = getE2iPlayerVersion(E2iPlayerDir)
except Exception:
printExc()
printERR("Something goes wrong. Unable to get E2iPlayer version from prepared package!")
msg = 'E2iPlayer v%s ready to install into "%s".\nDo you want to proceed?' % (E2iPlayerVer, INSTALL_PATH_BASE)
answer = ''
while answer not in ['Y', 'N']:
answer = raw_input(MSG_FORMAT.format(msg) + "\nY/N: ").strip().upper()
msg = ''
if answer == 'Y':
copyDirContent(E2iPlayerDir, INSTALL_PATH_BASE)
with open(os.path.join(INSTALL_PATH_BASE, "update", ".settings.txt"), "w") as f:
f.write("e2iplayer_login=%s\n" % LOGIN)
f.write("e2iplayer_password=%s\n" % PASSWORD)
f.write("preferredupdateserver=%s\n" % '3') # '3' - private server
e2irootfs = ''
for it in ('/hdd', '/media/hdd', '/media/usb', '/media/mmc'):
if os.path.isdir(it + '/e2irootfs'):
e2irootfs = it + '/e2irootfs'
break
if e2irootfs:
os.mkdir(e2irootfs + '/e2i_config')
os.mkdir(e2irootfs + '/e2i_recording')
os.mkdir(e2irootfs + '/e2i_temp')
os.mkdir(e2irootfs + '/e2i_cache')
f.write("urllist_path=%s/e2i_config\n" % e2irootfs)
f.write("buffering_path=%s/e2i_recording\n" % e2irootfs)
f.write("recording_path=%s/e2i_recording\n" % e2irootfs)
f.write("temp_path=%s/e2i_temp\n" % e2irootfs)
f.write("cache_path=%s/e2i_cache\n" % e2irootfs)
f.write("rambuffer_files=10\n")
f.write("rambuffer_network=20\n")
os.system("sync")
printMSG('Done. E2iPlayer version "%s" installed correctly.\nPlease remember to restart your Enigma2.' % (E2iPlayerVer))
else:
os.system('rm -rf "%s"' % E2iPlayerDir)
printMSG('Installation skipped.')