<%+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 parseNodeUrl(url){
	var m=url.match(/^(([^:\/?#]+:)?(?:\/\/((?:([^\/?#:]*)([^\/?#:]*)@)?([^\/?#:]*)(?::([^\/?#:]*))?)))?([^?#]*)(\?[^#]*)?(#.*)?$/),
		r={
			hash:m[10] || '',// #asd
			host:m[3] || '',// localhost:257
			hostname:m[6] || '',// localhost
			href:m[0] || '',// http://username:password@localhost:257/deploy/?asd=asd#asd
			origin:m[1] || '',// http://username:password@localhost:257
			pathname:m[8] || (m[1] ? '/':''),// /deploy/
			port:m[7] || '',// 257
			protocol:m[2] || '',// http:
			search:m[9] || '',// ?asd=asd
			passwd:m[4] || '',// username
			removed:m[5] || '' // password
		};
	if (r.protocol.length==2){
		r.protocol="file:///"+r.protocol.toUpperCase();
		r.origin=r.protocol+"//"+r.host;
	}
	r.href=r.origin+r.pathname+r.search+r.hash;
	return m && r;
}


function buildUrl(btn,urlname,sid){
	var opt={
		base:"cbid.bypass."+sid,
		get:function(opt){
			var id=this.base+'.'+opt;
			var obj=document.getElementsByName(id)[0] || document.getElementsByClassName(id)[0] || document.getElementById(id)
			if (obj){
				return obj;
			}else{
				return null;
			}
		},
		getlist:function(opt){
			var id=this.base+'.'+opt;
			var objs=document.getElementsByName(id) || document.getElementsByClassName(id);
			var ret=[];
			if (objs){
				for (var i=0;i < objs.length;i++){
					ret[i]=objs[i].value;
				}
			}else{
				alert("<%:Fatal on get option,please help in debug%>:"+opt);
			}
			return ret;
		},
		query:function(param,src,tval="1",fval="0"){
			var ret="&"+param+"=";
			var obj=this.get(src);
			if (obj){
				if (obj.type=="checkbox"){
					return ret+(obj.checked==true ? tval:fval);
				}else{
					return ret+encodeURIComponent(obj.value);
				}
			}
			return ''
		}
	}

	var s=document.getElementById(urlname+"-status");
	if (!s) return false;
	var v_type=opt.get("type").value;
	var v_alias=opt.get("alias").value;
		var _address = ""
		if (v_type) {
			try {
				var v_server = opt.get("server");
				const ipv6Regex = /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/;
				if (ipv6Regex.test(v_server.value)) {
					_address = "[" + v_server.value + "]"
				} else {
					_address = v_server.value
				}
			} catch (e) {
			}
		}
	var url=null;
	switch (v_type){
	case "ss":

		var v_port=opt.get("server_port");
		var v_method=opt.get("encrypt_method_ss");
		var v_password=opt.get("password");
		url=b64encsafe(v_method.value+":"+v_password.value)+"@"+
			_address+":"+
			v_port.value+"/?";
		var params='';
		var v_plugin=opt.get("plugin").value;
		if (v_plugin){
			var v_plugin_opts=opt.get("plugin_opts").value;
			if (v_plugin_opts && v_plugin_opts!=''){
				v_plugin+=encodeURI(";"+v_plugin_opts);
			}
			params+="&plugin="+encodeURI(v_plugin);
		}
		params+="#"+encodeURI(v_alias);
		if (params[0]=="&"){
			params=params.substring(1);
		}
		url+=params;
		break;
	case "ssr":

		var v_port=opt.get("server_port");
		var v_protocol=opt.get("protocol");
		var v_method=opt.get("encrypt_method");
		var v_obfs=opt.get("obfs");
		var v_password=opt.get("password");
		var v_obfs_param=opt.get("obfs_param");
		var v_protocol_param=opt.get("protocol_param");
		var ssr_str=_address+":"+
			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);
		url=b64encsafe(ssr_str);
		break;
	case "vmess":
		var info={};
		info.v="2";
		info.ps=v_alias;
		info.add=opt.get("server").value;
		info.port=opt.get("server_port").value;
		info.id=opt.get("uuid").value;
		info.aid=opt.get("alter_id").value || "0";
		var v_transport=opt.get("transport").value;
		if (v_transport=="ws"){
			info.host=opt.get("ws_host").value;
			info.path=opt.get("ws_path").value;
		}else if (v_transport=="h2"){
			info.host=opt.get("h2_host").value;
			info.path=opt.get("h2_path").value;
		}else if (v_transport=="tcp"){
			info.type=opt.get("tcp_guise").value;
			if (info.type=='http'){
				info.host=opt.get("http_host").value;
				info.path=opt.get("http_path").value;
			}
		}else if (v_transport=="kcp"){
			info.type=opt.get("kcp_guise").value;
			info.seed=opt.get("seed").value;
		}else if (v_transport=="quic"){
			info.type=opt.get("quic_guise").value;
			info.key=opt.get("quic_key").value;
			info.securty=opt.get("quic_security").value;
		}else if (v_transport=="grpc"){
			info.serviceName=opt.get("grpc_serviceName").value;
		}
		if (info.path && info.path!=''){
			info.path=encodeURI(info.path);
		}
		info.net=v_transport;
		info.security=opt.get("security").value || "auto";
		if (opt.get("tls").checked){
			var v_security = "tls";
			info.tls="tls";
			info.sni=opt.get("tls_host").value;
		}
		url=b64EncodeUnicode(JSON.stringify(info));
		break;
	case "vless":
		var v_password=opt.get("uuid");
		var v_port=opt.get("server_port");
		url=encodeURIComponent(v_password.value)+
			"@"+_address+
			":"+v_port.value+"?";
		var params='';
		var v_transport=opt.get("transport").value;
		if (v_transport=="ws"){
			params+=opt.query("host","ws_host");
			params+=opt.query("path","ws_path");
		}else if (v_transport=="h2"){
			params+=opt.query("host","h2_host");
			params+=opt.query("path","h2_path");
		}else if (v_transport=="tcp"){
			params+=opt.query("headerType","tcp_guise");
			params+=opt.query("host","http_host");
			params+=opt.query("path","http_path");
		} else if (v_transport == "mkcp") {
			v_transport = "kcp";
			params+=opt.query("headerType","kcp_guise");
			params+=opt.query("seed","seed");
		}else if (v_transport=="quic"){
			params+=opt.query("headerType","quic_guise");
			params+=opt.query("key","quic_key");
			params+=opt.query("quicSecurity","quic_security");
		}else if (v_transport=="grpc"){
			params+=opt.query("serviceName","grpc_serviceName");
		}
		params+="&type="+v_transport;
		params+=opt.query("encryption","vless_encryption");
		if (opt.get("tls").checked || opt.get("xtls").checked){
			if (opt.get("xtls").checked){
				v_security="xtls";
				var v_flow=opt.get("vless_flow").value;
				params+="&flow="+v_flow;
			}else{
				var v_security="tls";
			}
			params+="&security="+v_security;
			params+=opt.query("sni","tls_host");
		}
		params+="#"+encodeURI(v_alias);
		if (params[0]=="&"){
			params=params.substring(1);
		}
		url+=params;
		break;

	case "trojan":
		var v_password=opt.get("password");

		var v_port=opt.get("server_port");
		url=encodeURIComponent(v_password.value)+
			"@"+_address+
			":"+v_port.value+"/?";
		var params='';
		if (opt.get("tls").checked){
			params+=opt.query("sni","tls_host");
			params+="&tls=1"
			params+=opt.query("allowinsecure","insecure");
		}
		params+="#"+encodeURI(v_alias);
		if (params[0]=="&"){
			params=params.substring(1);
		}
		url+=params;

		break;

	case "hysteria2":
		 var v_port=opt.get("server_port");
			var params = "";
			params += opt.query("auth", "hy2_auth_password");
			params += opt.query("sni", "tls_host");
			params += opt.query("insecure", "insecure");
			params += opt.query("obfs", "salamander");
			params += opt.query("obfs-password", "hy2_obfs_password");
			
			var url =
				_address + ":" +
				v_port.value + "?" +
				params +
				"#" + encodeURI(v_alias);
	}
	if (url){
		url=v_type.toLowerCase()+"://"+url;
		var textarea=document.createElement("textarea");
		textarea.textContent= url;
		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 style=\'color:green\'><%:Share to clipboard successfully%></font>";
		}catch (ex){
			s.innerHTML="<font style=\'color:red\'><%:Unable share to clipboard%></font>";
		}finally{
			document.body.removeChild(textarea);
		}
			//alert(url);
	}else{
		s.innerHTML='';
		alert("<%:Not a supported Format%>: "+v_type);
	}
	return false;
}
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 style=\'color:green\'><%:Copy SSR to clipboard successfully.%></font>";
	} catch (ex) {
		s.innerHTML = "<font style=\'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 style=\'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]) {

			//alert(url);
		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 + '.hy_auth_password')[0].value = params.get("auth") ? "2" : "0";
			document.getElementsByName('cbid.bypass.' + sid + '.hy_auth_password')[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 style=\'color:green\'><%:Import configuration information successfully.%></font>";
			return false;
		case "hysteria2":
			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 + '.transport_protocol')[0].value = params.get("protocol") || "udp";
			document.getElementsByName('cbid.bypass.' + sid + '.hy2_obfs_type')[0].value = params.get("obfs") || "";
			document.getElementsByName('cbid.bypass.' + sid + '.hy2_obfs_password')[0].value = params.get("obfs_password")|| "";
			document.getElementsByName('cbid.bypass.' + sid + '.hy2_auth_password')[0].value = params.get("auth") || "";
			document.getElementsByName('cbid.bypass.' + sid + '.tls_host')[0].value = params.get("sni") || "";
			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 style=\'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 style=\'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 style=\'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 style=\'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 style=\'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 + '.alter_id')[0].value = ssm.aid;
			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"
				} else {
					document.getElementsByName('cbid.bypass.' + sid + '.http_host')[0].value = ssm.host;
					document.getElementsByName('cbid.bypass.' + sid + '.http_path')[0].value = ssm.path;
				}
				document.getElementsByName('cbid.bypass.' + sid + '.tcp_guise')[0].value = ssm.type;
				document.getElementsByName('cbid.bypass.' + sid + '.tcp_guise')[0].dispatchEvent(event);
			}
			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 == "httpupgrade") {
				document.getElementsByName('cbid.bypass.' + sid + '.httpupgrade_host')[0].value = ssm.host;
				document.getElementsByName('cbid.bypass.' + sid + '.httpupgrade_path')[0].value = ssm.path;
			}
			if (ssm.net == "splithttp") {
				document.getElementsByName('cbid.bypass.' + sid + '.splithttp_host')[0].value = ssm.host;
				document.getElementsByName('cbid.bypass.' + sid + '.splithttp_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 style=\'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") == "raw" ? "raw" : 
				(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", "xtls", "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);


				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")) : "";
				}
				if (params.get("security") === "xtls") {
					document.getElementsByName('cbid.bypass.' + sid + '.tls_flow')[0].value = params.get("flow") || "";
					document.getElementsByName('cbid.bypass.' + sid + '.tls_flow')[0].dispatchEvent(event);
				}
				document.getElementsByName('cbid.bypass.' + sid + '.fingerprint')[0].value = params.get("fp") || "";
				document.getElementsByName('cbid.bypass.' + sid + '.tls_host')[0].value = params.get("sni") || "";
			}
			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 "httpupgrade":
				if (params.get("security") !== "tls")
					document.getElementsByName('cbid.bypass.' + sid + '.httpupgrade_host')[0].value = params.get("host") ? decodeURIComponent(params.get("host")) : "";
				document.getElementsByName('cbid.bypass.' + sid + '.httpupgrade_path')[0].value = params.get("path") ? decodeURIComponent(params.get("path")) : "/";
				break;
			case "splithttp":
				if (params.get("security") !== "tls")
					document.getElementsByName('cbid.bypass.' + sid + '.splithttp_host')[0].value = params.get("host") ? decodeURIComponent(params.get("host")) : "";
				document.getElementsByName('cbid.bypass.' + sid + '.splithttp_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")) : "";
				}
			case "raw":
				document.getElementsByName('cbid.bypass.' + sid + '.raw_guise')[0].value = params.get("headerType") || "none";
				document.getElementsByName('cbid.bypass.' + sid + '.raw_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 style=\'color:green\'><%:Import configuration information successfully.%></font>";
			return false;
		default:
			s.innerHTML = "<font style=\'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%>')" />
<input type="button" class="btn cbi-button cbi-button-apply" value='<%:Build Share URL%>' onclick="return buildUrl(this, '<%=self.option%>', '<%=self.value%>')" />
<span id="<%=self.option%>-status"></span>
<%+cbi/valuefooter%>
