Skip to content

Commit

Permalink
improve stability, allow filter for multi ports
Browse files Browse the repository at this point in the history
  • Loading branch information
Dragon2fly committed Nov 11, 2015
1 parent 85fb2bd commit 413076c
Show file tree
Hide file tree
Showing 5 changed files with 289 additions and 234 deletions.
252 changes: 131 additions & 121 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
__author__ = "duc_tin"
__copyright__ = "Copyright 2015+, duc_tin"
__license__ = "GPLv2"
__version__ = "1.0"
__version__ = "1.25"
__maintainer__ = "duc_tin"
__email__ = "[email protected]"

import ConfigParser
import re
import sys
import socket
from collections import OrderedDict


def ctext(text, color):
Expand All @@ -35,127 +36,136 @@ def ctext(text, color):
return tformat + text + ENDC


def get_input(config_path, option):
if option[0] in ['c', 'config']:
proxy, port, ip, sort_by, use_proxy, s_country, s_port, fix_dns, dns, verbose = read_config(config_path)

while 1:
print ctext('\n Current settings:', 'B')
print ctext(' 1. Proxy address:', 'yB'), proxy, ctext('\t2. port: ', 'yB'), port
print ctext(' 3. Use proxy:', 'yB'), use_proxy
print ctext(' 4. Sort servers by:', 'yB'), sort_by
print ctext(' 5. Country filter:', 'yB'), s_country, ctext('\t\t6. VPN server\'s port: ', 'yB'), s_port
print ctext(' 7. Fix dns leaking:', 'yB'), fix_dns
print ctext(' 8. DNS list: ', 'yB'), dns
print ctext(' 9. Show openvpn log:', 'B'), verbose

user_input = raw_input('\nCommand or Enter to fetch server list: ')
if user_input == '':
print 'Process to vpn server list'
write_config(config_path, proxy, port, ip, sort_by, use_proxy, s_country, s_port, fix_dns, dns, verbose)
return
elif user_input == '1':
proxy = raw_input('Your http_proxy:')
ip = socket.gethostbyname(proxy)
elif user_input == '2':
user_input = 'abc'
while not user_input.strip().isdigit():
user_input = raw_input('Http proxy\'s port (eg: 8080): ')
port = user_input

elif user_input == '3':
while user_input.lower() not in ['y', 'n', 'yes', 'no']:
user_input = raw_input('Use proxy to connect to vpn? (yes|no): ')
else:
use_proxy = 'no' if user_input in 'no' else 'yes'

elif user_input == '4':
while user_input not in ['speed', 'ping', 'score', 'up time', 'uptime']:
user_input = raw_input('Sort servers by (speed | ping | score | up time): ')
sort_by = 'up time' if user_input == 'uptime' else user_input

elif user_input == '5':
while not re.match('^[a-z ]*$', user_input.lower().strip()):
user_input = raw_input('Country\'s name (eg: all(default), jp, japan):')
else:
s_country = 'all' if not user_input else user_input.lower()

elif user_input == '6':
user_input = 'abc'
while not user_input.strip().isdigit():
user_input = raw_input('VPN server\'s port (eg: 995): ')
if not user_input or 'all' == user_input: break
s_port = user_input if user_input else 'all'

elif user_input == '7':
while user_input.lower() not in ['y', 'n', 'yes', 'no']:
user_input = raw_input('Fix DNS:')
else:
fix_dns = 'no' if user_input in 'no' else 'yes'

elif user_input == '8':
print 'Default DNS are 8.8.8.8, 84.200.69.80, 208.67.222.222'
user_input='@'
while not re.match('[a-zA-Z0-9., ]*$', user_input.strip()):
user_input = raw_input('DNS server(s) with "," separated or Enter to use default:')
if user_input:
dns = user_input.replace(' ', '').split(',')
else:
dns = '8.8.8.8, 84.200.69.80, 208.67.222.222'

elif user_input == '9':
while user_input.lower() not in ['y', 'n', 'yes', 'no']:
user_input = raw_input('Use proxy to connect to vpn? (yes|no): ')
else:
verbose = 'no' if user_input in 'no' else 'yes'

elif user_input in ['q', 'quit', 'exit']:
print ctext('Goodbye'.center(40), 'gB')
sys.exit(0)
else:
print 'Invalid input'
def get_input(s, option):
"""
:type s: Setting
"""

else:
if option[0] not in ['c', 'config']:
print 'Wrong argument. Do you mean "config" or "restore" ?'
return

