Skip to content

Instantly share code, notes, and snippets.

@zhylmzr
Created January 10, 2019 03:51
Show Gist options
  • Save zhylmzr/91fc2dce2d3e2abb54a7b5d67a4652f1 to your computer and use it in GitHub Desktop.
Save zhylmzr/91fc2dce2d3e2abb54a7b5d67a4652f1 to your computer and use it in GitHub Desktop.
gfw encode and decode
  • 解码gfwlist.txt文件
  • 编码pac规则为gfwlist格式
main.py -l inFile outFile
main.py -b inFile outFile
#!/usr/local/bin/python3
import re, base64, sys, os
def outList(fileUrl, outUrl):
inFile = open(fileUrl, 'r')
content = base64.b64decode(inFile.read().encode()).decode()
inFile.close()
outFile = open(outUrl, 'w')
outFile.write(content)
outFile.close()
def listToGFWList(fileUrl, outUrl):
inFile = open(fileUrl, 'r')
content = base64.b64encode(inFile.read().encode()).decode()
inFile.close()
outFile = open(outUrl, 'w')
# 每行长度64
partten = re.compile(r'.{64}')
lines = partten.findall(content)
for line in lines:
outFile.write(line + '\n')
# 剩下最后一行不足64长度
index = len(lines) * 64
outFile.write(content[index:] + '\n')
outFile.close()
def argvCheck():
if len(sys.argv) != 4:
print('Error: params incorrect.')
return False
return True
if len(sys.argv) >= 1:
if sys.argv[1] in ('-l', 'list'):
if argvCheck():
outList(sys.argv[2], sys.argv[3])
elif sys.argv[1] in ('-b', 'build'):
if argvCheck():
listToGFWList(sys.argv[2], sys.argv[3])
else:
print('''\
Usage: %s [-l list]
%s [-b build]\
''' % ((os.path.basename(sys.argv[0]), ) * 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment