Add web api callbacks
This commit is contained in:
parent
695cd7503b
commit
215fbcba68
@ -30,6 +30,9 @@ class API:
|
|||||||
'''
|
'''
|
||||||
Main HTTP API (Flask)
|
Main HTTP API (Flask)
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
callbacks = {'public' : {}, 'private' : {}, 'ui' : {}}
|
||||||
|
|
||||||
def validateToken(self, token):
|
def validateToken(self, token):
|
||||||
'''
|
'''
|
||||||
Validate that the client token matches the given token
|
Validate that the client token matches the given token
|
||||||
@ -164,6 +167,44 @@ class API:
|
|||||||
self.mimeType = 'text/html'
|
self.mimeType = 'text/html'
|
||||||
response = siteData.split(b'-', 2)[-1]
|
response = siteData.split(b'-', 2)[-1]
|
||||||
resp = Response(response)
|
resp = Response(response)
|
||||||
|
elif action == "insertBlock":
|
||||||
|
response = {'success' : False, 'reason' : 'An unknown error occurred'}
|
||||||
|
|
||||||
|
try:
|
||||||
|
decoded = json.loads(data)
|
||||||
|
|
||||||
|
block = Block()
|
||||||
|
|
||||||
|
sign = False
|
||||||
|
|
||||||
|
for key in decoded:
|
||||||
|
val = decoded[key]
|
||||||
|
|
||||||
|
key = key.lower()
|
||||||
|
|
||||||
|
if key == 'type':
|
||||||
|
block.setType(val)
|
||||||
|
elif key in ['body', 'content']:
|
||||||
|
block.setContent(val)
|
||||||
|
elif key == 'parent':
|
||||||
|
block.setParent(val)
|
||||||
|
elif key == 'sign':
|
||||||
|
sign = (str(val).lower() == 'true')
|
||||||
|
|
||||||
|
hash = block.save(sign = sign)
|
||||||
|
|
||||||
|
if not hash is False:
|
||||||
|
response['success'] = true
|
||||||
|
response['hash'] = hash
|
||||||
|
response['reason'] = 'Successfully wrote block to file'
|
||||||
|
else:
|
||||||
|
response['reason'] = 'Faield to save the block'
|
||||||
|
except Exception as e:
|
||||||
|
logger.debug('insertBlock api request failed', error = e)
|
||||||
|
|
||||||
|
resp = Response(json.dumps(response))
|
||||||
|
elif action in callbacks['private']:
|
||||||
|
resp = Response(str(getCallback(action, scope = 'private')(request)))
|
||||||
else:
|
else:
|
||||||
resp = Response('(O_o) Dude what? (invalid command)')
|
resp = Response('(O_o) Dude what? (invalid command)')
|
||||||
endTime = math.floor(time.time())
|
endTime = math.floor(time.time())
|
||||||
@ -258,6 +299,8 @@ class API:
|
|||||||
peers = self._core.listPeers(getPow=True)
|
peers = self._core.listPeers(getPow=True)
|
||||||
response = ','.join(peers)
|
response = ','.join(peers)
|
||||||
resp = Response(response)
|
resp = Response(response)
|
||||||
|
elif action in callbacks['public']:
|
||||||
|
resp = Response(str(getCallback(action, scope = 'public')(request)))
|
||||||
else:
|
else:
|
||||||
resp = Response("")
|
resp = Response("")
|
||||||
|
|
||||||
@ -328,3 +371,31 @@ class API:
|
|||||||
# we exit rather than abort to avoid fingerprinting
|
# we exit rather than abort to avoid fingerprinting
|
||||||
logger.debug('Avoiding fingerprinting, exiting...')
|
logger.debug('Avoiding fingerprinting, exiting...')
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
def setCallback(action, callback, scope = 'public'):
|
||||||
|
if not scope in callbacks:
|
||||||
|
return False
|
||||||
|
|
||||||
|
callbacks[scope][action] = callback
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def removeCallback(action, scope = 'public'):
|
||||||
|
if (not scope in callbacks) or (not action in callbacks[scope]):
|
||||||
|
return False
|
||||||
|
|
||||||
|
del callbacks[scope][action]
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def getCallback(action, scope = 'public'):
|
||||||
|
if (not scope in callbacks) or (not action in callbacks[scope]):
|
||||||
|
return None
|
||||||
|
|
||||||
|
return callbacks[scope][action]
|
||||||
|
|
||||||
|
def getCallbacks(scope = None):
|
||||||
|
if (not scope is None) and (scope in callbacks):
|
||||||
|
return callbacks[scope]
|
||||||
|
|
||||||
|
return callbacks
|
||||||
|
@ -91,8 +91,6 @@ class Onionr:
|
|||||||
self.onionrCore = core.Core()
|
self.onionrCore = core.Core()
|
||||||
self.onionrUtils = OnionrUtils(self.onionrCore)
|
self.onionrUtils = OnionrUtils(self.onionrCore)
|
||||||
|
|
||||||
self.userOS = platform.system()
|
|
||||||
|
|
||||||
# Handle commands
|
# Handle commands
|
||||||
|
|
||||||
self.debug = False # Whole application debugging
|
self.debug = False # Whole application debugging
|
||||||
@ -542,7 +540,7 @@ class Onionr:
|
|||||||
subprocess.Popen([communicatorDaemon, "run", str(net.socksPort)])
|
subprocess.Popen([communicatorDaemon, "run", str(net.socksPort)])
|
||||||
logger.debug('Started communicator')
|
logger.debug('Started communicator')
|
||||||
events.event('daemon_start', onionr = self)
|
events.event('daemon_start', onionr = self)
|
||||||
api.API(self.debug)
|
self.api = api.API(self.debug)
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -38,7 +38,6 @@ class Block:
|
|||||||
self.btype = type
|
self.btype = type
|
||||||
self.bcontent = content
|
self.bcontent = content
|
||||||
|
|
||||||
|
|
||||||
# initialize variables
|
# initialize variables
|
||||||
self.valid = True
|
self.valid = True
|
||||||
self.raw = None
|
self.raw = None
|
||||||
@ -72,7 +71,9 @@ class Block:
|
|||||||
# logic
|
# logic
|
||||||
|
|
||||||
def decrypt(self, anonymous = True, encodedData = True):
|
def decrypt(self, anonymous = True, encodedData = True):
|
||||||
'''Decrypt a block, loading decrypted data into their vars'''
|
'''
|
||||||
|
Decrypt a block, loading decrypted data into their vars
|
||||||
|
'''
|
||||||
if self.decrypted:
|
if self.decrypted:
|
||||||
return True
|
return True
|
||||||
retData = False
|
retData = False
|
||||||
@ -102,7 +103,9 @@ class Block:
|
|||||||
return retData
|
return retData
|
||||||
|
|
||||||
def verifySig(self):
|
def verifySig(self):
|
||||||
'''Verify if a block's signature is signed by its claimed signer'''
|
'''
|
||||||
|
Verify if a block's signature is signed by its claimed signer
|
||||||
|
'''
|
||||||
core = self.getCore()
|
core = self.getCore()
|
||||||
|
|
||||||
if core._crypto.edVerify(data=self.signedData, key=self.signer, sig=self.signature, encodedData=True):
|
if core._crypto.edVerify(data=self.signedData, key=self.signer, sig=self.signature, encodedData=True):
|
||||||
@ -227,11 +230,13 @@ class Block:
|
|||||||
else:
|
else:
|
||||||
self.hash = self.getCore().insertBlock(self.getContent(), header = self.getType(), sign = sign)
|
self.hash = self.getCore().insertBlock(self.getContent(), header = self.getType(), sign = sign)
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
return self.getHash()
|
return self.getHash()
|
||||||
else:
|
else:
|
||||||
logger.warn('Not writing block; it is invalid.')
|
logger.warn('Not writing block; it is invalid.')
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error('Failed to save block.', error = e, timestamp = False)
|
logger.error('Failed to save block.', error = e, timestamp = False)
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# getters
|
# getters
|
||||||
|
@ -130,6 +130,22 @@ class CommandAPI:
|
|||||||
def get_commands(self):
|
def get_commands(self):
|
||||||
return self.pluginapi.get_onionr().getCommands()
|
return self.pluginapi.get_onionr().getCommands()
|
||||||
|
|
||||||
|
class WebAPI:
|
||||||
|
def __init__(self, pluginapi):
|
||||||
|
self.pluginapi = pluginapi
|
||||||
|
|
||||||
|
def register_callback(self, action, callback, scope = 'public'):
|
||||||
|
return self.pluginapi.get_onionr().api.setCallback(action, callback, scope = scope)
|
||||||
|
|
||||||
|
def unregister_callback(self, action, scope = 'public'):
|
||||||
|
return self.pluginapi.get_onionr().api.removeCallback(action, scope = scope)
|
||||||
|
|
||||||
|
def get_callback(self, action, scope = 'public'):
|
||||||
|
return self.pluginapi.get_onionr().api.getCallback(action, scope= scope)
|
||||||
|
|
||||||
|
def get_callbacks(self, scope = None):
|
||||||
|
return self.pluginapi.get_onionr().api.getCallbacks(scope = scope)
|
||||||
|
|
||||||
class pluginapi:
|
class pluginapi:
|
||||||
def __init__(self, onionr, data):
|
def __init__(self, onionr, data):
|
||||||
self.onionr = onionr
|
self.onionr = onionr
|
||||||
@ -142,6 +158,7 @@ class pluginapi:
|
|||||||
self.daemon = DaemonAPI(self)
|
self.daemon = DaemonAPI(self)
|
||||||
self.plugins = PluginAPI(self)
|
self.plugins = PluginAPI(self)
|
||||||
self.commands = CommandAPI(self)
|
self.commands = CommandAPI(self)
|
||||||
|
self.web = WebAPI(self)
|
||||||
|
|
||||||
def get_onionr(self):
|
def get_onionr(self):
|
||||||
return self.onionr
|
return self.onionr
|
||||||
@ -167,5 +184,8 @@ class pluginapi:
|
|||||||
def get_commandapi(self):
|
def get_commandapi(self):
|
||||||
return self.commands
|
return self.commands
|
||||||
|
|
||||||
|
def get_webapi(self):
|
||||||
|
return self.web
|
||||||
|
|
||||||
def is_development_mode(self):
|
def is_development_mode(self):
|
||||||
return self.get_onionr()._developmentMode
|
return self.get_onionr()._developmentMode
|
||||||
|
Loading…
Reference in New Issue
Block a user