<%+header%>
<%
-- Copyright (C) 2018-2020 Lienol <lawlienol@gmail.com>
%>
<script src="<%=resource%>/vue.min.js"></script>
<form>
	<div class="cbi-map" id="ssr_mudb_server_user_div">
		<h2 name="content">ShadowsocksR MuDB {{title}}</h2>
		<fieldset class="cbi-section">
			<div class="cbi-section-node">
			
				<div class="cbi-value" data-index="1" data-depends="[]">
					<label class="cbi-value-title">启用</label>
					<div class="cbi-value-field">
						<input type="hidden" id="enable" name="enable" v-model="user.enable">
						<input type="checkbox" :value="user.enable" @click="check_enable($event)"
							:checked="user.enable == 1">
					</div>
				</div>

				<div class="cbi-value" data-index="2" data-depends="[]">
					<label class="cbi-value-title">备注</label>
					<div class="cbi-value-field">
						<input name="user" type="text" class="cbi-input-text" v-model="user.user">
					</div>
				</div>

				<div class="cbi-value" data-index="3" data-depends="[]">
					<label class="cbi-value-title">端口</label>
					<div class="cbi-value-field">
						<input name="port" type="text" class="cbi-input-text" v-model="user.port"
							onkeyup="this.value=this.value.replace(/[^0-9-]+/,'');">
						<input name="old_port" type="hidden" :value="user.old_port">
					</div>
				</div>

				<div class="cbi-value" data-index="4" data-depends="[]">
					<label class="cbi-value-title">密码</label>
					<div class="cbi-value-field">
						<input name="passwd" type="text" class="cbi-input-text" v-model="user.passwd"
							onkeyup="value=value.replace(/[\u4e00-\u9fa5]/ig,'')">
					</div>
				</div>

				<div class="cbi-value" data-index="5" data-depends="[]">
					<label class="cbi-value-title">加密</label>
					<div class="cbi-value-field">
						<select class="cbi-input-select" name="method" size="1" v-model="user.method">
							<option v-model="method" v-for="method in method_list">{{method}}</option>
						</select>
					</div>
				</div>

				<div class="cbi-value" data-index="6" data-depends="[]">
					<label class="cbi-value-title">协议</label>
					<div class="cbi-value-field">
						<select class="cbi-input-select" name="protocol" size="1" v-model="user.protocol">
							<option v-model="protocol" v-for="protocol in protocol_list">{{protocol}}</option>
						</select>
					</div>
				</div>

				<div class="cbi-value" data-index="7" data-depends="[]">
					<label class="cbi-value-title">混淆</label>
					<div class="cbi-value-field">
						<select class="cbi-input-select" name="obfs" size="1" v-model="user.obfs">
							<option v-model="obfs" v-for="obfs in obfs_list">{{obfs}}</option>
						</select>
					</div>
				</div>

				<div class="cbi-value" data-index="8" data-depends="[]">
					<label class="cbi-value-title">设备数限制</label>
					<div class="cbi-value-field">
						<input name="device_limit" type="text" class="cbi-input-text" v-model="user.protocol_param"
							onkeyup="this.value=this.value.replace(/[^0-9-]+/,'');">
						<br>
						<div class="cbi-value-description">
							同一时间能链接的客户端数量(多端口模式，每个端口都是独立计算)，建议最少 2个。
						</div>
					</div>
				</div>

				<div class="cbi-value" data-index="9" data-depends="[]">
					<label class="cbi-value-title">单线程限速</label>
					<div class="cbi-value-field">
						<input name="speed_limit_per_con" type="text" class="cbi-input-text"
							v-model="user.speed_limit_per_con" onkeyup="this.value=this.value.replace(/[^0-9-]+/,'');">
						<br>
						<div class="cbi-value-description">
							单线程的限速上限，多线程即无效。0代表不限速。(单位：KB/S)
						</div>
					</div>
				</div>

				<div class="cbi-value" data-index="10" data-depends="[]">
					<label class="cbi-value-title">总限速</label>
					<div class="cbi-value-field">
						<input name="speed_limit_per_user" type="text" class="cbi-input-text"
							v-model="user.speed_limit_per_user" onkeyup="this.value=this.value.replace(/[^0-9-]+/,'');">
						<br>
						<div class="cbi-value-description">
							总速度限速上限，单个端口整体限速。0代表不限速。(单位：KB/S)
						</div>
					</div>
				</div>

				<div class="cbi-value" data-index="11" data-depends="[]">
					<label class="cbi-value-title">禁止的端口</label>
					<div class="cbi-value-field">
						<input name="forbidden_port" type="text" class="cbi-input-text" v-model="user.forbidden_port">
						<br>
						<div class="cbi-value-description">
							例如不允许访问 25端口，用户就无法通过SSR代理访问邮件端口25了，如果禁止了 80,443 那么用户将无法正常访问 http/https 网站。<br>封禁单个端口格式:
							25<br>封禁多个端口格式: 23,465<br>封禁端口段格式: 233-266<br>封禁多种格式端口: 25,465,233-666
						</div>
					</div>
				</div>

				<div class="cbi-value cbi-value-last" data-index="12" data-depends="[]">
					<label class="cbi-value-title">可用总流量</label>
					<div class="cbi-value-field">
						<input name="transfer_enable" type="text" class="cbi-input-text" v-model="user.transfer_enable"
							onkeyup="this.value=this.value.replace(/[^0-9-]+/,'');">
						<br>
						<div class="cbi-value-description">
							可使用的总流量上限(单位: GB, 1-838868)，0代表无限
						</div>
					</div>
				</div>
			</div>
			<br>
		</fieldset>
		<br>
		<div class="cbi-page-actions">
			<input class="cbi-button cbi-button-apply" type="button" value="保存并修改" @click="save()">
			<input class="cbi-button cbi-button-reset" type="button" value="返回" @click="goback()">
		</div>
	</div>
