Skip to content

Instantly share code, notes, and snippets.

@pd12bb
Last active March 12, 2022 11:55
Show Gist options
  • Select an option

  • Save pd12bb/4e3bfda3d5081e505dc7965886868830 to your computer and use it in GitHub Desktop.

Select an option

Save pd12bb/4e3bfda3d5081e505dc7965886868830 to your computer and use it in GitHub Desktop.
subencode
#!/bin/bash
## 订阅生成脚本,参数1为节点信息的jsonc 列表,参数2为输出
urlencode() {
# urlencode <string>
old_lang=$LANG
LANG=C
old_lc_collate=$LC_COLLATE
LC_COLLATE=C
local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf "$c" ;;
*) printf '%%%02X' "'$c" ;;
esac
done
LANG=$old_lang
LC_COLLATE=$old_lc_collate
}
raw=$(cat "$1" | sed -e 's/:\/\//"""/g' -e 's/\/\/.*//g' -e 's/"""/:\/\//g' -e 's/[[:blank:]]*$//g' -e 's/^[[:blank:]]*//g' | tr -d '\n' | sed -e 's/}{/}\n{/g')
target="$2"
number=$(echo "$raw" | grep -c '{')
for ((i=1;i<="$number";i++)); do
data=$(echo "$raw" | sed -n -e "${i} p")
if [[ -n "$(echo -n "$data" | grep '"ss"')" ]]; then
ss=$(echo -n "$data" | sed -e 's/\",\"/\"\n\"/g' -e 's/^{//g' -e 's/}$//g' | grep '"ss":' | sed -e 's/.*\"ss\"://g' -e 's/^[[:blank:]]*\"//g' -e 's/\"[[:blank:]]*$//g' | tr -d '\n' | base64 -w 0)
server=$(echo -n "$data" | sed -e 's/\",\"/\"\n\"/g' -e 's/^{//g' -e 's/}$//g' | grep '"server":' | sed -e 's/.*\"server\"://g' -e 's/^[[:blank:]]*\"//g' -e 's/\"[[:blank:]]*$//g')
name=$(urlencode "$(echo -n "$data" | sed -e 's/\",\"/\"\n\"/g' -e 's/^{//g' -e 's/}$//g' | grep '"ps":' | sed -e 's/.*\"ps\"://g' -e 's/^[[:blank:]]*\"//g' -e 's/\"[[:blank:]]*$//g')")
subscribe[$i]="ss://${ss}@${server}#${name}"
elif [[ -n "$(echo -n "$data" | grep '"raw"')" ]]; then
subscribe[$i]="$(echo -n "$data" | sed -e 's/\",\"/\"\n\"/g' -e 's/^{//g' -e 's/}$//g' | grep '"raw":' | sed -e 's/.*\"raw\"://g' -e 's/^[[:blank:]]*\"//g' -e 's/\"[[:blank:]]*$//g')"
else
encoded=$(echo "$data" | base64 -w 0)
subscribe[$i]="vmess://${encoded}"
fi
done
echo ${subscribe[@]} | tr ' ' '\n' | base64 -w 0 > "${target}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment