2020-04-03 09:19:20 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
2019-06-26 20:13:36 +00:00
|
|
|
|
2020-04-03 09:19:20 +00:00
|
|
|
send a command to the local API server
|
2020-01-04 12:13:10 +00:00
|
|
|
"""
|
2020-04-03 09:19:20 +00:00
|
|
|
import urllib
|
|
|
|
import time
|
2020-01-04 12:13:10 +00:00
|
|
|
|
|
|
|
import requests
|
2020-04-03 09:33:30 +00:00
|
|
|
import deadsimplekv
|
|
|
|
|
|
|
|
import logger
|
|
|
|
import config
|
2020-01-04 12:13:10 +00:00
|
|
|
|
|
|
|
from . import getclientapiserver
|
|
|
|
import filepaths
|
|
|
|
"""
|
2019-06-26 20:13:36 +00:00
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2019-07-18 17:40:48 +00:00
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2020-01-04 12:13:10 +00:00
|
|
|
"""
|
2019-07-18 17:40:48 +00:00
|
|
|
config.reload()
|
2019-07-20 06:02:30 +00:00
|
|
|
|
2020-04-03 09:19:20 +00:00
|
|
|
cache = deadsimplekv.DeadSimpleKV(filepaths.cached_storage,
|
|
|
|
refresh_seconds=1000)
|
|
|
|
|
|
|
|
|
2019-07-18 05:11:52 +00:00
|
|
|
def get_hostname():
|
2019-07-20 06:02:30 +00:00
|
|
|
hostname = ''
|
|
|
|
waited = 0
|
2020-07-17 18:49:18 +00:00
|
|
|
max_wait = 3
|
2019-07-20 06:02:30 +00:00
|
|
|
while True:
|
|
|
|
if cache.get('client_api') is None:
|
2019-07-20 15:52:03 +00:00
|
|
|
try:
|
|
|
|
hostname = getclientapiserver.get_client_API_server()
|
|
|
|
except FileNotFoundError:
|
|
|
|
hostname = False
|
|
|
|
else:
|
|
|
|
cache.put('hostname', hostname)
|
|
|
|
cache.flush()
|
2019-07-20 06:02:30 +00:00
|
|
|
else:
|
|
|
|
hostname = cache.get('hostname')
|
|
|
|
if hostname == '' or hostname is None:
|
2019-06-23 07:00:27 +00:00
|
|
|
time.sleep(1)
|
2020-07-17 18:49:18 +00:00
|
|
|
if waited == max_wait:
|
2019-06-23 07:00:27 +00:00
|
|
|
return False
|
2019-07-20 06:02:30 +00:00
|
|
|
else:
|
|
|
|
return hostname
|
2019-07-18 05:11:52 +00:00
|
|
|
|
2020-04-03 09:19:20 +00:00
|
|
|
|
2020-07-17 18:49:18 +00:00
|
|
|
def local_command(command, data='', silent=True, post=False,
|
|
|
|
post_data={}, max_wait=20,
|
2020-01-03 10:15:20 +00:00
|
|
|
is_json=False
|
|
|
|
):
|
2020-07-17 18:49:18 +00:00
|
|
|
"""Send a command to the local http API server, securely.
|
|
|
|
Intended for local clients, DO NOT USE for remote peers."""
|
2019-07-20 06:02:30 +00:00
|
|
|
hostname = get_hostname()
|
2020-07-17 18:49:18 +00:00
|
|
|
# if the api host cannot be reached, return False
|
|
|
|
if not hostname:
|
|
|
|
return False
|
2020-01-03 10:15:20 +00:00
|
|
|
|
2019-06-23 07:00:27 +00:00
|
|
|
if data != '':
|
|
|
|
data = '&data=' + urllib.parse.quote_plus(data)
|
|
|
|
payload = 'http://%s/%s%s' % (hostname, command, data)
|
2020-01-30 04:56:47 +00:00
|
|
|
if not config.get('client.webpassword'):
|
|
|
|
config.reload()
|
2019-07-20 15:52:03 +00:00
|
|
|
|
2019-06-23 07:00:27 +00:00
|
|
|
try:
|
|
|
|
if post:
|
2020-01-03 10:15:20 +00:00
|
|
|
if is_json:
|
|
|
|
ret_data = requests.post(
|
|
|
|
payload,
|
2020-07-17 18:49:18 +00:00
|
|
|
json=post_data,
|
2020-01-03 10:15:20 +00:00
|
|
|
headers={'token': config.get('client.webpassword'),
|
|
|
|
'Connection': 'close'},
|
2020-07-17 18:49:18 +00:00
|
|
|
timeout=(max_wait, max_wait)).text
|
2020-01-03 10:15:20 +00:00
|
|
|
else:
|
|
|
|
ret_data = requests.post(
|
|
|
|
payload,
|
2020-07-17 18:49:18 +00:00
|
|
|
data=post_data,
|
2020-01-03 10:15:20 +00:00
|
|
|
headers={'token': config.get('client.webpassword'),
|
|
|
|
'Connection': 'close'},
|
2020-07-17 18:49:18 +00:00
|
|
|
timeout=(max_wait, max_wait)).text
|
2019-06-23 07:00:27 +00:00
|
|
|
else:
|
2020-07-17 18:49:18 +00:00
|
|
|
ret_data = requests.get(payload,
|
|
|
|
headers={'token':
|
|
|
|
config.get('client.webpassword'),
|
|
|
|
'Connection': 'close'},
|
|
|
|
timeout=(max_wait, max_wait)).text
|
2019-06-23 07:00:27 +00:00
|
|
|
except Exception as error:
|
|
|
|
if not silent:
|
2020-07-17 18:49:18 +00:00
|
|
|
logger.error('Failed to make local request (command: %s):%s' %
|
|
|
|
(command, error), terminal=True)
|
2019-08-09 20:41:27 +00:00
|
|
|
ret_data = False
|
|
|
|
return ret_data
|