Last active
May 18, 2017 08:04
-
-
Save QXSoftware/e3a7912f686abf413edd49fa19aa609a to your computer and use it in GitHub Desktop.
GetFreeShadowsocks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#encoding:utf-8 | |
import traceback | |
import os | |
import os.path | |
import urllib2 | |
import json | |
import re | |
from bs4 import BeautifulSoup | |
urls = [ | |
'http://www.ishadowsocks.org/', | |
'http://ss.ishadowx.com/', | |
'http://isx.yt/', | |
'http://isx.tn/', | |
'' | |
] | |
class ShadowsocksConfig: | |
def __init__(self): | |
self.server = '' | |
self.server_port = 0 | |
self.password = '' | |
self.method = '' | |
self.obfs = 'plain' | |
self.protocol = 'origin' | |
self.remarks = '' | |
self.tcp_over_udp = False | |
self.udp_over_tcp = False | |
self.tcp_protocol = 0 | |
self.obfs_udp = False | |
self.enable = True | |
def to_dict(self): | |
d = {} | |
d["server"] = self.server | |
d["server_port"] = self.server_port | |
d["password"] = self.password | |
d["method"] = self.method | |
d["obfs"] = self.obfs | |
d["protocol"] = self.protocol | |
d["remarks"] = self.remarks | |
d["tcp_over_udp"] = self.tcp_over_udp | |
d["udp_over_tcp"] = self.udp_over_tcp | |
d["tcp_protocol"] = self.tcp_protocol | |
d["obfs_udp"] = self.obfs_udp | |
d["enable"] = self.enable | |
return d | |
def __repr__(self): | |
data = [ | |
('Server:', self.server), | |
('Port:', self.server_port), | |
('Password:', self.password), | |
('Method:', self.method), | |
('Obfs:', self.obfs), | |
('Protocol:', self.protocol) | |
] | |
tostring = [] | |
for item in data: | |
tostring.append('%-10s%-20s\n' % item) | |
return ''.join(tostring) | |
def read_url(): | |
for url in urls: | |
if url == '': | |
continue | |
try: | |
print 'Reading from %s ...\n' % url | |
content = urllib2.urlopen(url).read() | |
return content | |
except: | |
traceback.print_exc() | |
raise 'All urls have failed...' | |
def refresh_passwords(): | |
content = read_url() | |
soup = BeautifulSoup(content, 'html.parser') | |
divs = soup.find_all('div', 'hover-text') | |
def split_value(str): | |
parts = re.split(u':|:', str) | |
return parts | |
def split_protocol_obfs(str): | |
parts = re.split(u' ', str) | |
return parts | |
confs = [] | |
for div in divs: | |
h4s = div.find_all('h4') | |
conf = ShadowsocksConfig() | |
conf.server = h4s[0].find('span').string | |
conf.server_port = int(split_value(h4s[1].string)[1]) | |
conf.password = h4s[2].find('span').string | |
conf.method = split_value(h4s[3].string)[1] | |
if len(h4s) == 5 and not 'Click to view QR Code' in h4s[4].string: | |
conf.protocol = split_protocol_obfs(h4s[4].string)[0] | |
conf.obfs = split_protocol_obfs(h4s[4].string)[1] | |
confs.append(conf) | |
servers = [] | |
for conf in confs: | |
print conf | |
servers.append(conf.to_dict()) | |
conf_json = {} | |
if os.path.isfile('gui-config.json'): | |
conf_json = json.load(open('gui-config.json', 'r')) | |
conf_json['configs'] = servers | |
json.dump(conf_json, open('gui-config.json','w'), indent=4) | |
def restart_ss(): | |
ss = 'ShadowsocksR-dotnet4.0.exe' | |
os.system('taskkill /F /IM %s' % ss) | |
os.system('start %s' % ss) | |
def main(): | |
try: | |
refresh_passwords() | |
restart_ss() | |
print 'Done...' | |
except: | |
traceback.print_exc() | |
finally: | |
os.system('pause') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment