Skip to content

Instantly share code, notes, and snippets.

@jarrekk
Last active July 6, 2017 07:55
Show Gist options
  • Save jarrekk/740c9723f91865aad8bdac08384c70ff to your computer and use it in GitHub Desktop.
Save jarrekk/740c9723f91865aad8bdac08384c70ff to your computer and use it in GitHub Desktop.
Add/delete/query domain in shadowsocks/gfwlist.js
#!/usr/bin/env python
from __future__ import print_function
import getopt
import sys
import os
filePath = "/Users/jack/Dropbox/Sync/ShadowsocksX/gfwlist.js"
try:
opts, args = getopt.getopt(sys.argv[1:], "a:d:q:h:", ["help=", "add=", "delete=", "query="])
except getopt.GetoptError as e:
opts = None
print("Invalid parameter.\nPlease use python gflist.py -a/-d/-q domain.")
sys.exit(1)
else:
if not opts:
print("Invalid parameter.\nPlease use python gflist.py -a/-d/-q domain.")
sys.exit(1)
for opt, value in opts:
if opt in ("-h", "--help"):
print("python gflist.py -a/-d domain.")
sys.exit()
if opt in ("-a", "--add"):
action = "add"
domain = value
if opt in ("-d", "--delete"):
action = "delete"
domain = value
if opt in ("-q", "--query"):
action = "query"
domain = value
if action == "add":
try:
with open(filePath, "r") as f:
content = f.readlines()
line = content.index(" \"||google.com\",\n")
content.insert(line + 1, " \"||{0}\",\n".format(domain))
except Exception as e:
print(e)
else:
os.remove(filePath)
with open(filePath, "w") as f:
f.writelines(content)
print("Domain {0} add success!".format(domain))
if action == "delete":
try:
with open(filePath, "r") as f:
content = f.readlines()
content.remove(" \"||{0}\",\n".format(domain))
except Exception as e:
print(e)
else:
os.remove(filePath)
with open(filePath, "w") as f:
f.writelines(content)
print("Domain {0} delete success!".format(domain))
if action == "query":
count = 0
try:
with open(filePath, "r") as f:
for line in f.readlines():
if domain in line:
count += 1
print(str(count) + ":" + line, end="")
except Exception as e:
print(e)
finally:
if not count:
print("No match domain.\nYou can add it with -a option.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment