]>
git.ipfire.org Git - location/libloc.git/blob - src/python/downloader.py
2 ###############################################################################
4 # libloc - A library to determine the location of someone on the Internet #
6 # Copyright (C) 2020 IPFire Development Team <info@ipfire.org> #
8 # This library is free software; you can redistribute it and/or #
9 # modify it under the terms of the GNU Lesser General Public #
10 # License as published by the Free Software Foundation; either #
11 # version 2.1 of the License, or (at your option) any later version. #
13 # This library is distributed in the hope that it will be useful, #
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
16 # Lesser General Public License for more details. #
18 ###############################################################################
31 from . import __version__
32 from _location
import Database
, DATABASE_VERSION_LATEST
34 DATABASE_FILENAME
= "location.db.xz"
36 "https://location.ipfire.org/databases/",
40 log
= logging
.getLogger("location.downloader")
43 class Downloader(object):
44 def __init__(self
, version
=DATABASE_VERSION_LATEST
, mirrors
=None):
45 self
.version
= version
47 # Set mirrors or use defaults
48 self
.mirrors
= list(mirrors
or MIRRORS
)
51 random
.shuffle(self
.mirrors
)
53 # Get proxies from environment
54 self
.proxies
= self
._get
_proxies
()
56 def _get_proxies(self
):
59 for protocol
in ("https", "http"):
60 proxy
= os
.environ
.get("%s_proxy" % protocol
, None)
63 proxies
[protocol
] = proxy
67 def _make_request(self
, url
, baseurl
=None, headers
={}):
69 url
= urllib
.parse
.urljoin(baseurl
, url
)
71 req
= urllib
.request
.Request(url
, method
="GET")
75 "User-Agent" : "location/%s" % __version__
,
79 for header
in headers
:
80 req
.add_header(header
, headers
[header
])
83 for protocol
in self
.proxies
:
84 req
.set_proxy(self
.proxies
[protocol
], protocol
)
88 def _send_request(self
, req
, **kwargs
):
90 log
.debug("HTTP %s Request to %s" % (req
.method
, req
.host
))
91 log
.debug(" URL: %s" % req
.full_url
)
92 log
.debug(" Headers:")
93 for k
, v
in req
.header_items():
94 log
.debug(" %s: %s" % (k
, v
))
97 res
= urllib
.request
.urlopen(req
, **kwargs
)
99 except urllib
.error
.HTTPError
as e
:
100 # Log response headers
101 log
.debug("HTTP Response: %s" % e
.code
)
102 log
.debug(" Headers:")
103 for header
in e
.headers
:
104 log
.debug(" %s: %s" % (header
, e
.headers
[header
]))
106 # Raise all other errors
109 # Log response headers
110 log
.debug("HTTP Response: %s" % res
.code
)
111 log
.debug(" Headers:")
112 for k
, v
in res
.getheaders():
113 log
.debug(" %s: %s" % (k
, v
))
117 def download(self
, public_key
, timestamp
=None, tmpdir
=None, **kwargs
):
118 url
= "%s/%s" % (self
.version
, DATABASE_FILENAME
)
122 headers
["If-Modified-Since"] = timestamp
.strftime(
123 "%a, %d %b %Y %H:%M:%S GMT",
126 t
= tempfile
.NamedTemporaryFile(dir=tmpdir
, delete
=False)
129 for mirror
in self
.mirrors
:
130 # Prepare HTTP request
131 req
= self
._make
_request
(url
, baseurl
=mirror
, headers
=headers
)
134 with self
._send
_request
(req
) as res
:
135 decompressor
= lzma
.LZMADecompressor()
144 buf
= decompressor
.decompress(buf
)
148 # Write all data to disk
151 # Catch decompression errors
152 except lzma
.LZMAError
as e
:
153 log
.warning("Could not decompress downloaded file: %s" % e
)
156 except urllib
.error
.HTTPError
as e
:
157 # The file on the server was too old
159 log
.warning("%s is serving an outdated database. Trying next mirror..." % mirror
)
161 # Log any other HTTP errors
163 log
.warning("%s reported: %s" % (mirror
, e
))
165 # Throw away any downloaded content and try again
169 # Check if the downloaded database is recent
170 if not self
._check
_database
(t
, public_key
, timestamp
):
171 log
.warning("Downloaded database is outdated. Trying next mirror...")
173 # Throw away the data and try again
177 # Make the file readable for everyone
178 os
.chmod(t
.name
, stat
.S_IRUSR|stat
.S_IRGRP|stat
.S_IROTH
)
180 # Return temporary file
183 # Delete the temporary file after unsuccessful downloads
186 raise FileNotFoundError(url
)
188 def _check_database(self
, f
, public_key
, timestamp
=None):
190 Checks the downloaded database if it can be opened,
191 verified and if it is recent enough
193 log
.debug("Opening downloaded database at %s" % f
.name
)
195 db
= Database(f
.name
)
197 # Database is not recent
198 if timestamp
and db
.created_at
< timestamp
.timestamp():
201 log
.info("Downloaded new database from %s" % (time
.strftime(
202 "%a, %d %b %Y %H:%M:%S GMT", time
.gmtime(db
.created_at
),
205 # Verify the database
206 with
open(public_key
, "r") as f
:
208 log
.error("Could not verify database")