2019-06-23 07:00:27 +00:00
|
|
|
import urllib, requests, time
|
|
|
|
import logger
|
2019-06-23 17:41:07 +00:00
|
|
|
from onionrutils import getclientapiserver
|
|
|
|
def local_command(core_inst, command, data='', silent = True, post=False, postData = {}, maxWait=20):
|
2019-06-23 07:00:27 +00:00
|
|
|
'''
|
|
|
|
Send a command to the local http API server, securely. Intended for local clients, DO NOT USE for remote peers.
|
|
|
|
'''
|
|
|
|
# TODO: URL encode parameters, just as an extra measure. May not be needed, but should be added regardless.
|
|
|
|
hostname = ''
|
|
|
|
waited = 0
|
|
|
|
while hostname == '':
|
|
|
|
try:
|
2019-06-23 17:41:07 +00:00
|
|
|
hostname = getclientapiserver.get_client_API_server(core_inst)
|
2019-06-23 07:00:27 +00:00
|
|
|
except FileNotFoundError:
|
|
|
|
time.sleep(1)
|
|
|
|
waited += 1
|
|
|
|
if waited == maxWait:
|
|
|
|
return False
|
|
|
|
if data != '':
|
|
|
|
data = '&data=' + urllib.parse.quote_plus(data)
|
|
|
|
payload = 'http://%s/%s%s' % (hostname, command, data)
|
|
|
|
try:
|
|
|
|
if post:
|
2019-06-26 00:15:04 +00:00
|
|
|
retData = requests.post(payload, data=postData, headers={'token': core_inst.config.get('client.webpassword'), 'Connection':'close'}, timeout=(maxWait, maxWait)).text
|
2019-06-23 07:00:27 +00:00
|
|
|
else:
|
2019-06-26 00:15:04 +00:00
|
|
|
retData = requests.get(payload, headers={'token': core_inst.config.get('client.webpassword'), 'Connection':'close'}, timeout=(maxWait, maxWait)).text
|
2019-06-23 07:00:27 +00:00
|
|
|
except Exception as error:
|
|
|
|
if not silent:
|
|
|
|
logger.error('Failed to make local request (command: %s):%s' % (command, error), terminal=True)
|
|
|
|
retData = False
|
|
|
|
|
|
|
|
return retData
|