Browse Source

fix config: serialize/deserialize proxy

283
ThomasV 10 years ago
parent
commit
30763a6555
  1. 43
      lib/interface.py
  2. 10
      lib/network.py

43
lib/interface.py

@ -38,6 +38,28 @@ proxy_modes = ['socks4', 'socks5', 'http']
import util
def serialize_proxy(p):
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):
@ -68,7 +90,7 @@ class TcpInterface(threading.Thread):
self.host, self.port, self.protocol = self.server.split(':')
self.port = int(self.port)
self.use_ssl = (self.protocol == 's')
self.proxy = self.parse_proxy_options(self.config.get('proxy'))
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"]))
@ -271,25 +293,6 @@ class TcpInterface(threading.Thread):
self.unanswered_requests[self.message_id] = method, params, _id, queue
self.message_id += 1
def parse_proxy_options(self, s):
if type(s) == type({}): return s # fixme: type should be fixed
if type(s) != type(""): 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 stop(self):
if self.is_connected and self.protocol in 'st' and self.s:
self.s.shutdown(socket.SHUT_RDWR)

10
lib/network.py

@ -178,7 +178,7 @@ class Network(threading.Thread):
def get_parameters(self):
host, port, protocol = self.default_server.split(':')
proxy = self.proxy
proxy = interface.deserialize_proxy(self.proxy)
auto_connect = self.config.get('auto_cycle', True)
return host, port, protocol, proxy, auto_connect
@ -225,14 +225,16 @@ class Network(threading.Thread):
threading.Thread.start(self)
def set_parameters(self, host, port, protocol, proxy, auto_connect):
proxy_str = interface.serialize_proxy(proxy)
self.config.set_key('auto_cycle', auto_connect, True)
self.config.set_key("proxy", proxy, True)
self.config.set_key("proxy", proxy_str, True)
self.config.set_key("protocol", protocol, True)
server = ':'.join([ host, port, protocol ])
self.config.set_key("server", server, True)
if self.proxy != proxy or self.protocol != protocol:
self.proxy = proxy
if self.proxy != proxy_str or self.protocol != protocol:
print_error('restarting network')
self.proxy = proxy_str
self.protocol = protocol
for i in self.interfaces.values(): i.stop()
if auto_connect:

Loading…
Cancel
Save