<%+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 export_ssr_url(btn, urlname, sid) {
	var s = document.getElementById(urlname + '-status');
	if (!s) return false;
	var v_server = document.getElementsByName('cbid.bypass.' + sid + '.server')[0];
	var v_port = document.getElementsByName('cbid.bypass.' + sid + '.server_port')[0];
	var v_protocol = document.getElementsByName('cbid.bypass.' + sid + '.protocol')[0];
	var v_method = document.getElementsByName('cbid.bypass.' + sid + '.encrypt_method')[0];
	var v_obfs = document.getElementsByName('cbid.bypass.' + sid + '.obfs')[0];
	var v_password = document.getElementsByName('cbid.bypass.' + sid + '.password')[0];
	var v_obfs_param = document.getElementsByName('cbid.bypass.' + sid + '.obfs_param')[0];
	var v_protocol_param = document.getElementsByName('cbid.bypass.' + sid + '.protocol_param')[0];
	var v_alias = document.getElementsByName('cbid.bypass.' + sid + '.alias')[0];
	var ssr_str = v_server.value + ":" + v_port.value + ":" + v_protocol.value + ":" + v_method.value + ":" + v_obfs.value + ":" + b64encsafe(v_password.value) + "/?obfsparam=" + b64encsafe(v_obfs_param.value) + "&protoparam=" + b64encsafe(v_protocol_param.value) + "&remarks=" + b64encutf8safe(v_alias.value);
	var textarea = document.createElement("textarea");
	textarea.textContent = "ssr://" + b64encsafe(ssr_str);
	textarea.style.position = "fixed";
	document.body.appendChild(textarea);
	textarea.select();
	try {
		document.execCommand("copy"); // Security exception may be thrown by some browsers.
		s.innerHTML = "<font color='green'><%:Copy SSR to clipboard successfully.%></font>";
	} catch (ex) {
		s.innerHTML = "<font color='red'><%:Unable to copy SSR to clipboard.%></font>";
	} finally {
		document.body.removeChild(textarea);
	}
	return false;
}

