<%+cbi/valueheader%>
<%
local api = require "luci.model.cbi.bypass.api"
-%>
<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",
			client : true,
					 
			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("<%:Faltal 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) {
			alert("Never");
			return false;
		}
		opt.base = 'cbid.bypass.' + sid;
		opt.client = urlname.indexOf("server") === -1;
									
		var v_type = opt.get("type").value;
		var v_alias = opt.get("alias");
		var url = null;
		if (v_type === "ss") {
			var v_server = opt.get("server");
			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) + "@" +
				v_server.value + ":" +
				v_port.value + "/?";
			
			var params = "";
			var v_plugin = opt.get("plugin").value;
			if (v_plugin && v_plugin != "none") {
				if (v_plugin == "simple-obfs" || v_plugin == "obfs-local") {
					v_plugin = "obfs-local";
				}
				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 += "&group="
			params += "#" + encodeURI(v_alias.value);
			if (params[0] == "&") {
				params = params.substring(1);
			}
			url += params;
		} else if (v_type === "ssr") {
			var v_server = opt.get("server");
			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 = 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);
			url = b64encsafe(ssr_str);
		} else if (v_type === "vmess") {
			v_type = "vmess";
			var info = {};
			info.v = "2";
			info.ps = v_alias.value;
			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;
			
			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;
				info.host = opt.get("http_host").value;
				info.path = opt.get("http_path").value;
			} else if (v_transport === "mkcp") {
				v_transport = "kcp";
				info.type = opt.get("kcp_guise").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.path = 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));
		} else if (v_type === "vless") {
			v_type = "vless";
			var v_password = opt.get("uuid");
			var v_server = opt.get("server");
			var v_port = opt.get("server_port");
			url = encodeURIComponent(v_password.value) +
				"@" + v_server.value +
				":" + 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");
			} 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") {
				//不知道是用path还是serviceName，这里先这样吧
				params += opt.query("path", "grpc_serviceName");
				params += opt.query("serviceName", "grpc_serviceName");
			}
			params += "&type=" + v_transport;
			
			params += opt.query("encryption", "encryption");
			if (opt.get("tls")) {
			if (opt.get("tls").checked) {
				var v_security = "tls";
				params += "&security=" + v_security;
				params += opt.query("sni", "tls_host");
			}
			}
			if (opt.get("xtls")) {
			if (opt.get("xtls").checked) {
				v_security = "xtls";
				var v_flow = "xtls-rprx-direct";
				if (opt.get("vless_flow").value) {
					v_flow = opt.get("vless_flow").value;
				}
				params += "&flow=" + v_flow;
				params += "&security=" + v_security;
				params += opt.query("sni", "tls_host");
			}
			}
			params += "#" + encodeURI(v_alias.value);
			if (params[0] == "&") {
				params = params.substring(1);
			}
			url += params;
		} else if ((v_type === "xray" && opt.get("protocol").value === "trojan") || v_type === "trojan" || v_type === "trojan" || v_type === "trojan-go") {
			if (v_type === "xray" && opt.get("protocol").value === "trojan") {
				v_type = "trojan";
			}
			var v_password = opt.get(!opt.client && v_type === "trojan-go"  ? "passwords" : "password");
			var v_server = opt.get("server");
			var v_port = opt.get("server_port");
			url = encodeURIComponent(v_password.value) +
				"@" + v_server.value +
				":" + v_port.value + "/?";
			var params = "";
			if (opt.get("tls").checked) {
				   
				params += opt.query("sni", "tls_host");
				if (v_type !== "trojan-go") {
					params += "&tls=1"
					params += opt.query("allowinsecure", "insecure");
				}
												  
									   
				   
			}
			if (v_type === "trojan-go") {
				if (!opt.get("tls").checked && opt.get("trojan_transport").value === "original") {
					var plugin = {};
					plugin.type = opt.get("plugin_type").value;
					if (plugin.type !== "plaintext") {
						plugin.command = opt.get("plugin_cmd").value;
						plugin.option = opt.get("plugin_option").value;
						plugin.arg = opt.getlist("plugin_arg");
					}
					params += "&plugin=" + encodeURIComponent(JSON.stringify(plugin));
				}
				params += opt.query("type", "trojan_transport");
				var ws = (opt.get("trojan_transport").value.indexOf("ws") !== -1);
				var h2 = (opt.get("trojan_transport").value.indexOf("h2") !== -1);
				if (ws) {
					params += opt.query("host", "ws_host");
					params += opt.query("path", "ws_path");
				} else if (h2) {
					params += opt.query("host", "h2_host");
					params += opt.query("path", "h2_path");
				}
				var enc = "none";
				if (opt.get("ss_aead").checked === true) {
					enc = "ss;" +
						opt.get("ss_aead_method").value +
						":" + opt.get("ss_aead_pwd").value;
				}
				params += "&encryption=" + encodeURIComponent(enc);
								   
			}
			params += "#" + encodeURI(v_alias.value);
			if (params[0] == "&") {
				params = params.substring(1);
			}
			url += params;
		}
		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 color='green'><%:Share URL to clipboard successfully.%></font>";
			} catch (ex) {
				s.innerHTML = "<font color='red'><%:Share URL to clipboard unable.%></font>";
			} finally {
				document.body.removeChild(textarea);
			}
			//alert(url);
		} else {
			alert("<%:Not a supported scheme:%> " + v_type);
		}
		return false;
	}
	
	function fromUrl(btn, urlname, sid) {
		var opt = {
			base: 'cbid.bypass',
			client : true,
					 
			get: function(opt) {
				var obj;
				var id = this.base + '.' + opt;
				obj = document.getElementsByName(id)[0] || document.getElementById(id);
				if (obj) {
					return obj;
				} else {
																										
					return null;
				}
			},
			set: function(opt, val) {
				var obj;
				obj = this.get(opt)
				if (obj) {
					if (obj.type === 'checkbox') {
						var event = document.createEvent("HTMLEvents");
						event.initEvent("change", true, true);
						obj.checked = val;
						obj.dispatchEvent(event);
					} else {
						obj.value = val;
					}
				} else {
					//alert('<%:Faltal on set option, please help in debug: %>' + opt + ' = ' + val);
				}
			},
			setlist: function(opt, vlist) {
				var id = this.base + "." + opt;
				var objs = document.getElementsByName(id) || document.getElementsByClassName(id);
				if (objs) {
					var values = "";
					for (var i = 0; i < vlist.length; i++) {
						values += vlist[i] + ", ";
					}
					alert("Manually input the option:\n" + opt + "s:\n[" + values + "]");
				} else {
					//alert("<%:Faltal on set option, please help in debug: %>" + opt);
				}
			}
		}
		
		var s = document.getElementById(urlname + '-status');
		if (!s) {
			alert("Never");
			return false;
		}
		opt.base = 'cbid.bypass.' + sid;
		opt.client = urlname.indexOf("server") === -1;
									
		var ssrurl = prompt('<%:Paste Share URL Here%>', '');
		if (ssrurl === null || ssrurl === "") {
																
			return false;
		}
		s.innerHTML = "";
		var ssu = ssrurl.split('://');
		var event = document.createEvent("HTMLEvents");
		event.initEvent("change", true, true);
		if (ssu[0] === "ssr") {
			//var b64c = ssu[1].match(/([A-Za-z0-9_-]+)/);
			var sstr = b64decsafe(ssu[1]);
			var ploc = sstr.indexOf("/?");
			var url0 = "", param = "";
			if (ploc > 0) {
				url0 = sstr.substr(0, ploc);
				param = sstr.substr(ploc + 2);
			} else {
				var url0 = sstr;
			}
			console.log(param);
			var ssm = url0.match(/^(.+):([^:]+):([^:]*):([^:]+):([^:]*):([^:]+)/);
			if (!ssm || ssm.length < 7) {
				s.innerHTML = "<font color='red'><%:Invalid Share URL Format%></font>";
				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] || '');
				}
			}
			opt.set('type', "ssr");
			opt.get('type').dispatchEvent(event);
			opt.set('server', ssm[1]);
			opt.set('server_port', ssm[2]);
			opt.set('protocol', ssm[3]);
			opt.set('encrypt_method', ssm[4]);
			opt.set('obfs', ssm[5]);
			opt.set('password', b64decsafe(ssm[6]));
			opt.set('obfs_param', dictvalue(pdict, 'obfsparam'));
			opt.set('protocol_param', dictvalue(pdict, 'protoparam'));
			var rem = pdict['remarks'];
			if (typeof(rem) !== 'undefined' && rem !== '' && rem.length > 0)
				opt.set('alias', b64decutf8safe(rem));
		} else if (ssu[0] === "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];
				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);
				}
				opt.set('type', "ss");
				opt.get('type').dispatchEvent(event);
				opt.set('server', server);
				opt.set('server_port', port);
				opt.set('password', password || "");
				opt.set('encrypt_method_ss', method || "");
				opt.set('plugin', plugin || "none");
				if (plugin && plugin != "none") {
					opt.get('plugin').dispatchEvent(event);
					opt.set('plugin_opts', pluginOpts || "");
					opt.get('plugin_opts').dispatchEvent(event);
				}
				if (param !== undefined) {
					opt.set('alias', decodeURI(param));
				}
			} else {
				var sstr = b64decsafe(url0);
				var team = sstr.split('@');
				opt.set('type', "ss");
				opt.get('type').dispatchEvent(event);
				var part1 = team[0].split(':');
				var part2 = team[1].split(':');
				opt.set('server', part2[0]);
				opt.set('server_port', part2[1]);
				opt.set('password', part1[1]);
				opt.set('encrypt_method_ss', part1[0]);
				opt.set('plugin', "none");
				opt.get('plugin').dispatchEvent(event);
				//opt.set('ss_plugin_opts', "");
				if (param !== undefined) {
					opt.set('alias', decodeURI(param));
				}
			}
		} else if (ssu[0] === "trojan") {
			var stype = "trojan";
			var m = parseNodeUrl(ssrurl);
			console.log(m.search);
			var password = m.passwd;
			if (password === "") {
				s.innerHTML = "<font color='red'><%:Invalid Share URL Format%></font>";
				return false;
			}
			var queryParam = {};
			if (m.search.length > 1) {
				var query = m.search.split('?');
				var queryParams = query[1];
				var queryArray = queryParams.split('&');
				var params;
				for (i = 0; i < queryArray.length; i++) {
					params = queryArray[i].split('=');
					queryParam[decodeURIComponent(params[0]).toLowerCase()] = decodeURIComponent(params[1] || '');
				}
			}
			if (queryParam.mux || queryParam.ws || queryParam.h2 || queryParam.ss || queryParam.plugin) {
				stype = "trojan-go";
			}
			opt.set('type', stype);
			opt.get('type').dispatchEvent(event);
			opt.set('server', m.hostname);
			opt.set('server_port', m.port || "443");
			opt.set(!opt.client && stype === "trojan-go" ? 'passwords' : 'password', decodeURIComponent(password));
			var tls = true;
			if (stype === "trojan-go") {
				tls = queryParam.plugin === undefined;
			}
			if (tls === false) { alert("TODO: plugin params for trojan-go."); }
			opt.set('tls', tls);
			opt.get('tls').dispatchEvent(event);
			if (tls) {
				opt.set('tls_host', queryParam.peer || queryParam.sni || '');
				opt.set('insecure', queryParam.allowinsecure === '1');
			}
			var tran = 'original';
			var ws = queryParam.type.indexOf('ws') !== -1;
			var h2 = queryParam.type.indexOf('h2') !== -1;
			if (ws && h2) {
				tran = 'h2+ws'
			} else {
				if (ws) tran = 'ws';
				if (h2) tran = 'h2';
			}
			opt.set('trojan_transport', 'tran');
			opt.get('trojan_transport').dispatchEvent(event);
			if (ws) {
				opt.set('ws_host', queryParam.wshost || '');
				opt.set('ws_path', queryParam.wspath || '/');
			}
			if (h2) {
				opt.set('h2_host', queryParam.h2host || '');
				opt.set('h2_path', queryParam.h2path || '/');
			}
			var ss = queryParam.ss === '1';
			opt.set('ss_aead', ss);
			if (ss) {
				opt.set('ss_aead_method', queryParam.ssmethod.toLowerCase() || '');
				opt.set('ss_aead_pwd', queryParam.sspasswd || '');
			}
			opt.set('mux', queryParam.mux === '1');
			if (m.hash) {
				opt.set('alias', decodeURI(m.hash.substr(1)));
			}
		} else if (ssu[0] === "trojan-go") {
			var m = parseNodeUrl(ssrurl);
			console.log(m.search);
			var password = m.passwd;
			if (password === "") {
				s.innerHTML = "<font color='red'><%:Invalid Share URL Format%></font>";
				return false;
			}
			var queryParam = {};
			if (m.search.length > 1) {
				var query = m.search.split('?');
				var queryParams = query[1];
				var queryArray = queryParams.split('&');
				for (i = 0; i < queryArray.length; i++) {
					var params = queryArray[i].split('=');
					queryParam[decodeURIComponent(params[0]).toLowerCase()] = decodeURIComponent(params[1] || '');
				}
			}
			opt.set('type', 'trojan-go');
			opt.get('type').dispatchEvent(event);
			opt.set('server', m.hostname);
			opt.set('server_port', m.port || "443");
			opt.set(opt.client ? 'password' : 'passwords', decodeURIComponent(password));
			opt.set('tls', '1');
			opt.get('tls').dispatchEvent(event);
			opt.set('insecure', '0');
			opt.set('tls_host', queryParam.peer || queryParam.sni || '');
			var plugin = queryParam.plugin !== undefined;
			if (plugin) {
				opt.set('trojan_transport', 'original');
				opt.get('trojan_transport').dispatchEvent(event);
				var plugin = JSON.parse(queryParam.plugin);
				if (plugin) {
					opt.set('plugin_type', plugin.type);
					opt.get('plugin_type').dispatchEvent(event);
					if (plugin.type !== "plaintext") {
						opt.set('plugin_cmd', plugin.command);
						opt.set('plugin_option', plugin.option);
						opt.setlist('plugin_arg', plugin.arg);
					}
				} else
					alert(queryParam.plugin);
			}
											   
					   
									   
			 
																								 
														  
	
			var tran = 'original';
			var or = queryParam.type === undefined || queryParam.type === 'original';
			var ws = queryParam.type.indexOf('ws') !== -1;
			var h2 = queryParam.type.indexOf('h2') !== -1;
			if (ws && h2) {
				tran = 'h2+ws'
			} else {
				if (ws) tran = 'ws';
				if (h2) tran = 'h2';
			}
			opt.set('trojan_transport', tran);
			opt.get('trojan_transport').dispatchEvent(event);
			if (ws) {
				opt.set('ws_host', queryParam.host || '');
				opt.set('ws_path', queryParam.path || '/');
			}
			if (h2){
				opt.set('h2_host', queryParam.host || '');
				opt.set('h2_path', queryParam.path || '/');
			}
			var enc = {};
			var ss = false;
			if (queryParam.encryption) {
				var r = queryParam.encryption.match(/^(ss);([^;:]*)[;:](.*)$/),
				enc = {type: r[1], method: r[2], password: r[3]};
			}
			ss = enc.type === 'ss';
			opt.set('ss_aead', ss);
			if (ss) {
				opt.set('ss_aead_method', enc.method.toLowerCase() || '');
				opt.set('ss_aead_pwd', enc.password || '');
			}
			opt.set('mux', '1');
			if (m.hash) {
				opt.set('alias', decodeURI(m.hash.substr(1)));
			}
		} else if (ssu[0] === "vmess") {
			var sstr = b64DecodeUnicode(ssu[1]);
			var ploc = sstr.indexOf("/?");
			opt.set('type', "vmess");
			opt.get('type').dispatchEvent(event);
			var url0, param = "";
			if (ploc > 0) {
				url0 = sstr.substr(0, ploc);
				param = sstr.substr(ploc + 2);
			}
			var ssm = JSON.parse(sstr);
			console.log(ssm);
			opt.set('alias', ssm.ps);
			opt.set('server', ssm.add);
			opt.set('server_port', ssm.port);
			opt.set('alter_id', ssm.aid);
			opt.set('uuid', ssm.id);
			opt.set('tls', ssm.tls === "tls");
			opt.get('tls').dispatchEvent(event);
			if (ssm.tls === "tls") {
				var tls_host = ssm.host;
				if (ssm.sni) {
					tls_host = ssm.sni
				}
				opt.set('tls_host', tls_host);
			}
			ssm.net = ssm.net.toLowerCase();
			if (ssm.net === "kcp" || ssm.net === "mkcp")
				ssm.net = "mkcp"
			opt.set('transport', ssm.net);
			opt.get('transport').dispatchEvent(event);
			if (ssm.net === "tcp") {
				opt.set('tcp_guise', (ssm.host && ssm.path) ? "http" : "none");
				opt.get('tcp_guise').dispatchEvent(event);
				if (ssm.host && ssm.path) {
					opt.set('http_host', ssm.host);
					opt.set('http_path', ssm.path);
				}
			} else if (ssm.net === "ws") {
				opt.set('ws_host', ssm.host);
				opt.set('ws_path', ssm.path);
			} else if (ssm.net === "h2") {
				opt.set('h2_host', ssm.host);
				opt.set('h2_path', ssm.path);
			} else if (ssm.net === "quic") {
				opt.set('quic_security', ssm.securty);
				opt.set('quic_key', ssm.key);
			} else if (ssm.net === "kcp" || ssm.net === "mkcp") {
				opt.set('kcp_guise', ssm.type);
				opt.get('kcp_guise').dispatchEvent(event);
			} else if (ssm.net === "grpc") {
				opt.set('grpc_serviceName', ssm.path);
			}
		} else if (ssu[0] === "vless") {
			opt.set('type', "vless");
			opt.get('type').dispatchEvent(event);
			var m = parseNodeUrl(ssrurl);
			console.log(m.search);
			var password = m.passwd;
			if (password === "") {
				s.innerHTML = "<font color='red'><%:Invalid Share URL Format%></font>";
				return false;
			}
			opt.set('uuid', password);
			opt.set('server', m.hostname);
			opt.set('server_port', m.port || "443");
			var queryParam = {};
			if (m.search.length > 1) {
				var query = m.search.split('?');
				var queryParams = query[1];
				var queryArray = queryParams.split('&');
				var params;
				for (i = 0; i < queryArray.length; i++) {
					params = queryArray[i].split('=');
					queryParam[decodeURIComponent(params[0])] = decodeURIComponent(params[1] || '');
				}
			}
			
			opt.set('encryption', queryParam.encryption);
			if (queryParam.security) {
				if (queryParam.security == "tls" || queryParam.security == "xtls") {
					if (queryParam.security == "tls") {
						opt.set('tls', true);
						opt.get('tls').dispatchEvent(event);
					}
					if (queryParam.security == "xtls") {
						opt.set('xtls', true);
						opt.get('xtls').dispatchEvent(event);
						opt.set('vless_flow', queryParam.flow || "xtls-rprx-direct");
					}
					opt.set('tls_host', queryParam.sni || '');
					
					opt.set('insecure', true);
					if (queryParam.allowinsecure === '0') {
						opt.set('insecure', false);
					}
					opt.get('insecure').dispatchEvent(event);
				}
			}
			
			queryParam.type = queryParam.type.toLowerCase();
			if (queryParam.type === "kcp" || queryParam.type === "mkcp")
				queryParam.type = "mkcp"
			opt.set('transport', queryParam.type);
			opt.get('transport').dispatchEvent(event);
			if (queryParam.type === "tcp") {
				opt.set('tcp_guise', queryParam.headerType || "none");
				opt.get('tcp_guise').dispatchEvent(event);
				if (queryParam.headerType && queryParam.headerType != "none") {
					opt.set('http_host', queryParam.host || "");
					opt.set('http_path', queryParam.path || "");
				}
			} else if (queryParam.type === "ws") {
				opt.set('ws_host', queryParam.host || "");
				opt.set('ws_path', queryParam.path || "");
			} else if (queryParam.type === "h2") {
				opt.set('h2_host', queryParam.host || "");
				opt.set('h2_path', queryParam.path || "");
			} else if (queryParam.type === "quic") {
				opt.set('quic_guise', queryParam.headerType || "none");
				opt.get('quic_guise').dispatchEvent(event);
				opt.set('quic_security', queryParam.quicSecurity);
				opt.set('quic_key', queryParam.key);
			} else if (queryParam.type === "kcp" || queryParam.type === "mkcp") {
				opt.set('kcp_guise', queryParam.headerType || "none");
				opt.get('kcp_guise').dispatchEvent(event);
			} else if (queryParam.type === "grpc") {
				opt.set('grpc_serviceName', (queryParam.serviceName || queryParam.path) || "");
			}
			
			if (m.hash) {
				opt.set('alias', decodeURI(m.hash.substr(1)));
			}
		} else {
			s.innerHTML = "<font color='red'><%:Invalid Share URL Format%></font>: " + ssu[0];
			return false;
		}
		s.innerHTML = "<font color='green'><%:Import Finished %></font>";
		return false;
	}

//]]></script>
																				
<input type="button" class="btn cbi-button cbi-button-apply" value='<%:From Share URL%>' onclick="return fromUrl(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%>