</form>

<script>
	var api_url = '<%=luci.dispatcher.build_url("admin", "vpn", "ssr_mudb_server")%>'
	var vue = new Vue({
		el: '#ssr_mudb_server_user_div',
		data: {
			title: '用户配置',
			user: {
				enable: 1,
				forbidden_port: "",
				method: "none",
				protocol: "auth_chain_a",
				obfs: "tls1.2_ticket_auth",
				protocol_param: "2",
				speed_limit_per_con: 0,
				speed_limit_per_user: 0,
				transfer_enable: 0,
				user: "起个名字吧"

			},
			method_list: [
				"none",
				"table",
				"rc4",
				"rc4-md5",
				"aes-128-cfb",
				"aes-192-cfb",
				"aes-256-cfb",
				"aes-128-ctr",
				"aes-192-ctr",
				"aes-256-ctr",
				"bf-cfb",
				"cast5-cfb",
				"des-cfb",
				"rc2-cfb",
				"salsa20",
				"chacha20",
				"chacha20-ietf"
			],
			protocol_list: [
				"origin",
				"verify_simple",
				"verify_deflate",
				"verify_sha1",
				"auth_simple",
				"auth_sha1",
				"auth_sha1_v2",
				"auth_sha1_v4",
				"auth_aes128_md5",
				"auth_aes128_sha1",
				"auth_chain_a",
				"auth_chain_b",
				"auth_chain_c",
				"auth_chain_d"
			],
			obfs_list: [
				"plain",
				"http_simple",
				"http_post",
				"random_head",
				"tls_simple",
				"tls1.0_session_auth",
				"tls1.2_ticket_auth"
			]
		},
		methods: {
			check_enable: function (e) {
				if (e.currentTarget.checked == true) {
					this.user.enable = 1;
				} else {
					this.user.enable = 0;
				}
			},
			save: function () {
				var new_user = JSON.parse(JSON.stringify(this.user));
				if (new_user.port == null || isNaN(parseInt(new_user.port)) || parseInt(new_user.port) <= 0 || parseInt(new_user.port) >= 65536) {
					alert('端口必须是1-65535范围，且不能冲突！');
					return;
				}

				new_user.transfer_enable = new_user.transfer_enable == 0 ? 838868 : new_user.transfer_enable;
				new_user.transfer_enable = (new_user.transfer_enable * 1024 * 1024 * 1024);
				XHR.get(api_url + "/user_save", { json: JSON.stringify(new_user), action: this.user.old_port ? "edit" : "add" },
					function (x, result) {
						if (x.status == 200) {
							if (result.code && result.code == 1) {
								window.location.href = api_url;
							} else {
								if (result.msg)
									alert(result.msg);
								else
									alert("未知错误");
							}
						}
					}
				)
			},
			goback: function () {
				window.location.href = api_url;
			}
		}
	})

	var pathname = location.pathname;
	var port = pathname.substring(pathname.lastIndexOf('/') + 1);
	if (port && !isNaN(parseInt(port)) && parseInt(port) > 0 && parseInt(port) < 65536) {
		XHR.get(api_url + "/user_get", { port: port },
			function (x, result) {
				if (x.status == 200) {
					result.transfer_enable = (result.transfer_enable / 1024 / 1024 / 1024);
					result.old_port = result.port;
					vue.user = result;
				}
			}
		)
	}
</script>
<%+footer%>