rolled back and fixed startls

This commit is contained in:
emdee 2022-11-23 19:26:46 +00:00
parent 8daf6d54a9
commit 00f25b7ddc
2 changed files with 763 additions and 980 deletions

1356
jabber.py

File diff suppressed because it is too large Load Diff

133
support_jabber.py Normal file
View File

@ -0,0 +1,133 @@
import os
import weechat
w = weechat
def LOG_debug(what, level='none'):
# if jabber_debug_enabled():
w.prnt('', what)
class ProxyWrapper(object):
def __init__(self, name):
self.proxy_name = name
self.proxy_string = ""
self.proxy_type = ""
self.proxy_address = ""
self.proxy_port = ""
self.proxy_user = ""
self.proxy_password = ""
self.has_proxy = False
def configure(self):
if self.proxy_name:
self.proxy_string = "weechat.proxy.{}".format(self.proxy_name)
self.proxy_type = w.config_string(
w.config_get("{}.type".format(self.proxy_string))
)
if self.proxy_type in ['socks5', "http"]:
self.proxy_address = w.config_string(
w.config_get("{}.address".format(self.proxy_string))
)
self.proxy_port = w.config_integer(
w.config_get("{}.port".format(self.proxy_string))
)
self.proxy_user = w.config_string(
w.config_get("{}.username".format(self.proxy_string))
)
self.proxy_password = w.config_string(
w.config_get("{}.password".format(self.proxy_string))
)
self.has_proxy = True
else:
w_prnt(
"",
"\nWarning: weechat.network.proxy_curl is set to {} type (name: {}, conf string: {}). Only HTTP and SOCKS5 proxies are supported.\n\n".format(
self.proxy_type, self.proxy_name, self.proxy_string
),
)
def curl(self):
if not self.has_proxy:
return ""
if self.proxy_user and self.proxy_password:
user = "{}:{}@".format(self.proxy_user, self.proxy_password)
else:
user = ""
if self.proxy_port:
port = ":{}".format(self.proxy_port)
else:
port = ""
return "-x{}{}{}".format(user, self.proxy_address, port)
def make_proxy(self, sproxy):
proxy = {}
if sproxy.startswith('socks'):
pair = sproxy
pair = pair.replace('socks5h://', '')
pair = pair.replace('socks5://', '')
pair = pair.replace('socks4://', '')
pair = pair.replace('socks://', '')
if ':' in pair:
phost, pport = pair.split(':', 1)
# '127.0.0.1' 9050
proxy = {'host': phost, 'port': int(pport)}
elif sproxy.startswith('http'):
pair = sproxy
pair = pair.replace('https://', '')
pair = pair.replace('http://', '')
if ':' in pair:
phost, pport = pair.split(':', 1)
# '127.0.0.1' 9128
proxy = {'host': phost, 'port': int(pport)}
return proxy
def aget_proxy(self, ltypes=None):
if ltypes is None:
ltypes = ['http', 'socks5']
aproxy = {}
elt = f"jabber.server.{self.name}.proxy"
stor = w.config_string(w.config_get(elt))
if stor:
saddress = w.config_string(w.config_get(f"weechat.proxy.{stor}.address"))
iport = w.config_integer(w.config_get(f"weechat.proxy.{stor}.port"))
itype = w.config_integer(w.config_get(f"weechat.proxy.{stor}.type"))
atypes = {0: 'http', 1: 'socks', 2: 'socks'}
stype = atypes[itype]
if stype in ltypes:
aproxy = {'host': saddress,
'port': iport,
'type': stype,
}
LOG_debug(f"aget_proxy proxy {stor} {aproxy}")
return aproxy
sproxy = w.config_string(w.config_get("weechat.network.proxy_curl"))
if sproxy:
oproxy = ProxyWrapper(sproxy)
if oproxy.proxy_type in ltypes:
oproxy.configure()
aproxy = {'host': oproxy.proxy_address,
'port': int(oproxy.proxy_port),
'type': oproxy.proxy_type,
}
LOG_debug(f"aget_proxy proxy {sproxy} {aproxy}")
return aproxy
if 'socks_proxy' in ltypes:
if os.environ.get('socks_proxy', ''):
sproxy = os.environ.get('socks_proxy', '')
aproxy = make_proxy(self, sproxy)
LOG_debug(f"proxy {aproxy}")
LOG_debug(f"aget_proxy proxy env={sproxy} {aproxy}")
return aproxy
if 'https_proxy' in ltypes:
sproxy = os.environ.get('https_proxy', '')
if sproxy:
aproxy = make_proxy(self, sproxy)
LOG_debug(f"proxy {aproxy}")
LOG_debug(f"aget_proxy proxy env={sproxy} {aproxy}")
return aproxy
return aproxy