]>
Commit | Line | Data |
---|---|---|
d2790370 | 1 | import io |
d5ed35b6 FV |
2 | import json |
3 | import traceback | |
4 | import hashlib | |
d2790370 | 5 | import subprocess |
46353f67 | 6 | import sys |
d5ed35b6 FV |
7 | from zipimport import zipimporter |
8 | ||
9 | from .utils import * | |
10 | from .version import __version__ | |
11 | ||
12 | def rsa_verify(message, signature, key): | |
13 | from struct import pack | |
14 | from hashlib import sha256 | |
15 | from sys import version_info | |
16 | def b(x): | |
17 | if version_info[0] == 2: return x | |
18 | else: return x.encode('latin1') | |
19 | assert(type(message) == type(b(''))) | |
20 | block_size = 0 | |
21 | n = key[0] | |
22 | while n: | |
23 | block_size += 1 | |
24 | n >>= 8 | |
25 | signature = pow(int(signature, 16), key[1], key[0]) | |
26 | raw_bytes = [] | |
27 | while signature: | |
28 | raw_bytes.insert(0, pack("B", signature & 0xFF)) | |
29 | signature >>= 8 | |
30 | signature = (block_size - len(raw_bytes)) * b('\x00') + b('').join(raw_bytes) | |
31 | if signature[0:2] != b('\x00\x01'): return False | |
32 | signature = signature[2:] | |
33 | if not b('\x00') in signature: return False | |
34 | signature = signature[signature.index(b('\x00'))+1:] | |
35 | if not signature.startswith(b('\x30\x31\x30\x0D\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01\x05\x00\x04\x20')): return False | |
36 | signature = signature[19:] | |
37 | if signature != sha256(message).digest(): return False | |
38 | return True | |
39 | ||
46353f67 | 40 | def update_self(to_screen, verbose): |
d5ed35b6 FV |
41 | """Update the program file with the latest version from the repository""" |
42 | ||
43ff1a34 | 43 | UPDATE_URL = "http://rg3.github.io/youtube-dl/update/" |
d5ed35b6 FV |
44 | VERSION_URL = UPDATE_URL + 'LATEST_VERSION' |
45 | JSON_URL = UPDATE_URL + 'versions.json' | |
46 | UPDATES_RSA_KEY = (0x9d60ee4d8f805312fdb15a62f87b95bd66177b91df176765d13514a0f1754bcd2057295c5b6f1d35daa6742c3ffc9a82d3e118861c207995a8031e151d863c9927e304576bc80692bc8e094896fcf11b66f3e29e04e3a71e9a11558558acea1840aec37fc396fb6b65dc81a1c4144e03bd1c011de62e3f1357b327d08426fe93, 65537) | |
47 | ||
d5ed35b6 | 48 | if not isinstance(globals().get('__loader__'), zipimporter) and not hasattr(sys, "frozen"): |
f9bd64c0 | 49 | to_screen(u'It looks like you installed youtube-dl with a package manager, pip, setup.py or a tarball. Please use that to update.') |
d5ed35b6 FV |
50 | return |
51 | ||
52 | # Check if there is a new version | |
53 | try: | |
54 | newversion = compat_urllib_request.urlopen(VERSION_URL).read().decode('utf-8').strip() | |
55 | except: | |
56 | if verbose: to_screen(compat_str(traceback.format_exc())) | |
57 | to_screen(u'ERROR: can\'t find the current version. Please try again later.') | |
58 | return | |
59 | if newversion == __version__: | |
60 | to_screen(u'youtube-dl is up-to-date (' + __version__ + ')') | |
61 | return | |
62 | ||
63 | # Download and check versions info | |
64 | try: | |
65 | versions_info = compat_urllib_request.urlopen(JSON_URL).read().decode('utf-8') | |
66 | versions_info = json.loads(versions_info) | |
67 | except: | |
68 | if verbose: to_screen(compat_str(traceback.format_exc())) | |
69 | to_screen(u'ERROR: can\'t obtain versions info. Please try again later.') | |
70 | return | |
71 | if not 'signature' in versions_info: | |
72 | to_screen(u'ERROR: the versions file is not signed or corrupted. Aborting.') | |
73 | return | |
74 | signature = versions_info['signature'] | |
75 | del versions_info['signature'] | |
76 | if not rsa_verify(json.dumps(versions_info, sort_keys=True).encode('utf-8'), signature, UPDATES_RSA_KEY): | |
77 | to_screen(u'ERROR: the versions file signature is invalid. Aborting.') | |
78 | return | |
79 | ||
d2790370 PH |
80 | version_id = versions_info['latest'] |
81 | to_screen(u'Updating to version ' + version_id + '...') | |
82 | version = versions_info['versions'][version_id] | |
3bf79c75 | 83 | |
46a127ee | 84 | print_notes(to_screen, versions_info['versions']) |
d5ed35b6 | 85 | |
46353f67 PH |
86 | filename = sys.argv[0] |
87 | # Py2EXE: Filename could be different | |
88 | if hasattr(sys, "frozen") and not os.path.isfile(filename): | |
89 | if os.path.isfile(filename + u'.exe'): | |
90 | filename += u'.exe' | |
91 | ||
d5ed35b6 FV |
92 | if not os.access(filename, os.W_OK): |
93 | to_screen(u'ERROR: no write permissions on %s' % filename) | |
94 | return | |
95 | ||
96 | # Py2EXE | |
97 | if hasattr(sys, "frozen"): | |
98 | exe = os.path.abspath(filename) | |
99 | directory = os.path.dirname(exe) | |
100 | if not os.access(directory, os.W_OK): | |
101 | to_screen(u'ERROR: no write permissions on %s' % directory) | |
102 | return | |
103 | ||
104 | try: | |
105 | urlh = compat_urllib_request.urlopen(version['exe'][0]) | |
106 | newcontent = urlh.read() | |
107 | urlh.close() | |
108 | except (IOError, OSError) as err: | |
109 | if verbose: to_screen(compat_str(traceback.format_exc())) | |
110 | to_screen(u'ERROR: unable to download latest version') | |
111 | return | |
112 | ||
113 | newcontent_hash = hashlib.sha256(newcontent).hexdigest() | |
114 | if newcontent_hash != version['exe'][1]: | |
115 | to_screen(u'ERROR: the downloaded file hash does not match. Aborting.') | |
116 | return | |
117 | ||
118 | try: | |
119 | with open(exe + '.new', 'wb') as outf: | |
120 | outf.write(newcontent) | |
121 | except (IOError, OSError) as err: | |
122 | if verbose: to_screen(compat_str(traceback.format_exc())) | |
123 | to_screen(u'ERROR: unable to write the new version') | |
124 | return | |
125 | ||
126 | try: | |
127 | bat = os.path.join(directory, 'youtube-dl-updater.bat') | |
d2790370 PH |
128 | with io.open(bat, 'w') as batfile: |
129 | batfile.write(u""" | |
130 | @echo off | |
131 | echo Waiting for file handle to be closed ... | |
d5ed35b6 | 132 | ping 127.0.0.1 -n 5 -w 1000 > NUL |
d2790370 PH |
133 | move /Y "%s.new" "%s" > NUL |
134 | echo Updated youtube-dl to version %s. | |
135 | start /b "" cmd /c del "%%~f0"&exit /b" | |
136 | \n""" % (exe, exe, version_id)) | |
d5ed35b6 | 137 | |
d2790370 PH |
138 | subprocess.Popen([bat]) # Continues to run in the background |
139 | return # Do not show premature success messages | |
d5ed35b6 FV |
140 | except (IOError, OSError) as err: |
141 | if verbose: to_screen(compat_str(traceback.format_exc())) | |
142 | to_screen(u'ERROR: unable to overwrite current version') | |
143 | return | |
144 | ||
145 | # Zip unix package | |
146 | elif isinstance(globals().get('__loader__'), zipimporter): | |
147 | try: | |
148 | urlh = compat_urllib_request.urlopen(version['bin'][0]) | |
149 | newcontent = urlh.read() | |
150 | urlh.close() | |
151 | except (IOError, OSError) as err: | |
152 | if verbose: to_screen(compat_str(traceback.format_exc())) | |
153 | to_screen(u'ERROR: unable to download latest version') | |
154 | return | |
155 | ||
156 | newcontent_hash = hashlib.sha256(newcontent).hexdigest() | |
157 | if newcontent_hash != version['bin'][1]: | |
158 | to_screen(u'ERROR: the downloaded file hash does not match. Aborting.') | |
159 | return | |
160 | ||
161 | try: | |
162 | with open(filename, 'wb') as outf: | |
163 | outf.write(newcontent) | |
164 | except (IOError, OSError) as err: | |
165 | if verbose: to_screen(compat_str(traceback.format_exc())) | |
166 | to_screen(u'ERROR: unable to overwrite current version') | |
167 | return | |
168 | ||
169 | to_screen(u'Updated youtube-dl. Restart youtube-dl to use the new version.') | |
3bf79c75 | 170 | |
46a127ee | 171 | def get_notes(versions, fromVersion): |
3bf79c75 PH |
172 | notes = [] |
173 | for v,vdata in sorted(versions.items()): | |
174 | if v > fromVersion: | |
175 | notes.extend(vdata.get('notes', [])) | |
46a127ee PH |
176 | return notes |
177 | ||
178 | def print_notes(to_screen, versions, fromVersion=__version__): | |
179 | notes = get_notes(versions, fromVersion) | |
3bf79c75 PH |
180 | if notes: |
181 | to_screen(u'PLEASE NOTE:') | |
182 | for note in notes: | |
183 | to_screen(note) |