【伟哥自用】tly.comのss链接转json
// ==UserScript==
// @name 【伟哥自用】tly.comのss链接转json
// @namespace http://tampermonkey.net/
// @version 0.1
// @description parse Shadowsocks subscription links from tly.com and generate json config outputs
// @author 朱焱伟
// @match https://tly70.com/modules/node_qr.php*
// @match https://www.tly12.com/modules/node_qr.php*
// @include https://tly*.com/modules/node_qr.php*
// @include https://www.tly*.com/modules/node_qr.php*
// @icon https://www.google.com/s2/favicons?sz=64&domain=tly12.com
// @grant GM_setClipboard
// ==/UserScript==
(function() {
'use strict';
class TlyDecoder{
fetchsslink(){
return document.querySelector("p#ssqr_text").textContent;
}
ss2arr(sslink){
let data = "";
if(String(sslink).startsWith('ss://')){
data = sslink.slice(5);
}
/* https://stackoverflow.com/questions/246801/how-can-you-encode-a-string-to-base64-in-javascript/247261#247261
* atob : decode BASE64 string
*/
let encodedString = atob(data);
let arr = encodedString.split(':');
return arr;
}
sslinkDecode(sslink) {/** resolve ss://XXXX */
let arr = this.ss2arr(sslink);
let method = arr[0];
let ip = arr[1].split('@')[1];
let password = arr[1].split('@')[0];
let port = Number(arr[2]);
/** hardcoded default sslink config:
* local_address "127.0.0.1"
* local_port 1080
* timeout: 300
* (you can modify them if you like)
*/
let ssconf_obj = {
server:ip,
server_port:port,
password:password,
local_address: "127.0.0.1",
local_port: 1080,
timeout: 300,
method:method
}
let ssConfig_str = JSON.stringify(ssconf_obj,null,4);
return ssConfig_str;
}
test(){
/** sslink example: ss://YWVzLTI1Ni1nY206MTIzMTIzQGZ1Y2tsaWhlbmdkYW8uY29tOjY2NjY=
* btoa("aes-256-gcm:123123@fucklihengdao.com:6666")
* 'YWVzLTI1Ni1nY206MTIzMTIzQGZ1Y2tsaWhlbmdkYW8uY29tOjY2NjY='
*/
console.log(this.sslinkDecode("ss://YWVzLTI1Ni1nY206MTIzMTIzQGZ1Y2tsaWhlbmdkYW8uY29tOjY2NjY="));
}
}
function getCopyContent(){
let tlydecoder = new TlyDecoder();
/* tlydecoder.test(); */
let sslink = tlydecoder.fetchsslink();
let to_copy = tlydecoder.sslinkDecode(sslink);
return to_copy
}
function AddJsonCodeArea(json_text){
let jsonCodeArea = document.createElement("p");
/**
* https://stackoverflow.com/questions/883977/display-json-as-html
* If you are deliberately displaying it for the end user, wrap the JSON text in <PRE> and <CODE> tags
*/
jsonCodeArea.innerHTML= "<pre><code>"+json_text+"</code></pre>";
document.body.append(jsonCodeArea);
}
function AddCopyButton(json_text){
let btn = document.createElement("button");
btn.innerHTML="一键复制";
GM_setClipboard(json_text)
document.body.append(btn);
}
let to_copy = getCopyContent();
console.log(to_copy);
AddJsonCodeArea(to_copy);
AddCopyButton(to_copy);
})();
/**
* ==================================================================================================
* A UserScript to parse Shadowsocks subscription links from tly.com and generate json config outputs
* ==================================================================================================
* The local_port and timeout in the shadowsocks settings is hardcoded,
* the default is 1080 for local port and 300 for timeout.
*
* - sudo systemctl start shadowsocks-libev-local@config
* - sudo systemctl stop shadowsocks-libev-local@config
* - sudo gedit /etc/shadowsocks-libev/config.json
*
* I am currently using privoxy and shadowsocks-libev, but I'm tired of
* converting ss://XXX links on tly.com to formatted JSON in terminal,
* that's the reason why I wrote this userscript.
*
* References:
* - https://github.com/OliverLew/ssr-decode
* - https://github.com/HTML50/ssr2json
* - https://stackoverflow.com/questions/883977/display-json-as-html
*
*/