Update authproxy.py to python3 version from upstream
This commit is contained in:
@@ -1,8 +1,3 @@
|
|||||||
#!/usr/bin/env python2
|
|
||||||
# Copyright (c) 2016-2024 The Hush developers
|
|
||||||
# Distributed under the GPLv3 software license, see the accompanying
|
|
||||||
# file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Copyright 2011 Jeff Garzik
|
Copyright 2011 Jeff Garzik
|
||||||
|
|
||||||
@@ -38,45 +33,37 @@
|
|||||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
"""
|
"""
|
||||||
|
|
||||||
try:
|
|
||||||
import http.client as httplib
|
|
||||||
except ImportError:
|
|
||||||
import httplib
|
|
||||||
import base64
|
import base64
|
||||||
import decimal
|
import decimal
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
try:
|
from http.client import HTTPConnection, HTTPSConnection, BadStatusLine
|
||||||
import urllib.parse as urlparse
|
from urllib.parse import urlparse
|
||||||
except ImportError:
|
|
||||||
import urlparse
|
USER_AGENT = "AuthServiceProxy/0.1"
|
||||||
|
|
||||||
USER_AGENT = "FUCKjl777LULZ"
|
|
||||||
HTTP_TIMEOUT = 600
|
HTTP_TIMEOUT = 600
|
||||||
|
|
||||||
log = logging.getLogger("BitcoinRPC")
|
log = logging.getLogger("BitcoinRPC")
|
||||||
|
|
||||||
class JSONRPCException(Exception):
|
class JSONRPCException(Exception):
|
||||||
def __init__(self, rpc_error):
|
def __init__(self, rpc_error):
|
||||||
Exception.__init__(self)
|
Exception.__init__(self, rpc_error.get("message"))
|
||||||
self.error = rpc_error
|
self.error = rpc_error
|
||||||
|
|
||||||
|
|
||||||
def EncodeDecimal(o):
|
def EncodeDecimal(o):
|
||||||
if isinstance(o, decimal.Decimal):
|
if isinstance(o, decimal.Decimal):
|
||||||
return round(o, 8)
|
return str(o)
|
||||||
raise TypeError(repr(o) + " is not JSON serializable")
|
raise TypeError(repr(o) + " is not JSON serializable")
|
||||||
|
|
||||||
class AuthServiceProxy(object):
|
|
||||||
|
class AuthServiceProxy():
|
||||||
__id_count = 0
|
__id_count = 0
|
||||||
|
|
||||||
def __init__(self, service_url, service_name=None, timeout=HTTP_TIMEOUT, connection=None):
|
def __init__(self, service_url, service_name=None, timeout=HTTP_TIMEOUT, connection=None):
|
||||||
self.__service_url = service_url
|
self.__service_url = service_url
|
||||||
self.__service_name = service_name
|
self._service_name = service_name
|
||||||
self.__url = urlparse.urlparse(service_url)
|
self.__url = urlparse(service_url)
|
||||||
if self.__url.port is None:
|
|
||||||
port = 80
|
|
||||||
else:
|
|
||||||
port = self.__url.port
|
|
||||||
(user, passwd) = (self.__url.username, self.__url.password)
|
(user, passwd) = (self.__url.username, self.__url.password)
|
||||||
try:
|
try:
|
||||||
user = user.encode('utf8')
|
user = user.encode('utf8')
|
||||||
@@ -89,23 +76,25 @@ class AuthServiceProxy(object):
|
|||||||
authpair = user + b':' + passwd
|
authpair = user + b':' + passwd
|
||||||
self.__auth_header = b'Basic ' + base64.b64encode(authpair)
|
self.__auth_header = b'Basic ' + base64.b64encode(authpair)
|
||||||
|
|
||||||
if connection:
|
self.timeout = timeout
|
||||||
# Callables re-use the connection of the original proxy
|
self._set_conn(connection)
|
||||||
self.__conn = connection
|
|
||||||
elif self.__url.scheme == 'https':
|
|
||||||
self.__conn = httplib.HTTPSConnection(self.__url.hostname, port,
|
|
||||||
None, None, False,
|
|
||||||
timeout)
|
|
||||||
else:
|
|
||||||
self.__conn = httplib.HTTPConnection(self.__url.hostname, port,
|
|
||||||
False, timeout)
|
|
||||||
|
|
||||||
|
def _set_conn(self, connection=None):
|
||||||
|
port = 80 if self.__url.port is None else self.__url.port
|
||||||
|
if connection:
|
||||||
|
self.__conn = connection
|
||||||
|
self.timeout = connection.timeout
|
||||||
|
elif self.__url.scheme == 'https':
|
||||||
|
self.__conn = HTTPSConnection(self.__url.hostname, port, timeout=self.timeout)
|
||||||
|
else:
|
||||||
|
self.__conn = HTTPConnection(self.__url.hostname, port, timeout=self.timeout)
|
||||||
|
|
||||||
def __getattr__(self, name):
|
def __getattr__(self, name):
|
||||||
if name.startswith('__') and name.endswith('__'):
|
if name.startswith('__') and name.endswith('__'):
|
||||||
# Python internal stuff
|
# Python internal stuff
|
||||||
raise AttributeError
|
raise AttributeError
|
||||||
if self.__service_name is not None:
|
if self._service_name is not None:
|
||||||
name = "%s.%s" % (self.__service_name, name)
|
name = "%s.%s" % (self._service_name, name)
|
||||||
return AuthServiceProxy(self.__service_url, name, connection=self.__conn)
|
return AuthServiceProxy(self.__service_url, name, connection=self.__conn)
|
||||||
|
|
||||||
def _request(self, method, path, postdata):
|
def _request(self, method, path, postdata):
|
||||||
@@ -124,8 +113,9 @@ class AuthServiceProxy(object):
|
|||||||
# If connection was closed, try again.
|
# If connection was closed, try again.
|
||||||
# Python 3.5+ raises BrokenPipeError instead of BadStatusLine when the connection was reset.
|
# Python 3.5+ raises BrokenPipeError instead of BadStatusLine when the connection was reset.
|
||||||
# ConnectionResetError happens on FreeBSD with Python 3.4.
|
# ConnectionResetError happens on FreeBSD with Python 3.4.
|
||||||
# These classes don't exist in Python 2.x, so we can't refer to them directly.
|
# This can be simplified now that we depend on Python 3 (previously, we could not
|
||||||
if ((isinstance(e, httplib.BadStatusLine) and e.line == "''")
|
# refer to BrokenPipeError or ConnectionResetError which did not exist on Python 2)
|
||||||
|
if ((isinstance(e, BadStatusLine) and e.line == "''")
|
||||||
or e.__class__.__name__ in ('BrokenPipeError', 'ConnectionResetError')):
|
or e.__class__.__name__ in ('BrokenPipeError', 'ConnectionResetError')):
|
||||||
self.__conn.close()
|
self.__conn.close()
|
||||||
self.__conn.request(method, path, postdata, headers)
|
self.__conn.request(method, path, postdata, headers)
|
||||||
@@ -136,13 +126,12 @@ class AuthServiceProxy(object):
|
|||||||
def __call__(self, *args):
|
def __call__(self, *args):
|
||||||
AuthServiceProxy.__id_count += 1
|
AuthServiceProxy.__id_count += 1
|
||||||
|
|
||||||
log.debug("-%s-> %s %s"%(AuthServiceProxy.__id_count, self.__service_name,
|
log.debug("-%s-> %s %s"%(AuthServiceProxy.__id_count, self._service_name,
|
||||||
json.dumps(args, default=EncodeDecimal)))
|
json.dumps(args, default=EncodeDecimal)))
|
||||||
postdata = json.dumps({'version': '1.1',
|
postdata = json.dumps({'version': '1.1',
|
||||||
'method': self.__service_name,
|
'method': self._service_name,
|
||||||
'params': args,
|
'params': args,
|
||||||
'id': AuthServiceProxy.__id_count}, default=EncodeDecimal)
|
'id': AuthServiceProxy.__id_count}, default=EncodeDecimal)
|
||||||
print("self.__url.path=" + self.__url.path)
|
|
||||||
response = self._request('POST', self.__url.path, postdata)
|
response = self._request('POST', self.__url.path, postdata)
|
||||||
if response['error'] is not None:
|
if response['error'] is not None:
|
||||||
raise JSONRPCException(response['error'])
|
raise JSONRPCException(response['error'])
|
||||||
@@ -162,6 +151,11 @@ class AuthServiceProxy(object):
|
|||||||
if http_response is None:
|
if http_response is None:
|
||||||
raise JSONRPCException({
|
raise JSONRPCException({
|
||||||
'code': -342, 'message': 'missing HTTP response from server'})
|
'code': -342, 'message': 'missing HTTP response from server'})
|
||||||
|
|
||||||
|
content_type = http_response.getheader('Content-Type')
|
||||||
|
if content_type != 'application/json':
|
||||||
|
raise JSONRPCException({
|
||||||
|
'code': -342, 'message': 'non-JSON HTTP response with \'%i %s\' from server' % (http_response.status, http_response.reason)})
|
||||||
|
|
||||||
responsedata = http_response.read().decode('utf8')
|
responsedata = http_response.read().decode('utf8')
|
||||||
response = json.loads(responsedata, parse_float=decimal.Decimal)
|
response = json.loads(responsedata, parse_float=decimal.Decimal)
|
||||||
|
|||||||
Reference in New Issue
Block a user