check for oserror when binding local ips

This commit is contained in:
Kevin Froman 2019-01-08 01:25:56 -06:00
parent 8c72242eaf
commit 75c8abd9e0
2 changed files with 18 additions and 6 deletions

View File

@ -20,7 +20,7 @@
import flask, cgi import flask, cgi
from flask import request, Response, abort, send_from_directory from flask import request, Response, abort, send_from_directory
from gevent.pywsgi import WSGIServer 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 import core
from onionrblockapi import Block from onionrblockapi import Block
import onionrutils, onionrexceptions, onionrcrypto, blockimporter, onionrevents as events, logger, config, onionr 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)''' '''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))] hostOctets = [str(127), str(random.randint(0x02, 0xFF)), str(random.randint(0x02, 0xFF)), str(random.randint(0x02, 0xFF))]
data = '.'.join(hostOctets) 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: with open(filePath, 'w') as bindFile:
bindFile.write(data) bindFile.write(data)
return data return data
class PublicAPI: class PublicAPI:

View File

@ -737,7 +737,7 @@ class Block:
return (blocks[-1], blocks) return (blocks[-1], blocks)
return blocks[-1] return blocks[-1]
def exists(hash): def exists(bHash):
''' '''
Checks if a block is saved to file or not Checks if a block is saved to file or not
@ -751,7 +751,7 @@ class Block:
''' '''
# no input data? scrap it. # no input data? scrap it.
if hash is None: if bHash is None:
return False return False
''' '''
if type(hash) == Block: if type(hash) == Block:
@ -759,8 +759,10 @@ class Block:
else: else:
blockfile = onionrcore.Core().dataDir + 'blocks/%s.dat' % hash blockfile = onionrcore.Core().dataDir + 'blocks/%s.dat' % hash
''' '''
if isinstance(bHash, Block):
ret = isinstance(onionrstorage.getData(onionrcore.Core(), hash.getHash()), type(None)) bHash = bHash.getHash()
ret = isinstance(onionrstorage.getData(onionrcore.Core(), bHash), type(None))
return not ret return not ret