Skip to content

Commit

Permalink
Add support for proxy settings anxdpanic#884
Browse files Browse the repository at this point in the history
  • Loading branch information
MoojMidge committed Sep 22, 2024
1 parent 0024baa commit 4529083
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 1 deletion.
1 change: 1 addition & 0 deletions addon.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<import addon="script.module.requests" version="2.27.1"/>
<import addon="inputstream.adaptive" version="19.0.0"/>
<import addon="script.module.inputstreamhelper" version="0.2.2" optional="true"/>
<import addon="script.module.pysocks" optional="true"/>
</requires>
<extension point="xbmc.python.pluginsource" library="resources/lib/plugin.py">
<provides>video</provides>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@
CONNECT_TIMEOUT = 'requests.timeout.connect' # (int)
READ_TIMEOUT = 'requests.timeout.read' # (int)

PROXY_SOURCE = 'requests.proxy.source' # (int)
PROXY_ENABLED = 'requests.proxy.enabled' # (bool)
PROXY_TYPE = 'requests.proxy.type' # (int)
PROXY_SERVER = 'requests.proxy.server' # (str)
PROXY_PORT = 'requests.proxy.port' # (int)
PROXY_USERNAME = 'requests.proxy.username' # (str)
PROXY_PASSWORD = 'requests.proxy.password' # (str)

HTTPD_PORT = 'kodion.http.port' # (int)
HTTPD_LISTEN = 'kodion.http.listen' # (str)
HTTPD_WHITELIST = 'kodion.http.ip.whitelist' # (str)
Expand Down
3 changes: 3 additions & 0 deletions resources/lib/youtube_plugin/kodion/network/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def __init__(self, context, exc_type=None):
settings = context.get_settings()
self._verify = settings.verify_ssl()
self._timeout = settings.get_timeout()
self._proxy = settings.proxy_settings()