while 1:
proxy, port, ip, sort_by, use_proxy, s_country, s_port, fix_dns, dns, verbose = s[:]
print ctext('\n Current settings:', 'B')
print ctext(' 1. Proxy address:', 'yB'), proxy, ctext('\t2. port: ', 'yB'), port
print ctext(' 3. Use proxy:', 'yB'), use_proxy
print ctext(' 4. Sort servers by:', 'yB'), sort_by
print ctext(' 5. Country filter:', 'yB'), s_country, ctext('\t\t6. VPN server\'s port: ', 'yB'), s_port
print ctext(' 7. Fix dns leaking:', 'yB'), fix_dns
print ctext(' 8. DNS list: ', 'yB'), dns
print ctext(' 9. Show openvpn log:', 'B'), verbose

user_input = raw_input('\nCommand or Enter to fetch server list: ')
if user_input == '':
print 'Process to vpn server list'
s.write()
return
elif user_input == '1':
s.proxy['address'] = raw_input('Your http_proxy:')
s.proxy['ip'] = socket.gethostbyname(proxy)
elif user_input == '2':
user_input = 'abc'
while not user_input.strip().isdigit():
user_input = raw_input('Http proxy\'s port (eg: 8080): ')
s.proxy['port'] = user_input

elif user_input == '3':
while user_input.lower() not in ['y', 'n', 'yes', 'no']:
user_input = raw_input('Use proxy to connect to vpn? (yes|no): ')
else:
s.proxy['use proxy'] = 'no' if user_input in 'no' else 'yes'

elif user_input == '4':
while user_input not in ['speed', 'ping', 'score', 'up time', 'uptime']:
user_input = raw_input('Sort servers by (speed | ping | score | up time): ')
s.sort['key'] = 'up time' if user_input == 'uptime' else user_input

elif user_input == '5':
while not re.match('^[a-z ]*$', user_input.lower().strip()):
user_input = raw_input('Country\'s name (eg: all(default), jp, japan):')
else:
s.filter['country'] = 'all' if not user_input else user_input.lower()

elif user_input == '6':
user_input = 'abc'
while not user_input.strip().isdigit():
user_input = raw_input('VPN server\'s port (eg: 995): ')
if not user_input or 'all' == user_input: break
s.filter['port'] = user_input if user_input else 'all'

elif user_input == '7':
while user_input.lower() not in ['y', 'n', 'yes', 'no']:
user_input = raw_input('Fix DNS:')
else:
s.dns['fix_dns'] = 'no' if user_input in 'no' else 'yes'

elif user_input == '8':
print 'Default DNS are 8.8.8.8, 84.200.69.80, 208.67.222.222'
user_input = '@'
while not re.match('[a-zA-Z0-9., ]*$', user_input.strip()):
user_input = raw_input('DNS server(s) with "," separated or Enter to use default:')
if user_input:
s.dns['dns'] = user_input.replace(' ', '').split(',')
else:
s.dns['dns'] = '8.8.8.8, 84.200.69.80, 208.67.222.222'

elif user_input == '9':
while user_input.lower() not in ['y', 'n', 'yes', 'no']:
user_input = raw_input('Show openvpn log: ')
else:
s.openvpn['verbose'] = 'no' if user_input in 'no' else 'yes'

elif user_input in ['q', 'quit', 'exit']:
print ctext('Goodbye'.center(40), 'gB')
sys.exit(0)
else:
print 'Invalid input'


class Setting:
def __init__(self, path):
self.path = path
self.parser = ConfigParser.SafeConfigParser()

self.proxy = OrderedDict([('use proxy', 'no'), ('address', ''),
('port', ''),
('ip', '')])

self.sort = {'key': 'score'}

self.filter = OrderedDict([('country', 'all'), ('port', 'all')])

self.dns = OrderedDict([('fix_dns', 'yes'), ('dns', '8.8.8.8')])

self.openvpn = {'verbose': 'yes'}

self.sections = OrderedDict([('proxy', self.proxy),
('sort', self.sort),
('country_filter', self.filter),
('DNS_leak', self.dns),
('openvpn', self.openvpn)])

def __getitem__(self, index):
data = []
for sec in self.sections.values():
data += sec.values()

return data[index]

def write(self):
for sect in self.sections:
if not self.parser.has_section(sect):
self.parser.add_section(sect)
for content in self.sections[sect]:
self.parser.set(sect, content, self.sections[sect][content])

with open(self.path, 'w+') as configfile:
self.parser.write(configfile)

def load(self):
self.parser.read(self.path)

