Skip to content

Commit

Permalink
Merge pull request #1 from bafflingbug/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
bafflingbug authored Dec 8, 2017
2 parents 2329458 + 497037d commit 154b4e0
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,5 @@
### 写在最后
- 这个一个个人项目,没有做很多的兼容性判断。可能会在其他环境下无法运行
- python的运行环境是2.7
- ~~python的运行环境是2.7~~ 同时兼容python2.7及python3+
- 选择python和php的原因是我现在使用的两台服务器的运行环境
File renamed without changes.
65 changes: 47 additions & 18 deletions python/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,78 @@
from network import *
from ssrs import *
import sys
from py3 import py3


if __name__ == "__main__":
reload(sys)
sys.setdefaultencoding('utf-8')
if not py3:
reload(sys)
sys.setdefaultencoding('utf-8')
update = False
dir_path = os.path.dirname(os.path.realpath(__file__))
MD5_file = None
log_file = None
try:
MD5_file = open(dir_path + '/MD5.json', 'r')
fileMD5 = json.load(MD5_file)
log_file = open(dir_path + '/log.json', 'r')
logfile = json.load(log_file)
except:
fileMD5 = {}
logfile = {}
update = True
finally:
if MD5_file:
MD5_file.close()
update = new_config(dir_path + '/config.json', fileMD5, update)
if log_file:
log_file.close()
u = new_config(dir_path + '/config.json', logfile)
update = u if not update else update
config_file = open(dir_path + '/config.json', 'r')
config = json.load(config_file)
config_file.close()
group = getgroup('http://' if not config['ssl'] else 'https://' + config['main-server'] + '/group.php')
if 'group' not in fileMD5.keys() or group != fileMD5['group']:
if 'group' not in logfile.keys() or group != logfile['group']:
print(group)
update = True
fileMD5['group'] = group
logfile['group'] = group

ssr_url = ''

for ss_config in config['ss-config-file']:
update = new_config(ss_config['config-file'], fileMD5, update)
ssr_url += ss2URL(ss_config, config, group)
u = new_config(ss_config['config-file'], logfile)
update = u if not update else update
url = ss2URL(ss_config, config, group)
if url is not False:
ssr_url += url
if 'port' not in logfile[ss_config['config-file']].keys() or logfile[ss_config['config-file']]['port'] is not True:
update = True
logfile[ss_config['config-file']]['port'] = True
else:
if 'port' not in logfile[ss_config['config-file']].keys() or logfile[ss_config['config-file']]['port'] is not False:
update = True
logfile[ss_config['config-file']]['port'] = False

for ssr_config in config['ssr-config-file']:
update = new_config(ssr_config['config-file'], fileMD5, update)
ssr_url += ssr2URL(ssr_config, config, group)
u = new_config(ssr_config['config-file'], logfile)
update = u if not update else update
url = ssr2URL(ssr_config, config, group)
if url is not False:
ssr_url += url
if 'port' not in logfile[ssr_config['config-file']].keys() or logfile[ssr_config['config-file']]['port'] is not True:
update = True
logfile[ssr_config['config-file']]['port'] = True
else:
if 'port' not in logfile[ssr_config['config-file']].keys() or logfile[ssr_config['config-file']]['port'] is not False:
update = True
logfile[ssr_config['config-file']]['port'] = False
if update:
print('post')
print(ssr_url)
MD5_file = open(dir_path + '/MD5.json', 'w')
json.dump(fileMD5, MD5_file)
MD5_file = open(dir_path + '/log.json', 'w')
json.dump(logfile, MD5_file)
MD5_file.close()
res = post('http://' if not config['ssl'] else 'https://' + config['main-server'] + '/post.php', ssr_url,
config['token'], config['host'])
else:
print('active')
state = active('http://' if not config['ssl'] else 'https://' + config['main-server'] + '/active.php', config['token'], config['host'])
if state.find('error:2') >= 0:
MD5_file = open(dir_path + '/MD5.json', 'w')
MD5_file = open(dir_path + '/log.json', 'w')
MD5_file.truncate()
MD5_file.close()
os.system('python ' + dir_path + '/ssrs.py')
49 changes: 40 additions & 9 deletions python/network.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,52 @@
import urllib
import urllib2
import socket
from py3 import py3

if py3:
from urllib import request, parse
else:
import urllib
import urllib2


def portopen(ip,port):
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
s.connect((ip, int(port)))
s.shutdown(2)
print('%d is open' % port)
return True
except:
print('%d is down' % port)
return False


def getgroup(url):
res = urllib2.urlopen(url)
if py3:
res = request.urlopen(url)
else:
res = urllib2.urlopen(url)
return str(res.read())


def post(url, data, token, host):
parm = urllib.urlencode({'ssr': data, "token": token, "host": host})
req = urllib2.Request(url, parm)
res = urllib2.urlopen(req)
if py3:
data = parse.urlencode({'ssr': data, "token": token, "host": host}).encode('utf-8')
req = request.Request(url, data=data)
res = request.urlopen(req)
else:
parm = urllib.urlencode({'ssr': data, "token": token, "host": host})
req = urllib2.Request(url, parm)
res = urllib2.urlopen(req)
return str(res.read())


def active(url, token, host):
parm = urllib.urlencode({"token": token, "host": host})
req = urllib2.Request(url, parm)
res = urllib2.urlopen(req)
if py3:
data = parse.urlencode({"token": token, "host": host}).encode('utf-8')
req = request.Request(url, data=data)
res = request.urlopen(req)
else:
parm = urllib.urlencode({"token": token, "host": host})
req = urllib2.Request(url, parm)
res = urllib2.urlopen(req)
return str(res.read())
6 changes: 6 additions & 0 deletions python/py3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import sys

if sys.version > '3':
py3 = True
else:
py3 = False
14 changes: 10 additions & 4 deletions python/ssrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@
import hashlib
from network import *

def new_config(file, fileMD5, update):
def new_config(file, logfile):
config_file = open(file, 'rb')
md5 = hashlib.md5(config_file.read()).hexdigest()
config_file.close()
if file in fileMD5.keys() and fileMD5[file] == md5:
return update
fileMD5[file] = md5
if file not in logfile.keys():
logfile[file] = {}
if 'md5' in logfile[file].keys() and logfile[file]['md5'] == md5:
return False
logfile[file]['md5'] = md5
return True


def ssr2URL(ssr, config, group):
ssr_config_file = open(ssr['config-file'], 'r')
ssr_config = json.load(ssr_config_file)
if not portopen('127.0.0.1', ssr_config['server_port']):
return False
param_str = "obfsparam=" + base64.urlsafe_b64encode(ssr_config['obfs_param'].encode() if not ssr_config['obfs_param'] is None else ''.encode()).decode().rstrip('=')
if 'protocol_param' in ssr_config.keys() and (not ssr_config['protocol_param'] == ""):
param_str += '&protoparam=' + base64.urlsafe_b64encode(ssr_config['protocol_param'].encode()).decode().rstrip('=')
Expand All @@ -31,6 +35,8 @@ def ssr2URL(ssr, config, group):
def ss2URL(ss, config, group):
ss_config_file = open(ss['config-file'], 'r')
ss_config = json.load(ss_config_file)
if not portopen('127.0.0.1', ss_config['server_port']):
return False
param_str = "obfsparam=" + ''
if 'remarks' in ss.keys() and (not ss['remarks'] == ""):
param_str += '&remarks=' + base64.urlsafe_b64encode(ss['remarks'].encode()).decode().rstrip('=')
Expand Down

0 comments on commit 154b4e0

Please sign in to comment.