Skip to content

Instantly share code, notes, and snippets.

@BenSYZ
Created August 17, 2021 19:10
Show Gist options
  • Save BenSYZ/fd334542d07ccde9dd72612292c586a5 to your computer and use it in GitHub Desktop.
Save BenSYZ/fd334542d07ccde9dd72612292c586a5 to your computer and use it in GitHub Desktop.
####################################################
# the _ after password filed will cause error, change it with 6, after decode there will be a '/' in the string, so
# if without the last '=' invalid. `echo -n "$str"| base64 -d |base64` give me an extra '=', so added
# /_/6/: after password fieled
# /\///: after password field
# /-/+/: remark 韩国
#string1=$(tail -n1 ss |sed 's/_/6/g' |sed 's/$/=/' |base64 -d |sed 's/\///'| sed 's/-/+/g')
# base 64 decoded string, must be divisible by 4, if not appending with =
# https://www.ruanyifeng.com/blog/2008/06/base64.html
# 1. BASE decode ->
#ssr://SSR(encoded)
# before decode SSR, SSR needs:
# replasing _ with 6, resulting password field end with /:
# 2. SSR decode ->
#server:port:protocol:encryption_method:obfuscation:PASSWORD(encoded)/[:?]obfsparam=OBFSPARAM_VAL(encoded)&protoparam=PROTOPARAM_VAL(encoded)&remarks=REMARKS_VAL(encoded)&group=GROUP_VAL(encoded)
# before /, parameters for ss
# after /, extra parameters for ssr
# remove the begin [:?] and then, replace & with ; so that you can use eval
# remark field: replacing - with + for Korea, and _ with / for Canada
make_json(){
local export_type="$1"
local remarks="$2"
local server="$3"
local port="$4"
local encryption_method="$5"
local password="$6"
json+=$(cat <<-END
{
"method": "$encryption_method",
"password": "$password",
"remarks": "$remarks",
"server": "$server",
"server_port": $port,
END
)
if [[ "$export_type" == "android" ]];then
json+=$(cat <<-END
"route": "bypass-lan-china",
"remote_dns": "dns.google",
"ipv6": false,
"metered": false,
"proxy_apps": {
"enabled": true,
"bypass": false
},
"udpdns": false
},
END
)
#elif [[ "$export_type" == "ss-qt5" ]];then
else
json="${json%%,}"
json+="},"
fi
}
write_sslocal_json(){
local remarks="$1"
local server="$2"
local port="$3"
local encryption_method="$4"
local password="$5"
json+=$(cat <<-END
{
"server": "$server",
"server_port": $port,
"local_address": "127.0.0.1",
"local_port":8080,
"password": "$password",
"timeout":600,
"method": "$encryption_method",
},
END
)
}
append_equal_sign(){
local string="$1"
local residue=$(expr $(echo -n "$string" |wc -c) % 4)
#echo $residue
#echo $residue
#residue=0 # 0
#residue=1 # 3
#residue=2 # 2
#residue=3 # 1
if [ "$residue" -ne 0 ];then
for i in $( seq 3 -1 "$residue" );do
string+='='
done
fi
#echo $residue: $string
echo $string
}
parse_subscription(){
local url="$1"
local export_type="$2"
#ss_txt=$(cat ./ss.txt)
ss_txt=$(curl "$url")
#echo $ss_txt
ss_txt=$(append_equal_sign "$ss_txt")
SSRs=$(echo -n "$ss_txt" |base64 -d |awk -v 'FS=//' '{print $2}')
i=0
while read SSR;do
SSR=$(append_equal_sign "$SSR")
# password filed end
SSR=$(echo -n "$SSR" | sed 's/_/6/g')
#echo "$SSR"
# remark in South Korea and Canda
SSR_decoded=$(echo -n "$SSR"| base64 -d | sed 's/-/+/g; s/_/\//g')
SS_decoded=$(echo -n "$SSR_decoded"|awk -v "FS=/[:?]" '{print $1}')
SS_decoded=${SS_decoded/ /}
#echo $SS_decoded
#echo $SSR_decoded
read server port protocol encryption_method obfuscation password < <( echo -n "$SS_decoded"|\
sed -n 's/\([a-z0-9.]*\):\([0-9]*\):\([a-z]*\):\([a-z0-9A-Z+-]*\):\([A-Za-z_/]*\):\(.*\)$/\1 \2 \3 \4 \5 \6/p')
password=$(echo $password|base64 -d)
#echo $server
#i=$(( $i+1 ))
#if [ $i -eq 10 ];then
# break
#fi
# sed 1. remove the begin trash word.
# 2. var=val& -> var=val;
#echo $SSR_decoded
eval $(echo -n "$SSR_decoded" |awk -v 'FS=/[:?]' '{print $2}'|sed 's/^[:?]//; s/&/; /g') || exit 0
obfsparam=$(append_equal_sign "$obfsparam" )
protoparam=$(append_equal_sign "$protoparam")
remarks=$(append_equal_sign "$remarks" )
group=$(append_equal_sign "$group" )
obfsparam=$(echo -n "$obfsparam" | base64 -d)
protoparam=$(echo -n "$protoparam" | base64 -d)
remarks=$(echo -n "$remarks" | base64 -d)
group=$(echo -n "$group" | base64 -d)
#obfsparam=${obfsparam/ /}
#protoparam=${protoparam/ /}
#remarks=${remarks/ /}
#group=${group/ /}
#echo $remarks
#obfsparam=$(echo -n "$obfsparam" | base64 -d)
#echo obf:$obfsparam
#echo ---
#protoparam=$(echo -n "$protoparam" | base64 -d)
#echo prop:$protoparam
#echo ---
#echo $SSR_decoded
#remarks=$(echo -n "$remarks" | base64 -d)
#echo rem:$remarks
#echo ---
#group=$(echo -n "$group" | base64 -d)
#echo gro:$group
#echo ---
#echo $server $port $protocol $encryption_method $obfuscation $password '|' "$obfsparam" "$protoparam" "$remarks" "$group"
make_json "$export_type" "$remarks" "$server" "$port" "$encryption_method" "$password"
done < <(echo "$SSRs")
json="${json%%,}"
}
main(){
local url="$1"
local export_type="$2"
local export_file="$3"
#export_type="ss-qt5"
#export_file="ss-android.json"
if [[ "$export_type" == "android" ]];then
json='['
#elif [[ "$config_type" == "ss-qt5" ]];then
else
json='{"configs":['
fi
parse_subscription "$url" "$export_type" "$export_file"
if [[ "$export_type" == "android" ]];then
json+=']'
#elif [[ "$config_type" == "ss-qt5" ]];then
else
json+='],
"localPort": 8080,
"shareOverLan": false}'
fi
#echo "$json" > "a.json"
echo "$json" |jq > "$export_file"
}
main "$1" "$2" "$3"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment