Permalink
Browse files

Merge branch 'master' into p3

  • Loading branch information...
masonkatz committed Sep 6, 2017
2 parents 48c03e4 + 5742f10 commit cbb145bacce90ce9f4cb90e45844685b52b0f4e5
View
@@ -9,4 +9,6 @@
.DS_Store
roll-*.xml
.*.swp
<<<<<<< HEAD
*/3rdparty/
menu.cfg

This file was deleted.

Oops, something went wrong.
@@ -30,10 +30,9 @@ def save_file(content, location, filename):
def file_exists(local_file):
return os.path.isfile(local_file)
def lookup_file(params):
print ('http://%s/avalanche/lookup' % tracker_settings['TRACKER'])
def lookup_file(hashcode):
try:
res = requests.get('http://%s/avalanche/lookup' % (tracker_settings['TRACKER']), params=params)
res = requests.get('http://%s/avalanche/lookup/%s' % (tracker_settings['TRACKER'], hashcode))
except:
raise
return res
@@ -46,15 +45,23 @@ def get_file(peer, remote_file):
return res
def register_file(params):
def register_file(port, hashcode):
try:
res = requests.get('http://%s/avalanche/register' % (tracker_settings['TRACKER']), params=params)
res = requests.post('http://%s/avalanche/register/%s/%s' % (
tracker_settings['TRACKER'],
port,
hashcode)
)
except:
raise
def unregister_file(params):
def unregister_file(hashcode, params):
try:
res = requests.get('http://%s/avalanche/unregister' % (tracker_settings['TRACKER']), params=params)
res = requests.delete('http://%s/avalanche/unregister/hashcode/%s' % (
tracker_settings['TRACKER'],
hashcode),
params=params
)
except:
raise
@@ -63,49 +70,56 @@ def stream_it(response, content):
@app.route('/install/<path:path>/<filename>')
def get_file_locally(path, filename):
file_location = '%s/%s' % (tracker_settings['LOCAL_SAVE_LOCATION'], path)
local_file = '%s/%s' % (file_location, filename)
remote_file = '/install/%s/%s' % (path, filename)
im_the_requester = request.remote_addr == "127.0.0.1"
save_location = tracker_settings['LOCAL_SAVE_LOCATION']
file_location = '%s/%s' % (save_location, path)
local_file = '%s/%s' % (file_location, filename)
remote_file = '/install/%s/%s' % (path, filename)
im_the_requester = request.remote_addr == "127.0.0.1"
environment = tracker_settings['ENVIRONMENT']
# check if file is local
if im_the_requester and not file_exists(local_file):
params = {'port': tracker_settings['PORT'], 'hashcode': hashit(remote_file)}
res = lookup_file(params)
hashcode = hashit(remote_file)
port = tracker_settings['PORT']
params = {'port': port, 'hashcode': hashcode}
res = lookup_file(hashcode)
payload = res.json()
if tracker_settings['ENVIRONMENT'] == 'regular' and res.status_code == 200 and payload['success'] and payload['peers']:
successful = res.status_code == 200 and payload['success']
if environment == 'regular' and successful and payload['peers']:
for peer in payload['peers']:
try:
peer_res = get_file(peer, remote_file)
if peer_res.status_code == 200:
save_file(peer_res.content, '%s/%s/' % (tracker_settings['LOCAL_SAVE_LOCATION'], path), filename)
if tracker_settings['ENVIRONMENT'] == 'regular':
register_file(params)
save_file(peer_res.content, '%s/%s/' % (save_location, path), filename)
if environment == 'regular':
register_file(port, hashcode)
break
else:
unregister_params = params.copy()
unregister_params["peer"] = peer.split(":")[0]
unregister_file(unregister_params);
unregister_file(hashcode, unregister_params);
except:
unregister_params = params.copy()
unregister_params["peer"] = peer.split(":")[0]
unregister_file(unregister_params);
unregister_file(hashcode, unregister_params);
else:
# if no peers worked, use the frontend
tracker_res = requests.get('http://%s%s' % (tracker_settings['TRACKER'], remote_file))
if tracker_res.status_code == 200:
save_file(tracker_res.content, '%s/%s/' % (tracker_settings['LOCAL_SAVE_LOCATION'], path), filename)
save_file(tracker_res.content, '%s/%s/' % (save_location, path), filename)
if tracker_settings['SAVE_FILES']:
register_file(params)
register_file(port, hashcode)
else:
tracker_res = requests.get('http://%s%s' % (tracker_settings['TRACKER'], remote_file))
if tracker_res.status_code == 200:
save_file(tracker_res.content, '%s/%s/' % (tracker_settings['LOCAL_SAVE_LOCATION'], path), filename)
save_file(tracker_res.content, '%s/%s/' % (save_location, path), filename)
if tracker_settings['SAVE_FILES']:
register_file(params)
register_file(port, hashcode)
if file_exists(local_file):
return send_from_directory(unquote(file_location), unquote(filename))
@@ -118,7 +132,7 @@ def running():
@app.route('/done')
def peerdone():
peerdone_res = requests.get('http://%s/avalanche/peerdone' % tracker_settings['TRACKER'])
peerdone_res = requests.delete('http://%s/avalanche/peerdone' % tracker_settings['TRACKER'])
@app.errorhandler(404)
def page_not_found(e):
@@ -10,37 +10,39 @@
app = Flask(__name__)
packages = {}
peers = {}
MAX_PEERS = 3
@app.route('/avalanche/lookup', methods=['GET'])
def lookup():
app.logger.debug("Requester : %s" % request.remote_addr )
app.logger.debug("Requesting Lookup: %s" % request.args )
res = {}
res['success'] = True
ipaddr = request.remote_addr
port = request.args.get('port') or 80
hashcode = request.args.get('hashcode')
#app.logger.debug("peers before: %s" % peers )
#app.logger.debug("packages before: %s" % packages )
packages = {}
peers = {}
MAX_PEERS = 3
@app.errorhandler(404)
def four_o_four(error=None):
error_message = error if type(error) is str else "File not found."
message = {
'status': 404,
'message': error_message
}
resp = jsonify(message)
resp.status_code = 404
return resp
@app.route('/avalanche/lookup/<hashcode>', methods=['GET'])
def lookup(hashcode):
res = {}
res['success'] = True
ipaddr = request.remote_addr
# check if hash exists
if(hashcode not in packages):
packages[hashcode] = []
# check if peer exists and is in our database
if(ipaddr not in peers):
hosts = [host['value'] for host in stack.api.Call('list.host.attr', ["attr=hostaddr"])]
if ipaddr in hosts:
peers[ipaddr] = {
'ready': False,
'port': port
}
else:
abort(404)
if ipaddr not in hosts:
return four_o_four("Host not managed by frontend")
# return list of peers with the request hash
res['peers'] = []
@@ -55,19 +57,16 @@ def lookup():
if len(res['peers']) == 10:
break
#app.logger.debug("peers after: %s" % peers )
#app.logger.debug("packages after: %s" % packages )
#app.logger.debug("response: %s" % res )
#app.logger.debug("\n\n\n\n\n")
return jsonify(res)
@app.route('/avalanche/register', methods=['GET'])
def register():
res = {}
res['success'] = True
ipaddr = request.remote_addr
port = request.args.get('port') or 80
hashcode = request.args.get('hashcode')
@app.route('/avalanche/register/<port>/<hashcode>', methods=['POST'])
def register(port=80, hashcode=None):
res = {}
res['success'] = True
ipaddr = request.remote_addr
if not hashcode:
return four_o_four("asdf")
# check if hash exists
if(hashcode not in packages):
@@ -81,7 +80,7 @@ def register():
'port': port
}
else:
abort(404)
return four_o_four()
# register file
packages[hashcode].append(ipaddr)
@@ -91,19 +90,28 @@ def register():
return jsonify(res)
@app.route('/avalanche/unregister', methods=['GET'])
def unregister():
@app.route('/avalanche/unregister/hashcode/<hashcode>', methods=['DELETE'])
def unregister(hashcode):
ipaddr = unquote(request.args['peer'])
res = {}
res['success'] = True
app.logger.debug("unquoted ip addr: %s", ipaddr)
hashcode = request.args['hashcode']
if ipaddr in packages[hashcode]:
if packages.has_key(ipaddr) and ipaddr in packages[hashcode]:
packages[hashcode].remove(ipaddr)
res['message'] = "'%s' was unregistered for hash: %s" % (ipaddr, hashcode)
else:
res['message'] = "'%s' was not registered for hash: %s" % (ipaddr, hashcode)
return ""
@app.route('/avalanche/peerdone', methods=['GET'])
return jsonify(res)
@app.route('/avalanche/peerdone', methods=['DELETE'])
def peerdone():
ipaddr = request.remote_addr
res = {}
res['success'] = True
for package in packages:
if ipaddr in packages[package]:
packages[package].remove(ipaddr)

0 comments on commit cbb145b

Please sign in to comment.