-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: host domain login & refactor: calc utils
- Loading branch information
Showing
10 changed files
with
119 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// 校验IP地址是否合法 | ||
const IPv4SegmentFormat = '(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'; | ||
const IPv4AddressFormat = `(${IPv4SegmentFormat}[.]){3}${IPv4SegmentFormat}`; | ||
const IPv4AddressRegExp = new RegExp(`^${IPv4AddressFormat}$`); | ||
|
||
const IPv6SegmentFormat = '(?:[0-9a-fA-F]{1,4})'; | ||
const IPv6AddressRegExp = new RegExp('^(' + | ||
`(?:${IPv6SegmentFormat}:){7}(?:${IPv6SegmentFormat}|:)|` + | ||
`(?:${IPv6SegmentFormat}:){6}(?:${IPv4AddressFormat}|:${IPv6SegmentFormat}|:)|` + | ||
`(?:${IPv6SegmentFormat}:){5}(?::${IPv4AddressFormat}|(:${IPv6SegmentFormat}){1,2}|:)|` + | ||
`(?:${IPv6SegmentFormat}:){4}(?:(:${IPv6SegmentFormat}){0,1}:${IPv4AddressFormat}|(:${IPv6SegmentFormat}){1,3}|:)|` + | ||
`(?:${IPv6SegmentFormat}:){3}(?:(:${IPv6SegmentFormat}){0,2}:${IPv4AddressFormat}|(:${IPv6SegmentFormat}){1,4}|:)|` + | ||
`(?:${IPv6SegmentFormat}:){2}(?:(:${IPv6SegmentFormat}){0,3}:${IPv4AddressFormat}|(:${IPv6SegmentFormat}){1,5}|:)|` + | ||
`(?:${IPv6SegmentFormat}:){1}(?:(:${IPv6SegmentFormat}){0,4}:${IPv4AddressFormat}|(:${IPv6SegmentFormat}){1,6}|:)|` + | ||
`(?::((?::${IPv6SegmentFormat}){0,5}:${IPv4AddressFormat}|(?::${IPv6SegmentFormat}){1,7}|:))` + | ||
')(%[0-9a-zA-Z-.:]{1,})?$'); | ||
|
||
export function isIP(str, version = '4') { | ||
version = String(version); | ||
if (!version) { | ||
return isIP(str, 4) || isIP(str, 6); | ||
} | ||
if (version === '4') { | ||
return IPv4AddressRegExp.test(str); | ||
} | ||
if (version === '6') { | ||
return IPv6AddressRegExp.test(str); | ||
} | ||
return false; | ||
} | ||
|
||
// 校验域名是否合法 | ||
const options = { | ||
require_tld: true, | ||
allow_underscores: false, | ||
allow_trailing_dot: false, | ||
allow_numeric_tld: false, | ||
allow_wildcard: false, | ||
ignore_max_length: false, | ||
}; | ||
|
||
export function isFQDN(str) { | ||
|
||
/* Remove the optional trailing dot before checking validity */ | ||
if (options.allow_trailing_dot && str[str.length - 1] === '.') { | ||
str = str.substring(0, str.length - 1); | ||
} | ||
|
||
/* Remove the optional wildcard before checking validity */ | ||
if (options.allow_wildcard === true && str.indexOf('*.') === 0) { | ||
str = str.substring(2); | ||
} | ||
|
||
const parts = str.split('.'); | ||
const tld = parts[parts.length - 1]; | ||
|
||
if (options.require_tld) { | ||
// disallow fqdns without tld | ||
if (parts.length < 2) { | ||
return false; | ||
} | ||
|
||
if (!options.allow_numeric_tld && !/^([a-z\u00A1-\u00A8\u00AA-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]{2,}|xn[a-z0-9-]{2,})$/i.test(tld)) { | ||
return false; | ||
} | ||
|
||
// disallow spaces | ||
if (/\s/.test(tld)) { | ||
return false; | ||
} | ||
} | ||
|
||
// reject numeric TLDs | ||
if (!options.allow_numeric_tld && /^\d+$/.test(tld)) { | ||
return false; | ||
} | ||
|
||
return parts.every((part) => { | ||
if (part.length > 63 && !options.ignore_max_length) { | ||
return false; | ||
} | ||
|
||
if (!/^[a-z_\u00a1-\uffff0-9-]+$/i.test(part)) { | ||
return false; | ||
} | ||
|
||
// disallow full-width chars | ||
if (/[\uff01-\uff5e]/.test(part)) { | ||
return false; | ||
} | ||
|
||
// disallow parts starting or ending with hyphen | ||
if (/^-|-$/.test(part)) { | ||
return false; | ||
} | ||
|
||
if (!options.allow_underscores && /_/.test(part)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
}); | ||
} |