2017-12-26 07:25:29 +00:00
|
|
|
'''
|
|
|
|
Onionr - P2P Microblogging Platform & Social network
|
2018-01-14 08:48:23 +00:00
|
|
|
|
|
|
|
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
|
2018-01-02 08:43:29 +00:00
|
|
|
from flask import request, Response, abort
|
2018-01-14 08:48:23 +00:00
|
|
|
from multiprocessing import Process
|
2018-05-01 07:25:31 +00:00
|
|
|
from gevent.wsgi import WSGIServer
|
2018-02-23 01:58:36 +00:00
|
|
|
import sys, random, threading, hmac, hashlib, base64, time, math, os, logger, config
|
2018-01-02 08:43:29 +00:00
|
|
|
|
|
|
|
from core import Core
|
2018-02-07 09:04:58 +00:00
|
|
|
import onionrutils, onionrcrypto
|
2017-12-26 07:25:29 +00:00
|
|
|
class API:
|
2018-02-04 03:44:29 +00:00
|
|
|
'''
|
|
|
|
Main HTTP API (Flask)
|
|
|
|
'''
|
2018-01-02 08:43:29 +00:00
|
|
|
def validateToken(self, token):
|
2018-01-14 08:48:23 +00:00
|
|
|
'''
|
2018-02-04 03:44:29 +00:00
|
|
|
Validate that the client token (hmac) matches the given token
|
2018-01-14 08:48:23 +00:00
|
|
|
'''
|
2018-04-18 03:43:33 +00:00
|
|
|
try:
|
|
|
|
if not hmac.compare_digest(self.clientToken, token):
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
except TypeError:
|
2018-01-02 08:43:29 +00:00
|
|
|
return False
|
|
|
|
|
2018-02-23 01:58:36 +00:00
|
|
|
def __init__(self, debug):
|
2018-02-04 03:44:29 +00:00
|
|
|
'''
|
|
|
|
Initialize the api server, preping variables for later use
|
2018-01-14 08:48:23 +00:00
|
|
|
|
2018-02-04 03:44:29 +00:00
|
|
|
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-01-14 08:48:23 +00:00
|
|
|
'''
|
2018-02-23 01:58:36 +00:00
|
|
|
|
|
|
|
config.reload()
|
2018-05-11 03:19:48 +00:00
|
|
|
|
2018-02-23 01:58:36 +00:00
|
|
|
if config.get('devmode', 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
|
2018-01-05 09:16:21 +00:00
|
|
|
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-03-03 07:26:02 +00:00
|
|
|
bindPort = int(config.get('client')['port'])
|
2018-01-02 08:43:29 +00:00
|
|
|
self.bindPort = bindPort
|
2018-03-03 07:26:02 +00:00
|
|
|
self.clientToken = config.get('client')['client_hmac']
|
2018-04-16 02:22:19 +00:00
|
|
|
self.timeBypassToken = base64.b16encode(os.urandom(32)).decode()
|
|
|
|
|
2018-05-19 21:32:21 +00:00
|
|
|
self.i2pEnabled = config.get('i2p')['host']
|
|
|
|
|
2018-04-23 01:43:17 +00:00
|
|
|
self.mimeType = 'text/plain'
|
|
|
|
|
2018-04-16 02:22:19 +00:00
|
|
|
with open('data/time-bypass.txt', 'w') as bypass:
|
|
|
|
bypass.write(self.timeBypassToken)
|
|
|
|
|
2018-02-22 08:41:05 +00:00
|
|
|
if not os.environ.get("WERKZEUG_RUN_MAIN") == "true":
|
2018-04-18 03:43:33 +00:00
|
|
|
logger.debug('Your web password (KEEP SECRET): ' + logger.colors.underline + self.clientToken)
|
2017-12-27 01:13:19 +00:00
|
|
|
|
2018-01-14 08:48:23 +00:00
|
|
|
if not debug and not self._developmentMode:
|
2017-12-27 01:13:19 +00:00
|
|
|
hostNums = [random.randint(1, 255), random.randint(1, 255), random.randint(1, 255)]
|
|
|
|
self.host = '127.' + str(hostNums[0]) + '.' + str(hostNums[1]) + '.' + str(hostNums[2])
|
|
|
|
else:
|
2018-01-26 07:22:48 +00:00
|
|
|
self.host = '127.0.0.1'
|
2018-01-14 08:48:23 +00:00
|
|
|
hostFile = open('data/host.txt', 'w')
|
|
|
|
hostFile.write(self.host)
|
|
|
|
hostFile.close()
|
2017-12-27 01:13:19 +00:00
|
|
|
|
2017-12-28 00:18:00 +00:00
|
|
|
@app.before_request
|
|
|
|
def beforeReq():
|
2018-01-14 08:48:23 +00:00
|
|
|
'''
|
2018-02-04 03:44:29 +00:00
|
|
|
Simply define the request as not having yet failed, before every request.
|
2018-01-14 08:48:23 +00:00
|
|
|
'''
|
2018-01-05 09:16:21 +00:00
|
|
|
self.requestFailed = False
|
2018-02-04 03:44:29 +00:00
|
|
|
|
2017-12-28 00:18:00 +00:00
|
|
|
return
|
|
|
|
|
2017-12-27 05:00:02 +00:00
|
|
|
@app.after_request
|
|
|
|
def afterReq(resp):
|
2018-01-05 09:16:21 +00:00
|
|
|
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
|
|
|
|
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
|
|
|
|
if self.mimeType != 'text/plain':
|
|
|
|
self.mimeType = 'text/plain'
|
2018-02-04 03:44:29 +00:00
|
|
|
|
2017-12-27 05:00:02 +00:00
|
|
|
return resp
|
2018-01-26 07:22:48 +00:00
|
|
|
|
2018-01-02 08:43:29 +00:00
|
|
|
@app.route('/client/')
|
|
|
|
def private_handler():
|
2018-04-16 02:22:19 +00:00
|
|
|
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 = ''
|
2018-01-05 09:16:21 +00:00
|
|
|
startTime = math.floor(time.time())
|
2018-01-02 08:43:29 +00:00
|
|
|
# we should keep a hash DB of requests (with hmac) to prevent replays
|
|
|
|
action = request.args.get('action')
|
|
|
|
#if not self.debug:
|
|
|
|
token = request.args.get('token')
|
2018-04-16 02:22:19 +00:00
|
|
|
|
2018-01-02 08:43:29 +00:00
|
|
|
if not self.validateToken(token):
|
|
|
|
abort(403)
|
2018-01-05 09:16:21 +00:00
|
|
|
self.validateHost('private')
|
2018-01-02 08:43:29 +00:00
|
|
|
if action == 'hello':
|
|
|
|
resp = Response('Hello, World! ' + request.host)
|
2018-01-14 08:48:23 +00:00
|
|
|
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()
|
2018-01-14 08:48:23 +00:00
|
|
|
resp = Response('Goodbye')
|
2018-04-16 02:22:19 +00:00
|
|
|
elif action == 'ping':
|
|
|
|
resp = Response('pong')
|
2018-01-02 08:43:29 +00:00
|
|
|
elif action == 'stats':
|
2018-01-26 07:22:48 +00:00
|
|
|
resp = Response('me_irl')
|
2018-05-01 07:01:57 +00:00
|
|
|
raise Exception
|
2018-04-23 01:43:17 +00:00
|
|
|
elif action == 'site':
|
|
|
|
block = data
|
|
|
|
siteData = self._core.getData(data)
|
|
|
|
response = 'not found'
|
|
|
|
if siteData != '' and siteData != False:
|
|
|
|
self.mimeType = 'text/html'
|
2018-04-23 02:58:24 +00:00
|
|
|
response = siteData.split(b'-', 2)[-1]
|
2018-04-23 01:43:17 +00:00
|
|
|
resp = Response(response)
|
2018-01-02 08:43:29 +00:00
|
|
|
else:
|
|
|
|
resp = Response('(O_o) Dude what? (invalid command)')
|
2018-01-05 09:16:21 +00:00
|
|
|
endTime = math.floor(time.time())
|
|
|
|
elapsed = endTime - startTime
|
2018-04-16 02:22:19 +00:00
|
|
|
|
|
|
|
# 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)
|
2018-02-04 03:44:29 +00:00
|
|
|
|
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
|
|
|
|
2018-01-05 09:16:21 +00:00
|
|
|
@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-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 == 'getHMAC':
|
2018-02-08 22:58:39 +00:00
|
|
|
resp = Response(self._crypto.generateSymmetric())
|
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':
|
2018-05-19 21:32:21 +00:00
|
|
|
resp = Response('\n'.join(self._core.getBlockList()))
|
2018-05-03 03:22:07 +00:00
|
|
|
elif action == 'directMessage':
|
|
|
|
resp = Response(self._core.handle_direct_connection(data))
|
2018-04-19 01:17:47 +00:00
|
|
|
elif action == 'announce':
|
|
|
|
if data != '':
|
|
|
|
# TODO: require POW for this
|
|
|
|
if self._core.addAddress(data):
|
|
|
|
resp = Response('Success')
|
|
|
|
else:
|
|
|
|
resp = Response('')
|
|
|
|
else:
|
|
|
|
resp = Response('')
|
2018-01-25 22:39:09 +00:00
|
|
|
# setData should be something the communicator initiates, not this api
|
2018-01-21 09:12:41 +00:00
|
|
|
elif action == 'getData':
|
2018-04-23 02:10:09 +00:00
|
|
|
if self._utils.validateHash(data):
|
|
|
|
if not os.path.exists('data/blocks/' + data + '.db'):
|
2018-04-23 04:17:16 +00:00
|
|
|
try:
|
|
|
|
resp = base64.b64encode(self._core.getData(data))
|
|
|
|
except TypeError:
|
|
|
|
resp = ""
|
2018-01-27 21:49:48 +00:00
|
|
|
if resp == False:
|
|
|
|
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-01-27 21:49:48 +00:00
|
|
|
else:
|
|
|
|
resp = Response("")
|
2018-01-18 07:49:13 +00:00
|
|
|
|
|
|
|
return resp
|
2018-01-05 09:16:21 +00:00
|
|
|
|
2017-12-27 05:00:02 +00:00
|
|
|
@app.errorhandler(404)
|
|
|
|
def notfound(err):
|
2018-01-05 09:16:21 +00:00
|
|
|
self.requestFailed = True
|
|
|
|
resp = Response("")
|
2018-02-04 03:44:29 +00:00
|
|
|
|
2018-01-02 08:43:29 +00:00
|
|
|
return resp
|
2018-02-04 03:44:29 +00:00
|
|
|
|
2018-01-02 08:43:29 +00:00
|
|
|
@app.errorhandler(403)
|
|
|
|
def authFail(err):
|
2018-01-05 09:16:21 +00:00
|
|
|
self.requestFailed = True
|
|
|
|
resp = Response("403")
|
2018-02-04 03:44:29 +00:00
|
|
|
|
2017-12-27 05:00:02 +00:00
|
|
|
return resp
|
2018-02-04 03:44:29 +00:00
|
|
|
|
2018-01-22 02:49:11 +00:00
|
|
|
@app.errorhandler(401)
|
|
|
|
def clientError(err):
|
|
|
|
self.requestFailed = True
|
|
|
|
resp = Response("Invalid request")
|
2018-02-04 03:44:29 +00:00
|
|
|
|
2018-01-22 02:49:11 +00:00
|
|
|
return resp
|
2018-02-21 02:44:56 +00:00
|
|
|
if not os.environ.get("WERKZEUG_RUN_MAIN") == "true":
|
2018-04-19 01:47:35 +00:00
|
|
|
logger.info('Starting client on ' + self.host + ':' + str(bindPort) + '...', timestamp=True)
|
2017-12-27 01:13:19 +00:00
|
|
|
|
2018-03-03 04:19:01 +00:00
|
|
|
try:
|
2018-05-03 01:14:00 +00:00
|
|
|
self.http_server = WSGIServer((self.host, bindPort), app)
|
|
|
|
self.http_server.serve_forever()
|
2018-05-01 07:25:31 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
|
|
|
#app.run(host=self.host, port=bindPort, debug=False, threaded=True)
|
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
|
|
|
|
2018-01-05 09:16:21 +00:00
|
|
|
def validateHost(self, hostType):
|
2018-02-04 03:44:29 +00:00
|
|
|
'''
|
|
|
|
Validate various features of the request including:
|
|
|
|
|
2018-01-14 08:48:23 +00:00
|
|
|
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?
|
2018-01-14 08:48:23 +00:00
|
|
|
'''
|
2017-12-27 05:00:02 +00:00
|
|
|
if self.debug:
|
|
|
|
return
|
2017-12-27 01:13:19 +00:00
|
|
|
# Validate host header, to protect against DNS rebinding attacks
|
2018-01-02 08:43:29 +00:00
|
|
|
host = self.host
|
2018-01-05 09:16:21 +00:00
|
|
|
if hostType == 'private':
|
2018-01-27 00:10:38 +00:00
|
|
|
if not request.host.startswith('127') and not self._utils.checkIsIP(request.host):
|
2018-01-05 09:16:21 +00:00
|
|
|
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'):
|
2018-01-05 09:16:21 +00:00
|
|
|
abort(403)
|
2017-12-27 01:13:19 +00:00
|
|
|
# Validate x-requested-with, to protect against CSRF/metadata leaks
|
2018-05-19 21:32:21 +00:00
|
|
|
|
|
|
|
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)
|