Skip to content

Commit

Permalink
Merge pull request ZeroNetX#225 from caryoscelus/fix-geoip-load
Browse files Browse the repository at this point in the history
Fix geoip load
  • Loading branch information
caryoscelus authored Jul 30, 2023
2 parents a209f6a + b358435 commit 0f3a489
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 48 deletions.
19 changes: 9 additions & 10 deletions plugins/Sidebar/SidebarPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ def actionSidebarGetHtmlTag(self, to):
def downloadGeoLiteDb(self, db_path):
import gzip
import shutil
from util import helper
import requests

if config.offline or config.tor == 'always':
return False
Expand All @@ -617,19 +617,18 @@ def downloadGeoLiteDb(self, db_path):
downloadl_err = None
try:
# Download
response = helper.httpRequest(db_url)
data_size = response.getheader('content-length')
response = requests.get(db_url, stream=True)
data_size = response.headers.get('content-length')
if data_size is None:
data.write(response.content)
data_size = int(data_size)
data_recv = 0
data = io.BytesIO()
while True:
buff = response.read(1024 * 512)
if not buff:
break
for buff in response.iter_content(chunk_size=1024 * 512):
data.write(buff)
data_recv += 1024 * 512
if data_size:
progress = int(float(data_recv) / int(data_size) * 100)
self.cmd("progress", ["geolite-info", _["Downloading GeoLite2 City database (one time only, ~20MB)..."], progress])
progress = int(float(data_recv) / data_size * 100)
self.cmd("progress", ["geolite-info", _["Downloading GeoLite2 City database (one time only, ~20MB)..."], progress])
self.log.info("GeoLite2 City database downloaded (%s bytes), unpacking..." % data.tell())
data.seek(0)

Expand Down
38 changes: 0 additions & 38 deletions src/util/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,44 +195,6 @@ def mergeDicts(dicts):
return dict(back)


# Request https url using gevent SSL error workaround
def httpRequest(url, as_file=False):
if url.startswith("http://"):
import urllib.request
response = urllib.request.urlopen(url)
else: # Hack to avoid Python gevent ssl errors
import socket
import http.client
import ssl

host, request = re.match("https://(.*?)(/.*?)$", url).groups()

conn = http.client.HTTPSConnection(host)
sock = socket.create_connection((conn.host, conn.port), conn.timeout, conn.source_address)

context = ssl.create_default_context()
context.minimum_version = ssl.TLSVersion.TLSv1_2

conn.sock = context.wrap_socket(sock, conn.key_file, conn.cert_file)
conn.request("GET", request)
response = conn.getresponse()
if response.status in [301, 302, 303, 307, 308]:
logging.info("Redirect to: %s" % response.getheader('Location'))
response = httpRequest(response.getheader('Location'))

if as_file:
import io
data = io.BytesIO()
while True:
buff = response.read(1024 * 16)
if not buff:
break
data.write(buff)
return data
else:
return response


def timerCaller(secs, func, *args, **kwargs):
gevent.spawn_later(secs, timerCaller, secs, func, *args, **kwargs)
func(*args, **kwargs)
Expand Down

0 comments on commit 0f3a489

Please sign in to comment.