mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-09-04 04:45:16 +00:00
Added SOCKS support, with cmdline and SimpleConfig options
This commit is contained in:
parent
af3fe1722b
commit
aa6f631f2e
4 changed files with 48 additions and 15 deletions
9
electrum
9
electrum
|
@ -37,9 +37,9 @@ except ImportError:
|
||||||
sys.exit("Error: AES does not seem to be installed. Try 'sudo pip install slowaes'")
|
sys.exit("Error: AES does not seem to be installed. Try 'sudo pip install slowaes'")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from lib import Wallet, WalletSynchronizer, format_satoshis, mnemonic, prompt_password, SimpleConfig
|
from lib import Wallet, WalletSynchronizer, format_satoshis, mnemonic, prompt_password, parse_proxy_options, SimpleConfig
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from electrum import Wallet, WalletSynchronizer, format_satoshis, mnemonic, prompt_password, SimpleConfig
|
from electrum import Wallet, WalletSynchronizer, format_satoshis, mnemonic, prompt_password, parse_proxy_options, SimpleConfig
|
||||||
|
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
|
|
||||||
|
@ -116,8 +116,11 @@ if __name__ == '__main__':
|
||||||
parser.add_option("-s", "--fromaddr", dest="from_addr", default=None, help="set source address for payto/mktx. if it isn't in the wallet, it will ask for the private key unless supplied in the format public_key:private_key. It's not saved in the wallet.")
|
parser.add_option("-s", "--fromaddr", dest="from_addr", default=None, help="set source address for payto/mktx. if it isn't in the wallet, it will ask for the private key unless supplied in the format public_key:private_key. It's not saved in the wallet.")
|
||||||
parser.add_option("-c", "--changeaddr", dest="change_addr", default=None, help="set the change address for payto/mktx. default is a spare address, or the source address if it's not in the wallet")
|
parser.add_option("-c", "--changeaddr", dest="change_addr", default=None, help="set the change address for payto/mktx. default is a spare address, or the source address if it's not in the wallet")
|
||||||
parser.add_option("-r", "--remote", dest="remote_url", default=None, help="URL of a remote wallet")
|
parser.add_option("-r", "--remote", dest="remote_url", default=None, help="URL of a remote wallet")
|
||||||
|
parser.add_option("-p", "--proxy", dest="proxy", default=simple_config.config["proxy"], help="set proxy [type:]host[:port], where type is socks4,socks5 or http")
|
||||||
options, args = parser.parse_args()
|
options, args = parser.parse_args()
|
||||||
|
|
||||||
|
if type(options.proxy) == type(''):
|
||||||
|
options.proxy = parse_proxy_options(options.proxy)
|
||||||
|
|
||||||
wallet = Wallet()
|
wallet = Wallet()
|
||||||
wallet.set_path(options.wallet_path)
|
wallet.set_path(options.wallet_path)
|
||||||
|
@ -179,7 +182,7 @@ if __name__ == '__main__':
|
||||||
sys.exit("Error: Unknown GUI: " + options.gui)
|
sys.exit("Error: Unknown GUI: " + options.gui)
|
||||||
|
|
||||||
gui = gui.ElectrumGui(wallet)
|
gui = gui.ElectrumGui(wallet)
|
||||||
interface = WalletSynchronizer(wallet, True, gui.server_list_changed)
|
interface = WalletSynchronizer(wallet, True, gui.server_list_changed, options.proxy)
|
||||||
interface.start()
|
interface.start()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
from wallet import Wallet, format_satoshis, prompt_password
|
from wallet import Wallet, format_satoshis, prompt_password
|
||||||
from interface import WalletSynchronizer
|
from interface import WalletSynchronizer, parse_proxy_options
|
||||||
from interface import TcpStratumInterface
|
from interface import TcpStratumInterface
|
||||||
from simple_config import SimpleConfig
|
from simple_config import SimpleConfig
|
||||||
|
|
|
@ -29,6 +29,7 @@ DEFAULT_SERVERS = [ 'ecdsa.org:50001:t',
|
||||||
'uncle-enzo.info:50001:t',
|
'uncle-enzo.info:50001:t',
|
||||||
'electrum.bytesized-hosting.com:50001:t'] # list of default servers
|
'electrum.bytesized-hosting.com:50001:t'] # list of default servers
|
||||||
|
|
||||||
|
proxy_modes = ['off', 'socks4', 'socks5', 'http' ]
|
||||||
|
|
||||||
def replace_keys(obj, old_key, new_key):
|
def replace_keys(obj, old_key, new_key):
|
||||||
if isinstance(obj, dict):
|
if isinstance(obj, dict):
|
||||||
|
@ -48,13 +49,29 @@ def old_to_new(d):
|
||||||
replace_keys(d, 'is_in', 'is_input')
|
replace_keys(d, 'is_in', 'is_input')
|
||||||
replace_keys(d, 'raw_scriptPubKey', 'raw_output_script')
|
replace_keys(d, 'raw_scriptPubKey', 'raw_output_script')
|
||||||
|
|
||||||
|
def parse_proxy_options(s):
|
||||||
|
proxy = { "mode":"socks5", "host":"localhost" }
|
||||||
|
args = s.split(':')
|
||||||
|
n = 0
|
||||||
|
if proxy_modes.count(args[n]) == 1:
|
||||||
|
proxy["mode"] = args[n]
|
||||||
|
n += 1
|
||||||
|
if len(args) > n:
|
||||||
|
proxy["host"] = args[n]
|
||||||
|
n += 1
|
||||||
|
if len(args) > n:
|
||||||
|
proxy["port"] = args[n]
|
||||||
|
else:
|
||||||
|
proxy["port"] = "8080" if proxy["mode"] == "http" else "1080"
|
||||||
|
return proxy
|
||||||
|
|
||||||
class Interface(threading.Thread):
|
class Interface(threading.Thread):
|
||||||
def __init__(self, host, port, debug_server):
|
def __init__(self, host, port, debug_server, proxy):
|
||||||
threading.Thread.__init__(self)
|
threading.Thread.__init__(self)
|
||||||
self.daemon = True
|
self.daemon = True
|
||||||
self.host = host
|
self.host = host
|
||||||
self.port = port
|
self.port = port
|
||||||
|
self.proxy = proxy
|
||||||
|
|
||||||
self.servers = [] # actual list from IRC
|
self.servers = [] # actual list from IRC
|
||||||
self.rtime = 0
|
self.rtime = 0
|
||||||
|
@ -122,8 +139,8 @@ class Interface(threading.Thread):
|
||||||
class PollingInterface(Interface):
|
class PollingInterface(Interface):
|
||||||
""" non-persistent connection. synchronous calls"""
|
""" non-persistent connection. synchronous calls"""
|
||||||
|
|
||||||
def __init__(self, host, port, debug_server):
|
def __init__(self, host, port, debug_server, proxy):
|
||||||
Interface.__init__(self, host, port, debug_server)
|
Interface.__init__(self, host, port, debug_server, proxy)
|
||||||
self.session_id = None
|
self.session_id = None
|
||||||
self.debug_server = debug_server
|
self.debug_server = debug_server
|
||||||
|
|
||||||
|
@ -175,6 +192,10 @@ class HttpStratumInterface(PollingInterface):
|
||||||
def send(self, messages):
|
def send(self, messages):
|
||||||
import urllib2, json, time, cookielib
|
import urllib2, json, time, cookielib
|
||||||
|
|
||||||
|
if self.proxy["mode"] != "off":
|
||||||
|
import socks
|
||||||
|
socks.setdefaultproxy(proxy_modes.index(self.proxy["mode"]), self.proxy["host"], int(self.proxy["port"]) )
|
||||||
|
socks.wrapmodule(urllib2)
|
||||||
cj = cookielib.CookieJar()
|
cj = cookielib.CookieJar()
|
||||||
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
|
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
|
||||||
urllib2.install_opener(opener)
|
urllib2.install_opener(opener)
|
||||||
|
@ -233,16 +254,23 @@ class HttpStratumInterface(PollingInterface):
|
||||||
class TcpStratumInterface(Interface):
|
class TcpStratumInterface(Interface):
|
||||||
"""json-rpc over persistent TCP connection, asynchronous"""
|
"""json-rpc over persistent TCP connection, asynchronous"""
|
||||||
|
|
||||||
def __init__(self, host, port, debug_server):
|
def __init__(self, host, port, debug_server, proxy):
|
||||||
Interface.__init__(self, host, port, debug_server)
|
Interface.__init__(self, host, port, debug_server, proxy)
|
||||||
self.debug_server = debug_server
|
self.debug_server = debug_server
|
||||||
|
|
||||||
def init_socket(self):
|
def init_socket(self):
|
||||||
self.s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
|
global proxy_modes
|
||||||
|
if self.proxy["mode"] == "off":
|
||||||
|
self.s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
|
||||||
|
else:
|
||||||
|
import socks
|
||||||
|
self.s = socks.socksocket()
|
||||||
|
print "Using Proxy", self.proxy
|
||||||
|
self.s.setproxy(proxy_modes.index(self.proxy["mode"]), self.proxy["host"], int(self.proxy["port"]) )
|
||||||
self.s.settimeout(60)
|
self.s.settimeout(60)
|
||||||
self.s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
|
self.s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
|
||||||
try:
|
try:
|
||||||
self.s.connect(( self.host, self.port))
|
self.s.connect(( self.host.encode('ascii'), int(self.port)))
|
||||||
self.is_connected = True
|
self.is_connected = True
|
||||||
self.send([('server.version', [ELECTRUM_VERSION])])
|
self.send([('server.version', [ELECTRUM_VERSION])])
|
||||||
print "Connected to %s:%d"%(self.host,self.port)
|
print "Connected to %s:%d"%(self.host,self.port)
|
||||||
|
@ -306,14 +334,16 @@ class TcpStratumInterface(Interface):
|
||||||
|
|
||||||
class WalletSynchronizer(threading.Thread):
|
class WalletSynchronizer(threading.Thread):
|
||||||
|
|
||||||
def __init__(self, wallet, loop=False, servers_loaded_callback=None):
|
def __init__(self, wallet, loop=False, servers_loaded_callback=None, proxy=None):
|
||||||
threading.Thread.__init__(self)
|
threading.Thread.__init__(self)
|
||||||
self.daemon = True
|
self.daemon = True
|
||||||
self.wallet = wallet
|
self.wallet = wallet
|
||||||
self.loop = loop
|
self.loop = loop
|
||||||
|
self.proxy = proxy
|
||||||
self.init_interface()
|
self.init_interface()
|
||||||
self.servers_loaded_callback = servers_loaded_callback
|
self.servers_loaded_callback = servers_loaded_callback
|
||||||
|
|
||||||
|
|
||||||
def init_interface(self):
|
def init_interface(self):
|
||||||
try:
|
try:
|
||||||
host, port, protocol = self.wallet.server.split(':')
|
host, port, protocol = self.wallet.server.split(':')
|
||||||
|
@ -332,7 +362,7 @@ class WalletSynchronizer(threading.Thread):
|
||||||
print_error("Error: Unknown protocol")
|
print_error("Error: Unknown protocol")
|
||||||
InterfaceClass = TcpStratumInterface
|
InterfaceClass = TcpStratumInterface
|
||||||
|
|
||||||
self.interface = InterfaceClass(host, port, self.wallet.debug_server)
|
self.interface = InterfaceClass(host, port, self.wallet.debug_server, self.proxy)
|
||||||
self.wallet.interface = self.interface
|
self.wallet.interface = self.interface
|
||||||
|
|
||||||
def handle_response(self, r):
|
def handle_response(self, r):
|
||||||
|
|
|
@ -3,7 +3,7 @@ import os
|
||||||
from util import user_dir
|
from util import user_dir
|
||||||
|
|
||||||
class SimpleConfig:
|
class SimpleConfig:
|
||||||
default_options = {"gui": "lite"}
|
default_options = {"gui": "lite", "proxy": { "mode": "off", "host":"localhost", "port":"8080" } }
|
||||||
|
|
||||||
def set_key(self, key, value, save = True):
|
def set_key(self, key, value, save = True):
|
||||||
self.config[key] = value
|
self.config[key] = value
|
||||||
|
|
Loading…
Add table
Reference in a new issue