def read_config(config_path):
parser = ConfigParser.SafeConfigParser()
parser.read(config_path)
use_proxy = parser.get('proxy', 'use proxy')
proxy = parser.get('proxy', 'address')
port = parser.get('proxy', 'port')
ip = parser.get('proxy', 'ip')
sort_by = parser.get('sort', 'key')
s_country = parser.get('country_filter', 'country')
s_port = parser.get('country_filter', 'port')
fix_dns = parser.get('DNS_leak', 'fix_dns')
dns = parser.get('DNS_leak', 'dns')
verbose = parser.get('openvpn', 'verbose')
return proxy, port, ip, sort_by, use_proxy, s_country, s_port, fix_dns, dns, verbose


def write_config(config_path, proxy, port, ip, parameter, use_proxy, s_country, s_port, fix_dns, dns, verbose='no'):
parser = ConfigParser.SafeConfigParser()
parser.add_section('proxy')
parser.set('proxy', 'use proxy', use_proxy)
parser.set('proxy', 'address', proxy)
parser.set('proxy', 'port', port)
parser.set('proxy', 'ip', ip)

parser.add_section('sort')
parser.set('sort', 'key', parameter)

parser.add_section('country_filter')
parser.set('country_filter', 'country', s_country)
parser.set('country_filter', 'port', s_port)

parser.add_section('DNS_leak')
parser.set('DNS_leak', 'fix_dns', fix_dns)
parser.set('DNS_leak', 'dns', dns)

parser.add_section('openvpn')
parser.set('openvpn', 'verbose', verbose)

with open(config_path, 'w+') as configfile:
parser.write(configfile)
for sect in self.sections:
for content in self.sections[sect]:
self.sections[sect][content] = self.parser.get(sect, content)
29 changes: 21 additions & 8 deletions ui_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
__author__ = "duc_tin"
__copyright__ = "Copyright 2015+, duc_tin"
__license__ = "GPLv2"
__version__ = "1.1"
__version__ = "1.25"
__maintainer__ = "duc_tin"
__email__ = "[email protected]"

import urwid
import re

# -----------------------------------------------here we go-------------------------------------------------------------

Expand Down Expand Up @@ -111,8 +112,7 @@ def __init__(self, key=None, value=''):
self.trigger = key
info = urwid.Text("'ESC' to clear, leave blank or 'all' for all country and port", 'center')
self.country = urwid.Edit(('attention', u' \N{BULLET} Country: '), edit_text=value[0], wrap='clip')
self.port = urwid.IntEdit(('attention', u' \N{BULLET} Port : '), default=value[1])
self.port.set_wrap_mode('clip')
self.port = urwid.Edit(('attention', u' \N{BULLET} Port : '), edit_text=value[1], wrap='clip')
exit_but = urwid.Padding(urwid.Button('OKay'.center(8), self.item_callback), 'center', 12)
filter_ = [urwid.AttrMap(wid, None, 'popbgs') for wid in (self.country, self.port, exit_but)]

Expand All @@ -132,11 +132,19 @@ def item_callback(self, Button, data=None):
self.port.set_edit_text('all')
port = 'all'
self._emit("close")
elif port != 'all' and not 0 <= int(port) <= 65535:

elif port != 'all' and not set(port) <= set(' 0123456789'):
self.port.set_edit_text('Invalid number!')
return
else:
self.chosen = country, port
self._emit("close")
for p in re.findall(r'\d+', port):
if p == 'Invalid number!': return
if p != 'all' and not 0 <= int(p) <= 65535:
self.port.set_edit_text('Invalid number!')
return

self.chosen = country, port
self._emit("close")

def keypress(self, size, key):
position = self.pile.focus_position
Expand Down Expand Up @@ -190,10 +198,15 @@ def item_callback(self, Button, data=None):
port = self.input_port.edit_text
addr = self.input_addr.edit_text.replace('http://', '')
if self.yn == 'yes':
if not addr:
addr = 'Invalid Address!'
self.input_addr.set_edit_text('Invalid Address!')
if not port:
port = 'Invalid number!'
self.input_port.set_edit_text('Invalid number!')
if 'Invalid' in addr + port:
return
if not addr or not port:
self.input_addr.set_edit_text('Invalid Address!')

if not 0 <= int(port) <= 65535:
self.input_port.set_edit_text('Invalid number!')
else:
Expand Down
3 changes: 1 addition & 2 deletions vpn_indicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ def run(self, *args):

def reload(self, data_in):
if data_in:
if len(data_in) < 25:
print data_in
print data_in[:12]

self.last_recv = data_in.split(';')
if 'connected' in data_in:
Expand Down
Loading

0 comments on commit 413076c

Please sign in to comment.