Skip to content

Instantly share code, notes, and snippets.

@flymop
Created April 22, 2018 01:11
Show Gist options
  • Save flymop/b6e710ecf7edcf9dd904bab12a7e8979 to your computer and use it in GitHub Desktop.
Save flymop/b6e710ecf7edcf9dd904bab12a7e8979 to your computer and use it in GitHub Desktop.
generate shadowsocks sip003 uri scheme and qrcode (only support obfs plugin)
# -*- coding: utf-8 -*-
#generate shadowsocks sip003 uri scheme and qrcode (only support obfs plugin)
import base64
import ipaddress
import json
import urllib.parse
import qrcode
# for check
must_fields = ["server", "server_port", "password", "method"]
# plugin config
useplugin = False
plugin = ""
obfs = "http"
obfs_host = "www.bing.com"
#read config file
with open("config_file.json", "r") as f:
config = json.load(f)
# check necessary field
for name in must_fields:
if name not in config.keys() and config[name]:
raise Exception("lack necessary field")
# plugin
if "plugin" in config.keys() and config["plugin"]:
useplugin = True
plugin = config["plugin"]
plugin_opts = dict(item.split("=") for item in config["plugin_opts"].split(";"))
if "obfs" in plugin_opts.keys():
obfs = plugin_opts["obfs"]
if "obfs-host" in plugin_opts.keys():
obfs_host = plugin_opts["obfs-host"]
else:
if "y" == input("active an obfs plugin? y/n:"):
useplugin = True
plugin = input("input obfs-sever or obfs-local:")
obfs_type = input("input obfs type:tls or http:")
if "tls" == obfs_type or "http" == obfs_type:
obfs = obfs_type
obfs_host = input("input obfs host:")
if useplugin and plugin != "obfs-server" and plugin != "obfs-local":
raise Exception("not a support plugin")
hostname = ""
try:
ip_obj = ipaddress.ip_address(config["server"])
if isinstance(ip_obj, ipaddress.IPv6Address):
hostname = "[" + config["server"] + "]"
except:
pass
if not hostname:
hostname = config["server"]
user_info = base64.b64encode((config["method"] + ":" + config["password"]).encode("utf-8"))
ss_uri = "ss://" + user_info.decode("utf-8") + "@" + hostname + ":" + str(config["server_port"])
if useplugin:
obfs_str = plugin + ";obfs=" + obfs + ";obfs-host=" + obfs_host
ss_uri += ("/?plugin=" + urllib.parse.quote_plus(obfs_str))
print("ss sip003 uri schema:" + ss_uri)
img = qrcode.make(ss_uri)
img.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment