From 02d6af51b09bd7d155c73474594d15a75b693ed6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Lavaud-Wernert?= Date: Wed, 19 Jun 2024 17:45:11 +0200 Subject: [PATCH] Add proxy settings --- .../resource.language.en_gb/strings.po | 38 +++++++++++++++++++ .../resource.language.fr_fr/strings.po | 38 +++++++++++++++++++ resources/lib/providers/cache_provider.py | 5 +-- resources/lib/utils/orange.py | 4 +- resources/lib/utils/request.py | 35 +++++++++++++++++ resources/settings.xml | 7 ++++ 6 files changed, 123 insertions(+), 4 deletions(-) diff --git a/resources/language/resource.language.en_gb/strings.po b/resources/language/resource.language.en_gb/strings.po index a34ada0..de3256e 100644 --- a/resources/language/resource.language.en_gb/strings.po +++ b/resources/language/resource.language.en_gb/strings.po @@ -56,6 +56,44 @@ msgctxt "#30204" msgid "Help 30204" msgstr "" +# Proxy settings (from 30300 to 30399) + +msgctxt "#30300" +msgid "Proxy" +msgstr "" + +msgctxt "#30301" +msgid "Enable" +msgstr "" + +msgctxt "#30302" +msgid "Help 30302" +msgstr "" + +msgctxt "#30303" +msgid "Type" +msgstr "" + +msgctxt "#30304" +msgid "Help 30304" +msgstr "" + +msgctxt "#30305" +msgid "IP" +msgstr "" + +msgctxt "#30306" +msgid "Help 30306" +msgstr "" + +msgctxt "#30307" +msgid "Port" +msgstr "" + +msgctxt "#30308" +msgid "Help 30308" +msgstr "" + # Dialogs (from 30900 to 30999) msgctxt "#30900" diff --git a/resources/language/resource.language.fr_fr/strings.po b/resources/language/resource.language.fr_fr/strings.po index 7484523..67bcd0e 100644 --- a/resources/language/resource.language.fr_fr/strings.po +++ b/resources/language/resource.language.fr_fr/strings.po @@ -56,6 +56,44 @@ msgctxt "#30204" msgid "Help 30204" msgstr "" +# Proxy settings (from 30300 to 30399) + +msgctxt "#30300" +msgid "Proxy" +msgstr "Proxy" + +msgctxt "#30301" +msgid "Activer" +msgstr "" + +msgctxt "#30302" +msgid "Help 30302" +msgstr "" + +msgctxt "#30303" +msgid "Type" +msgstr "Type" + +msgctxt "#30304" +msgid "Help 30304" +msgstr "" + +msgctxt "#30305" +msgid "IP" +msgstr "Adresse IP" + +msgctxt "#30306" +msgid "Help 30306" +msgstr "" + +msgctxt "#30307" +msgid "Port" +msgstr "Numéro de port" + +msgctxt "#30308" +msgid "Help 30308" +msgstr "" + # Dialogs (from 30900 to 30999) msgctxt "#30900" diff --git a/resources/lib/providers/cache_provider.py b/resources/lib/providers/cache_provider.py index 7fdf002..f3157e4 100644 --- a/resources/lib/providers/cache_provider.py +++ b/resources/lib/providers/cache_provider.py @@ -2,7 +2,6 @@ import json import os -from urllib.error import URLError import xbmc import xbmcvfs @@ -38,8 +37,8 @@ def get_streams(self) -> list: streams = self.provider.get_streams() with open(os.path.join(self.cache_folder, "streams.json"), "wb") as file: file.write(json.dumps(streams).encode("utf-8")) - except URLError: - log("Can't reach server: load streams from cache", xbmc.LOGWARNING) + except Exception: + log("Can't load streams: using cache instead", xbmc.LOGWARNING) with open(os.path.join(self.cache_folder, "streams.json"), encoding="utf-8") as file: streams = json.loads("".join(file.readlines())) diff --git a/resources/lib/utils/orange.py b/resources/lib/utils/orange.py index 2710d45..53b33f1 100644 --- a/resources/lib/utils/orange.py +++ b/resources/lib/utils/orange.py @@ -10,7 +10,7 @@ import xbmc -from lib.utils.request import get_random_ua +from lib.utils.request import get_random_ua, install_proxy from lib.utils.xbmc import get_drm, get_global_setting, log _EPG_ENDPOINT = "https://rp-ott-mediation-tv.woopic.com/api-gw/live/v3/applications/STB4PC/programs?period={period}&epgIds=all&mco={mco}" @@ -205,6 +205,8 @@ def _build_request(url: str, additional_headers: dict = None) -> Request: if additional_headers is None: additional_headers = {} + install_proxy() + return Request(url, headers={"User-Agent": get_random_ua(), "Host": urlparse(url).netloc, **additional_headers}) diff --git a/resources/lib/utils/request.py b/resources/lib/utils/request.py index 2e8c265..e10f258 100644 --- a/resources/lib/utils/request.py +++ b/resources/lib/utils/request.py @@ -2,6 +2,10 @@ from random import randint +# from socks import SOCKS5 +# from sockshandler import SocksiPyHandler +from lib.utils.xbmc import get_addon_setting + _USER_AGENTS = [ # Chrome "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.3", @@ -25,3 +29,34 @@ def get_random_ua() -> str: """Get a randomised user agent.""" return _USER_AGENTS[randint(0, len(_USER_AGENTS) - 1)] + + +def install_proxy(): + """Install proxy server for the next requests.""" + if get_addon_setting("proxy.enabled") != "true": + return + + ip = get_addon_setting("proxy.ip") + port = get_addon_setting("proxy.port") + + if ip == "" or port == "": + return + + # protocol = get_addon_setting('proxy.protocol') + + # if protocol == 'HTTP': + # proxies = { + # 'https': f"http://{ip}:{port}", + # 'http': f"http://{ip}:{port}" + # } + # log(proxies, xbmc.LOGDEBUG) + # proxy_support = ProxyHandler(proxies) + + # elif protocol == 'Socks5 Local DNS': + # proxy_support = SocksiPyHandler(SOCKS5, ip, int(port), False) + + # elif protocol == 'Socks5 Remote DNS': + # proxy_support = SocksiPyHandler(SOCKS5, ip, int(port), True) + + # opener = build_opener(proxy_support) + # install_opener(opener) diff --git a/resources/settings.xml b/resources/settings.xml index 652dd55..bcf04a7 100644 --- a/resources/settings.xml +++ b/resources/settings.xml @@ -11,4 +11,11 @@ + + + + + + +