Onionr/onionr/api.py

586 lines
22 KiB
Python
Raw Normal View History

2017-12-26 07:25:29 +00:00
'''
Onionr - P2P Microblogging Platform & Social network
This file handles all incoming http requests to the client, using Flask
'''
'''
2017-12-26 07:25:29 +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
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
2017-12-27 05:00:02 +00:00
import flask
from flask import request, Response, abort, send_from_directory
from multiprocessing import Process
from gevent.pywsgi import WSGIServer
2018-08-04 03:47:56 +00:00
import sys, random, threading, hmac, hashlib, base64, time, math, os, json
from core import Core
2018-05-19 22:11:51 +00:00
from onionrblockapi import Block
2018-08-13 03:48:33 +00:00
import onionrutils, onionrexceptions, onionrcrypto, blockimporter, onionrevents as events, logger, config
2018-05-19 22:11:51 +00:00
2017-12-26 07:25:29 +00:00
class API:
'''
Main HTTP API (Flask)
'''
2018-07-30 00:37:12 +00:00
2018-08-04 02:52:45 +00:00
callbacks = {'public' : {}, 'private' : {}}
2018-07-30 00:37:12 +00:00
def validateToken(self, token):
'''
Validate that the client token matches the given token
'''
try:
if not hmac.compare_digest(self.clientToken, token):
return False
else:
return True
except TypeError:
return False
2018-08-04 02:52:45 +00:00
def guessMime(path):
'''
Guesses the mime type from the input filename
'''
mimetypes = {
'html' : 'text/html',
'js' : 'application/javascript',
'css' : 'text/css',
'png' : 'image/png',
'jpg' : 'image/jpeg'
}
for mimetype in mimetypes:
logger.debug(path + ' endswith .' + mimetype + '?')
if path.endswith('.%s' % mimetype):
logger.debug('- True!')
return mimetypes[mimetype]
else:
logger.debug('- no')
logger.debug('%s not in %s' % (path, mimetypes))
return 'text/plain'
2018-02-23 01:58:36 +00:00
def __init__(self, debug):
'''
Initialize the api server, preping variables for later use
This initilization defines all of the API entry points and handlers for the endpoints and errors
This also saves the used host (random localhost IP address) to the data folder in host.txt
'''
2018-02-23 01:58:36 +00:00
config.reload()
2018-05-19 22:11:51 +00:00
2018-06-14 04:17:58 +00:00
if config.get('dev_mode', True):
2018-01-10 03:50:38 +00:00
self._developmentMode = True
2018-01-26 07:22:48 +00:00
logger.set_level(logger.LEVEL_DEBUG)
2018-01-10 03:50:38 +00:00
else:
self._developmentMode = False
2018-01-26 07:22:48 +00:00
logger.set_level(logger.LEVEL_INFO)
2017-12-27 05:00:02 +00:00
self.debug = debug
self._privateDelayTime = 3
self._core = Core()
2018-02-07 09:04:58 +00:00
self._crypto = onionrcrypto.OnionrCrypto(self._core)
2018-01-26 06:28:11 +00:00
self._utils = onionrutils.OnionrUtils(self._core)
2017-12-27 05:00:02 +00:00
app = flask.Flask(__name__)
2018-06-14 04:17:58 +00:00
bindPort = int(config.get('client.port', 59496))
self.bindPort = bindPort
2018-06-14 04:17:58 +00:00
self.clientToken = config.get('client.hmac')
self.timeBypassToken = base64.b16encode(os.urandom(32)).decode()
2018-06-14 04:17:58 +00:00
self.i2pEnabled = config.get('i2p.host', False)
2018-04-23 01:43:17 +00:00
self.mimeType = 'text/plain'
2018-08-04 02:52:45 +00:00
self.overrideCSP = False
2018-04-23 01:43:17 +00:00
with open('data/time-bypass.txt', 'w') as bypass:
bypass.write(self.timeBypassToken)
if not debug and not self._developmentMode:
hostOctets = [127, random.randint(0x02, 0xFF), random.randint(0x02, 0xFF), random.randint(0x02, 0xFF)]
self.host = '.'.join(hostOctets)
else:
2018-01-26 07:22:48 +00:00
self.host = '127.0.0.1'
2018-06-14 04:17:58 +00:00
with open('data/host.txt', 'w') as file:
file.write(self.host)
@app.before_request
def beforeReq():
'''
Simply define the request as not having yet failed, before every request.
'''
self.requestFailed = False
return
2017-12-27 05:00:02 +00:00
@app.after_request
def afterReq(resp):
if not self.requestFailed:
resp.headers['Access-Control-Allow-Origin'] = '*'
2018-04-23 01:43:17 +00:00
#else:
# resp.headers['server'] = 'Onionr'
resp.headers['Content-Type'] = self.mimeType
2018-08-04 02:52:45 +00:00
if not self.overrideCSP:
resp.headers["Content-Security-Policy"] = "default-src 'none'; script-src 'none'; object-src 'none'; style-src data: 'unsafe-inline'; img-src data:; media-src 'none'; frame-src 'none'; font-src 'none'; connect-src 'none'"
2018-01-26 07:22:48 +00:00
resp.headers['X-Frame-Options'] = 'deny'
2018-01-28 01:53:24 +00:00
resp.headers['X-Content-Type-Options'] = "nosniff"
2018-04-23 01:43:17 +00:00
resp.headers['server'] = 'Onionr'
# reset to text/plain to help prevent browser attacks
2018-08-04 02:52:45 +00:00
self.mimeType = 'text/plain'
self.overrideCSP = False
2017-12-27 05:00:02 +00:00
return resp
2018-09-21 04:47:40 +00:00
2018-07-31 04:52:48 +00:00
@app.route('/www/private/<path:path>')
def www_private(path):
startTime = math.floor(time.time())
2018-07-31 04:41:32 +00:00
if request.args.get('timingToken') is None:
timingToken = ''
else:
timingToken = request.args.get('timingToken')
2018-07-31 04:41:32 +00:00
2018-07-31 04:52:48 +00:00
if not config.get("www.private.run", True):
2018-07-31 04:41:32 +00:00
abort(403)
2018-07-31 04:52:48 +00:00
self.validateHost('private')
endTime = math.floor(time.time())
elapsed = endTime - startTime
if not hmac.compare_digest(timingToken, self.timeBypassToken):
if elapsed < self._privateDelayTime:
time.sleep(self._privateDelayTime - elapsed)
return send_from_directory('static-data/www/private/', path)
@app.route('/www/public/<path:path>')
def www_public(path):
if not config.get("www.public.run", True):
abort(403)
self.validateHost('public')
return send_from_directory('static-data/www/public/', path)
2018-07-31 05:02:01 +00:00
@app.route('/ui/<path:path>')
2018-07-31 04:52:48 +00:00
def ui_private(path):
startTime = math.floor(time.time())
2018-08-04 02:52:45 +00:00
'''
2018-07-31 04:52:48 +00:00
if request.args.get('timingToken') is None:
timingToken = ''
else:
timingToken = request.args.get('timingToken')
2018-08-04 02:52:45 +00:00
'''
2018-07-31 04:52:48 +00:00
if not config.get("www.ui.run", True):
abort(403)
if config.get("www.ui.private", True):
2018-07-31 04:41:32 +00:00
self.validateHost('private')
2018-07-31 04:52:48 +00:00
else:
self.validateHost('public')
2018-07-31 04:41:32 +00:00
2018-08-04 02:52:45 +00:00
'''
endTime = math.floor(time.time())
elapsed = endTime - startTime
2018-07-31 04:41:32 +00:00
if not hmac.compare_digest(timingToken, self.timeBypassToken):
if elapsed < self._privateDelayTime:
time.sleep(self._privateDelayTime - elapsed)
2018-08-04 02:52:45 +00:00
'''
logger.debug('Serving %s' % path)
self.mimeType = API.guessMime(path)
self.overrideCSP = True
2018-07-31 04:41:32 +00:00
2018-08-04 02:52:45 +00:00
return send_from_directory('static-data/www/ui/dist/', path, mimetype = API.guessMime(path))
@app.route('/client/')
def private_handler():
if request.args.get('timingToken') is None:
timingToken = ''
else:
timingToken = request.args.get('timingToken')
2018-04-23 01:43:17 +00:00
data = request.args.get('data')
try:
data = data
except:
data = ''
startTime = math.floor(time.time())
action = request.args.get('action')
#if not self.debug:
token = request.args.get('token')
if not self.validateToken(token):
abort(403)
2018-07-31 04:41:32 +00:00
events.event('webapi_private', onionr = None, data = {'action' : action, 'data' : data, 'timingToken' : timingToken, 'token' : token})
self.validateHost('private')
if action == 'hello':
resp = Response('Hello, World! ' + request.host)
elif action == 'shutdown':
2018-05-02 06:50:29 +00:00
# request.environ.get('werkzeug.server.shutdown')()
2018-05-03 01:14:00 +00:00
self.http_server.stop()
resp = Response('Goodbye')
elif action == 'ping':
resp = Response('pong')
2018-07-30 00:37:12 +00:00
elif action == "insertBlock":
response = {'success' : False, 'reason' : 'An unknown error occurred'}
2018-08-04 03:47:56 +00:00
if not ((data is None) or (len(str(data).strip()) == 0)):
try:
decoded = json.loads(data)
2018-07-30 00:37:12 +00:00
2018-08-04 03:47:56 +00:00
block = Block()
2018-07-30 00:37:12 +00:00
2018-08-04 03:47:56 +00:00
sign = False
2018-07-30 00:37:12 +00:00
2018-08-04 03:47:56 +00:00
for key in decoded:
val = decoded[key]
2018-07-30 00:37:12 +00:00
2018-08-04 03:47:56 +00:00
key = key.lower()
2018-07-30 00:37:12 +00:00
2018-08-04 03:47:56 +00:00
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')
2018-07-30 00:37:12 +00:00
2018-08-04 03:47:56 +00:00
hash = block.save(sign = sign)
2018-07-30 00:37:12 +00:00
2018-08-04 03:47:56 +00:00
if not hash is False:
response['success'] = True
response['hash'] = hash
response['reason'] = 'Successfully wrote block to file'
else:
response['reason'] = 'Failed to save the block'
except Exception as e:
logger.warn('insertBlock api request failed', error = e)
logger.debug('Here\'s the request: %s' % data)
else:
response = {'success' : False, 'reason' : 'Missing `data` parameter.', 'blocks' : {}}
2018-07-30 00:37:12 +00:00
resp = Response(json.dumps(response))
2018-08-04 03:47:56 +00:00
elif action == 'searchBlocks':
response = {'success' : False, 'reason' : 'An unknown error occurred', 'blocks' : {}}
if not ((data is None) or (len(str(data).strip()) == 0)):
try:
decoded = json.loads(data)
type = None
signer = None
signed = None
parent = None
reverse = False
limit = None
for key in decoded:
val = decoded[key]
key = key.lower()
if key == 'type':
type = str(val)
elif key == 'signer':
if isinstance(val, list):
signer = val
else:
signer = str(val)
elif key == 'signed':
signed = (str(val).lower() == 'true')
elif key == 'parent':
parent = str(val)
elif key == 'reverse':
reverse = (str(val).lower() == 'true')
elif key == 'limit':
limit = 10000
if val is None:
val = limit
limit = min(limit, int(val))
blockObjects = Block.getBlocks(type = type, signer = signer, signed = signed, parent = parent, reverse = reverse, limit = limit)
logger.debug('%s results for query %s' % (len(blockObjects), decoded))
blocks = list()
for block in blockObjects:
blocks.append({
'hash' : block.getHash(),
'type' : block.getType(),
'content' : block.getContent(),
'signature' : block.getSignature(),
'signedData' : block.getSignedData(),
'signed' : block.isSigned(),
'valid' : block.isValid(),
'date' : (int(block.getDate().strftime("%s")) if not block.getDate() is None else None),
'parent' : (block.getParent().getHash() if not block.getParent() is None else None),
'metadata' : block.getMetadata(),
'header' : block.getHeader()
})
response['success'] = True
response['blocks'] = blocks
response['reason'] = 'Success'
except Exception as e:
logger.warn('searchBlock api request failed', error = e)
logger.debug('Here\'s the request: %s' % data)
else:
response = {'success' : False, 'reason' : 'Missing `data` parameter.', 'blocks' : {}}
resp = Response(json.dumps(response))
2018-07-31 04:41:32 +00:00
elif action in API.callbacks['private']:
2018-07-30 00:37:12 +00:00
resp = Response(str(getCallback(action, scope = 'private')(request)))
else:
resp = Response('(O_o) Dude what? (invalid command)')
endTime = math.floor(time.time())
elapsed = endTime - startTime
# if bypass token not used, delay response to prevent timing attacks
if not hmac.compare_digest(timingToken, self.timeBypassToken):
if elapsed < self._privateDelayTime:
time.sleep(self._privateDelayTime - elapsed)
2017-12-27 05:00:02 +00:00
return resp
2018-05-02 06:50:29 +00:00
2018-04-25 06:56:40 +00:00
@app.route('/')
def banner():
self.mimeType = 'text/html'
self.validateHost('public')
try:
with open('static-data/index.html', 'r') as html:
resp = Response(html.read())
except FileNotFoundError:
resp = Response("")
return resp
2017-12-27 05:00:02 +00:00
@app.route('/public/upload/', methods=['POST'])
def blockUpload():
self.validateHost('public')
resp = 'failure'
try:
data = request.form['block']
except KeyError:
logger.warn('No block specified for upload')
pass
else:
if sys.getsizeof(data) < 100000000:
2018-08-13 03:48:33 +00:00
try:
if blockimporter.importBlockFromData(data, self._core):
resp = 'success'
else:
logger.warn('Error encountered importing uploaded block')
except onionrexceptions.BlacklistedBlock:
logger.debug('uploaded block is blacklisted')
pass
resp = Response(resp)
return resp
@app.route('/public/announce/', methods=['POST'])
def acceptAnnounce():
self.validateHost('public')
resp = 'failure'
powHash = ''
randomData = ''
newNode = ''
ourAdder = self._core.hsAddress.encode()
try:
newNode = request.form['node'].encode()
except KeyError:
logger.warn('No block specified for upload')
pass
else:
try:
randomData = request.form['random']
randomData = base64.b64decode(randomData)
except KeyError:
logger.warn('No random data specified for upload')
else:
nodes = newNode + self._core.hsAddress.encode()
nodes = self._core._crypto.blake2bHash(nodes)
powHash = self._core._crypto.blake2bHash(randomData + nodes)
try:
powHash = powHash.decode()
except AttributeError:
pass
if powHash.startswith('0000'):
try:
newNode = newNode.decode()
except AttributeError:
pass
if self._core.addAddress(newNode):
resp = 'Success'
else:
logger.warn(newNode.decode() + ' failed to meet POW: ' + powHash)
resp = Response(resp)
return resp
@app.route('/public/')
def public_handler():
# Public means it is publicly network accessible
self.validateHost('public')
action = request.args.get('action')
2018-01-11 09:04:12 +00:00
requestingPeer = request.args.get('myID')
2018-01-18 07:49:13 +00:00
data = request.args.get('data')
2018-04-19 01:17:47 +00:00
try:
2018-04-23 01:43:17 +00:00
data = data
2018-04-19 01:17:47 +00:00
except:
data = ''
2018-07-31 04:41:32 +00:00
events.event('webapi_public', onionr = None, data = {'action' : action, 'data' : data, 'requestingPeer' : requestingPeer, 'request' : request})
2018-01-10 03:50:38 +00:00
if action == 'firstConnect':
pass
2018-01-18 07:49:13 +00:00
elif action == 'ping':
resp = Response("pong!")
2018-02-07 09:04:58 +00:00
elif action == 'getSymmetric':
2018-02-08 22:58:39 +00:00
resp = Response(self._crypto.generateSymmetric())
2018-01-26 06:28:11 +00:00
elif action == 'getDBHash':
resp = Response(self._utils.getBlockDBHash())
elif action == 'getBlockHashes':
resp = Response('\n'.join(self._core.getBlockList()))
2018-01-25 22:39:09 +00:00
# setData should be something the communicator initiates, not this api
elif action == 'getData':
2018-06-07 08:15:01 +00:00
resp = ''
2018-04-23 02:10:09 +00:00
if self._utils.validateHash(data):
2018-08-17 00:02:14 +00:00
if os.path.exists('data/blocks/' + data + '.dat'):
block = Block(hash=data.encode(), core=self._core)
2018-06-08 07:47:14 +00:00
resp = base64.b64encode(block.getRaw().encode()).decode()
2018-06-07 08:15:01 +00:00
if len(resp) == 0:
2018-01-27 21:49:48 +00:00
abort(404)
resp = ""
resp = Response(resp)
2018-02-27 21:23:49 +00:00
elif action == 'pex':
response = ','.join(self._core.listAdders())
if len(response) == 0:
response = 'none'
resp = Response(response)
2018-03-16 20:38:33 +00:00
elif action == 'kex':
2018-05-07 06:55:03 +00:00
peers = self._core.listPeers(getPow=True)
response = ','.join(peers)
2018-03-16 20:38:33 +00:00
resp = Response(response)
2018-07-31 04:41:32 +00:00
elif action in API.callbacks['public']:
2018-07-30 00:37:12 +00:00
resp = Response(str(getCallback(action, scope = 'public')(request)))
2018-01-27 21:49:48 +00:00
else:
resp = Response("")
2018-01-18 07:49:13 +00:00
return resp
2017-12-27 05:00:02 +00:00
@app.errorhandler(404)
def notfound(err):
self.requestFailed = True
resp = Response("")
return resp
@app.errorhandler(403)
def authFail(err):
self.requestFailed = True
resp = Response("403")
2017-12-27 05:00:02 +00:00
return resp
@app.errorhandler(401)
def clientError(err):
self.requestFailed = True
resp = Response("Invalid request")
return resp
if not os.environ.get("WERKZEUG_RUN_MAIN") == "true":
logger.info('Starting client on ' + self.host + ':' + str(bindPort) + '...', timestamp=False)
2018-03-03 04:19:01 +00:00
try:
while len(self._core.hsAddress) == 0:
self._core.refreshFirstStartVars()
time.sleep(0.5)
2018-05-03 01:14:00 +00:00
self.http_server = WSGIServer((self.host, bindPort), app)
self.http_server.serve_forever()
except KeyboardInterrupt:
pass
2018-03-03 04:19:01 +00:00
except Exception as e:
logger.error(str(e))
logger.fatal('Failed to start client on ' + self.host + ':' + str(bindPort) + ', exiting...')
exit(1)
2018-01-26 07:22:48 +00:00
def validateHost(self, hostType):
'''
Validate various features of the request including:
If private (/client/), is the host header local?
If public (/public/), is the host header onion or i2p?
2018-01-26 07:22:48 +00:00
Was X-Request-With used?
'''
2017-12-27 05:00:02 +00:00
if self.debug:
return
# Validate host header, to protect against DNS rebinding attacks
host = self.host
if hostType == 'private':
if not request.host.startswith('127') and not self._utils.checkIsIP(request.host):
abort(403)
elif hostType == 'public':
2018-01-18 07:49:13 +00:00
if not request.host.endswith('onion') and not request.host.endswith('i2p'):
abort(403)
# Validate x-requested-with, to protect against CSRF/metadata leaks
if not self.i2pEnabled and request.host.endswith('i2p'):
abort(403)
2018-01-12 09:06:24 +00:00
if not self._developmentMode:
2018-01-10 03:50:38 +00:00
try:
2018-01-26 07:22:48 +00:00
request.headers['X-Requested-With']
2018-01-10 03:50:38 +00:00
except:
# we exit rather than abort to avoid fingerprinting
2018-01-26 07:22:48 +00:00
logger.debug('Avoiding fingerprinting, exiting...')
sys.exit(1)
2018-07-30 00:37:12 +00:00
def setCallback(action, callback, scope = 'public'):
2018-07-31 04:41:32 +00:00
if not scope in API.callbacks:
2018-07-30 00:37:12 +00:00
return False
2018-07-31 04:41:32 +00:00
API.callbacks[scope][action] = callback
2018-07-30 00:37:12 +00:00
return True
def removeCallback(action, scope = 'public'):
2018-07-31 04:41:32 +00:00
if (not scope in API.callbacks) or (not action in API.callbacks[scope]):
2018-07-30 00:37:12 +00:00
return False
2018-07-31 04:41:32 +00:00
del API.callbacks[scope][action]
2018-07-30 00:37:12 +00:00
return True
def getCallback(action, scope = 'public'):
2018-07-31 04:41:32 +00:00
if (not scope in API.callbacks) or (not action in API.callbacks[scope]):
2018-07-30 00:37:12 +00:00
return None
2018-07-31 04:41:32 +00:00
return API.callbacks[scope][action]
2018-07-30 00:37:12 +00:00
def getCallbacks(scope = None):
2018-07-31 04:41:32 +00:00
if (not scope is None) and (scope in API.callbacks):
return API.callbacks[scope]
2018-07-30 00:37:12 +00:00
2018-07-31 04:41:32 +00:00
return API.callbacks