Skip to content

Commit b6df0f8

Browse files
committed
Fixed errors & Py2/3 compatibility
1 parent e9711e7 commit b6df0f8

File tree

2 files changed

+89
-45
lines changed

2 files changed

+89
-45
lines changed

src/protocol.py

+87-43
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,9 @@ def encodeHost(host):
114114
return '\xfd\x87\xd8\x7e\xeb\x43' + base64.b32decode(
115115
host.split(".")[0], True)
116116
elif host.find(':') == -1:
117-
import pdb; pdb.set_trace()
118117
if PY3:
119-
return '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF' + \
120-
socket.inet_aton(host).decode('utf-8')
118+
return b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF' + \
119+
socket.inet_aton(host)
121120
else:
122121
return '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF' + \
123122
socket.inet_aton(host)
@@ -166,54 +165,99 @@ def checkIPAddress(host, private=False):
166165
Returns hostStandardFormat if it is a valid IP address,
167166
otherwise returns False
168167
"""
169-
if host[0:12] == '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF':
170-
hostStandardFormat = socket.inet_ntop(socket.AF_INET, host[12:])
171-
return checkIPv4Address(host[12:], hostStandardFormat, private)
172-
elif host[0:6] == '\xfd\x87\xd8\x7e\xeb\x43':
173-
# Onion, based on BMD/bitcoind
174-
hostStandardFormat = base64.b32encode(host[6:]).lower() + ".onion"
175-
if private:
176-
return False
177-
return hostStandardFormat
168+
if not isinstance(host, str):
169+
if host[0:12] == b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF':
170+
hostStandardFormat = socket.inet_ntop(socket.AF_INET, host[12:])
171+
return checkIPv4Address(host[12:], hostStandardFormat, private)
172+
elif host[0:6] == b'\xfd\x87\xd8\x7e\xeb\x43':
173+
# Onion, based on BMD/bitcoind
174+
hostStandardFormat = base64.b32encode(host[6:]).lower() + ".onion"
175+
if private:
176+
return False
177+
return hostStandardFormat
178+
else:
179+
try:
180+
hostStandardFormat = socket.inet_ntop(socket.AF_INET6, host)
181+
except ValueError:
182+
return False
183+
if hostStandardFormat == "":
184+
# This can happen on Windows systems which are
185+
# not 64-bit compatible so let us drop the IPv6 address.
186+
return False
187+
return checkIPv6Address(host, hostStandardFormat, private)
178188
else:
179-
try:
180-
hostStandardFormat = socket.inet_ntop(socket.AF_INET6, host)
181-
except ValueError:
182-
return False
183-
if hostStandardFormat == "":
184-
# This can happen on Windows systems which are
185-
# not 64-bit compatible so let us drop the IPv6 address.
186-
return False
187-
return checkIPv6Address(host, hostStandardFormat, private)
189+
if host[0:12] == '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF':
190+
hostStandardFormat = socket.inet_ntop(socket.AF_INET, host[12:])
191+
return checkIPv4Address(host[12:], hostStandardFormat, private)
192+
elif host[0:6] == '\xfd\x87\xd8\x7e\xeb\x43':
193+
# Onion, based on BMD/bitcoind
194+
hostStandardFormat = base64.b32encode(host[6:]).lower() + ".onion"
195+
if private:
196+
return False
197+
return hostStandardFormat
198+
else:
199+
try:
200+
hostStandardFormat = socket.inet_ntop(socket.AF_INET6, host)
201+
except ValueError:
202+
return False
203+
if hostStandardFormat == "":
204+
# This can happen on Windows systems which are
205+
# not 64-bit compatible so let us drop the IPv6 address.
206+
return False
207+
return checkIPv6Address(host, hostStandardFormat, private)
188208

189209

190210
def checkIPv4Address(host, hostStandardFormat, private=False):
191211
"""
192212
Returns hostStandardFormat if it is an IPv4 address,
193213
otherwise returns False
194214
"""
195-
if host[0] == '\x7F': # 127/8
196-
if not private:
197-
logger.debug(
198-
'Ignoring IP address in loopback range: %s',
199-
hostStandardFormat)
200-
return hostStandardFormat if private else False
201-
if host[0] == '\x0A': # 10/8
202-
if not private:
203-
logger.debug(
204-
'Ignoring IP address in private range: %s', hostStandardFormat)
205-
return hostStandardFormat if private else False
206-
if host[0:2] == '\xC0\xA8': # 192.168/16
207-
if not private:
208-
logger.debug(
209-
'Ignoring IP address in private range: %s', hostStandardFormat)
210-
return hostStandardFormat if private else False
211-
if host[0:2] >= '\xAC\x10' and host[0:2] < '\xAC\x20': # 172.16/12
212-
if not private:
213-
logger.debug(
214-
'Ignoring IP address in private range: %s', hostStandardFormat)
215-
return hostStandardFormat if private else False
216-
return False if private else hostStandardFormat
215+
if not isinstance(host, str):
216+
if host[0] == 127: # 127/8
217+
if not private:
218+
logger.debug(
219+
'Ignoring IP address in loopback range: %s',
220+
hostStandardFormat)
221+
return hostStandardFormat if private else False
222+
if host[0] == 10: # 10/8
223+
if not private:
224+
logger.debug(
225+
'Ignoring IP address in private range: %s', hostStandardFormat)
226+
return hostStandardFormat if private else False
227+
if host[0:2] == b'\xC0\xA8': # 192.168/16
228+
if not private:
229+
logger.debug(
230+
'Ignoring IP address in private range: %s', hostStandardFormat)
231+
return hostStandardFormat if private else False
232+
if host[0:2] >= b'\xAC\x10' and host[0:2] < '\xAC\x20': # 172.16/12
233+
if not private:
234+
logger.debug(
235+
'Ignoring IP address in private range: %s', hostStandardFormat)
236+
return hostStandardFormat if private else False
237+
return False if private else hostStandardFormat
238+
else:
239+
if host[0] == '\x7F': # 127/8
240+
if not private:
241+
logger.debug(
242+
'Ignoring IP address in loopback range: %s',
243+
hostStandardFormat)
244+
return hostStandardFormat if private else False
245+
if host[0] == '\x0A': # 10/8
246+
if not private:
247+
logger.debug(
248+
'Ignoring IP address in private range: %s', hostStandardFormat)
249+
return hostStandardFormat if private else False
250+
if host[0:2] == '\xC0\xA8': # 192.168/16
251+
if not private:
252+
logger.debug(
253+
'Ignoring IP address in private range: %s', hostStandardFormat)
254+
return hostStandardFormat if private else False
255+
if host[0:2] >= '\xAC\x10' and host[0:2] < '\xAC\x20': # 172.16/12
256+
if not private:
257+
logger.debug(
258+
'Ignoring IP address in private range: %s', hostStandardFormat)
259+
return hostStandardFormat if private else False
260+
return False if private else hostStandardFormat
217261

218262

219263
def checkIPv6Address(host, hostStandardFormat, private=False):

src/tests/test_protocol.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ def test_check_local(self):
1414
# import pdb; pdb.set_trace()
1515

1616
self.assertTrue(
17-
protocol.checkIPAddress(protocol.encodeHost('127.0.0.1').decode('ISO-8859-1'), True))
17+
protocol.checkIPAddress(protocol.encodeHost('127.0.0.1'), True))
1818
self.assertTrue(
19-
protocol.checkIPAddress(protocol.encodeHost('192.168.0.1').decode('ISO-8859-1'), True))
19+
protocol.checkIPAddress(protocol.encodeHost('192.168.0.1'), True))
2020

2121
self.assertTrue(
2222
not protocol.checkSocksIP('127.0.0.1')

0 commit comments

Comments
 (0)