Skip to content

Instantly share code, notes, and snippets.

@jizusun
Last active April 4, 2020 13:45
Show Gist options
  • Save jizusun/3b6b1ac3166c40748d85b7cc3051d7c4 to your computer and use it in GitHub Desktop.
Save jizusun/3b6b1ac3166c40748d85b7cc3051d7c4 to your computer and use it in GitHub Desktop.
SSR 链接解析
// https://github.com/shadowsocksr-backup/shadowsocks-rss/wiki/SSR-QRcode-scheme
// https://stackoverflow.com/a/30106551/3074866
function b64DecodeUnicode(str) {
return decodeURIComponent(Array.prototype.map.call(atob(str), function(c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)
}).join(''))
}
decodeUrlSafeBase64 = (sBase64) => {
return b64DecodeUnicode(sBase64.replace(/_/g, '/').replace(/-/g, '+'))
}
parseSsrLink = (sUrl) => {
if (!sUrl.match("^ssr:.*")) {
return "Invalid SSR link."
}
sUrl = sUrl.replace("ssr://", '')
const sResult = decodeUrlSafeBase64(sUrl),
aConfig = sResult.split('/?'),
aRequired = aConfig[0].split(":"),
aOptional = aConfig[1].split('&').map((item) => item.split("=")),
oRequired = {
host: aRequired[0],
port: aRequired[1],
protocol: aRequired[2],
method: aRequired[3],
obfs: aRequired[4],
pass: decodeUrlSafeBase64(aRequired[5]),
};
let oOptional = {};
aOptional.forEach(function(data){
try {
oOptional[data[0]] = decodeUrlSafeBase64(data[1])
} catch (e) {
console.log(data)
}
});
// ssr://base64(host:port:protocol:method:obfs:base64pass/?obfsparam=base64param&protoparam=base64param&remarks=base64remarks&group=base64group&udpport=0&uot=0)
return {
required: oRequired,
optional: oOptional
};
}
// sUrl = 'ssr://MTI3LjAuMC4xOjEyMzQ6YXV0aF9hZXMxMjhfbWQ1OmFlcy0xMjgtY2ZiOnRsczEuMl90aWNrZXRfYXV0aDpZV0ZoWW1KaS8_b2Jmc3BhcmFtPVluSmxZV3QzWVRFeExtMXZaUSZyZW1hcmtzPTVyV0w2Sy1WNUxpdDVwYUg'
// parseSsrLink(sUrl)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment