2022-04-09 03:59:20 +08:00
|
|
|
package top.octopusyan.config;
|
|
|
|
|
|
|
|
import com.jfoenix.validation.RegexValidator;
|
|
|
|
import com.jfoenix.validation.RequiredFieldValidator;
|
|
|
|
import com.jfoenix.validation.StringLengthValidator;
|
|
|
|
import com.jfoenix.validation.base.ValidatorBase;
|
|
|
|
import top.octopusyan.model.ProxySetupModel;
|
|
|
|
import top.octopusyan.utils.DomainUtil;
|
|
|
|
|
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author : octopus yan
|
|
|
|
* @email : octopus_yan@foxmail.com
|
|
|
|
* @description : JFX文本校验
|
|
|
|
* @create : 2022-4-2 17:03
|
|
|
|
*/
|
|
|
|
public class TextValidate {
|
|
|
|
// /**
|
|
|
|
// * 账号格式错误
|
|
|
|
// */
|
|
|
|
// public static ValidatorBase AccountFail = new ValidatorBase() {
|
|
|
|
// @Override
|
|
|
|
// protected void eval() {
|
|
|
|
// setMessage("账号格式错误");
|
|
|
|
// hasErrors.set(true);
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 账号不能为空
|
|
|
|
*/
|
|
|
|
public static RequiredFieldValidator AccoountRequired = new RequiredFieldValidator("账号不能为空!");
|
|
|
|
/**
|
|
|
|
* 账号格式
|
|
|
|
*/
|
|
|
|
public static RegexValidator AccoountValidator = new RegexValidator("账号格式有误!");
|
|
|
|
/**
|
|
|
|
* 密码不能为空
|
|
|
|
*/
|
|
|
|
public static RequiredFieldValidator PasswordRequired = new RequiredFieldValidator("密码不能为空!");
|
|
|
|
/**
|
|
|
|
* 邮箱地址格式验证
|
|
|
|
*/
|
|
|
|
public static RegexValidator EmailFormat = new RegexValidator("邮箱地址格式错误");
|
|
|
|
/**
|
|
|
|
* 域名为空检查
|
|
|
|
*/
|
|
|
|
public static RequiredFieldValidator DomainRequired = new RequiredFieldValidator("域名不能为空!");
|
2022-04-16 00:47:52 +08:00
|
|
|
/**
|
|
|
|
* 访问密码为空检查
|
|
|
|
*/
|
|
|
|
public static RequiredFieldValidator p2pPwdRequired = new RequiredFieldValidator("访问密码不能为空!");
|
|
|
|
/**
|
|
|
|
* 访问服务名为空检查
|
|
|
|
*/
|
|
|
|
public static RequiredFieldValidator p2pServerNameRequired = new RequiredFieldValidator("访问服务名称不能为空!");
|
2022-04-09 03:59:20 +08:00
|
|
|
/**
|
|
|
|
* 端口为空检查
|
|
|
|
*/
|
|
|
|
public static RequiredFieldValidator PortRequired = new RequiredFieldValidator("端口不能为空");
|
|
|
|
/**
|
|
|
|
* 端口格式检查
|
|
|
|
*/
|
|
|
|
public static final RegexValidator PortFormat = new RegexValidator("端口格式错误");
|
|
|
|
/**
|
|
|
|
* IP为空检查
|
|
|
|
*/
|
|
|
|
public static RequiredFieldValidator IpRequired = new RequiredFieldValidator("本地IP不能为空");
|
2022-04-16 00:47:52 +08:00
|
|
|
/**
|
|
|
|
* http访问用户名格式检查
|
|
|
|
*/
|
|
|
|
public static final RegexValidator HttpUserFormat = new RegexValidator("用户名格式错误");
|
|
|
|
/**
|
|
|
|
* http访问密码格式检查
|
|
|
|
*/
|
|
|
|
public static final RegexValidator HttpPwdFormat = new RegexValidator("密码格式错误");
|
|
|
|
/**
|
|
|
|
* http访问密码格式检查
|
|
|
|
*/
|
|
|
|
public static final RegexValidator P2pPwdFormat = new RegexValidator("密码格式错误");
|
2022-04-09 03:59:20 +08:00
|
|
|
/**
|
|
|
|
* IP格式检查
|
|
|
|
*/
|
|
|
|
public static final RegexValidator IpFormat = new RegexValidator("本地IP格式错误");
|
|
|
|
|
|
|
|
static {
|
|
|
|
EmailFormat.setRegexPattern("^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$");
|
|
|
|
AccoountValidator.setRegexPattern("^[a-zA-Z0-9_-]*$");
|
|
|
|
PortFormat.setRegexPattern("^([0-9]|[1-9]\\d{1,3}|[1-5]\\d{4}|6[0-4]\\d{4}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5])$");
|
2022-04-16 00:47:52 +08:00
|
|
|
IpFormat.setRegexPattern("^(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])\\" +
|
|
|
|
".(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])$");
|
|
|
|
HttpUserFormat.setRegexPattern("^[a-zA-Z0-9_-]*$");
|
|
|
|
HttpPwdFormat.setRegexPattern("^[a-zA-Z0-9_-]*$");
|
|
|
|
P2pPwdFormat.setRegexPattern("^[a-zA-Z0-9_-]{3,18}$");
|
2022-04-09 03:59:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 文本长度校验
|
|
|
|
* <p/> message > name + "长度有误"
|
|
|
|
*/
|
|
|
|
public static StringLengthValidator getLengthValidator(String name, int length) {
|
|
|
|
return new StringLengthValidator(name + "长度有误", length);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 文本长度校验
|
|
|
|
* <p/> message > name + "长度有误,应在" + min + "到" + max + "之间"
|
|
|
|
*/
|
|
|
|
public static RegexValidator getLengthValidator(int min, int max, String name) {
|
|
|
|
String message = name + "长度有误,应在" + min + "到" + max + "之间";
|
|
|
|
|
|
|
|
RegexValidator validator = new RegexValidator(message);
|
|
|
|
|
|
|
|
validator.setRegexPattern("[a-zA-Z0-9_-]{" + min + "," + max + "}$");
|
|
|
|
|
|
|
|
return validator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 域名格式检查
|
|
|
|
*/
|
|
|
|
public static ValidatorBase domainFormatValidator(ProxySetupModel model) {
|
|
|
|
|
2022-04-10 23:57:07 +08:00
|
|
|
return new ValidatorBase("域名格式错误,支持数字字母下划线") {
|
2022-04-09 03:59:20 +08:00
|
|
|
@Override
|
|
|
|
protected void eval() {
|
2022-04-10 23:57:07 +08:00
|
|
|
if (!DomainUtil.isCustomize(model.get())) {
|
2022-04-16 00:47:52 +08:00
|
|
|
if (ProxyConfig.isHttp(model)) {
|
2022-04-10 23:57:07 +08:00
|
|
|
// http / https
|
|
|
|
boolean matches = Pattern.compile("^[a-zA-Z0-9_-]{3,18}$").matcher(model.getDomain()).matches();
|
|
|
|
hasErrors.set(!matches);
|
|
|
|
} else {
|
|
|
|
// tcp / udp
|
|
|
|
hasErrors.set(false);
|
|
|
|
}
|
|
|
|
} else {
|
2022-04-09 03:59:20 +08:00
|
|
|
boolean matches = Pattern.compile("^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$")
|
|
|
|
.matcher(model.getDomain() + model.getDomainSuffix()).matches();
|
|
|
|
hasErrors.set(!matches);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 域名格式检查
|
|
|
|
*/
|
|
|
|
public static ValidatorBase domainLengthValidator(ProxySetupModel model) {
|
|
|
|
|
2022-04-10 23:57:07 +08:00
|
|
|
return new RegexValidator("子域名长度不小于3个字符") {
|
2022-04-09 03:59:20 +08:00
|
|
|
@Override
|
|
|
|
protected void eval() {
|
|
|
|
setRegexPattern("^[a-zA-Z0-9_-]{3,18}$");
|
2022-04-16 00:47:52 +08:00
|
|
|
if (ProxyConfig.isHttp(model) && !DomainUtil.isCustomize(model.get())) {
|
2022-04-09 03:59:20 +08:00
|
|
|
super.eval();
|
|
|
|
} else {
|
|
|
|
hasErrors.set(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 自定义域名解析检查
|
|
|
|
*/
|
|
|
|
public static ValidatorBase domainAddressValidator(ProxySetupModel model) {
|
|
|
|
|
|
|
|
return new ValidatorBase("请输入您的域名,并解析至: " + ProxyConfig.getServerIP(model.getServer())) {
|
|
|
|
@Override
|
|
|
|
protected void eval() {
|
|
|
|
if (!DomainUtil.isCustomize(model.get()))
|
|
|
|
hasErrors.set(false);
|
|
|
|
else
|
|
|
|
hasErrors.set(
|
|
|
|
!Objects.equals(
|
|
|
|
ProxyConfig.getServerIP(model.getServer()),
|
|
|
|
DomainUtil.getDomainAddress(model.getDomain())
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|