2020-07-10 00:58:55 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
2021-10-02 12:39:16 +02:00
|
|
|
import ssl
|
|
|
|
import json
|
2020-07-10 00:58:55 +02:00
|
|
|
import xml.etree.ElementTree as ET
|
2023-03-31 17:38:52 +02:00
|
|
|
import http.client
|
2021-10-30 18:39:27 +02:00
|
|
|
from urllib.request import urlopen, Request
|
|
|
|
from urllib.parse import urljoin
|
|
|
|
from urllib.error import HTTPError
|
2021-10-02 12:39:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_context(verify):
|
|
|
|
ctx = None
|
|
|
|
if not verify:
|
|
|
|
ctx = ssl.create_default_context()
|
|
|
|
ctx.check_hostname = False
|
|
|
|
ctx.verify_mode = ssl.CERT_NONE
|
|
|
|
return ctx
|
2020-07-10 00:58:55 +02:00
|
|
|
|
2021-07-11 17:37:42 +02:00
|
|
|
|
2020-07-10 00:58:55 +02:00
|
|
|
def get_apikey(configfile="/root/.config/syncthing/config.xml"):
|
|
|
|
try:
|
|
|
|
tree = ET.parse(configfile)
|
|
|
|
root = tree.getroot()
|
|
|
|
apikey = root.find("./gui/apikey").text
|
|
|
|
return apikey
|
2020-09-15 13:14:34 +02:00
|
|
|
except (FileNotFoundError, ET.ParseError, AttributeError) as exc:
|
|
|
|
raise f"Exception {exc} occured"
|
2021-01-17 17:43:56 +01:00
|
|
|
|
2021-07-11 17:37:42 +02:00
|
|
|
return None
|
|
|
|
|
2020-07-10 00:58:55 +02:00
|
|
|
|
2023-02-19 17:59:18 +01:00
|
|
|
def get_config(url="http://localhost:8384", verify=False, apikey=get_apikey()):
|
2021-12-29 15:45:48 +01:00
|
|
|
fullurl = f"{url}/rest/config"
|
2021-10-02 12:39:16 +02:00
|
|
|
req = Request(method="GET",
|
|
|
|
url=fullurl)
|
|
|
|
req.add_header("X-API-Key", apikey)
|
2024-01-03 13:30:44 +01:00
|
|
|
try:
|
|
|
|
res = urlopen(req, context=get_context(verify))
|
2021-10-02 12:39:16 +02:00
|
|
|
|
2024-01-03 13:30:44 +01:00
|
|
|
ret = json.loads(res.read())
|
|
|
|
except urllib.error.URLError:
|
|
|
|
return None
|
2021-10-02 12:39:16 +02:00
|
|
|
if res.status == 200:
|
2020-07-10 00:58:55 +02:00
|
|
|
return ret
|
2021-01-17 17:43:56 +01:00
|
|
|
|
2020-07-10 00:58:55 +02:00
|
|
|
return None
|
|
|
|
|
2021-07-11 17:37:42 +02:00
|
|
|
|
2023-02-19 17:59:18 +01:00
|
|
|
def set_config_options(url="http://localhost:8384", verify=False, apikey=get_apikey(), options={}):
|
|
|
|
fullurl = f"{url}/rest/config/options"
|
|
|
|
req = Request(method="PUT",
|
|
|
|
url=fullurl,
|
|
|
|
data=json.dumps(options).encode())
|
|
|
|
req.add_header("Content-Type", "application/json")
|
|
|
|
req.add_header("X-API-Key", apikey)
|
|
|
|
try:
|
|
|
|
res = urlopen(req, context=get_context(verify))
|
|
|
|
except HTTPError as err:
|
|
|
|
if err.status != 307:
|
|
|
|
return False
|
|
|
|
req.full_url = urljoin(url, err.headers['Location'])
|
|
|
|
res = urlopen(req, context=get_context(verify))
|
2024-01-03 13:30:44 +01:00
|
|
|
except urllib.error.URLError:
|
|
|
|
return None
|
2023-02-19 17:59:18 +01:00
|
|
|
if res.status == 200:
|
|
|
|
return True
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
def set_config_gui(url="http://localhost:8384", verify=False, apikey=get_apikey(), gui={}):
|
|
|
|
fullurl = f"{url}/rest/config/gui"
|
|
|
|
req = Request(method="PUT",
|
|
|
|
url=fullurl,
|
|
|
|
data=json.dumps(gui).encode())
|
|
|
|
req.add_header("Content-Type", "application/json")
|
|
|
|
req.add_header("X-API-Key", apikey)
|
|
|
|
try:
|
|
|
|
res = urlopen(req, context=get_context(verify))
|
|
|
|
except HTTPError as err:
|
|
|
|
if err.status != 307:
|
|
|
|
return False
|
|
|
|
req.full_url = urljoin(url, err.headers['Location'])
|
|
|
|
res = urlopen(req, context=get_context(verify))
|
|
|
|
except http.client.RemoteDisconnected as err:
|
|
|
|
return True
|
2024-01-03 13:30:44 +01:00
|
|
|
except urllib.error.URLError:
|
|
|
|
return None
|
2023-02-19 17:59:18 +01:00
|
|
|
if res.status == 200:
|
|
|
|
return True
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
## deprecated
|
|
|
|
def _set_config(url="http://localhost:8384", verify=False, apikey=get_apikey(), config={}):
|
2021-12-29 15:45:48 +01:00
|
|
|
fullurl = f"{url}/rest/config"
|
2021-10-02 12:39:16 +02:00
|
|
|
req = Request(method="POST",
|
|
|
|
url=fullurl,
|
|
|
|
data=json.dumps(config).encode())
|
2021-11-14 13:35:25 +01:00
|
|
|
req.add_header("Content-Type", "application/json")
|
2021-10-02 12:39:16 +02:00
|
|
|
req.add_header("X-API-Key", apikey)
|
2021-10-30 18:39:27 +02:00
|
|
|
try:
|
|
|
|
res = urlopen(req, context=get_context(verify))
|
|
|
|
except HTTPError as err:
|
|
|
|
if err.status != 307:
|
|
|
|
return None
|
|
|
|
req.full_url = urljoin(url, err.headers['Location'])
|
|
|
|
res = urlopen(req, context=get_context(verify))
|
2024-01-03 13:30:44 +01:00
|
|
|
except urllib.error.URLError:
|
|
|
|
return None
|
2021-10-02 12:39:16 +02:00
|
|
|
if res.status == 200:
|
2020-07-10 00:58:55 +02:00
|
|
|
return True
|
2021-01-17 17:43:56 +01:00
|
|
|
|
2020-07-10 00:58:55 +02:00
|
|
|
return None
|
|
|
|
|
2021-07-11 17:37:42 +02:00
|
|
|
|
2023-02-19 17:59:18 +01:00
|
|
|
def insync(url="http://localhost:8384", verify=False, apikey=get_apikey()):
|
2021-12-29 15:45:48 +01:00
|
|
|
fullurl = f"{url}/rest/config/restart-required"
|
2021-10-02 12:39:16 +02:00
|
|
|
req = Request(method="GET",
|
|
|
|
url=fullurl)
|
|
|
|
req.add_header("X-API-Key", apikey)
|
2024-01-03 13:30:44 +01:00
|
|
|
try:
|
|
|
|
res = urlopen(req, context=get_context(verify))
|
|
|
|
ret = json.loads(res.read())
|
|
|
|
if res.status == 200:
|
|
|
|
return ret
|
|
|
|
except urllib.error.URLError:
|
|
|
|
return None
|
2021-01-17 17:43:56 +01:00
|
|
|
|
2020-07-10 00:58:55 +02:00
|
|
|
return None
|
|
|
|
|
2021-07-11 17:37:42 +02:00
|
|
|
|
2023-02-19 17:59:18 +01:00
|
|
|
def restart(url="http://localhost:8384", verify=False, apikey=get_apikey()):
|
2021-06-14 23:05:24 +02:00
|
|
|
fullurl = f"{url}/rest/system/restart"
|
2021-10-02 12:39:16 +02:00
|
|
|
req = Request(method="POST",
|
|
|
|
url=fullurl)
|
|
|
|
req.add_header("X-API-Key", apikey)
|
2024-01-03 13:30:44 +01:00
|
|
|
try:
|
|
|
|
res = urlopen(req, context=get_context(verify))
|
|
|
|
if res.status == 200:
|
|
|
|
return {}
|
|
|
|
except urllib.error.URLError:
|
|
|
|
return None
|
2021-01-17 17:43:56 +01:00
|
|
|
|
2020-09-15 13:14:34 +02:00
|
|
|
return None
|