From 75c8abd9e08f36a92214926e81566bd5692a3e86 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Tue, 8 Jan 2019 01:25:56 -0600 Subject: [PATCH] check for oserror when binding local ips --- onionr/api.py | 14 ++++++++++++-- onionr/onionrblockapi.py | 10 ++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/onionr/api.py b/onionr/api.py index 48c70d37..fa7833fe 100755 --- a/onionr/api.py +++ b/onionr/api.py @@ -20,7 +20,7 @@ import flask, cgi from flask import request, Response, abort, send_from_directory from gevent.pywsgi import WSGIServer -import sys, random, threading, hmac, hashlib, base64, time, math, os, json +import sys, random, threading, hmac, hashlib, base64, time, math, os, json, socket import core from onionrblockapi import Block import onionrutils, onionrexceptions, onionrcrypto, blockimporter, onionrevents as events, logger, config, onionr @@ -47,9 +47,19 @@ def setBindIP(filePath): '''Set a random localhost IP to a specified file (intended for private or public API localhost IPs)''' hostOctets = [str(127), str(random.randint(0x02, 0xFF)), str(random.randint(0x02, 0xFF)), str(random.randint(0x02, 0xFF))] data = '.'.join(hostOctets) - + + # Try to bind IP. Some platforms like Mac block non normal 127.x.x.x + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + try: + s.bind((data, 0)) + except OSError: + logger.warn('Your platform appears to not support random local host addresses 127.x.x.x. Falling back to 127.0.0.1.') + data = '127.0.0.1' + s.close() + with open(filePath, 'w') as bindFile: bindFile.write(data) + return data class PublicAPI: diff --git a/onionr/onionrblockapi.py b/onionr/onionrblockapi.py index c76934a5..04dcf65a 100644 --- a/onionr/onionrblockapi.py +++ b/onionr/onionrblockapi.py @@ -737,7 +737,7 @@ class Block: return (blocks[-1], blocks) return blocks[-1] - def exists(hash): + def exists(bHash): ''' Checks if a block is saved to file or not @@ -751,7 +751,7 @@ class Block: ''' # no input data? scrap it. - if hash is None: + if bHash is None: return False ''' if type(hash) == Block: @@ -759,8 +759,10 @@ class Block: else: blockfile = onionrcore.Core().dataDir + 'blocks/%s.dat' % hash ''' - - ret = isinstance(onionrstorage.getData(onionrcore.Core(), hash.getHash()), type(None)) + if isinstance(bHash, Block): + bHash = bHash.getHash() + + ret = isinstance(onionrstorage.getData(onionrcore.Core(), bHash), type(None)) return not ret