if isinstance(exc_type, tuple):
self._default_exc = (RequestException,) + exc_type
Expand Down Expand Up @@ -89,6 +90,8 @@ def request(self, url, method='GET',
timeout = self._timeout
if verify is None:
verify = self._verify
if proxies is None:
proxies = self._proxy
if allow_redirects is None:
allow_redirects = True

Expand Down
134 changes: 133 additions & 1 deletion resources/lib/youtube_plugin/kodion/settings/abstract_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
import sys

from ..constants import SETTINGS
from ..utils import current_system_version, validate_ip_address
from ..utils import (
current_system_version,
get_kodi_setting_value,
validate_ip_address,
)


class AbstractSettings(object):
Expand Down Expand Up @@ -206,6 +210,134 @@ def get_timeout(self):
read_timout = self.get_int(SETTINGS.READ_TIMEOUT, 27)
return connect_timeout, read_timout

_PROXY_TYPE_SCHEME = {
0: 'http',
1: 'socks4',
2: 'socks4a',
3: 'socks5',
4: 'socks5h',
5: 'https',
}

_PROXY_SETTINGS = {
SETTINGS.PROXY_ENABLED: {
'value': None,
'type': bool,
'default': False,
'kodi_name': 'network.usehttpproxy',
},
SETTINGS.PROXY_TYPE: {
'value': None,
'type': int,
'default': 0,
'kodi_name': 'network.httpproxytype',
},
SETTINGS.PROXY_SERVER: {
'value': None,
'type': str,
'default': '',
'kodi_name': 'network.httpproxyserver',
},
SETTINGS.PROXY_PORT: {
'value': None,
'type': int,
'default': 8080,
'kodi_name': 'network.httpproxyport',
},
SETTINGS.PROXY_USERNAME: {
'value': None,
'type': str,
'default': '',
'kodi_name': 'network.httpproxyusername',
},
SETTINGS.PROXY_PASSWORD: {
'value': None,
'type': str,
'default': '',
'kodi_name': 'network.httpproxypassword',
},
}

def proxy_settings(self, value=None, as_mapping=True):
if value is not None:
for setting_name, setting in value.items():
setting_value = setting.get('value')
if setting_value is None:
continue

setting_type = setting.get('type', int)
if setting_type is int:
self.set_int(setting_name, setting_value)
elif setting_type is str:
self.set_string(setting_name, setting_value)
else:
self.set_bool(setting_name, setting_value)
return value

proxy_source = self.get_int(SETTINGS.PROXY_SOURCE, 0)
if not proxy_source:
return None

settings = {}
for setting_name, setting in self._PROXY_SETTINGS.items():
setting_default = setting.get('default')
setting_type = setting.get('type', int)
if proxy_source == 1:
setting_value = get_kodi_setting_value(
setting.get('kodi_name'),
process=setting_type,
) or setting_default
elif setting_type is int:
setting_value = self.get_int(setting_name, setting_default)
elif setting_type is str:
setting_value = self.get_string(setting_name, setting_default)
else:
setting_value = self.get_bool(setting_name, setting_default)

settings[setting_name] = {
'value': setting_value,
'type': setting_type,
'default': setting_default,
}

if not as_mapping:
return settings

if proxy_source == 1 and not settings[SETTINGS.PROXY_ENABLED]['value']:
return None

host = settings[SETTINGS.PROXY_SERVER]['value']
if not host:
return None

port = settings[SETTINGS.PROXY_PORT]['value']
if port:
host_port_string = ':'.join((host, str(port)))
else:
host_port_string = host

username = settings[SETTINGS.PROXY_USERNAME]['value']
if username:
password = settings[SETTINGS.PROXY_PASSWORD]['value']
if password:
auth_string = ':'.join((username, password))
else:
auth_string = username
auth_string += '@'
else:
auth_string = ''

proxy_string = ''.join((
self._PROXY_TYPE_SCHEME[settings[SETTINGS.PROXY_TYPE]['value']],
'://',
auth_string,
host_port_string,
))
return {
'http': proxy_string,
'https': proxy_string,
}

def allow_dev_keys(self):
return self.get_bool(SETTINGS.ALLOW_DEV_KEYS, False)

Expand Down
83 changes: 83 additions & 0 deletions resources/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,89 @@
<formatlabel>14045</formatlabel>
</control>
</setting>
<setting id="requests.proxy.source" type="integer" label="713" help="36380">
<level>0</level>
<default>1</default> <!-- Kodi proxy -->
<constraints>
<options>
<option label="20009">1</option> <!-- Kodi proxy -->
<option label="20190">2</option> <!-- Custom proxy -->
<option label="13106">0</option> <!-- Disabled -->
</options>
</constraints>
<control format="string" type="spinner"/>
</setting>
<setting id="requests.proxy.enabled" type="boolean" parent="requests.proxy.source" label="708">
<level>4</level>
<default>false</default>
<control type="toggle"/>
</setting>
<setting id="requests.proxy.type" type="integer" parent="requests.proxy.source" label="1180" help="36381">
<level>0</level>
<default>0</default>
<constraints>
<options>
<option label="1181">0</option> <!-- PROXY_HTTP -->
<option label="1186">5</option> <!-- PROXY_HTTPS -->
<option label="1182">1</option> <!-- PROXY_SOCKS4 -->
<option label="1183">2</option> <!-- PROXY_SOCKS4A -->
<option label="1184">3</option> <!-- PROXY_SOCKS5 -->
<option label="1185">4</option> <!-- PROXY_SOCKS5_REMOTE -->
</options>
</constraints>
<dependencies>
<dependency type="visible" setting="requests.proxy.source">2</dependency>
</dependencies>
<control type="list" format="string"/>
</setting>
<setting id="requests.proxy.server" type="string" parent="requests.proxy.source" label="706" help="36382">
<level>0</level>
<default/>
<constraints>
<allowempty>true</allowempty>
</constraints>
<dependencies>
<dependency type="visible" setting="requests.proxy.source">2</dependency>
</dependencies>
<control type="edit" format="string"/>
</setting>
<setting id="requests.proxy.port" type="integer" parent="requests.proxy.source" label="730" help="36383">
<level>0</level>
<default>8080</default>
<constraints>
<minimum>1</minimum>
<step>1</step>
<maximum>65535</maximum>
</constraints>
<dependencies>
<dependency type="visible" setting="requests.proxy.source">2</dependency>
</dependencies>
<control type="edit" format="integer"/>
</setting>
<setting id="requests.proxy.username" type="string" parent="requests.proxy.source" label="1048" help="36384">
<level>0</level>
<default/>
<constraints>
<allowempty>true</allowempty>
</constraints>
<dependencies>
<dependency type="visible" setting="requests.proxy.source">2</dependency>
</dependencies>
<control type="edit" format="string"/>
</setting>
<setting id="requests.proxy.password" type="string" parent="requests.proxy.source" label="733" help="36385">
<level>0</level>
<default/>
<constraints>
<allowempty>true</allowempty>
</constraints>
<dependencies>
<dependency type="visible" setting="requests.proxy.source">2</dependency>
</dependencies>
<control type="edit" format="string">
<hidden>true</hidden>
</control>
</setting>
</group>
<group id="9" label="30628">
<setting id="kodion.http.listen" type="string" label="30643" help="">
Expand Down

0 comments on commit 4529083

Please sign in to comment.