function import_ssr_url(btn, urlname, sid) {
		var s = document.getElementById(urlname + '-status');
		if (!s) return false;
		var ssrurl = prompt("<%:Paste sharing link here%>", "");
		if (ssrurl == null || ssrurl == "") {
			s.innerHTML = "<font color='red'><%:User cancelled.%></font>";
			return false;
		}
		s.innerHTML = "";
		//var ssu = ssrurl.match(/ssr:\/\/([A-Za-z0-9_-]+)/i);
		var ssu = ssrurl.split('://');
		//console.log(ssu.length);
		var event = document.createEvent("HTMLEvents");
		event.initEvent("change", true, true);
		switch (ssu[0]) {
		case "hysteria":
			try {
				var url = new URL("http://" + ssu[1]);
				var params = url.searchParams;
			} catch(e) {
				alert(e);
				return false;
			}

			document.getElementsByName('cbid.bypass.' + sid + '.type')[0].value = ssu[0];
			document.getElementsByName('cbid.bypass.' + sid + '.type')[0].dispatchEvent(event);
			document.getElementsByName('cbid.bypass.' + sid + '.server')[0].value = url.hostname;
			document.getElementsByName('cbid.bypass.' + sid + '.server_port')[0].value = url.port || "80";
			document.getElementsByName('cbid.bypass.' + sid + '.hysteria_protocol')[0].value = params.get("protocol") || "udp";
			document.getElementsByName('cbid.bypass.' + sid + '.auth_type')[0].value = params.get("auth") ? "2" : "0";
			document.getElementsByName('cbid.bypass.' + sid + '.auth_type')[0].dispatchEvent(event);
			document.getElementsByName('cbid.bypass.' + sid + '.auth_payload')[0].value = params.get("auth") || "";
			document.getElementsByName('cbid.bypass.' + sid + '.uplink_capacity')[0].value = params.get("upmbps") || "";
			document.getElementsByName('cbid.bypass.' + sid + '.downlink_capacity')[0].value = params.get("downmbps") || "";
			document.getElementsByName('cbid.bypass.' + sid + '.seed')[0].value = params.get("obfsParam") || "";
			document.getElementsByName('cbid.bypass.' + sid + '.tls_host')[0].value = params.get("peer") || "";
			document.getElementsByName('cbid.bypass.' + sid + '.quic_tls_alpn')[0].value = params.get("alpn") || "";
			document.getElementsByName('cbid.bypass.' + sid + '.insecure')[0].checked = params.get("insecure") ? true : false;
			document.getElementsByName('cbid.bypass.' + sid + '.alias')[0].value = url.hash ? decodeURIComponent(url.hash.slice(1)) : "";

			s.innerHTML = "<font color='green'><%:Import configuration information successfully.%></font>";
			return false;
		case "ss":
			var url0, param = "";
			var sipIndex = ssu[1].indexOf("@");
			var ploc = ssu[1].indexOf("#");
			if (ploc > 0) {
				url0 = ssu[1].substr(0, ploc);
				param = ssu[1].substr(ploc + 1);
			} else {
				url0 = ssu[1];
			}
			if (sipIndex != -1) {
				// SIP002
				var userInfo = b64decsafe(url0.substr(0, sipIndex));
				var temp = url0.substr(sipIndex + 1).split("/?");
				var serverInfo = temp[0].split(":");
				var server = serverInfo[0];
				var port = serverInfo[1].replace("/","");
				var method, password, plugin, pluginOpts;
				if (temp[1]) {
					var pluginInfo = decodeURIComponent(temp[1]);
					var pluginIndex = pluginInfo.indexOf(";");
					var pluginNameInfo = pluginInfo.substr(0, pluginIndex);
					plugin = pluginNameInfo.substr(pluginNameInfo.indexOf("=") + 1);
					pluginOpts = pluginInfo.substr(pluginIndex + 1);
				}
				var userInfoSplitIndex = userInfo.indexOf(":");
				if (userInfoSplitIndex != -1) {
					method = userInfo.substr(0, userInfoSplitIndex);
					password = userInfo.substr(userInfoSplitIndex + 1);
				}
				document.getElementsByName('cbid.bypass.' + sid + '.type')[0].value = ssu[0];
				document.getElementsByName('cbid.bypass.' + sid + '.type')[0].dispatchEvent(event);
				document.getElementsByName('cbid.bypass.' + sid + '.server')[0].value = server;
				document.getElementsByName('cbid.bypass.' + sid + '.server_port')[0].value = port;
				document.getElementsByName('cbid.bypass.' + sid + '.password')[0].value = password || "";
				document.getElementsByName('cbid.bypass.' + sid + '.encrypt_method_ss')[0].value = method || "";
				document.getElementsByName('cbid.bypass.' + sid + '.plugin')[0].value = plugin || "none";
				document.getElementsByName('cbid.bypass.' + sid + '.plugin')[0].dispatchEvent(event);
				if (plugin != undefined) {
				document.getElementsByName('cbid.bypass.' + sid + '.plugin_opts')[0].value = pluginOpts || "";
				}
				if (param != undefined) {
					document.getElementsByName('cbid.bypass.' + sid + '.alias')[0].value = decodeURI(param);
				}
				s.innerHTML = "<font color='green'><%:Import configuration information successfully.%></font>";
			} else {
				var sstr = b64decsafe(url0);
				document.getElementsByName('cbid.bypass.' + sid + '.type')[0].value = ssu[0];
				document.getElementsByName('cbid.bypass.' + sid + '.type')[0].dispatchEvent(event);
				var team = sstr.split('@');
				var part1 = team[0].split(':');
				var part2 = team[1].split(':');
				document.getElementsByName('cbid.bypass.' + sid + '.server')[0].value = part2[0];
				document.getElementsByName('cbid.bypass.' + sid + '.server_port')[0].value = part2[1];
				document.getElementsByName('cbid.bypass.' + sid + '.password')[0].value = part1[1];
				document.getElementsByName('cbid.bypass.' + sid + '.encrypt_method_ss')[0].value = part1[0];
				if (param != undefined) {
					document.getElementsByName('cbid.bypass.' + sid + '.alias')[0].value = decodeURI(param);
				}
				s.innerHTML = "<font color='green'><%:Import configuration information successfully.%></font>";
			}
			return false;
		case "ssr":
			var sstr = b64decsafe(ssu[1]);
			var ploc = sstr.indexOf("/?");
			document.getElementsByName('cbid.bypass.' + sid + '.type')[0].value = ssu[0];
			document.getElementsByName('cbid.bypass.' + sid + '.type')[0].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.getElementsByName('cbid.bypass.' + sid + '.server')[0].value = ssm[1];
			document.getElementsByName('cbid.bypass.' + sid + '.server_port')[0].value = ssm[2];
			document.getElementsByName('cbid.bypass.' + sid + '.protocol')[0].value = ssm[3];
			document.getElementsByName('cbid.bypass.' + sid + '.encrypt_method')[0].value = ssm[4];
			document.getElementsByName('cbid.bypass.' + sid + '.obfs')[0].value = ssm[5];
			document.getElementsByName('cbid.bypass.' + sid + '.password')[0].value = b64decsafe(ssm[6]);
			document.getElementsByName('cbid.bypass.' + sid + '.obfs_param')[0].value = dictvalue(pdict, 'obfsparam');
			document.getElementsByName('cbid.bypass.' + sid + '.protocol_param')[0].value = dictvalue(pdict, 'protoparam');
			var rem = pdict['remarks'];
			if (typeof (rem) != 'undefined' && rem != '' && rem.length > 0) document.getElementsByName('cbid.bypass.' + sid + '.alias')[0].value = b64decutf8safe(rem);
			s.innerHTML = "<font color='green'><%:Import configuration information successfully.%></font>";
			return false;
		case "trojan":
			try {
				var url = new URL("http://" + ssu[1]);
			} catch(e) {
				alert(e)
				return false;
			}

			document.getElementsByName('cbid.bypass.' + sid + '.alias')[0].value = url.hash ? decodeURIComponent(url.hash.slice(1)) : "";
			document.getElementsByName('cbid.bypass.' + sid + '.type')[0].value = "v2ray";
			document.getElementsByName('cbid.bypass.' + sid + '.type')[0].dispatchEvent(event);
			document.getElementsByName('cbid.bypass.' + sid + '.v2ray_protocol')[0].value = "trojan";
			document.getElementsByName('cbid.bypass.' + sid + '.v2ray_protocol')[0].dispatchEvent(event);
			document.getElementsByName('cbid.bypass.' + sid + '.server')[0].value = url.hostname;
			document.getElementsByName('cbid.bypass.' + sid + '.server_port')[0].value = url.port || "80";
			document.getElementsByName('cbid.bypass.' + sid + '.password')[0].value = decodeURIComponent(url.username);
			document.getElementsByName('cbid.bypass.' + sid + '.tls')[0].checked = true;
			document.getElementsByName('cbid.bypass.' + sid + '.tls')[0].dispatchEvent(event);
			document.getElementsByName('cbid.bypass.' + sid + '.tls_host')[0].value = url.searchParams.get("sni");

			s.innerHTML = "<font color='green'><%:Import configuration information successfully.%></font>";
			return false;
		case "vmess":
			var sstr = b64DecodeUnicode(ssu[1]);
			var ploc = sstr.indexOf("/?");
			document.getElementsByName('cbid.bypass.' + sid + '.type')[0].value = "v2ray";
			document.getElementsByName('cbid.bypass.' + sid + '.type')[0].dispatchEvent(event);
			document.getElementsByName('cbid.bypass.' + sid + '.v2ray_protocol')[0].value = "vmess";
			document.getElementsByName('cbid.bypass.' + sid + '.v2ray_protocol')[0].dispatchEvent(event);
			var url0, param = "";
			if (ploc > 0) {
				url0 = sstr.substr(0, ploc);
				param = sstr.substr(ploc + 2);
			}
			var ssm = JSON.parse(sstr);
			document.getElementsByName('cbid.bypass.' + sid + '.alias')[0].value = ssm.ps;
			document.getElementsByName('cbid.bypass.' + sid + '.server')[0].value = ssm.add;
			document.getElementsByName('cbid.bypass.' + sid + '.server_port')[0].value = ssm.port;
			document.getElementsByName('cbid.bypass.' + sid + '.vmess_id')[0].value = ssm.id;
			document.getElementsByName('cbid.bypass.' + sid + '.transport')[0].value = ssm.net;
			document.getElementsByName('cbid.bypass.' + sid + '.transport')[0].dispatchEvent(event);
			if (ssm.net == "tcp") {
				if (ssm.type && ssm.type != "http") {
					ssm.type = "none"
				}
				document.getElementsByName('cbid.bypass.' + sid + '.tcp_guise')[0].value = ssm.type;
				document.getElementsByName('cbid.bypass.' + sid + '.tcp_guise')[0].dispatchEvent(event);
				document.getElementsByName('cbid.bypass.' + sid + '.http_host')[0].value = ssm.host;
				document.getElementsByName('cbid.bypass.' + sid + '.http_path')[0].value = ssm.path;
			}
			if (ssm.net == "ws") {
				document.getElementsByName('cbid.bypass.' + sid + '.ws_host')[0].value = ssm.host;
				document.getElementsByName('cbid.bypass.' + sid + '.ws_path')[0].value = ssm.path;
			}
			if (ssm.net == "h2") {
				document.getElementsByName('cbid.bypass.' + sid + '.h2_host')[0].value = ssm.host;
				document.getElementsByName('cbid.bypass.' + sid + '.h2_path')[0].value = ssm.path;
			}
			if (ssm.net == "quic") {
				document.getElementsByName('cbid.bypass.' + sid + '.quic_security')[0].value = ssm.securty;
				document.getElementsByName('cbid.bypass.' + sid + '.quic_key')[0].value = ssm.key;
			}
			if (ssm.net == "kcp") {
				document.getElementsByName('cbid.bypass.' + sid + '.kcp_guise')[0].value = ssm.type;
			}
			if (ssm.tls == "tls") {
				document.getElementsByName('cbid.bypass.' + sid + '.tls')[0].checked = true;
				document.getElementsByName('cbid.bypass.' + sid + '.tls')[0].dispatchEvent(event);
				document.getElementsByName('cbid.bypass.' + sid + '.tls_host')[0].value = ssm.sni || ssm.host;
			}
			document.getElementsByName('cbid.bypass.' + sid + '.mux')[0].checked = true;
			document.getElementsByName('cbid.bypass.' + sid + '.mux')[0].dispatchEvent(event);
			s.innerHTML = "<font color='green'><%:Import configuration information successfully.%></font>";
			return false;
		case "vless":
			try {
				var url = new URL("http://" + ssu[1]);
				var params = url.searchParams;
			} catch(e) {
				alert(e)
				return false;
			}

			document.getElementsByName('cbid.bypass.' + sid + '.alias')[0].value = url.hash ? decodeURIComponent(url.hash.slice(1)) : "";
			document.getElementsByName('cbid.bypass.' + sid + '.type')[0].value = "v2ray";
			document.getElementsByName('cbid.bypass.' + sid + '.type')[0].dispatchEvent(event);
			document.getElementsByName('cbid.bypass.' + sid + '.v2ray_protocol')[0].value = "vless";
			document.getElementsByName('cbid.bypass.' + sid + '.v2ray_protocol')[0].dispatchEvent(event);
			document.getElementsByName('cbid.bypass.' + sid + '.server')[0].value = url.hostname;
			document.getElementsByName('cbid.bypass.' + sid + '.server_port')[0].value = url.port || "80";
			document.getElementsByName('cbid.bypass.' + sid + '.vmess_id')[0].value = url.username;
			document.getElementsByName('cbid.bypass.' + sid + '.transport')[0].value = params.get("type") == "http" ? "h2" : params.get("type") || "tcp";
			document.getElementsByName('cbid.bypass.' + sid + '.transport')[0].dispatchEvent(event);
			document.getElementsByName('cbid.bypass.' + sid + '.vless_encryption')[0].value = params.get("encryption") || "none";
			if ([ "tls", "reality" ].includes(params.get("security"))) {
				document.getElementsByName('cbid.bypass.' + sid + '.' + params.get("security"))[0].checked = true;
				document.getElementsByName('cbid.bypass.' + sid + '.' + params.get("security"))[0].dispatchEvent(event);

				document.getElementsByName('cbid.bypass.' + sid + '.fingerprint')[0].value = params.get("fp") || "";
				document.getElementsByName('cbid.bypass.' + sid + '.tls_flow')[0].value = params.get("flow") || "";
				document.getElementsByName('cbid.bypass.' + sid + '.tls_host')[0].value = params.get("sni") || "";

				if (params.get("security") === "reality") {
					document.getElementsByName('cbid.bypass.' + sid + '.reality_publickey')[0].value = params.get("pbk") ? decodeURIComponent(params.get("pbk")) : "";
					document.getElementsByName('cbid.bypass.' + sid + '.reality_shortid')[0].value = params.get("sid") || "";
					document.getElementsByName('cbid.bypass.' + sid + '.reality_spiderx')[0].value = params.get("spx") ? decodeURIComponent(params.get("spx")) : "";
				}
			}
			switch (params.get("type")) {
			case "ws":
				if (params.get("security") !== "tls")
					document.getElementsByName('cbid.bypass.' + sid + '.ws_host')[0].value = params.get("host") ? decodeURIComponent(params.get("host")) : "";
				document.getElementsByName('cbid.bypass.' + sid + '.ws_path')[0].value = params.get("path") ? decodeURIComponent(params.get("path")) : "/";
				break;
			case "kcp":
				document.getElementsByName('cbid.bypass.' + sid + '.kcp_guise')[0].value = params.get("headerType") || "none";
				document.getElementsByName('cbid.bypass.' + sid + '.seed')[0].value = params.get("seed") || "";
				break;
			case "http":
			/* this is non-standard, bullshit */
			case "h2":
				document.getElementsByName('cbid.bypass.' + sid + '.h2_host')[0].value = params.get("host") ? decodeURIComponent(params.get("host")) : "";
				document.getElementsByName('cbid.bypass.' + sid + '.h2_path')[0].value = params.get("path") ? decodeURIComponent(params.get("path")) : "";
				break;
			case "quic":
				document.getElementsByName('cbid.bypass.' + sid + '.quic_guise')[0].value = params.get("headerType") || "none";
				document.getElementsByName('cbid.bypass.' + sid + '.quic_security')[0].value = params.get("quicSecurity") || "none";
				document.getElementsByName('cbid.bypass.' + sid + '.quic_key')[0].value = params.get("key") || "";
				break;
			case "grpc":
				document.getElementsByName('cbid.bypass.' + sid + '.serviceName')[0].value = params.get("serviceName") || "";
				document.getElementsByName('cbid.bypass.' + sid + '.grpc_mode')[0].value = params.get("mode") || "gun";
				break;
			case "tcp":
				document.getElementsByName('cbid.bypass.' + sid + '.tcp_guise')[0].value = params.get("headerType") || "none";
				document.getElementsByName('cbid.bypass.' + sid + '.tcp_guise')[0].dispatchEvent(event);
				if (params.get("headerType") === "http") {
					document.getElementsByName('cbid.bypass.' + sid + '.http_host')[0].value = params.get("host") ? decodeURIComponent(params.get("host")) : "";
					document.getElementsByName('cbid.bypass.' + sid + '.http_path')[0].value = params.get("path") ? decodeURIComponent(params.get("path")) : "";
				}
				break;
			}
			s.innerHTML = "<font color='green'><%:Import configuration information successfully.%></font>";
			return false;
		default:
			s.innerHTML = "<font color='red'><%:Invalid format.%></font>";
			return false;
		}
	}
	//]]>
</script>
<input type="button" class="btn cbi-button cbi-button-apply" value="<%:Import%>" onclick="return import_ssr_url(this, '<%=self.option%>', '<%=self.value%>')" />
<span id="<%=self.option%>-status"></span>
<%+cbi/valuefooter%>
