mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-09-02 18:25:21 +00:00
move proxy logic to network.py. reload socket module if proxy is disabled
This commit is contained in:
parent
ae7405a10f
commit
799a08514b
2 changed files with 46 additions and 43 deletions
|
@ -19,7 +19,6 @@
|
||||||
|
|
||||||
import random, ast, re, errno, os
|
import random, ast, re, errno, os
|
||||||
import threading, traceback, sys, time, json, Queue
|
import threading, traceback, sys, time, json, Queue
|
||||||
import socks
|
|
||||||
import socket
|
import socket
|
||||||
import ssl
|
import ssl
|
||||||
|
|
||||||
|
@ -31,37 +30,10 @@ from util import print_error, print_msg
|
||||||
from simple_config import SimpleConfig
|
from simple_config import SimpleConfig
|
||||||
|
|
||||||
import x509
|
import x509
|
||||||
|
|
||||||
DEFAULT_TIMEOUT = 5
|
|
||||||
proxy_modes = ['socks4', 'socks5', 'http']
|
|
||||||
|
|
||||||
|
|
||||||
import util
|
import util
|
||||||
|
|
||||||
def serialize_proxy(p):
|
DEFAULT_TIMEOUT = 5
|
||||||
if type(p) != dict:
|
|
||||||
return None
|
|
||||||
return ':'.join([p.get('mode'),p.get('host'), p.get('port')])
|
|
||||||
|
|
||||||
def deserialize_proxy(s):
|
|
||||||
if type(s) != str:
|
|
||||||
return None
|
|
||||||
if s.lower() == 'none':
|
|
||||||
return None
|
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
def Interface(server, config = None):
|
def Interface(server, config = None):
|
||||||
|
@ -90,17 +62,6 @@ class TcpInterface(threading.Thread):
|
||||||
self.host, self.port, self.protocol = self.server.split(':')
|
self.host, self.port, self.protocol = self.server.split(':')
|
||||||
self.port = int(self.port)
|
self.port = int(self.port)
|
||||||
self.use_ssl = (self.protocol == 's')
|
self.use_ssl = (self.protocol == 's')
|
||||||
self.proxy = deserialize_proxy(self.config.get('proxy'))
|
|
||||||
if self.proxy:
|
|
||||||
self.proxy_mode = proxy_modes.index(self.proxy["mode"]) + 1
|
|
||||||
socks.setdefaultproxy(self.proxy_mode, self.proxy["host"], int(self.proxy["port"]))
|
|
||||||
# fixme: side effect, client needs restart in order to get out of proxy mode
|
|
||||||
socket.socket = socks.socksocket
|
|
||||||
# prevent dns leaks, see http://stackoverflow.com/questions/13184205/dns-over-proxy
|
|
||||||
def getaddrinfo(*args):
|
|
||||||
return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (args[0], args[1]))]
|
|
||||||
socket.getaddrinfo = getaddrinfo
|
|
||||||
|
|
||||||
|
|
||||||
def process_response(self, response):
|
def process_response(self, response):
|
||||||
if self.debug:
|
if self.debug:
|
||||||
|
|
|
@ -6,6 +6,8 @@ import sys
|
||||||
import random
|
import random
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
|
import socks
|
||||||
|
import socket
|
||||||
|
|
||||||
from util import user_dir, print_error, print_msg
|
from util import user_dir, print_error, print_msg
|
||||||
from bitcoin import *
|
from bitcoin import *
|
||||||
|
@ -80,6 +82,32 @@ def pick_random_server(p='s'):
|
||||||
|
|
||||||
from simple_config import SimpleConfig
|
from simple_config import SimpleConfig
|
||||||
|
|
||||||
|
proxy_modes = ['socks4', 'socks5', 'http']
|
||||||
|
|
||||||
|
def serialize_proxy(p):
|
||||||
|
if type(p) != dict:
|
||||||
|
return None
|
||||||
|
return ':'.join([p.get('mode'),p.get('host'), p.get('port')])
|
||||||
|
|
||||||
|
def deserialize_proxy(s):
|
||||||
|
if type(s) != str:
|
||||||
|
return None
|
||||||
|
if s.lower() == 'none':
|
||||||
|
return None
|
||||||
|
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 Network(threading.Thread):
|
class Network(threading.Thread):
|
||||||
|
@ -118,7 +146,6 @@ class Network(threading.Thread):
|
||||||
|
|
||||||
self.banner = ''
|
self.banner = ''
|
||||||
self.interface = None
|
self.interface = None
|
||||||
self.proxy = interface.deserialize_proxy(self.config.get('proxy'))
|
|
||||||
self.heights = {}
|
self.heights = {}
|
||||||
self.merkle_roots = {}
|
self.merkle_roots = {}
|
||||||
self.utxo_roots = {}
|
self.utxo_roots = {}
|
||||||
|
@ -132,6 +159,8 @@ class Network(threading.Thread):
|
||||||
self.connection_status = 'connecting'
|
self.connection_status = 'connecting'
|
||||||
self.requests_queue = Queue.Queue()
|
self.requests_queue = Queue.Queue()
|
||||||
|
|
||||||
|
self.set_proxy(deserialize_proxy(self.config.get('proxy')))
|
||||||
|
|
||||||
|
|
||||||
def get_server_height(self):
|
def get_server_height(self):
|
||||||
return self.heights.get(self.default_server, 0)
|
return self.heights.get(self.default_server, 0)
|
||||||
|
@ -236,8 +265,21 @@ class Network(threading.Thread):
|
||||||
self.blockchain.start()
|
self.blockchain.start()
|
||||||
threading.Thread.start(self)
|
threading.Thread.start(self)
|
||||||
|
|
||||||
|
def set_proxy(self, proxy):
|
||||||
|
self.proxy = proxy
|
||||||
|
if proxy:
|
||||||
|
proxy_mode = proxy_modes.index(proxy["mode"]) + 1
|
||||||
|
socks.setdefaultproxy(proxy_mode, proxy["host"], int(proxy["port"]))
|
||||||
|
socket.socket = socks.socksocket
|
||||||
|
# prevent dns leaks, see http://stackoverflow.com/questions/13184205/dns-over-proxy
|
||||||
|
def getaddrinfo(*args):
|
||||||
|
return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (args[0], args[1]))]
|
||||||
|
socket.getaddrinfo = getaddrinfo
|
||||||
|
else:
|
||||||
|
reload(socket)
|
||||||
|
|
||||||
def set_parameters(self, host, port, protocol, proxy, auto_connect):
|
def set_parameters(self, host, port, protocol, proxy, auto_connect):
|
||||||
proxy_str = interface.serialize_proxy(proxy)
|
proxy_str = serialize_proxy(proxy)
|
||||||
server_str = ':'.join([ host, port, protocol ])
|
server_str = ':'.join([ host, port, protocol ])
|
||||||
self.config.set_key('auto_cycle', auto_connect, True)
|
self.config.set_key('auto_cycle', auto_connect, True)
|
||||||
self.config.set_key("proxy", proxy_str, True)
|
self.config.set_key("proxy", proxy_str, True)
|
||||||
|
@ -248,7 +290,7 @@ class Network(threading.Thread):
|
||||||
|
|
||||||
if self.proxy != proxy or self.protocol != protocol:
|
if self.proxy != proxy or self.protocol != protocol:
|
||||||
print_error('restarting network')
|
print_error('restarting network')
|
||||||
self.proxy = proxy
|
self.set_proxy(proxy)
|
||||||
self.protocol = protocol
|
self.protocol = protocol
|
||||||
for i in self.interfaces.values(): i.stop()
|
for i in self.interfaces.values(): i.stop()
|
||||||
if auto_connect:
|
if auto_connect:
|
||||||
|
|
Loading…
Add table
Reference in a new issue