<%+cbi/valueheader%>
<script type="text/javascript">//<![CDATA[
	function padright(str, cnt, pad) {
		return str + Array(cnt + 1).join(pad);
	}
	function b64EncodeUnicode(str) {
		return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function (match, p1) {
			return String.fromCharCode('0x' + p1);
		}));
	}
	function b64encutf8safe(str) {
		return b64EncodeUnicode(str).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/g, '');
	}
	function b64DecodeUnicode(str) {
		return decodeURIComponent(Array.prototype.map.call(atob(str), function (c) {
			return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
		}).join(''));
	}
	function b64decutf8safe(str) {
		var l;
		str = str.replace(/-/g, "+").replace(/_/g, "/");
		l = str.length;
		l = (4 - l % 4) % 4;
		if (l)
			str = padright(str, l, "=");
		return b64DecodeUnicode(str);
	}
	function b64encsafe(str) {
		return btoa(str).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/g, '')
	}
	function b64decsafe(str) {
		var l;
		str = str.replace(/-/g, "+").replace(/_/g, "/");
		l = str.length;
		l = (4 - l % 4) % 4;
		if (l)
			str = padright(str, l, "=");
		return atob(str);
	}
	function dictvalue(d, key) {
		var v = d[key];
		if (typeof (v) == 'undefined' || v == '')
			return '';
		return b64decsafe(v);
	}


	function import_ssr_url(btn, urlname, sid) {
		var s = document.getElementById(urlname + '-status');
		if (!s)
			return false;
		var ssrurl = prompt("ssr:// | ss:// | vmess:// | trojan://", "");
		if (ssrurl == null || ssrurl == "") {
			s.innerHTML = "<font color='red'><%:Operation Aborted%></font>";
			return false;
		}
		s.innerHTML = "";
		//var ssu = ssrurl.match(/ssr:\/\/([A-Za-z0-9_-]+)/i);
		var ssu = ssrurl.split('://');
		console.log(ssu.length);
		if ((ssu[0] != "ssr" && ssu[0] != "ss" && ssu[0] != "vmess" && ssu[0] != "trojan") || ssu[1] == "") {
			s.innerHTML = "<font color='red'><%:Invalid link%></font>";
			return false;
		}

		var event = document.createEvent("HTMLEvents");
		event.initEvent("change", true, true);
		if (ssu[0] == "ssr") {
			var sstr = b64decsafe(ssu[1]);
			var ploc = sstr.indexOf("/?");
			document.getElementById('cbid.clash.' + sid + '.type').value = "ssr";
			document.getElementById('cbid.clash.' + sid + '.type').dispatchEvent(event);
			var url0, param = "";
			if (ploc > 0) {
				url0 = sstr.substr(0, ploc);
				param = sstr.substr(ploc + 2);
			}
			var ssm = url0.match(/^(.+):([^:]+):([^:]*):([^:]+):([^:]*):([^:]+)/);
			if (!ssm || ssm.length < 7)
				return false;
			var pdict = {};
			if (param.length > 2)
			{
				var a = param.split('&');
				for (var i = 0; i < a.length; i++) {
					var b = a[i].split('=');
					pdict[decodeURIComponent(b[0])] = decodeURIComponent(b[1] || '');
				}
			}
			document.getElementById('cbid.clash.' + sid + '.server').value = ssm[1];
			document.getElementById('cbid.clash.' + sid + '.port').value = ssm[2];
			document.getElementById('cbid.clash.' + sid + '.protocol').value = ssm[3];
			document.getElementById('cbid.clash.' + sid + '.cipher_ssr').value = ssm[4];
			document.getElementById('cbid.clash.' + sid + '.obfs_ssr').value = ssm[5];
			document.getElementById('cbid.clash.' + sid + '.password').value = b64decsafe(ssm[6]);
			document.getElementById('cbid.clash.' + sid + '.obfsparam').value = dictvalue(pdict, 'obfsparam');
			document.getElementById('cbid.clash.' + sid + '.protocolparam').value = dictvalue(pdict, 'protoparam');
			var rem = pdict['remarks'];
			if (typeof (rem) != 'undefined' && rem != '' && rem.length > 0)
			document.getElementById('cbid.clash.' + sid + '.name').value = b64decutf8safe(rem);
			s.innerHTML = "<font color='green'><%:Import ShadowsocksR config info successful%></font>";
			return false;
		} else if (ssu[0] == "ss") {

				
				var sipIndex = ssu[1].split("@");
				var sstr = b64decsafe(sipIndex[0]);
				var ssm = sstr.split(':');
				var team = sipIndex[1].split(':');

				//var obfs = port[1].split('=');
				var temp = ssu[1].split("?");
				
				if (temp[1]) {
					var pluginInfo = decodeURIComponent(temp[1]);
				}

				document.getElementById('cbid.clash.' + sid + '.type').value = "ss";
				document.getElementById('cbid.clash.' + sid + '.type').dispatchEvent(event);
				document.getElementById('cbid.clash.' + sid + '.server').value = team[0];
				document.getElementById('cbid.clash.' + sid + '.password').value = ssm[1];
				document.getElementById('cbid.clash.' + sid + '.cipher').value = ssm[0];
				
				if (temp[1]) {
				var name = pluginInfo.split("#");
				var port = team[1].split('?');
				var obfslocal = name[0].split(';')
				var obfs = obfslocal[1].split('=')
				var obfshost = obfslocal[2].split('=')
				document.getElementById('cbid.clash.' + sid + '.port').value = port[0];
				document.getElementById('cbid.clash.' + sid + '.name').value = name[1];
				document.getElementById('cbid.clash.' + sid + '.obfs').value = obfs[1];
				document.getElementById('cbid.clash.' + sid + '.obfs').dispatchEvent(event);				
				document.getElementById('cbid.clash.' + sid + '.host').value = obfshost[1];
				}
				if (!temp[1]) {
				var name2 = sipIndex[1].split('#');
				var port = name2[0].split(':');
				document.getElementById('cbid.clash.' + sid + '.port').value = port[1];
				document.getElementById('cbid.clash.' + sid + '.name').value = name2[1];
				document.getElementById('cbid.clash.' + sid + '.obfs').value = " ";
				document.getElementById('cbid.clash.' + sid + '.obfs').dispatchEvent(event);
				}
				
				s.innerHTML = "<font color='green'><%:Import Shadowsocks config info successful%></font>";
				return false;
				
		} else if (ssu[0] == "trojan") {
		
			var url0, param = "";
			var ploc = ssu[1].indexOf("#");
			if (ploc > 0) {
				url0 = ssu[1].substr(0, ploc);
				param = ssu[1].substr(ploc + 1);
			} else {
				url0 = ssu[1]
			}
			var sstr = url0;
			var name = ssrurl.split('#');
			document.getElementById('cbid.clash.' + sid + '.type').value = "trojan";
			document.getElementById('cbid.clash.' + sid + '.type').dispatchEvent(event);
			var team = sstr.split('@');
			var password = team[0]
			var serverPart = team[1].split(':');
			var others = serverPart[1].split('?');
			var port = others[0]
			var queryParam = {}
			if(others.length > 1) {
				var queryParams = others[1]
				var queryArray = queryParams.split('&')
				for (i = 0; i < queryArray.length; i++) {
					var params = queryArray[i].split('=');
					queryParam[decodeURIComponent(params[0])] = decodeURIComponent(params[1] || '');
				}
			}
			document.getElementById('cbid.clash.' + sid + '.name').value = name[1];
			document.getElementById('cbid.clash.' + sid + '.server').value = serverPart[0];
			document.getElementById('cbid.clash.' + sid + '.port').value = port;
			document.getElementById('cbid.clash.' + sid + '.password').value = password;
			document.getElementById('cbid.clash.' + sid + '.sni').value = serverPart[0];
			document.getElementById('cbid.clash.' + sid + '.alpn_http').checked = true;
			document.getElementById('cbid.clash.' + sid + '.alpn_h2').checked = false;
			
			
			
			s.innerHTML = "<font color='green'><%:Import Trojan config info successful%></font>";
			return false;
		} else if (ssu[0] == "vmess") {
			var sstr = b64DecodeUnicode(ssu[1]);
			var ploc = sstr.indexOf("/?");
			document.getElementById('cbid.clash.' + sid + '.type').value = "vmess";
			document.getElementById('cbid.clash.' + sid + '.type').dispatchEvent(event);
			var url0, param = "";
			if (ploc > 0) {
				url0 = sstr.substr(0, ploc);
				param = sstr.substr(ploc + 2);
			}
			var ssm = JSON.parse(sstr);
			document.getElementById('cbid.clash.' + sid + '.name').value = ssm.ps;
			document.getElementById('cbid.clash.' + sid + '.server').value = ssm.add;
			document.getElementById('cbid.clash.' + sid + '.port').value = ssm.port;
			document.getElementById('cbid.clash.' + sid + '.alterId').value = ssm.aid;
			document.getElementById('cbid.clash.' + sid + '.uuid').value = ssm.id;
			

			
			if (ssm.net == "tcp") {
			document.getElementById('cbid.clash.' + sid + '.obfs_vmess').value = "none";
			document.getElementById('cbid.clash.' + sid + '.obfs_vmess').dispatchEvent(event);
			
			if (ssm.tls == "tls" || ssm.tls == "1" || ssm.tls == "true") {
			document.getElementById('cbid.clash.' + sid + '.tls').value = "true";
			document.getElementById('cbid.clash.' + sid + '.tls').dispatchEvent(event);
			}
			
			}
			
			if (ssm.net == "ws") {
			document.getElementById('cbid.clash.' + sid + '.obfs_vmess').value = "websocket";
			document.getElementById('cbid.clash.' + sid + '.obfs_vmess').dispatchEvent(event);
			document.getElementById('cbid.clash.' + sid + '.path').value = ssm.path;
			document.getElementById('cbid.clash.' + sid + '.custom').value = ssm.host;	

			if (ssm.tls == "tls" || ssm.tls == "1" || ssm.tls == "true") {
			document.getElementById('cbid.clash.' + sid + '.tls').value = "true";
			document.getElementById('cbid.clash.' + sid + '.tls').dispatchEvent(event);
			}
			
			}
			if (ssm.net == "h2") {
			document.getElementById('cbid.clash.' + sid + '.obfs_vmess').value = "http";
			document.getElementById('cbid.clash.' + sid + '.obfs_vmess').dispatchEvent(event);
			document.getElementById('cbid.clash.' + sid + '.http_path').value = ssm.path;
			document.getElementById('cbid.clash.' + sid + '.keep_alive').checked = true;
			document.getElementById('cbid.clash.' + sid + '.keep_alive').dispatchEvent(event);	

			if (ssm.tls == "tls" || ssm.tls == "1" || ssm.tls == "true") {
			document.getElementById('cbid.clash.' + sid + '.tls').value = "true";
			document.getElementById('cbid.clash.' + sid + '.tls').dispatchEvent(event);
			}
			
			}


			s.innerHTML = "<font color='green'><%:Import V2ray config info successful%></font>";
			return false;
		}
	}
//]]></script>
<input type="button" class="cbi-button cbi-button-apply" value="<%:Import config info%>" onclick="return import_ssr_url(this, '<%=self.option%>', '<%=self.value%>')" />
<span id="<%=self.option%>-status"></span>
<%+cbi/valuefooter%>
