<%+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.shadowsocksr.' + sid + '.server')[0];
	var v_port = document.getElementsByName('cbid.shadowsocksr.' + sid + '.server_port')[0];
	var v_protocol = document.getElementsByName('cbid.shadowsocksr.' + sid + '.protocol')[0];
	var v_method = document.getElementsByName('cbid.shadowsocksr.' + sid + '.encrypt_method')[0];
	var v_obfs = document.getElementsByName('cbid.shadowsocksr.' + sid + '.obfs')[0];
	var v_password = document.getElementsByName('cbid.shadowsocksr.' + sid + '.password')[0];
	var v_obfs_param = document.getElementsByName('cbid.shadowsocksr.' + sid + '.obfs_param')[0];
	var v_protocol_param = document.getElementsByName('cbid.shadowsocksr.' + sid + '.protocol_param')[0];
	var v_alias = document.getElementsByName('cbid.shadowsocksr.' + 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 "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.shadowsocksr.' + sid + '.type')[0].value = ssu[0];
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.type')[0].dispatchEvent(event);
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.server')[0].value = server;
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.server_port')[0].value = port;
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.password')[0].value = password || "";
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.encrypt_method_ss')[0].value = method || "";
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.plugin')[0].value = plugin || "none";
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.plugin')[0].dispatchEvent(event);
				if (plugin != undefined) {
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.plugin_opts')[0].value = pluginOpts || "";
				}
				if (param != undefined) {
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.alias')[0].value = decodeURI(param);
				}
				s.innerHTML = "<font color='green'><%:Import configuration information successfully.%></font>";
			} else {
				var sstr = b64decsafe(url0);
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.type')[0].value = ssu[0];
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.type')[0].dispatchEvent(event);
				var team = sstr.split('@');
				var part1 = team[0].split(':');
				var part2 = team[1].split(':');
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.server')[0].value = part2[0];
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.server_port')[0].value = part2[1];
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.password')[0].value = part1[1];
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.encrypt_method_ss')[0].value = part1[0];
				if (param != undefined) {
					document.getElementsByName('cbid.shadowsocksr.' + 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.shadowsocksr.' + sid + '.type')[0].value = ssu[0];
			document.getElementsByName('cbid.shadowsocksr.' + 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.shadowsocksr.' + sid + '.server')[0].value = ssm[1];
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.server_port')[0].value = ssm[2];
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.protocol')[0].value = ssm[3];
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.encrypt_method')[0].value = ssm[4];
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.obfs')[0].value = ssm[5];
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.password')[0].value = b64decsafe(ssm[6]);
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.obfs_param')[0].value = dictvalue(pdict, 'obfsparam');
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.protocol_param')[0].value = dictvalue(pdict, 'protoparam');
			var rem = pdict['remarks'];
			if (typeof (rem) != 'undefined' && rem != '' && rem.length > 0) document.getElementsByName('cbid.shadowsocksr.' + sid + '.alias')[0].value = b64decutf8safe(rem);
			s.innerHTML = "<font color='green'><%:Import configuration information successfully.%></font>";
			return false;
		case "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;
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.type')[0].value = "v2ray";
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.type')[0].dispatchEvent(event);
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.v2ray_protocol')[0].value = "trojan";
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.v2ray_protocol')[0].dispatchEvent(event);
			var team = sstr.split('@');
			var password = team[0]
			var serverPart = team[1].split(':');		
			var others = serverPart[1].split('?');
			var port = parseInt(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.getElementsByName('cbid.shadowsocksr.' + sid + '.server')[0].value = serverPart[0];
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.server_port')[0].value = port || '443';
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.password')[0].value = password;
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.tls')[0].checked = true;
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.tls')[0].dispatchEvent(event);
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.tls_host')[0].value = queryParam.sni || '';
			if (param != undefined) {
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.alias')[0].value = decodeURI(param);
			}
			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.shadowsocksr.' + sid + '.type')[0].value = "v2ray";
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.type')[0].dispatchEvent(event);
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.v2ray_protocol')[0].value = "vmess";
			document.getElementsByName('cbid.shadowsocksr.' + 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.shadowsocksr.' + sid + '.alias')[0].value = ssm.ps;
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.server')[0].value = ssm.add;
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.server_port')[0].value = ssm.port;
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.alter_id')[0].value = ssm.aid;
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.vmess_id')[0].value = ssm.id;
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.transport')[0].value = ssm.net;
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.transport')[0].dispatchEvent(event);
			if (ssm.net == "tcp") {
				if (ssm.type && ssm.type != "http") {
					ssm.type = "none"
				}
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.tcp_guise')[0].value = ssm.type;
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.tcp_guise')[0].dispatchEvent(event);
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.http_host')[0].value = ssm.host;
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.http_path')[0].value = ssm.path;
			}
			if (ssm.net == "ws") {
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.ws_host')[0].value = ssm.host;
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.ws_path')[0].value = ssm.path;
			}
			if (ssm.net == "h2") {
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.h2_host')[0].value = ssm.host;
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.h2_path')[0].value = ssm.path;
			}
			if (ssm.net == "quic") {
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.quic_security')[0].value = ssm.securty;
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.quic_key')[0].value = ssm.key;
			}
			if (ssm.net == "kcp") {
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.kcp_guise')[0].value = ssm.type;
			}
			if (ssm.tls == "tls") {
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.tls')[0].checked = true;
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.tls')[0].dispatchEvent(event);
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.tls_host')[0].value = ssm.host;
			}
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.mux')[0].checked = true;
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.mux')[0].dispatchEvent(event);
			s.innerHTML = "<font color='green'><%:Import configuration information successfully.%></font>";
			return false;
		case "vless":
			var url0, param = "";
			var ploc = ssu[1].indexOf("#");
			if (ploc > 0) {
				url0 = ssu[1].substr(0, ploc);
				param = decodeURIComponent(ssu[1].substr(ploc + 1));
			} else {
				url0 = ssu[1]
			}
			var sstr = url0;
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.type')[0].value = "v2ray";
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.type')[0].dispatchEvent(event);
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.v2ray_protocol')[0].value = "vless";
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.v2ray_protocol')[0].dispatchEvent(event);
			var team = sstr.split('@');
			var uuid = 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.getElementsByName('cbid.shadowsocksr.' + sid + '.server')[0].value = serverPart[0];
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.server_port')[0].value = port;
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.vmess_id')[0].value = uuid;
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.transport')[0].value = queryParam.type ? (queryParam.type == "http" ? "h2" : queryParam.type) : "tcp";
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.transport')[0].dispatchEvent(event);
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.vless_encryption')[0].value = queryParam.encryption || "none";
			if (queryParam.security == "tls") {
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.tls')[0].checked = true;
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.tls')[0].dispatchEvent(event);
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.tls_host')[0].value = queryParam.sni || serverPart[0];
			}
			switch (queryParam.type) {
			case "ws":
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.ws_host')[0].value = queryParam.host;
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.ws_path')[0].value = queryParam.path || "/";
				break;
			case "kcp":
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.kcp_guise')[0].value = queryParam.headerType || "none";
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.kcp_guise')[0].dispatchEvent(event);
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.seed')[0].value = queryParam.seed;
				break;
			case "http":
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.h2_host')[0].value = queryParam.host || serverPart[0];
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.h2_path')[0].value = queryParam.path || "/";
				break;
			case "quic":
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.quic_guise')[0].value = queryParam.headerType || "none";
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.quic_guise')[0].dispatchEvent(event);
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.quic_security')[0].value = queryParam.quicSecurity || "none";
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.quic_key')[0].value = queryParam.key;
				break;
			case "grpc":
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.serviceName')[0].value = queryParam.serviceName;
				break;
			default:
				if (queryParam.security == "xtls") {
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.xtls')[0].checked = true;
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.xtls')[0].dispatchEvent(event);
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.vless_flow')[0].value = queryParam.flow || "xtls-rprx-splice";
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.tls_host')[0].value = queryParam.sni || serverPart[0];
				}			
				break;
			}
			if (param != undefined) {
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.alias')[0].value = decodeURI(param);
			}
			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%>
