2017-12-26 07:25:29 +00:00
|
|
|
'''
|
|
|
|
Onionr - P2P Microblogging Platform & Social network
|
|
|
|
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-11 09:04:12 +00:00
|
|
|
import configparser, sys, random, threading, hmac, hashlib, base64, time, math, gnupg, os
|
2018-01-02 08:43:29 +00:00
|
|
|
|
|
|
|
from core import Core
|
2017-12-27 01:13:19 +00:00
|
|
|
'''
|
|
|
|
Main API
|
|
|
|
'''
|
2017-12-26 07:25:29 +00:00
|
|
|
class API:
|
2018-01-02 08:43:29 +00:00
|
|
|
def validateToken(self, token):
|
|
|
|
if self.clientToken != token:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
2017-12-27 01:13:19 +00:00
|
|
|
def __init__(self, config, debug):
|
2018-01-10 03:50:38 +00:00
|
|
|
if os.path.exists('dev-enabled'):
|
|
|
|
print('DEVELOPMENT MODE ENABLED (THIS IS LESS SECURE!)')
|
|
|
|
self._developmentMode = True
|
|
|
|
else:
|
|
|
|
self._developmentMode = False
|
2017-12-27 01:13:19 +00:00
|
|
|
self.config = config
|
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()
|
2017-12-27 05:00:02 +00:00
|
|
|
app = flask.Flask(__name__)
|
2017-12-27 01:13:19 +00:00
|
|
|
bindPort = int(self.config['CLIENT']['PORT'])
|
2018-01-02 08:43:29 +00:00
|
|
|
self.bindPort = bindPort
|
|
|
|
self.clientToken = self.config['CLIENT']['CLIENT HMAC']
|
2018-01-12 09:06:24 +00:00
|
|
|
print(self.clientToken)
|
2017-12-27 01:13:19 +00:00
|
|
|
|
|
|
|
if not debug:
|
|
|
|
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:
|
|
|
|
self.host = '127.0.0.1'
|
|
|
|
|
2017-12-28 00:18:00 +00:00
|
|
|
@app.before_request
|
|
|
|
def beforeReq():
|
2018-01-05 09:16:21 +00:00
|
|
|
self.requestFailed = False
|
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'] = '*'
|
|
|
|
else:
|
|
|
|
resp.headers['server'] = 'Onionr'
|
2017-12-27 05:00:02 +00:00
|
|
|
resp.headers['content-type'] = 'text/plain'
|
|
|
|
resp.headers["Content-Security-Policy"] = "default-src 'none'"
|
|
|
|
resp.headers['x-frame-options'] = 'deny'
|
|
|
|
return resp
|
|
|
|
|
2018-01-02 08:43:29 +00:00
|
|
|
@app.route('/client/')
|
|
|
|
def private_handler():
|
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')
|
|
|
|
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)
|
|
|
|
elif action == 'stats':
|
2018-01-05 09:16:21 +00:00
|
|
|
resp = Response('something')
|
|
|
|
elif action == 'init':
|
|
|
|
# generate PGP key
|
2018-01-06 08:51:26 +00:00
|
|
|
self._core.generateMainPGP()
|
2018-01-05 09:16:21 +00:00
|
|
|
pass
|
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
|
|
|
|
if elapsed < self._privateDelayTime:
|
|
|
|
time.sleep(self._privateDelayTime - elapsed)
|
2017-12-27 05:00:02 +00:00
|
|
|
return resp
|
|
|
|
|
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-10 03:50:38 +00:00
|
|
|
if action == 'firstConnect':
|
2018-01-12 09:06:24 +00:00
|
|
|
|
2018-01-10 03:50:38 +00:00
|
|
|
pass
|
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-01-02 08:43:29 +00:00
|
|
|
#resp.headers = getHeaders(resp)
|
|
|
|
return resp
|
|
|
|
@app.errorhandler(403)
|
|
|
|
def authFail(err):
|
2018-01-05 09:16:21 +00:00
|
|
|
self.requestFailed = True
|
|
|
|
resp = Response("403")
|
2017-12-27 05:00:02 +00:00
|
|
|
return resp
|
2017-12-27 01:13:19 +00:00
|
|
|
|
|
|
|
print('Starting client on ' + self.host + ':' + str(bindPort))
|
2018-01-02 08:43:29 +00:00
|
|
|
print('Client token:', self.clientToken)
|
2017-12-27 01:13:19 +00:00
|
|
|
|
2017-12-28 00:18:00 +00:00
|
|
|
app.run(host=self.host, port=bindPort, debug=True, threaded=True)
|
2017-12-27 01:13:19 +00:00
|
|
|
|
2018-01-05 09:16:21 +00:00
|
|
|
def validateHost(self, hostType):
|
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':
|
|
|
|
if not request.host.startswith('127'):
|
|
|
|
abort(403)
|
|
|
|
elif hostType == 'public':
|
|
|
|
if not request.host.endswith('onion') and not request.hosst.endswith('i2p'):
|
|
|
|
abort(403)
|
2017-12-27 01:13:19 +00:00
|
|
|
# Validate x-requested-with, to protect against CSRF/metadata leaks
|
2018-01-12 09:06:24 +00:00
|
|
|
if not self._developmentMode:
|
2018-01-10 03:50:38 +00:00
|
|
|
try:
|
|
|
|
request.headers['x-requested-with']
|
|
|
|
except:
|
|
|
|
# we exit rather than abort to avoid fingerprinting
|
|
|
|
sys.exit(1)
|